import { AppObjectController } from "@vived/core";
import { Vector2 } from "@vived/core";
import { MouseInputEntity, } from "../../MouseInput";
import { TryStickAppObjectUC } from "../UCs/TryStickAppObjectUC";
export class DoubleClickToStickMC extends AppObjectController {
    get mouseInput() {
        return this.getCachedSingleton(MouseInputEntity.type);
    }
    get tryStickSceneMesh() {
        return this.getCachedSingleton(TryStickAppObjectUC.type)?.tryWithPixel;
    }
    constructor(appObject) {
        super(appObject, DoubleClickToStickMC.type);
        this.maxClickTimeMS = 300;
        this.maxClickDistancePx = 5;
        this.initialClickPos = Vector2.Zero();
        this.initialClickTime = 0;
        this.clickCount = 0;
        this.onClick = (e) => {
            if (!this.tryStickSceneMesh) {
                this.error("Missing Component");
                return;
            }
            if (e.button !== 0) {
                this.cancelDoubleClick();
                return;
            }
            this.clickCount = 1 + this.clickCount;
            if (this.clickCount === 1) {
                this.initialClickPos = e.position;
                this.initialClickTime = Date.now();
            }
            else if (this.clickCount === 2) {
                const movement = Vector2.Subtract(this.initialClickPos, e.position)
                    .magnitued;
                const passedTime = Date.now() - this.initialClickTime;
                if (passedTime <= this.maxClickTimeMS &&
                    movement <= this.maxClickDistancePx) {
                    this.tryStickSceneMesh(e.position);
                }
                this.cancelDoubleClick();
            }
        };
        this.cancelDoubleClick = () => {
            this.initialClickPos = Vector2.Zero();
            this.initialClickTime = 0;
            this.clickCount = 0;
        };
        this.mouseInput?.registerController(this);
    }
}
DoubleClickToStickMC.type = "DoubleClickToStickMC";
//# sourceMappingURL=DoubleClickToStickMC.js.map