import * as path from "path";
export function assessMetadataQuality(track) {
    const issues = [];
    const suggestions = {};
    let score = 100;
    // Extract path components for analysis
    const pathParts = track.path.split(path.sep);
    const filename = path.basename(track.path, path.extname(track.path));
    // Common patterns that indicate bad metadata
    const suspiciousPatterns = [
        /^track\s*\d+$/i,
        /^unknown/i,
        /^untitled/i,
        /^audio/i,
        /^recording/i,
        /^\d+$/,
        /^[A-Z0-9]{8,}$/
    ];
    // Check for missing required fields
    if (!track.artist || track.artist.trim() === "") {
        issues.push("Missing artist");
        score -= 30;
    }
    if (!track.album || track.album.trim() === "") {
        issues.push("Missing album");
        score -= 20;
    }
    if (!track.title || track.title.trim() === "") {
        issues.push("Missing title");
        score -= 20;
    }
    // Check for suspicious values
    if (track.artist) {
        // Artist name is same as album name (common parsing error)
        if (track.album && track.artist.toLowerCase() === track.album.toLowerCase()) {
            issues.push("Artist and album names are identical");
            score -= 15;
        }
        // Artist name matches suspicious patterns
        if (suspiciousPatterns.some((pattern)=>pattern.test(track.artist))) {
            issues.push("Artist name looks generic or auto-generated");
            score -= 20;
        }
        // Artist name is just a number or year
        if (/^\d{4}$/.test(track.artist)) {
            issues.push("Artist appears to be a year");
            score -= 25;
            // Likely the year was parsed as artist
            if (!track.year) {
                suggestions.artist = undefined;
            }
        }
    }
    if (track.album) {
        // Album name matches suspicious patterns
        if (suspiciousPatterns.some((pattern)=>pattern.test(track.album))) {
            issues.push("Album name looks generic or auto-generated");
            score -= 15;
        }
        // Album name contains file path separators (likely parsed from path)
        if (track.album.includes("/") || track.album.includes("\\")) {
            issues.push("Album name contains path separators");
            score -= 20;
        }
    }
    if (track.title) {
        // Title is same as filename (no real metadata)
        if (track.title === filename) {
            issues.push("Title is identical to filename");
            score -= 10;
        }
        // Title contains file extensions
        if (/\.(mp3|flac|wav|m4a|ogg)$/i.test(track.title)) {
            issues.push("Title contains file extension");
            score -= 15;
            suggestions.title = track.title.replace(/\.(mp3|flac|wav|m4a|ogg)$/i, "");
        }
    }
    // Try to extract better metadata from file path
    // Common patterns: /Artist/Album/01 - Title.mp3
    //                 /Artist - Album/01 Title.mp3
    //                 /Music/Artist/[Year] Album/Title.mp3
    if (pathParts.length >= 3) {
        const parentDir = pathParts[pathParts.length - 2];
        const grandParentDir = pathParts[pathParts.length - 3];
        // Check for [Year] Album pattern
        const yearAlbumMatch = parentDir.match(/^\[?(\d{4})\]?\s*(.+)$/);
        if (yearAlbumMatch && !track.year) {
            const [, year, albumName] = yearAlbumMatch;
            if (!track.album || track.album === parentDir) {
                suggestions.album = albumName.trim();
            }
        }
        // Check for Artist - Album pattern
        const artistAlbumMatch = parentDir.match(/^(.+?)\s*-\s*(.+)$/);
        if (artistAlbumMatch) {
            const [, artist, album] = artistAlbumMatch;
            if (!track.artist || suspiciousPatterns.some((p)=>p.test(track.artist))) {
                suggestions.artist = artist.trim();
            }
            if (!track.album || suspiciousPatterns.some((p)=>p.test(track.album))) {
                suggestions.album = album.trim();
            }
        }
        // If parent dir looks like album and grandparent like artist
        if (!track.artist || suspiciousPatterns.some((p)=>p.test(track.artist))) {
            if (grandParentDir && !grandParentDir.match(/^(Music|music|Audio|Songs|iTunes|Collection)$/i)) {
                suggestions.artist = grandParentDir;
            }
        }
        if (!track.album || suspiciousPatterns.some((p)=>p.test(track.album))) {
            if (parentDir && !parentDir.match(/^(Music|music|Audio|Songs|iTunes|Collection)$/i)) {
                suggestions.album = parentDir;
            }
        }
    }
    // Try to parse track number and title from filename
    // Common patterns: 01 - Title.mp3, 01. Title.mp3, 01_Title.mp3, Artist - Album - 01 - Title.mp3
    let cleanedFilename = filename;
    // Remove common prefixes like "Artist - Album - " from filename
    if (track.artist && track.album) {
        // Escape regex special characters in artist/album names
        const escapeRegex = (str)=>str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
        const prefixPattern = new RegExp(`^${escapeRegex(track.artist)}\\s*-\\s*${escapeRegex(track.album)}\\s*-\\s*`, "i");
        cleanedFilename = cleanedFilename.replace(prefixPattern, "");
    }
    // Match various track number patterns
    const trackNumberMatch = cleanedFilename.match(/^(\d{1,3})\s*[\-_.\s]+(.+)$/);
    if (trackNumberMatch) {
        const [, trackNum, titlePart] = trackNumberMatch;
        if (!track.title || track.title === filename || track.title.includes(" - ")) {
            suggestions.title = titlePart.trim();
        }
    } else if (!track.title || track.title === filename) {
        // No track number found, use the whole cleaned filename
        suggestions.title = cleanedFilename.trim();
    }
    // Ensure score doesn't go below 0
    score = Math.max(0, score);
    return {
        overall: score,
        issues,
        suggestions: Object.keys(suggestions).length > 0 ? suggestions : {}
    };
}
// Batch process tracks and identify those needing review
export function identifyLowQualityMetadata(tracks, threshold = 70) {
    return tracks.map((track)=>({
            ...track,
            qualityScore: assessMetadataQuality(track)
        })).filter((track)=>track.qualityScore.overall < threshold).sort((a, b)=>a.qualityScore.overall - b.qualityScore.overall);
}

//# sourceMappingURL=metadataQuality.js.map