/** * 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. */exportconst KNOWN_FACES: ReadonlySet<string> = newSet([...(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. */exportfunction 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] ofObject.entries(doc asRecord<string, unknown>)) {const here = at ? `${at}.${key}` : key;if (key === "card" && value && typeof value === "object" && !Array.isArray(value) && "face"in value) {const card = value asRecord<string, unknown>; 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. */exportfunction faceProblem(doc: unknown): string | null {const offersProblem = (value: unknown): string | null => {if (!value || typeof value !== "object") returnnull;if (Array.isArray(value)) return value.map(offersProblem).find(Boolean) ?? null;const obj = value asRecord<string, unknown>;if (obj.card && typeof obj.card === "object") {const card = obj.card asRecord<string, unknown>;if ("offers"in card) {if (!Array.isArray(card.offers)) return"card.offers must be an ordered list";if (newSet(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"; } }returnObject.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 asRecord<string, unknown>).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)`; }returnnull;}