• Finds the best matches for a Kenmei manga entry from a list of AniList entries.

    Parameters

    • kenmeiManga: KenmeiManga

      The Kenmei manga entry to match.

    • anilistMangaList: AniListManga[]

      The list of AniList manga entries to compare against.

    • config: Partial<MatchEngineConfig> = {}

      Optional partial match engine configuration.

    Returns MangaMatchResult

    A MangaMatchResult containing the best matches and status.

    export function findBestMatches(
    kenmeiManga: KenmeiManga,
    anilistMangaList: AniListManga[],
    config: Partial<MatchEngineConfig> = {},
    ): MangaMatchResult {
    const matchConfig = { ...DEFAULT_MATCH_CONFIG, ...config };

    // Calculate match scores for each AniList manga
    const matchResults = anilistMangaList.map((manga) => {
    const matchScore = scoreMatch(kenmeiManga, manga, matchConfig);
    return {
    manga,
    confidence: matchScore.confidence,
    isExactMatch: matchScore.isExactMatch,
    matchedField: matchScore.matchedField,
    };
    });

    // Sort by confidence score (descending)
    matchResults.sort((a, b) => b.confidence - a.confidence);

    // Take only the top matches
    const topMatches = matchResults
    .slice(0, matchConfig.maxMatches)
    .filter((match) => match.confidence > 0);

    // Determine the match status
    let status: MangaMatchResult["status"] = "pending";

    if (topMatches.length === 0) {
    status = "pending"; // No matches found
    } else if (topMatches[0].isExactMatch) {
    status = "matched"; // Found an exact match
    } else if (
    topMatches[0].confidence >= matchConfig.confidenceThreshold &&
    (topMatches.length === 1 ||
    topMatches[0].confidence - topMatches[1].confidence > 20)
    ) {
    status = "matched"; // High confidence and significant gap to next match
    } else {
    status = "pending"; // Multiple potential matches or low confidence
    }

    // Format as MangaMatchResult
    return {
    kenmeiManga,
    anilistMatches: topMatches.map(({ manga, confidence }) => ({
    manga,
    confidence,
    })),
    status,
    selectedMatch: status === "matched" ? topMatches[0].manga : undefined,
    matchDate: new Date(),
    };
    }