import { ThreeUtils } from "../utils";
import { Constants } from "../Constants";
export class AlFixedToOrbitCamera {
    static get Object() {
        return {
            schema: {
                distanceFromTarget: { type: "number", default: 0.1 },
                target: { type: "vec3" }
            },
            init(_data) {
                this.tickFunction = AFRAME.utils.throttle(this.tickFunction, Constants.minFrameMS, this);
                if (this.data.target) {
                    let targ = ThreeUtils.objectToVector3(this.data.target);
                    this.state = {
                        distanceFromTarget: this.data.distanceFromTarget,
                        target: targ
                    };
                }
                else {
                    this.state = {
                        distanceFromTarget: this.data.distanceFromTarget,
                        target: new THREE.Vector3(0, 0, 0)
                    };
                }
            },
            update(_oldData) {
                let targ = ThreeUtils.objectToVector3(this.data.target);
                this.state = {
                    distanceFromTarget: this.data.distanceFromTarget,
                    target: targ
                };
            },
            tickFunction() {
                let el = this.el;
                let state = this.state;
                const camPos = el.sceneEl.camera.position;
                const dir = state.target
                    .clone()
                    .sub(camPos.clone()).normalize();
                el.object3D.position.copy(dir.multiplyScalar(state.distanceFromTarget));
                el.object3D.lookAt(camPos);
            },
            tick() {
                this.tickFunction();
            }
        };
    }
    static get Tag() {
        return "al-fixed-to-orbit-camera";
    }
}
