"""Apply the project ground manager integration to the existing application.""" from pathlib import Path root = Path(__file__).resolve().parents[1] p = root / 'src/main.ts' s = p.read_text() s = "import { createProjectGroundManager } from './view/project-ground';\nimport { applyProjectGround, groundInputChanged } from './core/project-ground';\nimport { registerGroundImporter } from '../pierzainer_ground/ground-link';\n" + s s = s.replace('let groundModel: GroundModelFile | null = null;', 'let groundModel: GroundModelFile | null = null;\nlet groundProjectVersion = 0;\nlet groundImportSerial = 0;') marker = 'function el' insert = '''async function importProjectGround(text: string, name: string): Promise { const version = groundProjectVersion, serial = ++groundImportSerial; const imported = parseGroundModel(text); await Promise.all((imported.mapLayers ?? []).map(decodeGroundMapImage)); if (version !== groundProjectVersion || serial !== groundImportSerial) throw new Error('프로젝트 또는 불러올 모델이 변경되어 이전 결과를 적용하지 않았습니다.'); groundModel = imported; groundMapMode = imported.mapLayers?.length ? 'satellite' : 'none'; setGroundMapMode(groundMapMode); state.visible.ground = true; setupScene(); renderAll(); setStatus(`${name} 지반모델을 프로젝트 공통으로 불러왔습니다. 설계 입력은 비교 후 적용하세요.`); } registerGroundImporter(importProjectGround); const projectGround = createProjectGroundManager({ snapshot: () => ({ model: groundModel, piers: state.piers }), importModel: importProjectGround, apply: (names, fields) => { if (!groundModel) return; for (const p of state.piers) if (names.has(p.input.name)) p.draft = applyProjectGround(p.draft, groundModel, fields); renderAll(); }, regenerate: () => { state.piers = state.piers.map(p => groundInputChanged(p.input, p.draft) ? regenerateEntry(p) : p); setupScene(); renderAll(); setStatus('지반 입력이 변경된 교각을 다시 생성했습니다. 생성 상태와 알림을 확인하세요.'); }, }); function resetProjectGround(): void { groundProjectVersion++; groundImportSerial++; projectGround.reset(); window.dispatchEvent(new Event('pierzainer:project-changed')); } ''' assert marker in s s = s.replace(marker, insert + marker, 1) s = s.replace(' updatePartToggles();\n $', ' updatePartToggles();\n projectGround.refresh();\n $', 1) start = s.index(' // ② 지반 모델 (승인 2026-10-23):') end = s.index(' if (!groundModel) {', start) s = s[:start] + " wrap.appendChild(el('p', '프로젝트 공통 지반모델은 ① 형상의 [프로젝트 지반모델]에서 생성·불러오기·선택 적용합니다.', 'muted small'));\n\n" + s[end:] s = s.replace(' * The ground model import and the 지반고 apply stay at the bottom, as before.', ' * Project model generation/import is managed from ①; this card retains local inputs.') s = s.replace(' state.project = { ...parsed.project };', ' resetProjectGround();\n state.project = { ...parsed.project };', 1) marker = " state.project = { ...state.project, name, bridge: $('bridge-name').value.trim() };" assert marker in s s = s.replace(marker, " resetProjectGround();\n groundModel = null;\n groundMapMode = 'none';\n setGroundMapMode(groundMapMode);\n" + marker, 1) p.write_text(s)