/** * The faces the app draws, by the open id a card names (`"card": {"face": * "todo"}`, Loom step 5): Loom's registry and the faces CottonUI still draws * until each moves (apps/loom/faces.json and faces-pending.json, written by * `pnpm gen:faces`), bundled with the Worker so a definition naming any other * face is refused here with the id named, never drawn as nothing on a phone. * The core refuses the same ids (`card::FaceId`). */ import registry from "../../loom/faces.json"; import pending from "../../loom/faces-pending.json"; /** Every face id the app draws. */ export const KNOWN_FACES: ReadonlySet = new Set([...(registry as { id: string }[]).map((row) => row.id), ...(pending as string[])]); /** Each card `doc` holds — a table's own, a view's of a tool or a table's tool — as where it stands and the face it names. */ export function cardsOf(doc: unknown, at = ""): { at: string; face: unknown }[] { if (!doc || typeof doc !== "object") return []; if (Array.isArray(doc)) return doc.flatMap((item, i) => cardsOf(item, `${at}.${i}`)); const out: { at: string; face: unknown }[] = []; for (const [key, value] of Object.entries(doc as Record)) { const here = at ? `${at}.${key}` : key; if (key === "card" && value && typeof value === "object" && !Array.isArray(value) && "face" in value) { const card = value as Record; out.push({ at: here, face: card.face }); if (Array.isArray(card.offers)) card.offers.forEach((face, i) => out.push({ at: `${here}.offers.${i}`, face })); if (Array.isArray(card.faces)) card.faces.forEach((rule, i) => out.push({ at: `${here}.faces.${i}`, face: rule?.face })); } else out.push(...cardsOf(value, here)); } return out; } /** What is wrong with the faces `doc`'s cards name, or null: each names a face the app draws. */ export function faceProblem(doc: unknown): string | null { const offersProblem = (value: unknown): string | null => { if (!value || typeof value !== "object") return null; if (Array.isArray(value)) return value.map(offersProblem).find(Boolean) ?? null; const obj = value as Record; if (obj.card && typeof obj.card === "object") { const card = obj.card as Record; if ("offers" in card) { if (!Array.isArray(card.offers)) return "card.offers must be an ordered list"; if (new Set(card.offers).size !== card.offers.length) return "card.offers names each face once"; if (card.offers.length && !card.offers.includes(card.face)) return "card.offers must include card.face"; } } return Object.values(obj).map(offersProblem).find(Boolean) ?? null; }; const problem = offersProblem(doc); if (problem) return problem; const id = doc && typeof doc === "object" && !Array.isArray(doc) ? (doc as Record).id : undefined; const whose = typeof id === "string" ? ` of ${JSON.stringify(id)}` : ""; for (const card of cardsOf(doc)) { if (typeof card.face === "string" && KNOWN_FACES.has(card.face)) continue; return `the ${card.at}${whose} names the face ${JSON.stringify(card.face)}, which is no face the app draws (apps/loom/faces.json, faces-pending.json)`; } return null; }