"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TouchLandmark = void 0;
exports.makeTouchLandmark = makeTouchLandmark;
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const Babylon_1 = require("../../Babylon");
const TouchController_1 = require("../../TouchController");
const TryAddNewLandmarkUC_1 = require("../UseCases/TryAddNewLandmarkUC");
const TryPreviewLandmarkUC_1 = require("../UseCases/TryPreviewLandmarkUC");
class TouchLandmark extends core_1.AppObjectController {
}
exports.TouchLandmark = TouchLandmark;
TouchLandmark.type = "TOUCH_LANDMARK_CONTROLLER";
function makeTouchLandmark(appObject) {
    return new TouchLandmarkImp(appObject);
}
class TouchLandmarkImp extends TouchLandmark {
    get tryPreviewLandmarkUc() {
        return this.getCachedSingleton(TryPreviewLandmarkUC_1.TryPreviewLandmarkUC.type);
    }
    get tryAddLandmarkUc() {
        return this.getCachedSingleton(TryAddNewLandmarkUC_1.TryAddNewLandmarkUC.type);
    }
    constructor(appObject) {
        super(appObject, TouchLandmark.type);
        this.lastTouch = core_2.Vector2.Zero();
        this.allow = (e) => {
            if (e.touches.length === 1) {
                return true;
            }
            return false;
        };
        this.onTouchStart = (e) => {
            if (!this.allow(e))
                return;
            if (!this.tryPreviewLandmarkUc)
                return;
            const position = e.touches[0].position;
            this.lastTouch = position;
            const ray = (0, Babylon_1.pixelToSceneRay)(position, this.appObjects);
            this.tryPreviewLandmarkUc.tryPreviewLandmark(ray, true);
        };
        this.onTouchMove = (e) => {
            if (!this.allow(e))
                return;
            if (e.touchCount > 1)
                return;
            if (!this.tryPreviewLandmarkUc)
                return;
            const position = e.touches[0].position;
            this.lastTouch = position;
            const ray = (0, Babylon_1.pixelToSceneRay)(position, this.appObjects);
            this.tryPreviewLandmarkUc.tryPreviewLandmark(ray, true);
        };
        this.onTouchEnd = (e) => {
            if (!this.allow(e))
                return;
            if (e.touchCount > 1)
                return;
            if (!this.tryAddLandmarkUc)
                return;
            if (!this.tryPreviewLandmarkUc)
                return;
            const ray = (0, Babylon_1.pixelToSceneRay)(this.lastTouch, this.appObjects);
            this.tryAddLandmarkUc.addFromRay(ray);
            this.tryPreviewLandmarkUc.hidePreview();
        };
        const touchInput = TouchController_1.TouchInputEntity.get(this.appObjects);
        if (touchInput) {
            touchInput.registerController(this);
        }
        else {
            this.warn("Unable to find TouchInputEntity");
        }
    }
}
//# sourceMappingURL=TouchLandmark.js.map