/** * Home's fan deals a tool whose tables hold no rows after Settings, in the * kit's order; a tool with rows keeps its place (owner, 26 Oct 2026). The * object decides which are empty — it holds every row, and a phone's copy * keeps only a window — and says so on the home board it serves: the board's * `tools` part names them as `empty`. The phone deals what it is served. * * A tool's tables are the ones it names and the ones its views read * (Web pages reads Documents' document, Birthdays reads People's person); a * tool that reads none is never empty. The package's own tool is no card. */typeDoc = Record<string, unknown>;/** Every table `tool` names or a view of it reads. */exportfunction tablesOf(tool: Doc): string[] {const named = Array.isArray(tool.tables) ? tool.tables.filter((t): t is string => typeof t === "string") : [];const views = Array.isArray(tool.views) ? (tool.views asDoc[]) : [];const read = views.map((v) => (v.read asDoc | undefined)?.table).filter((t): t is string => typeof t === "string");return [...newSet([...named, ...read])];}/** * The ids of `tools` whose every table holds no row, in `order` (the kit's), * a tool the kit does not ship after them by id; `cards` says which tools are * cards on the fan at all. */exportfunction emptyTools(tools: Doc[], order: string[], cards: (tool: Doc) => boolean, holdsRows: (table: string) => boolean): string[] {const rank = (id: string): number => {const at = order.indexOf(id);return at < 0 ? order.length : at; };return tools .filter(cards) .filter((tool) => {const tables = tablesOf(tool);return tables.length > 0 && tables.every((t) => !holdsRows(t)); }) .map((tool) => String(tool.id)) .sort((a, b) => rank(a) - rank(b) || a.localeCompare(b));}/** * The home board as served, its `tools` part naming `empty`: on a whole board * the part itself; on one kept as changes against the shipped board * (`board_overlay`), a change to that part, which the phone's board read lays * over the shipped Home. A Home the person dropped the tools part from is left * as it is. */exportfunction withEmpty(board: Doc, empty: string[]): Doc {if (board.board_overlay !== undefined) {const dropped = Array.isArray(board.dropped) ? board.dropped : [];if (dropped.includes("tools")) return board;const changes = { fields: {}, removed: [], ...((board.changes asDoc | undefined) ?? {}) } asDoc;const fields = { ...(changes.fields asDoc) };const parts = { items: {}, removed: [], ...((fields.parts asDoc | undefined) ?? {}) } asDoc;const items = { ...(parts.items asDoc) };const tools = { fields: {}, removed: [], ...((items.tools asDoc | undefined) ?? {}) } asDoc; items.tools = { ...tools, fields: { ...(tools.fields asDoc), empty: { value: empty } } }; fields.parts = { ...parts, items };return { ...board, changes: { ...changes, fields } }; }if (!Array.isArray(board.parts)) return board;return { ...board, parts: (board.parts asDoc[]).map((p) => (p.kind === "tools" ? { ...p, empty } : p)) };}/** * A board as it is kept: its `tools` part without the `empty` the object * works out and serves (`withEmpty`), so a board a phone sends back as it was * served stores what the person said and nothing derived. On a board kept as * changes against the shipped one, the change naming `empty` goes. */exportfunction withoutEmpty(board: Doc): Doc {if (board.board_overlay !== undefined) {const changes = board.changes asDoc | undefined;const fields = changes?.fields asDoc | undefined;const parts = fields?.parts asDoc | undefined;const items = parts?.items asDoc | undefined;const tools = items?.tools asDoc | undefined;const toolFields = tools?.fields asDoc | undefined;if (!toolFields || !("empty"in toolFields)) return board;const { empty: _empty, ...kept } = toolFields;return { ...board, changes: { ...changes, fields: { ...fields, parts: { ...parts, items: { ...items, tools: { ...tools, fields: kept } } } } } }; }if (!Array.isArray(board.parts)) return board;return { ...board, parts: (board.parts asDoc[]).map((p) => {if (p.kind !== "tools" || !("empty"in p)) return p;const { empty: _empty, ...kept } = p;return kept; }), };}