/*!
 * Copyright (c) 2026 Vertex Software LLC. All rights reserved.
 */
import { Point } from "@vertexvis/geometry";
import { getMouseClientPosition } from "../../lib/dom";
import { MarkupInteractionHandler, } from "../../lib/markup/interactions";
import { getMarkupBoundingClientRect } from "../viewer-markup/dom";
import { translatePointToRelative, translatePointToScreen, } from "../viewer-markup/markup-utils";
export class ArrowMarkupInteractionHandler extends MarkupInteractionHandler {
    constructor(markupEl, interactionBegin, interactionEnd, scalingOptions) {
        super(scalingOptions);
        this.markupEl = markupEl;
        this.interactionBegin = interactionBegin;
        this.interactionEnd = interactionEnd;
        this.anchor = 'end';
    }
    editAnchor(anchor, event) {
        if (this.markupEl.mode === 'edit') {
            this.anchor = anchor;
            this.startInteraction(event);
        }
    }
    startInteraction(event) {
        this.handleInteractionAttempt(event);
    }
    computeBoundingRect() {
        return getMarkupBoundingClientRect(this.markupEl);
    }
    handleInteractionAttempt(event) {
        var _a;
        if (this.markupEl.mode !== '' &&
            this.pointerId == null &&
            this.elementBounds != null) {
            this.pointerId = event.pointerId;
            this.markupEl.start =
                (_a = this.markupEl.start) !== null && _a !== void 0 ? _a : translatePointToRelative(getMouseClientPosition(event, this.elementBounds), this.elementBounds, this.scalingOptions.originatingViewport, this.scalingOptions.centeringBehavior, this.scalingOptions.scale, this.scalingOptions.offset);
            this.interactionBegin.emit();
            this.acceptInteraction();
        }
    }
    handleInteractionMove(event) {
        if (this.elementBounds != null && this.pointerId === event.pointerId) {
            const position = translatePointToRelative(getMouseClientPosition(event, this.elementBounds), this.elementBounds, this.scalingOptions.originatingViewport, this.scalingOptions.centeringBehavior, this.scalingOptions.scale, this.scalingOptions.offset);
            if (this.anchor === 'start') {
                this.markupEl.start = position;
            }
            else if (this.anchor === 'end') {
                this.markupEl.end = position;
            }
            else if (this.markupEl.start != null && this.markupEl.end != null) {
                const center = Point.create((this.markupEl.start.x + this.markupEl.end.x) / 2, (this.markupEl.start.y + this.markupEl.end.y) / 2);
                const xDifference = center.x - position.x;
                const yDifference = center.y - position.y;
                this.markupEl.start = Point.create(this.markupEl.start.x - xDifference, this.markupEl.start.y - yDifference);
                this.markupEl.end = Point.create(this.markupEl.end.x - xDifference, this.markupEl.end.y - yDifference);
            }
        }
    }
    handleInteractionEnd(event) {
        if (this.pointerId === event.pointerId) {
            const screenStart = this.markupEl.start != null && this.elementBounds != null
                ? translatePointToScreen(this.markupEl.start, this.elementBounds)
                : undefined;
            const screenEnd = this.markupEl.end != null && this.elementBounds != null
                ? translatePointToScreen(this.markupEl.end, this.elementBounds)
                : undefined;
            if (this.markupEl.mode !== '' &&
                screenStart != null &&
                screenEnd != null &&
                Point.distance(screenStart, screenEnd) >= 2) {
                const newlyCreatedMarkup = this.markupEl.mode !== 'edit';
                this.interactionEnd.emit({ markup: this.markupEl, newlyCreatedMarkup });
            }
            else {
                this.markupEl.start = undefined;
                this.markupEl.end = undefined;
            }
            this.pointerId = undefined;
        }
    }
}
//# sourceMappingURL=interactions.js.map
