import { createFailure, type FacadeResponse } from "./facade-envelope.js"; import { IMPORT_FIXTURE } from "./import-facade.js"; import { MESH_IO_FIXTURE } from "./mesh-io-facade.js"; import { PART_IO_FIXTURE } from "./part-io-facade.js"; import { PART_SAVE_FIXTURE } from "./part-save-facade.js"; import { TECHDRAW_NATIVE_FIXTURE } from "./techdraw-native-facade.js"; import { SPREADSHEET_CSV_FIXTURE } from "./spreadsheet-facade.js"; import { createFormatRuntimeLookup, resolveFormatAction, type FormatRuntimeIndex, type FormatRuntimeRecord } from "./format-runtime.js"; import { WebModuleRouter } from "./module-router.js"; const FIXTURES: Record = { "import.step": IMPORT_FIXTURE, "part.importExport": PART_IO_FIXTURE, "part.saveReopen": PART_SAVE_FIXTURE, "mesh.importExport": MESH_IO_FIXTURE, "techdraw.nativeImportExport": TECHDRAW_NATIVE_FIXTURE, "spreadsheet.importExportCsv": SPREADSHEET_CSV_FIXTURE, "points.importPly": { schema: "freecad-web-points/desktop-batch/v1", module: "Points", records: [{ apiId: "business-format:Points:import:.ply", name: "Points_Import", args: { path: "/tmp/points.ply" } }] }, "points.exportObj": { schema: "freecad-web-points/desktop-batch/v1", module: "Points", records: [{ apiId: "business-format:Points:export:.obj", name: "Points_Export", args: { path: "/tmp/points.obj", objectNames: ["Points"] } }] }, "openscad.importDxf": { schema: "freecad-web-openscad/import-dxf/v1", module: "OpenSCAD", documentId: "main-chain-document", path: "/tmp/openscad-import.dxf" }, }; export class FormatDispatcher { private readonly lookup: Map; constructor(private readonly router: WebModuleRouter, index: FormatRuntimeIndex) { this.lookup = createFormatRuntimeLookup(index); } async dispatch(formatId: string, documentId = "main-chain-document"): Promise { const record = this.lookup.get(formatId); if (!record) return createFailure(`format-${formatId}`, "unknown_format_id", "Format ID is not present in the generated runtime index", { formatId }); const route = resolveFormatAction(record); if (!route) return createFailure(`format-${formatId}`, "not_implemented", "Format record has no runnable native route", { formatId, module: record.module, direction: record.direction, kind: record.kind }); const payload = route.command === "draft.formatBatch" ? { schema: "freecad-web-draft/format-batch/v1", module: "Draft", records: [{ format: record.name.slice(1), direction: record.direction, documentId }] } : route.command === "openscad.importDxf" ? { ...(FIXTURES[route.command] as object), documentId } : FIXTURES[route.command]; if (payload === undefined) return createFailure(`format-${formatId}`, "not_implemented", "Format route has no locked native fixture", { formatId, command: route.command }); return this.router.request(route.module, route.command, payload, documentId); } }