import { AppObjectController } from "@vived/core";
import { Vector2 } from "@vived/core";
import { pixelToSceneRay } from "../../Babylon";
import { TouchInputEntity, } from "../../TouchController";
import { TryAddNewLandmarkUC } from "../UseCases/TryAddNewLandmarkUC";
import { TryPreviewLandmarkUC } from "../UseCases/TryPreviewLandmarkUC";
export class TouchLandmark extends AppObjectController {
}
TouchLandmark.type = "TOUCH_LANDMARK_CONTROLLER";
export function makeTouchLandmark(appObject) {
    return new TouchLandmarkImp(appObject);
}
class TouchLandmarkImp extends TouchLandmark {
    get tryPreviewLandmarkUc() {
        return this.getCachedSingleton(TryPreviewLandmarkUC.type);
    }
    get tryAddLandmarkUc() {
        return this.getCachedSingleton(TryAddNewLandmarkUC.type);
    }
    constructor(appObject) {
        super(appObject, TouchLandmark.type);
        this.lastTouch = 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 = 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 = 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 = pixelToSceneRay(this.lastTouch, this.appObjects);
            this.tryAddLandmarkUc.addFromRay(ray);
            this.tryPreviewLandmarkUc.hidePreview();
        };
        const touchInput = TouchInputEntity.get(this.appObjects);
        if (touchInput) {
            touchInput.registerController(this);
        }
        else {
            this.warn("Unable to find TouchInputEntity");
        }
    }
}
//# sourceMappingURL=TouchLandmark.js.map