• Scores a match between a Kenmei manga and an AniList manga entry. Returns a score between 0-100 and information about the match.

    Parameters

    Returns { confidence: number; isExactMatch: boolean; matchedField: string }

    An object containing confidence, isExactMatch, and matchedField.

    export function scoreMatch(
    kenmeiManga: KenmeiManga,
    anilistManga: AniListManga,
    config: Partial<MatchEngineConfig> = {},
    ): { confidence: number; isExactMatch: boolean; matchedField: string } {
    const matchConfig = { ...DEFAULT_MATCH_CONFIG, ...config };
    const {
    caseSensitive,
    preferEnglishTitles,
    preferRomajiTitles,
    useAlternativeTitles,
    } = matchConfig;

    // Normalize the Kenmei title
    const kenmeiTitle = normalizeString(kenmeiManga.title, caseSensitive);

    // Skip extremely short titles (likely errors)
    if (kenmeiTitle.length < matchConfig.minTitleLength) {
    return { confidence: 0, isExactMatch: false, matchedField: "none" };
    }

    // Array to store all similarity scores with their sources
    const scores: Array<{ field: string; score: number }> = [];

    // Check primary titles - early return if exact match found
    const primaryMatch = scorePrimaryTitles(kenmeiTitle, anilistManga, scores);
    if (primaryMatch) return primaryMatch;

    // Check alternative titles if enabled - early return if exact match found
    if (useAlternativeTitles) {
    const synonymMatch = scoreSynonyms(kenmeiTitle, anilistManga, scores);
    if (synonymMatch) return synonymMatch;

    const altTitleMatch = scoreAlternativeTitles(
    kenmeiManga,
    anilistManga,
    caseSensitive,
    matchConfig.minTitleLength,
    scores,
    );
    if (altTitleMatch) return altTitleMatch;
    }

    // Calculate final score with preferences
    return calculateFinalScore(scores, preferEnglishTitles, preferRomajiTitles);
    }