import { Interpolator, Quaternion } from '@zephyr3d/base';
import { AnimationTrack } from './animationtrack.js';

/**
 * Rotation animation track
 * @public
 */ class NodeRotationTrack extends AnimationTrack {
    _state;
    _interpolator;
    constructor(modeOrInterpolator, keyFramesOrEmbedded, embedded){
        if (modeOrInterpolator === undefined) {
            super(false);
        } else if (modeOrInterpolator instanceof Interpolator) {
            if (modeOrInterpolator.target !== 'quat') {
                throw new Error(`RotationTrack(): interpolator target must be 'quat'`);
            }
            super(keyFramesOrEmbedded ?? false);
            this._interpolator = modeOrInterpolator;
        } else {
            const keyFrames = keyFramesOrEmbedded;
            const inputs = new Float32Array(keyFrames.map((val)=>val.time));
            const outputs = new Float32Array(keyFrames.length * 4);
            for(let i = 0; i < keyFrames.length; i++){
                outputs[i * 4 + 0] = keyFrames[i].value.x;
                outputs[i * 4 + 1] = keyFrames[i].value.y;
                outputs[i * 4 + 2] = keyFrames[i].value.z;
                outputs[i * 4 + 3] = keyFrames[i].value.w;
            }
            super(embedded ?? false);
            this._interpolator = new Interpolator(modeOrInterpolator, 'quat', inputs, outputs);
        }
        this._state = new Quaternion();
    }
    get interpolator() {
        return this._interpolator;
    }
    set interpolator(interp) {
        if (interp && interp.target !== 'quat') {
            throw new Error(`RotationTrack(): interpolator target must be 'quat'`);
        }
        this._interpolator = interp ?? null;
    }
    /** {@inheritDoc AnimationTrack.calculateState} */ calculateState(target, currentTime) {
        this._interpolator.interpolate(currentTime, this._state);
        return this._state;
    }
    /** {@inheritDoc AnimationTrack.applyState} */ applyState(node, state) {
        node.rotation.set(state);
    }
    /** {@inheritDoc AnimationTrack.mixState} */ mixState(a, b, t) {
        return Quaternion.slerp(a, b, t);
    }
    /** {@inheritDoc AnimationTrack.getBlendId} */ getBlendId() {
        return 'node-rotation';
    }
    /** {@inheritDoc AnimationTrack.getDuration} */ getDuration() {
        return this._interpolator.maxTime;
    }
}

export { NodeRotationTrack };
//# sourceMappingURL=rotationtrack.js.map
