import { AppObjectController } from "@vived/core";
import { Vector2 } from "@vived/core";
import { TouchInputEntity, } from "../../TouchController";
import { FocusOnPixelUC } from "../UC/FocusOnPixel";
export class DoubleTapToFocus extends AppObjectController {
    get focusOnPixelUC() {
        return this.getCachedLocalComponent(FocusOnPixelUC.type);
    }
    checkForMovement(currentPixelPos) {
        const movement = Vector2.Subtract(this.initialTouchPos, currentPixelPos)
            .magnitued;
        if (movement > this.maxTapDistancePx) {
            this.cancelDoubleTap();
            return;
        }
    }
    constructor(appObject) {
        super(appObject, DoubleTapToFocus.type);
        this.focusDurationMS = 500;
        this.tapTimeMS = 300;
        this.maxTapDistancePx = 30;
        this.initialTouchPos = Vector2.Zero();
        this.initialTouchTime = 0;
        this.tapCount = 0;
        this.allow = (e) => {
            return true;
        };
        this.onTouchStart = (e) => {
            if (!this.allow(e)) {
                this.cancelDoubleTap();
                return;
            }
            if (e.touchCount !== 1) {
                this.cancelDoubleTap();
                return;
            }
            this.tapCount = this.tapCount + 1;
            if (this.tapCount === 1) {
                this.initialTouchPos = e.touchCenter;
                this.initialTouchTime = Date.now();
            }
            else if (this.tapCount === 2) {
                this.checkForMovement(e.touchCenter);
            }
        };
        this.onTouchMove = (e) => {
            if (this.tapCount !== 1)
                return;
            if (e.touchCount !== 1) {
                this.cancelDoubleTap();
                return;
            }
            this.checkForMovement(e.touchCenter);
        };
        this.onTouchEnd = () => {
            if (this.tapCount === 2) {
                const passedTime = Date.now() - this.initialTouchTime;
                if (passedTime > this.tapTimeMS) {
                    this.cancelDoubleTap();
                    return;
                }
                this.focusOnPixelUC?.centerOnPixel(this.initialTouchPos);
                this.tapCount = 0;
                this.initialTouchTime = 0;
            }
        };
        this.cancelDoubleTap = () => {
            this.initialTouchTime = 0;
            this.tapCount = 0;
        };
        const appObjects = appObject.appObjectRepo;
        const touchInput = TouchInputEntity.get(appObjects);
        if (touchInput) {
            touchInput.registerController(this);
        }
        else {
            this.warn("Unable to find TouchInputEntity");
        }
    }
}
DoubleTapToFocus.type = "DoubleTapToFocus";
//# sourceMappingURL=DoubleTapToFocus.js.map