import type { CopiedNameHit } from "./copiedRuleDocument"; /** * Bridges final-review → completed without query strings, and re-opens a rule * from profile (`/create/completed?ruleId=…`) after GET /api/rules/[id]. Profile * "Manage" links here; stakeholder "View" uses `/invite/[id]`. */ export const CREATE_FLOW_LAST_PUBLISHED_KEY = "createFlow.lastPublished"; export type StoredLastPublishedRule = { id: string; title: string; summary?: string | null; document: Record; /** Present after Make a copy; drives the copy-ready banner and lavender surface. */ copiedFromTitle?: string; copiedNameHit?: CopiedNameHit | null; }; export function writeLastPublishedRule(data: StoredLastPublishedRule): void { if (typeof sessionStorage === "undefined") return; sessionStorage.setItem(CREATE_FLOW_LAST_PUBLISHED_KEY, JSON.stringify(data)); } export function readLastPublishedRule(): StoredLastPublishedRule | null { if (typeof sessionStorage === "undefined") return null; const raw = sessionStorage.getItem(CREATE_FLOW_LAST_PUBLISHED_KEY); if (!raw) return null; try { const parsed = JSON.parse(raw) as unknown; if (!parsed || typeof parsed !== "object") return null; const o = parsed as Record; if (typeof o.id !== "string" || typeof o.title !== "string") return null; const doc = o.document; if (doc === null || typeof doc !== "object" || Array.isArray(doc)) { return null; } const summaryVal = o.summary; let summary: string | null | undefined; if (typeof summaryVal === "string") { summary = summaryVal; } else if (summaryVal === null) { summary = null; } else { summary = undefined; } let copiedFromTitle: string | undefined; if ( typeof o.copiedFromTitle === "string" && o.copiedFromTitle.trim().length > 0 ) { copiedFromTitle = o.copiedFromTitle; } let copiedNameHit: CopiedNameHit | null | undefined; if (o.copiedNameHit && typeof o.copiedNameHit === "object") { const hit = o.copiedNameHit as Record; if ( typeof hit.categoryName === "string" && typeof hit.entryTitle === "string" ) { copiedNameHit = { categoryName: hit.categoryName, entryTitle: hit.entryTitle, }; } } return { id: o.id, title: o.title, summary, document: doc as Record, ...(copiedFromTitle ? { copiedFromTitle } : {}), ...(copiedNameHit ? { copiedNameHit } : {}), }; } catch { return null; } }