import { ApplicationStatus, WorkflowStage } from "@/data/dummyData"; const LOCAL_KEY = "certification_overrides"; export type CertificationStage = WorkflowStage | "cc_review" | "cc_recommended" | "jd_recommended"; export interface CertificationOverride { currentStage?: CertificationStage; status?: ApplicationStatus; } export type EntityType = "hatchery" | "farm"; interface OverridesMap { [compositeId: string]: CertificationOverride; } function getKey(entityType: EntityType, id: string) { return `${entityType}:${id}`; } function readOverrides(): OverridesMap { if (typeof window === "undefined") return {}; try { const raw = localStorage.getItem(LOCAL_KEY); if (!raw) return {}; const parsed = JSON.parse(raw); if (!parsed || typeof parsed !== "object") return {}; return parsed as OverridesMap; } catch { return {}; } } function writeOverrides(map: OverridesMap) { if (typeof window === "undefined") return; try { localStorage.setItem(LOCAL_KEY, JSON.stringify(map)); } catch { // ignore } } export function getCertificationOverride( entityType: EntityType, id: string, ): CertificationOverride | undefined { const map = readOverrides(); return map[getKey(entityType, id)]; } export function updateCertificationOverride( entityType: EntityType, id: string, patch: CertificationOverride, ): CertificationOverride { const map = readOverrides(); const key = getKey(entityType, id); const existing = map[key] || {}; const merged: CertificationOverride = { ...existing, ...patch }; map[key] = merged; writeOverrides(map); return merged; } interface HasCertificationFields { id: string; entityType: EntityType; currentStage: string; status: string; } export function applyCertificationOverrides(rows: T[]): T[] { const map = readOverrides(); return rows.map((row) => { const override = map[getKey(row.entityType, row.id)]; if (!override) return row; return { ...row, currentStage: override.currentStage ?? row.currentStage, status: override.status ?? row.status, }; }); }