import { AppObjectController, Vector2 } from "@vived/core";
import { pixelToSceneRay } from "../../Babylon";
import { MouseInputEntity, } from "../../MouseInput";
import { ZSpaceEntity } from "../../zSpace/Entities/ZSpaceEntity";
import { PressAppObjectUC } from "../UC/PressAppObjectUC";
export class MousePressOnAppObject extends AppObjectController {
    constructor(appObject) {
        super(appObject, MousePressOnAppObject.type);
        this.zSpaceEntity = undefined;
        this.startPosition = Vector2.Zero();
        this.endPosition = Vector2.Zero();
        this.pressedID = null;
        this.isPressed = false;
        this.allow = (e) => {
            return true;
        };
        this.onDown = (e) => {
            if (e.button !== 0) {
                return;
            }
            if (!this.pressUC) {
                return;
            }
            if (this.zSpaceEntity && this.zSpaceEntity.emulate)
                return;
            if (!this.allow(e))
                return;
            this.startPosition = e.position;
            this.endPosition = this.startPosition;
            this.isPressed = true;
            const ray = pixelToSceneRay(e.position, this.appObjects);
            this.pressedID = this.pressUC?.tryPressAppObject(ray);
        };
        this.onUp = (e) => {
            this.isPressed = false;
            if (e.button !== 0) {
                return;
            }
            if (!this.pressUC) {
                return;
            }
            // Set pressedID to null
            this.pressUC.pressAppObject(this.pressedID, this.isPressed);
        };
        this.onMove = (e) => {
            if (!this.pressUC) {
                return;
            }
            if (this.isPressed) {
                this.endPosition = e.position;
                const movement = Vector2.Subtract(this.startPosition, this.endPosition)
                    .magnitued;
                if (movement > MousePressOnAppObject.maxTapDistancePx) {
                    // Set pressedID to null
                    this.pressUC.pressAppObject(null, this.isPressed);
                }
            }
        };
        this.zSpaceEntity = ZSpaceEntity.get(this.appObjects);
        this.pressUC = appObject.getComponent(PressAppObjectUC.type);
        if (!this.pressUC) {
            console.warn("MousePress has been added to an App Object that does not have a PressAppObjectUC. Add a PressAppObjectUC before adding the Controller.");
            return;
        }
        const mouseInput = MouseInputEntity.get(this.appObjects);
        if (mouseInput) {
            mouseInput.registerController(this);
        }
        else {
            this.warn("Unable to find MouseInputEntity");
        }
    }
}
MousePressOnAppObject.type = "MousePressOnAppObject";
MousePressOnAppObject.maxTapDistancePx = 5;
//# sourceMappingURL=MousePressOnAppObject.js.map