import { AppObjectController } from "@vived/core";
import { Vector2 } from "@vived/core";
import { TouchInputEntity, } from "../../TouchController";
import { TryStickAppObjectUC } from "../UCs/TryStickAppObjectUC";
export class DoubleTapToStickTC extends AppObjectController {
    get touchInput() {
        return this.getCachedSingleton(TouchInputEntity.type);
    }
    get tryStickSceneMesh() {
        return this.getCachedSingleton(TryStickAppObjectUC.type)?.tryWithPixel;
    }
    constructor(appObject) {
        super(appObject, DoubleTapToStickTC.type);
        this.tapTimeMS = 300;
        this.maxTapDistancePx = 30;
        this.initialTouchPos = Vector2.Zero();
        this.initialTouchTime = 0;
        this.tapCount = 0;
        this.onTap = (e) => {
            if (!this.tryStickSceneMesh) {
                this.error("Missing Component");
                return;
            }
            this.tapCount = 1 + this.tapCount;
            if (this.tapCount === 1) {
                this.initialTouchPos = e.touchCenter;
                this.initialTouchTime = Date.now();
            }
            else if (this.tapCount === 2) {
                const movement = Vector2.Subtract(this.initialTouchPos, e.touchCenter)
                    .magnitued;
                const passedTime = Date.now() - this.initialTouchTime;
                if (passedTime <= this.tapTimeMS && movement <= this.maxTapDistancePx) {
                    this.tryStickSceneMesh(e.touchCenter);
                }
                this.cancelDoubleTap();
            }
        };
        this.cancelDoubleTap = () => {
            this.initialTouchPos = Vector2.Zero();
            this.initialTouchTime = 0;
            this.tapCount = 0;
        };
        this.touchInput?.registerController(this);
    }
}
DoubleTapToStickTC.type = "DoubleTapToStickTC";
//# sourceMappingURL=DoubleTapToStickTC.js.map