"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DoubleClickToStickMC = void 0;
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const MouseInput_1 = require("../../MouseInput");
const TryStickAppObjectUC_1 = require("../UCs/TryStickAppObjectUC");
class DoubleClickToStickMC extends core_1.AppObjectController {
    get mouseInput() {
        return this.getCachedSingleton(MouseInput_1.MouseInputEntity.type);
    }
    get tryStickSceneMesh() {
        return this.getCachedSingleton(TryStickAppObjectUC_1.TryStickAppObjectUC.type)?.tryWithPixel;
    }
    constructor(appObject) {
        super(appObject, DoubleClickToStickMC.type);
        this.maxClickTimeMS = 300;
        this.maxClickDistancePx = 5;
        this.initialClickPos = core_2.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 = core_2.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 = core_2.Vector2.Zero();
            this.initialClickTime = 0;
            this.clickCount = 0;
        };
        this.mouseInput?.registerController(this);
    }
}
exports.DoubleClickToStickMC = DoubleClickToStickMC;
DoubleClickToStickMC.type = "DoubleClickToStickMC";
//# sourceMappingURL=DoubleClickToStickMC.js.map