• Generates preview statistics for import, comparing imported data with existing results. Identifies new, duplicate, and conflicting entries.

    Parameters

    • file: File

      File to preview

    Returns Promise<
        {
            totalCount: number;
            newCount: number;
            duplicateCount: number;
            conflictCount: number;
            validationErrors: string[];
            validationWarnings: string[];
        },
    >

    Preview statistics including counts and validation info

    Error if file cannot be parsed or validated

    export async function getImportPreview(file: File): Promise<{
    totalCount: number;
    newCount: number;
    duplicateCount: number;
    conflictCount: number;
    validationErrors: string[];
    validationWarnings: string[];
    }> {
    try {
    const importedData = await parseImportFile(file);
    const validation = validateImportedMatchData(importedData);

    // Get existing match results
    const existingResults = storage.getItem(STORAGE_KEYS.MATCH_RESULTS);
    const existingMatches: MatchResult[] = existingResults
    ? JSON.parse(existingResults)
    : [];

    // Create lookup maps
    const existingById = new Map<string | number, MatchResult>();
    const existingByTitle = new Map<string, MatchResult>();

    for (const match of existingMatches) {
    existingById.set(match.kenmeiManga.id, match);
    existingByTitle.set(match.kenmeiManga.title.toLowerCase(), match);
    }

    // Count statistics
    let newCount = 0;
    let duplicateCount = 0;
    let conflictCount = 0;

    for (const importedMatch of importedData.matches) {
    const id = importedMatch.kenmeiManga.id;
    const title = importedMatch.kenmeiManga.title.toLowerCase();

    const existingById_match = existingById.get(id);
    const existingByTitle_match = existingByTitle.get(title);
    const existing = existingById_match || existingByTitle_match;

    if (existing) {
    // Check if existing match is non-pending (potential conflict)
    if (existing.status === "pending") {
    duplicateCount++;
    } else {
    conflictCount++;
    }
    } else {
    newCount++;
    }
    }

    return {
    totalCount: importedData.matches.length,
    newCount,
    duplicateCount,
    conflictCount,
    validationErrors: validation.errors,
    validationWarnings: validation.warnings,
    };
    } catch (error) {
    console.error("[Export] Error generating import preview:", error);
    return {
    totalCount: 0,
    newCount: 0,
    duplicateCount: 0,
    conflictCount: 0,
    validationErrors: [
    error instanceof Error ? error.message : "Unknown error",
    ],
    validationWarnings: [],
    };
    }
    }