let ciProfile = {};
let ciBatchManifest;
let ciAssets;
function isRecord(value) {
    return typeof value === "object" && value !== null && !Array.isArray(value);
}
function normalizeManifest(value) {
    if (!Array.isArray(value))
        return [];
    const rows = [];
    for (const raw of value) {
        if (!isRecord(raw))
            continue;
        const batchName = String(raw.batch_name || "").trim();
        if (!batchName)
            continue;
        const parsedCount = Number(raw.expected_image_count || 0);
        rows.push({
            batch_name: batchName,
            expected_image_count: Number.isFinite(parsedCount) ? Math.max(0, Math.trunc(parsedCount)) : 0,
            priority: raw.priority === "test_key" ? "test_key" : "key",
            source_sheet: String(raw.source_sheet || "").trim(),
        });
    }
    return rows;
}
export function resetCiImportState() {
    ciProfile = {};
    ciBatchManifest = undefined;
    ciAssets = undefined;
}
export function loadPersistedCiProfile(value) {
    ciProfile = isRecord(value) ? { ...value } : {};
    ciBatchManifest = undefined;
    ciAssets = undefined;
}
export function setCiImportPayload(payload) {
    ciProfile = isRecord(payload.ci_profile) ? { ...payload.ci_profile } : {};
    ciBatchManifest = normalizeManifest(payload.ci_batch_manifest);
    ciAssets = Array.isArray(payload.ci_assets)
        ? payload.ci_assets.filter(isRecord).map((row) => ({ ...row }))
        : [];
}
export function getCiProfileForSave() {
    return { ...ciProfile };
}
export function getCiBatchManifestForSave() {
    return ciBatchManifest ? ciBatchManifest.map((row) => ({ ...row })) : undefined;
}
export function getCiAssetsForSave() {
    return ciAssets ? ciAssets.map((row) => ({ ...row })) : undefined;
}
