/* ============================================================================= * M01_MasterData_UI_Logic_Note.ts * 원문 표 읽기(`loadTable`) — 식 쉬운 말 풀이는 옛 상세 몸과 같이 걷음(855 2-E1). * * 순수 읽기 — `/elements` 로 표 파일을 찾고 `/table` 로 통째 읽음. * ========================================================================== */ import { API_BASE_URL } from "@config/config_frontend"; import { logicApiPaths, type ElementBrief } from "./M01_MasterData_UI_Logic_Api"; /* ── 원문 표(소요량·계수) 읽기 ─────────────────────────────────────────── */ export interface TableRow { file: string; 키: string; 이름?: string; 원문번호?: string; 출처?: string; 용도?: { 공종?: string; 대상?: string[]; 로직키?: string[] }; 조건?: Record; 값칸?: Record; 줄?: Record[]; } const tableCache = new Map>(); async function getJson(path: string): Promise { try { const res = await fetch(`${API_BASE_URL}${logicApiPaths().read}${path}`); if (!res.ok) return null; return (await res.json()) as T; } catch { return null; } } export function loadTable(key: string): Promise { const cached = tableCache.get(key); if (cached) return cached; const job = (async (): Promise => { for (const group of ["소요량", "계수"]) { const found = await getJson<{ items: ElementBrief[] }>( `/elements?group=${encodeURIComponent(group)}&q=${encodeURIComponent(key)}&limit=5`, ); const file = found?.items?.find((i) => i.ref === key)?.file; if (!file) continue; const got = await getJson<{ table: TableRow }>( `/table?file=${encodeURIComponent(file)}&key=${encodeURIComponent(key)}`, ); if (got?.table) return { ...got.table, file }; } return null; })(); tableCache.set(key, job); return job; }