import type { EditBatch } from "../../services/semantic-lifecycle/edit-batch.js"; import { applyEditBatch } from "../../services/semantic-lifecycle/edit-batch.js"; import { assertOutputSafe, atomicWrite, loadIR, loadStructured, structuredText, type LifecycleActionOptions } from "../../services/semantic-lifecycle/lifecycle-io.js"; import { diagramIRToDrawio } from "../../authoring/ir-to-drawio.js"; export function run(filePath: string, _page = 0, outputPath?: string, options: LifecycleActionOptions = {}): Record { if (!options.spec) throw new Error("edit requires --spec "); if (!options.dryRun && !outputPath) throw new Error("edit requires --output unless --dry-run is used"); if (outputPath) assertOutputSafe(outputPath, [filePath, options.spec]); const source = loadIR(filePath); const batch = loadStructured(options.spec) as EditBatch; const result = applyEditBatch(source, batch, { dryRun: options.dryRun }); let output: string | undefined; if (!options.dryRun && outputPath) { const content = /\.(drawio|xml)$/i.test(outputPath) ? diagramIRToDrawio(result.ir) : structuredText(result.ir, outputPath); output = atomicWrite(outputPath, content, [filePath, options.spec]); } return { action: "edit", output, applied: result.applied, dryRun: result.dryRun, preview: options.dryRun ? result.preview : undefined }; }