/*!
 * Copyright (c) 2026 Vertex Software LLC. All rights reserved.
 */
import { Point } from "@vertexvis/geometry";
import { MultiTouchInteractionHandler } from "./multiTouchInteractionHandler";
export class TouchInteractionHandler extends MultiTouchInteractionHandler {
    constructor() {
        super();
        this.handleTouchStart = this.handleTouchStart.bind(this);
        this.handleTouchMove = this.handleTouchMove.bind(this);
        this.handleTouchEnd = this.handleTouchEnd.bind(this);
    }
    dispose() {
        var _a;
        (_a = this.element) === null || _a === void 0 ? void 0 : _a.removeEventListener('touchstart', this.handleTouchStart);
        super.dispose();
    }
    initialize(element, api) {
        super.initialize(element, api);
        element.addEventListener('touchstart', this.handleTouchStart);
    }
    handleTouchStart(event) {
        if (event.touches.length >= 1) {
            event.preventDefault();
            const touch1 = event.touches[0];
            this.currentPosition1 = Point.create(touch1.screenX, touch1.screenY);
            if (event.touches[1] != null) {
                this.beginTwoPointTouch(this.currentPosition1, Point.create(event.touches[1].screenX, event.touches[1].screenY));
            }
            window.addEventListener('touchmove', this.handleTouchMove, {
                passive: false,
            });
            window.addEventListener('touchend', this.handleTouchEnd);
        }
    }
    handleTouchMove(event) {
        event.preventDefault();
        if (event.touches.length === 1) {
            this.handleOnePointTouchMove(event.touches[0]);
        }
        else if (event.touches.length === 2) {
            const point1 = Point.create(event.touches[0].clientX, event.touches[0].clientY);
            const point2 = Point.create(event.touches[1].clientX, event.touches[1].clientY);
            this.handleTwoPointTouchMove(point1, point2);
        }
    }
    handleTouchEnd(event) {
        var _a;
        event.preventDefault();
        (_a = this.interactionApi) === null || _a === void 0 ? void 0 : _a.endInteraction();
        this.isInteracting = false;
        window.removeEventListener('touchmove', this.handleTouchMove);
        window.removeEventListener('touchend', this.handleTouchEnd);
        this.endTwoPointTouch();
    }
    handleOnePointTouchMove(touch) {
        var _a, _b, _c;
        const position = Point.create(touch.screenX, touch.screenY);
        if (this.currentPosition1 != null) {
            const delta = Point.subtract(position, this.currentPosition1);
            const threshold = ((_a = this.interactionApi) === null || _a === void 0 ? void 0 : _a.pixelThreshold(true)) || 2;
            if (Point.distance(position, this.currentPosition1) >= threshold ||
                this.isInteracting) {
                (_b = this.interactionApi) === null || _b === void 0 ? void 0 : _b.beginInteraction();
                (_c = this.interactionApi) === null || _c === void 0 ? void 0 : _c.rotateCamera(delta);
                this.isInteracting = true;
            }
        }
        this.currentPosition1 = position;
    }
}
//# sourceMappingURL=touchInteractionHandler.js.map
