import { AppObjectController } from "@vived/core";
import { Vector2 } from "@vived/core";
import { ToolRepository, ToolState } from "../../Tools";
import { TouchInputEntity, } from "../../TouchController";
import { PanSceneRootUC } from "../UC/PanSceneRoot";
export class TouchPan extends AppObjectController {
    get panRootUC() {
        return this.getCachedLocalComponent(PanSceneRootUC.type);
    }
    get toolRepo() {
        return this.getCachedSingleton(ToolRepository.type);
    }
    constructor(appObject) {
        super(appObject, TouchPan.type);
        this.PAN_FINGER_DISTANCE_PX = 80;
        this.isPanning = false;
        this.allow = (e) => {
            const panTool = this.toolRepo?.getTool("PAN");
            if (!panTool)
                return false;
            const panToolIsAcvtive = panTool.state === ToolState.ACTIVE;
            if (e.touches.length === 1 && panToolIsAcvtive) {
                return true;
            }
            else if (e.touches.length === 2) {
                const dist = Vector2.Subtract(e.touches[0].position, e.touches[1].position).magnitued;
                if (dist > this.PAN_FINGER_DISTANCE_PX) {
                    return true;
                }
            }
            return false;
        };
        this.onTouchStart = (e) => {
            if (!this.allow(e)) {
                this.isPanning = false;
                this.panRootUC?.cancelPan();
                return;
            }
            this.panRootUC?.initializePan(e.touchCenter);
            this.isPanning = true;
        };
        this.onTouchMove = (e) => {
            if (!this.isPanning)
                return;
            const pixelPosition = e.touches[0].position;
            this.panRootUC?.doPan(pixelPosition);
        };
        this.onTouchEnd = (e) => {
            this.isPanning = false;
            this.panRootUC?.finishPan();
        };
        const appObjects = appObject.appObjectRepo;
        const touchInput = TouchInputEntity.get(appObjects);
        if (touchInput) {
            touchInput.registerController(this);
        }
        else {
            this.warn("Unable to find TouchInputEntity");
        }
    }
}
TouchPan.type = "TouchPan";
//# sourceMappingURL=TouchPan.js.map