// Parameter schema shared by all compositions. The author panel is generated from it, // and a composition's config is just a flat { key: value } object. export type ParamValue = number | string | boolean; export type Params = Record; interface Base { key: string; label: string; group: string; hint?: string; } export interface NumberSpec extends Base { kind: "number"; default: number; min: number; max: number; step?: number; /** slider moves logarithmically (min must be > 0) */ log?: boolean; unit?: string; } export interface SelectSpec extends Base { kind: "select"; default: string; options: string[]; } export interface BoolSpec extends Base { kind: "bool"; default: boolean; } export interface TextSpec extends Base { kind: "text"; default: string; } export type ParamSpec = NumberSpec | SelectSpec | BoolSpec | TextSpec; export function schemaDefaults(schema: ParamSpec[]): Params { const out: Params = {}; for (const s of schema) out[s.key] = s.default; return out; } /** Merge layers onto the schema, keeping only known keys with the right type. */ export function resolveParams(schema: ParamSpec[], ...layers: (Partial | null | undefined)[]): Params { const out = schemaDefaults(schema); for (const layer of layers) { if (!layer) continue; for (const s of schema) { const v = layer[s.key]; if (v === undefined) continue; if (s.kind === "number" && typeof v === "number" && Number.isFinite(v)) out[s.key] = v; else if (s.kind === "select" && typeof v === "string" && s.options.includes(v)) out[s.key] = v; else if (s.kind === "bool" && typeof v === "boolean") out[s.key] = v; else if (s.kind === "text" && typeof v === "string") out[s.key] = v; } } return out; } /** JSON in schema order, ready to paste over a composition's defaults.json. */ export function serializeParams(schema: ParamSpec[], params: Params): string { const ordered: Params = {}; for (const s of schema) ordered[s.key] = params[s.key]; return JSON.stringify(ordered, null, 2) + "\n"; }