import { buildOsmDetails } from './maps.helpers'; import { trekCommunityByKeys, type TrekCommunityValues } from './trek-places.client'; import { normalizePlaceWebsite } from '@trek/shared'; /** * Community values for places TREK did not find in its own index: a relation * Photon returned, a way from Overpass. The index rows carry them already * (the service lays them over its own answers); these places get them from * /v1/community, so a value one installation sent shows on the next open of * the same place everywhere. */ /** The service's key of an OSM details id: `relation:3389505` is `osm:relation/3389505`. */ export function communityKeyOf(placeId: string): string | null { const match = /^(node|way|relation):(\d+)$/.exec(placeId); return match ? `osm:${match[1]}/${match[2]}` : null; } /** The details record with what the community published laid over it; untouched where nothing was. */ export function withCommunityValues( details: Record, values: TrekCommunityValues | undefined, ): Record { if (!values?.community) return details; const out: Record = { ...details }; const website = normalizePlaceWebsite(values.contact?.website); if (website) out.website = website; if (values.contact?.phone) out.phone = values.contact.phone; if (values.contact?.email) out.email = values.contact.email; if (values.hours?.osm) { // The same shape OSM's own hours take in the details: one line per day, open now, the periods. const hours = buildOsmDetails({ opening_hours: values.hours.osm }, '', ''); if (hours.opening_hours) { out.opening_hours = hours.opening_hours; out.open_now = hours.open_now; out.opening_periods = hours.opening_periods; } } if (values.description?.text) out.summary = values.description.text; if (typeof values.community.version === 'number') out.community_version = values.community.version; return out; } /** Records built from TREK's own details layer, which carry the community answer already. */ const asked = new WeakSet(); export function markCommunityAsked(details: Record): Record { asked.add(details); return details; } /** OSM details as Overpass and Photon gave them, with the community's values for the same element. */ export async function osmDetailsWithCommunity( placeId: string, details: Record | null, ): Promise | null> { const key = communityKeyOf(placeId); if (!details || !key || asked.has(details)) return details; const published = await trekCommunityByKeys([key]); return withCommunityValues(details, published[key]); }