import type { Anchor, DockMode, Position, Size } from "./types"; export type PersistedContextState = { anchor?: Anchor; anchorOffset?: Position; size?: Size; hasCustomPosition?: boolean; }; export type PersistedState = { button?: Omit; window?: PersistedContextState; isOpen?: boolean; dockMode?: DockMode; selectedMenu?: string; selectedContext?: string; }; export function loadInspectorState(storageKey: string): PersistedState | null { if (typeof window === "undefined") { return null; } const raw = window.localStorage.getItem(storageKey); if (raw) { try { const parsed = JSON.parse(raw); if (parsed && typeof parsed === "object") { return parsed as PersistedState; } } catch { // Fall through to cookie migration path } } // Backwards compatibility: try to read the legacy cookie and migrate it if (typeof document !== "undefined") { const prefix = `${storageKey}=`; const entry = document.cookie .split("; ") .find((cookie) => cookie.startsWith(prefix)); if (entry) { const legacyRaw = entry.substring(prefix.length); try { const parsed = JSON.parse(decodeURIComponent(legacyRaw)); if (parsed && typeof parsed === "object") { return parsed as PersistedState; } } catch { return null; } } } return null; } export function saveInspectorState( storageKey: string, state: PersistedState, ): void { if (typeof window === "undefined") { return; } try { window.localStorage.setItem(storageKey, JSON.stringify(state)); } catch (error) { console.warn("Failed to persist inspector state", error); } } export function isValidAnchor(value: unknown): value is Anchor { if (!value || typeof value !== "object") { return false; } const candidate = value as Anchor; return ( (candidate.horizontal === "left" || candidate.horizontal === "right") && (candidate.vertical === "top" || candidate.vertical === "bottom") ); } export function isValidPosition(value: unknown): value is Position { if (!value || typeof value !== "object") { return false; } const candidate = value as Position; return isFiniteNumber(candidate.x) && isFiniteNumber(candidate.y); } export function isValidSize(value: unknown): value is Size { if (!value || typeof value !== "object") { return false; } const candidate = value as Size; return isFiniteNumber(candidate.width) && isFiniteNumber(candidate.height); } export function isFiniteNumber(value: unknown): value is number { return typeof value === "number" && Number.isFinite(value); } export function isValidDockMode(value: unknown): value is DockMode { return value === "floating" || value === "docked-left"; }