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

const tmpVec3 = new Vector3();
/**
 * Euler angle rotation animation track
 * @public
 */ class NodeEulerRotationTrack extends AnimationTrack {
    _state;
    _interpolator;
    constructor(modeOrInterpolator, keyFramesOrEmbedded, embedded){
        if (modeOrInterpolator === undefined) {
            super(false);
        } else if (modeOrInterpolator instanceof Interpolator) {
            if (modeOrInterpolator.target !== 'vec3') {
                throw new Error(`EulerRotationTrack(): interpolator target must be 'vec3'`);
            }
            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 * 3);
            for(let i = 0; i < keyFrames.length; i++){
                outputs[i * 3 + 0] = keyFrames[i].value.x;
                outputs[i * 3 + 1] = keyFrames[i].value.y;
                outputs[i * 3 + 2] = keyFrames[i].value.z;
            }
            super(embedded ?? false);
            this._interpolator = new Interpolator(modeOrInterpolator, 'vec3', inputs, outputs);
        }
        this._state = new Quaternion();
    }
    get interpolator() {
        return this._interpolator;
    }
    set interpolator(interp) {
        if (interp && interp.target !== 'vec3') {
            throw new Error(`EulerRotationTrack(): interpolator target must be 'vec3'`);
        }
        this._interpolator = interp ?? null;
    }
    /** {@inheritDoc AnimationTrack.calculateState} */ calculateState(target, currentTime) {
        this._interpolator.interpolate(currentTime, tmpVec3);
        this._state.fromEulerAngle(tmpVec3.x, tmpVec3.y, tmpVec3.z);
        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 { NodeEulerRotationTrack };
//# sourceMappingURL=eulerrotationtrack.js.map
