import type { CatalogPartType, HiveFrame, HivePart } from "./types"; // A new part as the editor adds it (dialog and drag and drop alike): every // state field set to its default, a new box filled with foundation frames. /** Level 0, the first option of a choice. */ export function defaultPartState(type: CatalogPartType): HivePart["state"] { const state: HivePart["state"] = {}; for (const field of type.stateSchema) state[field.key] = field.kind === "level" ? 0 : (field.options[0] ?? ""); return state; } /** Frames of a new box: every slot a foundation (spec 9.5). */ export function newBoxFrames(type: CatalogPartType): HiveFrame[] { return Array.from({ length: type.frameSlots ?? 0 }, (_, index): HiveFrame => ({ slot: index + 1, type: "FOUNDATION" })); } export function freshPart(type: CatalogPartType, id: string, state: HivePart["state"] = defaultPartState(type)): HivePart { const part: HivePart = { id, partTypeId: type.id, state }; if (type.kind === "BOX") part.frames = newBoxFrames(type); return part; }