import type { MapGeoJSONFeature } from "maplibre-gl"; import type { DetailFeature } from "../types"; import { formatValue } from "../utils/format-value"; import { SOURCE_LAYERS, type WaterNetworkSourceId } from "./sources"; export function getFeatureId(feature: MapGeoJSONFeature) { const raw = feature.properties?.id ?? feature.id; return raw === undefined || raw === null ? "" : String(raw); } export function getWaterNetworkSourceId(feature: MapGeoJSONFeature): WaterNetworkSourceId | null { const entry = Object.entries(SOURCE_LAYERS).find(([, sourceLayer]) => sourceLayer === feature.sourceLayer); return (entry?.[0] as WaterNetworkSourceId | undefined) ?? null; } export function toDetailFeature(feature: MapGeoJSONFeature): DetailFeature { const layer = getWaterNetworkSourceId(feature); if (!layer) { throw new Error(`Unsupported water network source layer: ${feature.sourceLayer ?? "unknown"}`); } const id = getFeatureId(feature); const properties = feature.properties ?? {}; const presentation = getFeaturePresentation(layer, id, properties); return { id, layer, ...presentation, properties }; } function getFeaturePresentation( layer: WaterNetworkSourceId, id: string, properties: Record ) { const displayId = id || "未命名"; switch (layer) { case "conduits": return { title: `管渠 ${displayId}`, subtitle: `DN${formatValue(properties.diameter)} · ${formatValue(properties.length)} m` }; case "junctions": return { title: `检查井 ${displayId}`, subtitle: `最大深度 ${formatValue(properties.max_depth)} m · 井底高程 ${formatValue(properties.invert_elevation)} m` }; case "orifices": return { title: `孔口 ${displayId}`, subtitle: `类型 ${formatValue(properties.orifice_type)} · 断面 ${formatValue(properties.shape)}` }; case "pumps": return { title: `泵 ${displayId}`, subtitle: `状态 ${formatValue(properties.status)} · 曲线 ${formatValue(properties.pump_curve)}` }; case "outfalls": return { title: `排放口 ${displayId}`, subtitle: `类型 ${formatValue(properties.outfall_type)} · 井底高程 ${formatValue(properties.invert_elevation)} m` }; } }