export const AGENT_PSD_FONTS_KEY = "agent_psd_fonts"; export interface AgentPsdFont { id: string; name: string; family: string; brandName?: string; fileName: string; fileUrl: string; filePath?: string; contentType?: string; createdAt: string; updatedAt: string; uploadedById?: string; } export interface AgentPsdFontListResponse { success: boolean; fonts: AgentPsdFont[]; error?: string; } export interface AgentPsdFontMutationResponse { success: boolean; font?: AgentPsdFont; fonts?: AgentPsdFont[]; error?: string; } function normalizeText(value: unknown) { return String(value || "").trim(); } function normalizeUrl(value: unknown) { const text = normalizeText(value); return /^https?:\/\//i.test(text) ? text : ""; } function toIsoDateString(value: unknown) { const text = normalizeText(value); if (!text) return null; const date = new Date(text); if (Number.isNaN(date.getTime())) return null; return date.toISOString(); } export function parseAgentPsdFonts(value: unknown) { if (!Array.isArray(value)) return [] as AgentPsdFont[]; const fonts: AgentPsdFont[] = []; for (const entry of value) { if (!entry || typeof entry !== "object" || Array.isArray(entry)) { continue; } const record = entry as Record; const id = normalizeText(record.id); const name = normalizeText(record.name); const family = normalizeText(record.family); const fileName = normalizeText(record.fileName); const fileUrl = normalizeUrl(record.fileUrl); const createdAt = toIsoDateString(record.createdAt); const updatedAt = toIsoDateString(record.updatedAt) || createdAt; if (!id || !name || !family || !fileName || !fileUrl || !createdAt || !updatedAt) { continue; } fonts.push({ id, name, family, brandName: normalizeText(record.brandName) || undefined, fileName, fileUrl, filePath: normalizeText(record.filePath) || undefined, contentType: normalizeText(record.contentType) || undefined, createdAt, updatedAt, uploadedById: normalizeText(record.uploadedById) || undefined, }); } return fonts.sort((left, right) => right.updatedAt.localeCompare(left.updatedAt)); } export function upsertAgentPsdFont(fonts: AgentPsdFont[], font: AgentPsdFont) { return [font, ...fonts.filter((entry) => entry.id !== font.id)].sort((left, right) => right.updatedAt.localeCompare(left.updatedAt) ); }