/**
 * Bindable scalar / colour primitives.
 *
 * A "bindable" value is either a plain literal or a reference to a value
 * source (``{value | color, source_id}``). These types and the four
 * accessor helpers are imported widely — over 30 frontend modules read
 * them through the ``types.ts`` barrel — so they live in their own file
 * to keep the entity-shape files free of helper-function noise.
 */

// ── Bindable Float ───────────────────────────────────────────
// A scalar that is either a static value (plain number) or bound to a value source (dict).

export type BindableFloat = number | { value: number; source_id: string };

/** Extract the static value from a BindableFloat. */
export function bindableValue(b: BindableFloat | undefined, fallback: number): number {
    if (b === undefined || b === null) return fallback;
    if (typeof b === 'number') return b;
    return b.value ?? fallback;
}

/** Extract the source_id from a BindableFloat (empty string = not bound). */
export function bindableSourceId(b: BindableFloat | undefined): string {
    if (b === undefined || b === null) return '';
    if (typeof b === 'number') return '';
    return b.source_id ?? '';
}

// ── Bindable Color ──────────────────────────────────────────
// An RGB color that is either static ([R,G,B] array) or bound to a color value source.

export type BindableColor = number[] | { color: number[]; source_id: string };

/** Extract the static [R,G,B] from a BindableColor. */
export function bindableColor(b: BindableColor | undefined, fallback: number[]): number[] {
    if (b === undefined || b === null) return fallback;
    if (Array.isArray(b)) return b;
    return b.color ?? fallback;
}

/** Extract the source_id from a BindableColor (empty string = not bound). */
export function bindableColorSourceId(b: BindableColor | undefined): string {
    if (b === undefined || b === null) return '';
    if (Array.isArray(b)) return '';
    return b.source_id ?? '';
}
