/*!
 * Copyright (c) 2026 Vertex Software LLC. All rights reserved.
 */
import { Point, Rectangle } from "@vertexvis/geometry";
import { getMouseClientPosition } from "../../lib/dom";
import { MarkupInteractionHandler, } from "../../lib/markup/interactions";
import { getMarkupBoundingClientRect } from "../viewer-markup/dom";
import { transformRectangle, translatePointsToBounds, translatePointToRelative, } from "../viewer-markup/markup-utils";
export class FreeformMarkupInteractionHandler extends MarkupInteractionHandler {
    constructor(markupEl, interactionBegin, interactionEnd, scalingOptions) {
        super(scalingOptions);
        this.markupEl = markupEl;
        this.interactionBegin = interactionBegin;
        this.interactionEnd = interactionEnd;
        this.anchor = 'bottom-right';
        this.handleResizeInteractionMove = (event) => {
            if (this.resizeStartPosition != null &&
                this.elementBounds != null &&
                this.resizeBounds != null &&
                this.resizePoints != null) {
                const position = translatePointToRelative(getMouseClientPosition(event, this.elementBounds), this.elementBounds, this.scalingOptions.originatingViewport, this.scalingOptions.centeringBehavior, this.scalingOptions.scale, this.scalingOptions.offset);
                const updatedBounds = transformRectangle(this.resizeBounds, this.resizeStartPosition, position, this.anchor, event.shiftKey);
                this.markupEl.points = translatePointsToBounds(this.resizePoints, this.resizeBounds, updatedBounds);
                this.markupEl.bounds = updatedBounds;
            }
        };
        this.handleResizeInteractionEnd = (event) => {
            window.removeEventListener('pointermove', this.handleResizeInteractionMove);
            window.removeEventListener('pointerup', this.handleResizeInteractionEnd);
            this.resizeBounds = undefined;
            const newlyCreatedMarkup = this.markupEl.mode !== 'edit';
            this.interactionEnd.emit({ markup: this.markupEl, newlyCreatedMarkup });
        };
    }
    editAnchor(anchor, event) {
        if (this.markupEl.mode === 'edit' && this.elementBounds != null) {
            this.resizeBounds = this.markupEl.bounds;
            this.resizePoints = this.markupEl.points;
            this.anchor = anchor;
            this.resizeStartPosition = translatePointToRelative(getMouseClientPosition(event, this.elementBounds), this.elementBounds, this.scalingOptions.originatingViewport, this.scalingOptions.centeringBehavior, this.scalingOptions.scale, this.scalingOptions.offset);
            window.addEventListener('pointermove', this.handleResizeInteractionMove);
            window.addEventListener('pointerup', this.handleResizeInteractionEnd);
        }
    }
    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;
            const screenPosition = getMouseClientPosition(event, this.elementBounds);
            const position = translatePointToRelative(screenPosition, this.elementBounds, this.scalingOptions.originatingViewport, this.scalingOptions.centeringBehavior, this.scalingOptions.scale, this.scalingOptions.offset);
            this.updateMinAndMax(position);
            this.markupEl.points = (_a = this.markupEl.points) !== null && _a !== void 0 ? _a : [position];
            this.interactionBegin.emit();
            this.acceptInteraction();
        }
    }
    handleInteractionMove(event) {
        if (this.pointerId === event.pointerId &&
            this.markupEl.points != null &&
            this.elementBounds != null) {
            const screenPosition = getMouseClientPosition(event, this.elementBounds);
            const position = translatePointToRelative(screenPosition, this.elementBounds, this.scalingOptions.originatingViewport, this.scalingOptions.centeringBehavior, this.scalingOptions.scale, this.scalingOptions.offset);
            this.updateMinAndMax(position);
            this.markupEl.points = [...this.markupEl.points, position];
        }
    }
    handleInteractionEnd(event) {
        if (this.pointerId === event.pointerId) {
            if (this.markupEl.mode !== '' &&
                this.markupEl.points != null &&
                this.markupEl.points.length > 2 &&
                this.elementBounds != null) {
                const screenPosition = getMouseClientPosition(event, this.elementBounds);
                const position = translatePointToRelative(screenPosition, this.elementBounds, this.scalingOptions.originatingViewport, this.scalingOptions.centeringBehavior, this.scalingOptions.scale, this.scalingOptions.offset);
                this.updateMinAndMax(position);
                this.markupEl.points = [...this.markupEl.points, position];
                const newlyCreatedMarkup = this.markupEl.mode !== 'edit';
                this.interactionEnd.emit({ markup: this.markupEl, newlyCreatedMarkup });
            }
            else {
                this.markupEl.points = undefined;
            }
            this.min = undefined;
            this.max = undefined;
            this.pointerId = undefined;
        }
    }
    updateMinAndMax(position) {
        this.min =
            this.min != null
                ? Point.create(Math.min(this.min.x, position.x), Math.min(this.min.y, position.y))
                : position;
        this.max =
            this.max != null
                ? Point.create(Math.max(this.max.x, position.x), Math.max(this.max.y, position.y))
                : position;
        this.markupEl.bounds = Rectangle.create(this.min.x, this.min.y, this.max.x - this.min.x, this.max.y - this.min.y);
    }
}
//# sourceMappingURL=interactions.js.map
