/** * A place a person picks is COPIED into their own record (owner, 14 Nov * 2026): `{place: "geonames:<n>"}` 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:<uuid>"}`) 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, typeData, typeToolContext } from"../../context.ts";importtype { Answered } from"../../../published.ts";importtype { Column, Table } from"./table.ts";/** Each picked column's resolved place, by the column's key. */exporttypePicks = Map<string, Data>;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. */exportasyncfunction picksOf(ctx: ToolContext, t: Table, values: unknown): Promise<Picks> {const picks: Picks = newMap();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 asData)[c.key];const id = value && typeof value === "object" && !Array.isArray(value) ? (value asData).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:<id>)`); } elseif (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:<id>)`); } }return picks;}/** The picked places written into the record: the column's words and its siblings' facts. */exportfunction 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<Column["place"]> }) | 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; }}asyncfunction published(ctx: ToolContext, id: string): Promise<Data> {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 })) asAnswered;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 };}