import type { CircuitTreeCircuitDto, CircuitTreeComponentDto, CircuitTreeResponseDto, CircuitTreeSectionDto, } from "../types.js"; export type CircuitStructureProjectionRow = | { rowKey: string; rowType: "headerComponent" | "footerComponent"; zone: "header" | "footer"; component: CircuitTreeComponentDto; } | { rowKey: string; rowType: "groupHeader" | "groupPlaceholder"; zone: "group"; section: CircuitTreeSectionDto; } | { rowKey: string; rowType: "groupComponent"; zone: "group"; section: CircuitTreeSectionDto; component: CircuitTreeComponentDto; } | { rowKey: string; rowType: "circuitBlock"; zone: "group"; section: CircuitTreeSectionDto; circuit: CircuitTreeCircuitDto; }; export function buildCircuitStructureProjection( tree: Pick< CircuitTreeResponseDto, "headerComponents" | "sections" | "footerComponents" > ): CircuitStructureProjectionRow[] { const rows: CircuitStructureProjectionRow[] = []; for (const component of ordered(tree.headerComponents)) { rows.push({ rowKey: `header-component:${component.id}`, rowType: "headerComponent", zone: "header", component, }); } for (const section of ordered(tree.sections)) { rows.push({ rowKey: `group:${section.id}`, rowType: "groupHeader", zone: "group", section, }); for (const component of ordered(section.components ?? [])) { rows.push({ rowKey: `group-component:${component.id}`, rowType: "groupComponent", zone: "group", section, component, }); } for (const circuit of ordered(section.circuits)) { rows.push({ rowKey: `circuit-block:${circuit.id}`, rowType: "circuitBlock", zone: "group", section, circuit, }); } rows.push({ rowKey: `group-placeholder:${section.id}`, rowType: "groupPlaceholder", zone: "group", section, }); } for (const component of ordered(tree.footerComponents)) { rows.push({ rowKey: `footer-component:${component.id}`, rowType: "footerComponent", zone: "footer", component, }); } return rows; } function ordered( values: readonly T[] ) { return [...values].sort( (left, right) => left.sortOrder - right.sortOrder || left.id.localeCompare(right.id) ); }