/** * A place a person picks is COPIED into their own record (owner, 14 Nov * 2026): `{place: "geonames:"}` names a row of Cotton's published places * (src/published.ts), and the write keeps its name in the place column and its * id, country, zone — and, where the definition names them, its coordinates * and the published version — in the column's read-only siblings. The record * is whole offline and never changes under the person when a later version is * published. Saved locations (`{place: "location:"}`) are retired * (TABLE DROP 1, 27 Nov 2026): such a pick is refused by name. * * The reads happen before the write (they cross to another object); the write * then fills the siblings as the resolver, which no tool's own values may do. */ import { fail, type Data, type ToolContext } from "../../context.ts"; import type { Answered } from "../../../published.ts"; import type { Column, Table } from "./table.ts"; /** Each picked column's resolved place, by the column's key. */ export type Picks = Map; const PLACE_ID = /^geonames:[1-9][0-9]{0,17}$/; const LOCATION_ID = /^location:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/; /** The places the values name, read from the published database before the write. */ export async function picksOf(ctx: ToolContext, t: Table, values: unknown): Promise { const picks: Picks = new Map(); if (!values || typeof values !== "object" || Array.isArray(values)) return picks; for (const c of t.columns) { if (c.type !== "place" || !c.place) continue; const value = (values as Data)[c.key]; const id = value && typeof value === "object" && !Array.isArray(value) ? (value as Data).place : undefined; if (typeof id !== "string") continue; if (LOCATION_ID.test(id)) { // Saved locations are retired (TABLE DROP 1): their table is no longer served. fail(`${c.key}: saved locations are retired; choose a place from Cotton's published places (geonames:)`); } else if (PLACE_ID.test(id)) { picks.set(c.key, await published(ctx, id)); } else { fail(`${c.key}: choose a place from Cotton's published places (geonames:)`); } } return picks; } /** The picked places written into the record: the column's words and its siblings' facts. */ export function applyPicks(t: Table, values: Data, picks: Picks): void { for (const [key, place] of picks) { const c = t.columns.find((column) => column.key === key) as (Column & { place: NonNullable }) | undefined; if (!c) continue; values[key] = place.name; values[c.place.id] = place.id; values[c.place.country] = place.country; values[c.place.zone] = place.zone; if (c.place.lat && typeof place.lat === "number") values[c.place.lat] = place.lat; if (c.place.lng && typeof place.lng === "number") values[c.place.lng] = place.lng; if (c.place.version && typeof place.version === "string") values[c.place.version] = place.version; } } async function published(ctx: ToolContext, id: string): Promise { const object = ctx.env.PUBLISHED.get(ctx.env.PUBLISHED.idFromName("cotton/places"), { locationHint: "apac" }); const answered = (await object.read("cotton", "places", { kind: "ids", table: "places", ids: [id], version: null })) as Answered; if ("refused" in answered) fail(`Cotton's places could not be read: ${answered.refused.message}`); const row = answered.ok.rows?.[0] ?? fail(`${id} is not one of Cotton's published places`); return { ...row, version: answered.ok.version }; }