import { AppObjectController, Vector2 } from "@vived/core";
import { pixelToSceneRay } from "../../Babylon";
import { TouchInputEntity, } from "../../TouchController";
import { SelectAppObjectUC } from "../UC/SelectAppObjectUC";
export class TapToSelectAppObject extends AppObjectController {
    constructor(appObject) {
        super(appObject, TapToSelectAppObject.type);
        this.startPosition = Vector2.Zero();
        this.endPosition = Vector2.Zero();
        this.doTouchEnd = false;
        this.allow = (e) => {
            return true;
        };
        this.onTouchStart = (e) => {
            this.doTouchEnd = false;
            if (e.touches.length !== 1) {
                return;
            }
            if (!this.allow(e)) {
                return;
            }
            this.startPosition = e.touches[0].position;
            this.endPosition = this.startPosition;
            this.doTouchEnd = true;
        };
        this.onTouchMove = (e) => {
            if (e.touches.length === 1) {
                this.endPosition = e.touches[0].position;
            }
            else {
                this.doTouchEnd = false;
            }
        };
        this.onTouchEnd = () => {
            if (!this.doTouchEnd) {
                return;
            }
            if (!this.selectUC) {
                return;
            }
            const movement = Vector2.Subtract(this.startPosition, this.endPosition)
                .magnitued;
            if (movement <= TapToSelectAppObject.maxTapDistancePx) {
                const ray = pixelToSceneRay(this.endPosition, this.appObjects);
                this.selectUC?.trySelectAppObject(ray);
            }
        };
        this.selectUC = appObject.getComponent(SelectAppObjectUC.type);
        if (!this.selectUC) {
            console.warn("MouseSelect has been added to an App Object that does not have a SelectAppObjectUC. Add a SelectAppObjectUC before adding the Controller.");
            return;
        }
        const touchInput = TouchInputEntity.get(this.appObjects);
        if (touchInput) {
            touchInput.registerController(this);
        }
        else {
            this.warn("Unable to find TouchInputEntity");
        }
    }
}
TapToSelectAppObject.type = "TapToSelectAppObject";
TapToSelectAppObject.maxTapDistancePx = 5;
//# sourceMappingURL=TapToSelectAppObject.js.map