import { useRef, useCallback } from 'react';

/**
 * Generic ref-based interpolation hook for orb state-driven animation.
 *
 * - When `state` is defined, target values come from the config map.
 * - When `state` is undefined, target values come from the `propValues` fallback
 *   (backwards-compatible — orb behaves exactly as if no state was set).
 * - Returns a mutable `valuesRef` read inside animation loops, and a `tick(dt)`
 *   function that advances the interpolation by `dt` seconds.
 * - Zero React re-renders — all mutation is ref-based.
 */
function useOrbState(state, config, propValues, damping = 4) {
    const valuesRef = useRef({ ...propValues });
    const tick = useCallback((dt) => {
        const target = state ? config[state] : propValues;
        const current = valuesRef.current;
        const factor = 1 - Math.exp(-damping * dt);
        for (const key in target) {
            current[key] = (current[key] +
                (target[key] - current[key]) * factor);
        }
    }, [state, config, propValues, damping]);
    return { valuesRef, tick };
}

export { useOrbState };
