/* exporter.js — collect busbars into the backend payload and download file. */ const Exporter = (() => { function buildPayload(state, params) { const cellsById = new Map(state.cells.map((c) => [c.id, c])); const busbars = state.busbars.map((bb) => ({ name: bb.name, color: bb.color, shape: bb.shape || "panel", strip_width: params.stripWidth, pad_radius: params.padRadius, hole_radius: params.holeRadius, hole_shape: params.holeShape || "cross", slit_width: params.slitWidth, neighbor_factor: params.neighborFactor, cells: bb.cells .map((cid) => { const c = cellsById.get(cid); return c ? { id: c.id, x: c.x, y: c.y } : null; }) .filter(Boolean), })); return { units: "mm", extrude: !!params.extrude, thickness: params.thickness, busbars, }; } async function exportFormat(fmt, state, params) { if (!state.busbars.length) { alert("Create at least one busbar before exporting."); return; } const payload = buildPayload(state, params); const res = await fetch(`/api/export/${fmt}`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(payload), }); if (!res.ok) { let msg; try { msg = (await res.json()).error; } catch { msg = await res.text(); } alert(`Export failed: ${msg}`); return; } const blob = await res.blob(); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = `busbars.${fmt}`; document.body.appendChild(a); a.click(); setTimeout(() => { URL.revokeObjectURL(url); a.remove(); }, 100); } return { exportFormat, buildPayload }; })();