import { setKeyWiring, type ContactWiring, type ElcFile } from "./elc"; /** Assigns a row-major matrix to the supplied keys. The input order is preserved. */ export function wireMatrixKeys( elc: ElcFile, keyIds: string[], rowPins: string[], colPins: string[], diode?: "a-to-b" | "b-to-a", ): ElcFile { if (!rowPins.length || !colPins.length) throw new Error("rowPins and colPins are required"); if (keyIds.length > rowPins.length * colPins.length) throw new Error("Matrix does not have enough row/column positions"); let next = elc; keyIds.forEach((key, i) => { const wiring: ContactWiring = { key, detect: "contact", terminals: { a: rowPins[Math.floor(i / colPins.length)]!, b: colPins[i % colPins.length]! }, ...(diode ? { diode } : {}), }; // Keep unknown extension fields on an existing ELC entry, just like normal edits. next = setKeyWiring(next, wiring); }); return next; }