/* ============================================================================= * common_util_line_stack_draft.ts * 줄 쌓기 기계 창 초안(PLAN 827-1) — 문서 칸 `내기계초안` 다루기(화면 · fetch 없음). * 초안 하나 = {표지 「EQP초안-N」, 키?(고칠 내 기계), 본뜬?(바탕 키), 줄(내 기계 몸), 시간당?(목록 금액 · 식 키)} * 줄 입력 기계='개인 EQP초안-N' 이 가리킴 · 단가산출 [저장] 때 서버가 만들기 · 고침 · 키 바꿈 * (짝 = `M02_MasterTemplete/M02_MyMachine_Draft.py` — 칸 이름 · 표지 꼴) * ========================================================================== */ import { varId, type FormulaDoc } from "./common_util_formula_doc"; export const DRAFT_FIELD = "내기계초안"; const TAG = /EQP초안-\d+/g; export interface DraftItem { 표지: string; 키?: string; 본뜬?: string; 줄: { 이름: string; 규격: string; 방식: string; 값?: Record; 시간당?: object }; 시간당?: { ok: boolean; 계?: number; 로직?: string | null }; } type Holder = FormulaDoc & { [DRAFT_FIELD]?: DraftItem[] }; export const draftsOf = (doc: FormulaDoc | null | undefined): DraftItem[] => ((doc as Holder | null | undefined)?.[DRAFT_FIELD] ?? []).filter(Boolean); /** 기계 입력 글(따옴표 뗌)이 초안인지 */ export const isDraftNo = (no: string | null | undefined): boolean => /^개인 EQP초안-\d+$/.test(no ?? ""); /** 문서가 안 쓰는 초안은 뺌 · 다 빠지면 칸 지움 */ export function pruneDrafts(doc: FormulaDoc): void { const box = doc as Holder; if (!box[DRAFT_FIELD]) return; const { [DRAFT_FIELD]: list, ...rest } = box; const used = new Set(JSON.stringify(rest).match(TAG) ?? []); const keep = (list ?? []).filter((d) => used.has(d.표지)); if (keep.length) box[DRAFT_FIELD] = keep; else delete box[DRAFT_FIELD]; } /** 기계 줄 연결 → 지금 기계 입력 글(따옴표 뗌) · 옵션 기호(`S_굴착기`)면 그 옵션 값 · 없음 = null */ export function machineNow( doc: FormulaDoc, link: { 갈래?: string; 입력?: Record }, ): string | null { const arg = link.갈래 === "단가산출" ? String(link.입력?.기계 ?? "").trim() : ""; if (!arg) return null; if (/^'.*'$/.test(arg)) return arg.slice(1, -1); const val = (doc.변수 ?? []).find((x) => varId(x) === arg)?.연결?.값; return val === undefined || val === null || String(val) === "" ? null : String(val); }