"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TouchPan = void 0;
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const Tools_1 = require("../../Tools");
const TouchController_1 = require("../../TouchController");
const PanSceneRoot_1 = require("../UC/PanSceneRoot");
class TouchPan extends core_1.AppObjectController {
    get panRootUC() {
        return this.getCachedLocalComponent(PanSceneRoot_1.PanSceneRootUC.type);
    }
    get toolRepo() {
        return this.getCachedSingleton(Tools_1.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 === Tools_1.ToolState.ACTIVE;
            if (e.touches.length === 1 && panToolIsAcvtive) {
                return true;
            }
            else if (e.touches.length === 2) {
                const dist = core_2.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 = TouchController_1.TouchInputEntity.get(appObjects);
        if (touchInput) {
            touchInput.registerController(this);
        }
        else {
            this.warn("Unable to find TouchInputEntity");
        }
    }
}
exports.TouchPan = TouchPan;
TouchPan.type = "TouchPan";
//# sourceMappingURL=TouchPan.js.map