/* ============================================================================= * B05_Profile_UI_Structures_Balloon.ts * 종단 구조물 알약 정보 라벨 줄(54-14) — 화면 요소 없는 순수 규칙. B05 · B06 알약 레인이 같이 씀. * * · 2행 — 1행 = 종류 이름(기타 = 손 이름) + 기준 측점(구간 = 시작 ~ 끝) · 2행 = 컨테이너마다 첫 옵션 * 값만 「 · 」 로 이음(제목 · 옵션 이름 없음 · 빈 값은 건너뜀) · 값이 하나도 없으면 1행만 · 메모 · 그 밖 옵션은 안 씀 * · 컨테이너 = 좌측 패널 폼과 같은 상자 — 명세 종류(`STRUCTURE_SPECS`)는 명세 컨테이너(선택 컨테이너는 * 인스턴스마다) · 그 밖 종류는 정의 옵션 그룹(`StructureType.groups`) * · 첫 옵션 = 지금 보이는 첫 입력 칸(자동 칸 · 갈래가 없어 가린 칸 · 끈 칸은 건너뜀) * · 값 `auto` = 「(자동)」 · 빈 값은 건너뜀(기본값으로 안 바꿈 · 기본값은 새 구조물 칸에 실제 값) * ========================================================================== */ import type { StructureInstance, StructureType } from "./B05_Profile_Api_Structures"; import { STRUCTURE_SPECS } from "../A00_Common/design/design_structure_pipe"; import { bodyOf, boxOptionsIn } from "../A00_Common/design/design_structure_box"; import { withLegacyGuards } from "../A00_Common/design/design_structure_ford"; import { branchOf, choiceLabel, choicesOf, readState, type Instance, type SpecContainer, type SpecItem, type StructureSpec, } from "../A00_Common/design/design_structure_spec"; const AUTO_MARK = "(자동)"; const SEP = " · "; /** 선택 컨테이너 개수를 밖에서 정함(배관 흙막이 = 유출 단 수 · 폼 `hooks.count` 와 같은 값). */ export type ContainerCount = (containerId: string) => number | null | undefined; /** 라벨 줄 — [이름 기준 측점, 컨테이너 값 이음?]. */ export function balloonLines( structure: StructureInstance, type: StructureType | undefined, head: { name: string; position: string }, count?: ContainerCount, ): string[] { const spec = STRUCTURE_SPECS[structure.type_id]; const values = ( spec ? specLines(spec, structure, type, count) : groupLines(structure.options ?? {}, type) ).filter(Boolean); const first = `${head.name} ${head.position}`; return values.length ? [first, values.join(SEP)] : [first]; } /** 명세 컨테이너 값 — 폼(`Drainage_Facility.setFacility` · `paintStructureSpec`)과 같은 값 · 이름(빈 값 = ""). */ function specLines( spec: StructureSpec, structure: StructureInstance, type: StructureType | undefined, count?: ContainerCount, ): string[] { let names = type?.spec_names ?? {}; if (structure.type_id === "pipe") { // 정의 파일의 옛 이름 「흙막이 추가」 — 명세 이름 「흙막이」가 앞섬(54-1) const { soil_add: _old, ...pipeNames } = names; names = pipeNames; } const state = formState(spec, structure.type_id, structure.options ?? {}); return spec.containers.flatMap((c) => instancesOf(c, state[c.id] ?? [], count).map((values) => { const item = c.items.find( (entry) => entry.kind !== "auto" && (!entry.by || branchOf(entry, values)), ); return item ? specValue(c, item, values, names) : ""; }), ); } /** 저장 값 → 폼 칸 값 — 폼(`Drainage_Facility.setFacility`)과 같은 손질(BOX 옛 설치 · 본체 규격 · * 세월교 옛 날개벽). */ function formState( spec: StructureSpec, typeId: string, options: Record, ): Record { const input = typeId === "box_culvert" ? boxOptionsIn(options) : typeId === "ford_bridge" ? withLegacyGuards(options) : options; const state = readState(spec, input); // 본체 규격은 저장 안 함 — 폭 · 높이로 되찾음 if (typeId === "box_culvert") state.body = [{ ...state.body?.[0], ...bodyOf(options) }]; return state; } /** 보이는 인스턴스 — 필수 = 한 벌 · 선택 = 개수가 정해졌으면 그 수(모자라면 빈 인스턴스) · 아니면 저장된 것. */ function instancesOf(c: SpecContainer, list: Instance[], count?: ContainerCount): Instance[] { if (c.required) return [list[0] ?? {}]; const n = count?.(c.id); return n === undefined || n === null ? list : Array.from({ length: n }, (_, index) => list[index] ?? {}); } function specValue( c: SpecContainer, item: SpecItem, values: Instance, names: Record, ): string { const shown = (raw: string): string => { const choice = choicesOf(item, values).find((ch) => ch.value === raw); return choice ? choiceLabel(c, item, choice, names) : raw; }; const raw = (values[item.id] ?? "").trim(); if (raw === "auto") return AUTO_MARK; return raw ? shown(raw) : ""; } /** 정의 옵션 그룹 값 — 그룹마다 첫 옵션(정의에 있고 켜진 칸 · 빈 값 = ""). */ function groupLines( options: Record, type: StructureType | undefined, ): string[] { const byKey = new Map((type?.options ?? []).map((option) => [option.key, option])); return (type?.groups ?? []).map((group) => { const option = group.options .map((key) => byKey.get(key)) .find((entry) => entry && entry.visible !== false && entry.enabled !== false); if (!option) return ""; const raw = options[option.key]; if (raw === "auto") return AUTO_MARK; return raw !== undefined && raw !== "" ? `${raw}${option.unit ?? ""}` : ""; }); }