import { FacadeHandlerError, FacadeRequest, FacadeResponse, FacadeSerialQueue, createFailure, createRequest, isFacadeRequest } from "./facade-envelope.js"; import type { OpenScadKernelModule } from "./openscad-inspect-facade.js"; export const OPENSCAD_IMPORT_DXF_SCHEMA = "freecad-web-openscad/import-dxf/v1" as const; export const OPENSCAD_IMPORT_DXF_COMMAND = "openscad.importDxf" as const; export interface OpenScadImportDxfPayload { schema: typeof OPENSCAD_IMPORT_DXF_SCHEMA; module: "OpenSCAD"; documentId: string; path: string; layer?: string; } export interface OpenScadImportDxfResult { schema: typeof OPENSCAD_IMPORT_DXF_SCHEMA; module: "OpenSCAD"; documentId: string; documentHandle: string; createdDocument: boolean; objectNames: string[]; objectCount: number; faceCount: number; recomputed: true; sourceEntry: string; resourcesReleased: true; } export type OpenScadImportDxfRequest = FacadeRequest; export function createOpenScadImportDxfRequest(requestId: string, payload: OpenScadImportDxfPayload): OpenScadImportDxfRequest { return createRequest(requestId, OPENSCAD_IMPORT_DXF_COMMAND, payload, payload.documentId); } export function isOpenScadImportDxfRequest(value: unknown): value is OpenScadImportDxfRequest { if (!isFacadeRequest(value)) return false; const request = value as OpenScadImportDxfRequest; const payload = request.payload; return request.command === OPENSCAD_IMPORT_DXF_COMMAND && payload?.schema === OPENSCAD_IMPORT_DXF_SCHEMA && payload?.module === "OpenSCAD" && typeof payload.documentId === "string" && payload.documentId.length > 0 && typeof payload.path === "string" && payload.path.length > 0 && (payload.layer === undefined || typeof payload.layer === "string"); } export async function dispatchOpenScadImportDxf(queue: FacadeSerialQueue, request: OpenScadImportDxfRequest, kernelSource: OpenScadKernelModule | Promise): Promise> { const requestId = request.requestId; if (!isOpenScadImportDxfRequest(request)) return createFailure(requestId, "invalid_request", "OpenSCAD DXF import envelope is invalid"); return queue.dispatch(request, async (payload, signal) => { if (signal.aborted) throw new FacadeHandlerError("cancelled", "request was cancelled"); const kernel = await kernelSource; const raw = String(kernel.ccall("fc_web_dispatch_json", "string", ["string"], [JSON.stringify({ cmd: OPENSCAD_IMPORT_DXF_COMMAND, args: payload })])); let reply: Record; try { reply = JSON.parse(raw) as Record; } catch { throw new FacadeHandlerError("native_protocol_error", "OpenSCAD native dispatcher returned invalid JSON"); } if (reply.ok !== true) throw new FacadeHandlerError(String(reply.code || "import_failed"), String(reply.error || "OpenSCAD DXF import failed")); if (reply.schema !== OPENSCAD_IMPORT_DXF_SCHEMA || reply.module !== "OpenSCAD" || reply.documentId !== payload.documentId || typeof reply.documentHandle !== "string" || !reply.documentHandle || typeof reply.createdDocument !== "boolean" || !Array.isArray(reply.objectNames) || reply.objectNames.some((name) => typeof name !== "string" || !name) || reply.objectCount !== reply.objectNames.length || typeof reply.faceCount !== "number" || !Number.isInteger(reply.faceCount) || reply.faceCount < 0 || reply.faceCount !== reply.objectCount || typeof reply.sourceEntry !== "string" || reply.sourceEntry.length === 0 || reply.recomputed !== true || reply.resourcesReleased !== true) { throw new FacadeHandlerError("native_protocol_error", "OpenSCAD DXF import returned an invalid result schema"); } return reply as unknown as OpenScadImportDxfResult; }); }