import { AppObjectController } from "@vived/core";
import { Vector2 } from "@vived/core";
import { ToolRepository, ToolState } from "../../Tools";
import { TouchInputEntity, } from "../../TouchController";
import { OrbitSceneRootUC } from "../UC/OrbitSceneRoot";
export class TouchOrbit extends AppObjectController {
    get orbitSceneRoot() {
        return this.getCachedLocalComponent(OrbitSceneRootUC.type);
    }
    get toolRepo() {
        return this.getCachedSingleton(ToolRepository.type);
    }
    constructor(appObject) {
        super(appObject, TouchOrbit.type);
        this.isOrbiting = false;
        this.ORBIT_FINGER_DISTANCE_PX = 80;
        this.allow = (e) => {
            const orbitTool = this.toolRepo?.getTool("ORBIT");
            if (!orbitTool)
                return false;
            const orbitToolIsActive = orbitTool.state === ToolState.ACTIVE;
            if (e.touches.length === 1 && orbitToolIsActive) {
                return true;
            }
            else if (e.touches.length === 2) {
                const dist = Vector2.Subtract(e.touches[0].position, e.touches[1].position).magnitued;
                if (dist < this.ORBIT_FINGER_DISTANCE_PX) {
                    return true;
                }
            }
            return false;
        };
        this.onTouchStart = (e) => {
            if (!this.allow(e)) {
                this.isOrbiting = false;
                this.orbitSceneRoot?.cancelOrbit();
                return;
            }
            this.orbitSceneRoot?.initializeOrbit(e.touchCenter);
            this.isOrbiting = true;
        };
        this.onTouchMove = (e) => {
            if (!this.isOrbiting)
                return;
            this.orbitSceneRoot?.doOrbit(e.touchCenter);
        };
        this.onTouchEnd = () => {
            this.isOrbiting = false;
            this.orbitSceneRoot?.finishOrbit();
        };
        const appObjects = appObject.appObjectRepo;
        const touchInput = TouchInputEntity.get(appObjects);
        if (touchInput) {
            touchInput.registerController(this);
        }
        else {
            this.warn("Unable to find TouchInputEntity");
        }
    }
}
TouchOrbit.type = "TouchOrbit";
//# sourceMappingURL=TouchOrbit.js.map