/*!
 * Copyright (c) 2026 Vertex Software LLC. All rights reserved.
 */
import { r as registerInstance, c as createEvent, h, H as Host, a as getElement } from './index-EpnuuCxl.js';
import { p as point } from './bundle-DfdVaPrt.js';
import { s as stampTemplateWithId } from './templates-DRh4_Z-g.js';
import { A as ArrowMarkup, C as CircleMarkup, F as FreeformMarkup } from './markup-DGlCz24T.js';
import { i as isVertexViewerArrowMarkup, a as isVertexViewerCircleMarkup, b as isVertexViewerFreeformMarkup } from './utils-DKzHYmwU.js';
import './bundle-DLfw87TV.js';
import './_commonjsHelpers-CG9AChtV.js';

const viewerMarkupCss = () => `:host{display:block;position:absolute;top:0;bottom:0;left:0;right:0;pointer-events:none}`;

const ViewerMarkup = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.markupAdded = createEvent(this, "markupAdded");
        this.markupChanged = createEvent(this, "markupChanged");
        this.markupRemoved = createEvent(this, "markupRemoved");
        this.markupSelectionChanged = createEvent(this, "markupSelectionChanged");
        /**
         * The type of markup to perform.
         */
        this.tool = 'arrow';
        /**
         * If `true`, disables adding or editing of markup through user
         * interaction.
         */
        this.disabled = false;
        /**
         * Indicates if new markup should be automatically selected.
         */
        this.selectNew = false;
        /**
         * The style of the starting anchor. This defaults to none.
         */
        this.startLineAnchorStyle = 'none';
        /**
         * The style of the ending anchor. This defaults to 'arrow-triangle.'
         */
        this.endLineAnchorStyle = 'arrow-triangle';
        /**
         * Defines the behavior of the provided markup when the originating viewport is smaller
         * than the current viewport, or is scaled to a size smaller than the current viewport
         * using the `scale` property.
         *
         * Options:
         * - `x-only`: Markup will be centered horizontally, but not vertically.
         * - `y-only`: Markup will be centered vertically, but not horizontally.
         * - `both`: Markup will be centered both horizontally and vertically.
         * - `none`: Markup will not be centered (default).
         */
        this.centeringBehavior = 'none';
        /**
         * The scale to render this markup at. This value is used to scale the element's bounds
         * along with any `offset` to determine the final computed coordinates.
         *
         * When provided, all computed coordinates will be scaled by this amount.
         */
        this.scale = 1;
        this.handleSlotChange = () => {
            this.updatePropsOnMarkupTool();
            this.updatePropsOnMarkups();
        };
        this.handlePointerMove = (event) => {
            if (this.pointerDownPosition != null &&
                point.distance(this.pointerDownPosition, point.create(event.clientX, event.clientY)) > 2) {
                this.toSelectMarkupId = undefined;
            }
        };
        this.handlePointerUp = async (event) => {
            if (!this.disabled) {
                const el = event.target;
                const markups = await this.getMarkupElements();
                const markup = markups.find((m) => m === el);
                if ((markup === null || markup === void 0 ? void 0 : markup.id) != null &&
                    markup.id !== '' &&
                    markup.id === this.toSelectMarkupId) {
                    this.selectedMarkupId = el.id;
                }
                else if (this.pointerDownPosition != null &&
                    point.distance(this.pointerDownPosition, point.create(event.clientX, event.clientY)) <= 2) {
                    this.selectedMarkupId = undefined;
                }
                this.toSelectMarkupId = undefined;
                this.pointerDownPosition = undefined;
            }
            window.removeEventListener('pointerup', this.handlePointerUp);
            window.removeEventListener('pointermove', this.handlePointerMove);
        };
    }
    disconnectedCallback() {
        window.removeEventListener('pointerup', this.handlePointerUp);
        window.removeEventListener('pointermove', this.handlePointerMove);
    }
    /**
     * Adds a new markup as a child to this component. A new markup
     * component will be created from the template specified by
     * `arrow-template-id`, `circle-template-id`, or if undefined
     * a default element will be created.
     *
     * @param markup The markup to add.
     * @returns The markup element that was created.
     * @see {@link arrowTemplateId}
     * @see {@link circleTemplateId}
     */
    async addMarkup(markup) {
        if (markup instanceof ArrowMarkup) {
            const { start, end, id } = markup;
            const el = this.createArrowMarkupElement();
            this.appendMarkupElement(el);
            el.id = id;
            el.start = start;
            el.end = end;
            el.startLineAnchorStyle = this.startLineAnchorStyle;
            el.endLineAnchorStyle = this.endLineAnchorStyle;
            return el;
        }
        else if (markup instanceof CircleMarkup) {
            const { bounds, id } = markup;
            const el = this.createCircleMarkupElement();
            this.appendMarkupElement(el);
            el.id = id;
            el.bounds = bounds;
            return el;
        }
        else if (markup instanceof FreeformMarkup) {
            const { bounds, points, id } = markup;
            const el = this.createFreeformMarkupElement();
            this.appendMarkupElement(el);
            el.id = id;
            el.points = points;
            el.bounds = bounds;
            return el;
        }
        else {
            throw new Error(`Cannot add markup. Unknown type '${markup}'.`);
        }
    }
    /**
     * Removes a markup with the given ID, and returns the HTML element
     * associated to the markup. Returns `undefined` if no markup is
     * found.
     *
     * @param id The ID of the markup to remove.
     * @returns The markup element, or undefined.
     */
    async removeMarkup(id) {
        const markup = await this.getMarkupElement(id);
        if (markup != null) {
            markup.remove();
            this.markupRemoved.emit(markup);
        }
        return markup;
    }
    /**
     * Returns a list of markup elements that are children of this component.
     *
     * @returns A list of all markups.
     * @see {@link getMarkupElement}
     */
    async getMarkupElements() {
        return Array.from(this.hostEl.children).filter((e) => isVertexViewerArrowMarkup(e) ||
            isVertexViewerCircleMarkup(e) ||
            isVertexViewerFreeformMarkup(e));
    }
    /**
     * Returns the markup element associated to the given ID.
     *
     * @param id The ID of the markup element to return.
     * @returns A markup element, or `undefined`.
     * @see {@link getMarkupElements}
     */
    async getMarkupElement(id) {
        const markup = await this.getMarkupElements();
        return markup.find((el) => el.id === id);
    }
    /**
     * @ignore
     */
    async handleSelectedMarkupIdChanged() {
        const markup = await this.getMarkupElements();
        markup.forEach((m) => {
            if (m.id === this.selectedMarkupId) {
                m.mode = 'edit';
                this.markupSelectionChanged.emit(m);
            }
            else {
                m.mode = '';
            }
        });
        if (this.selectedMarkupId == null) {
            this.markupSelectionChanged.emit(undefined);
        }
    }
    /**
     * @ignore
     */
    handlePropertyChanged() {
        this.updatePropsOnMarkupTool();
    }
    /**
     * @ignore
     */
    async handleViewerChanged(newViewer) {
        this.updatePropsOnMarkupTool();
        this.updatePropsOnMarkups();
    }
    /**
     * @ignore
     */
    handleDisabledChanged() {
        this.updatePropsOnMarkupTool();
    }
    /**
     * @ignore
     */
    async handleStartLineAnchorStyleChanged() {
        this.updatePropsOnMarkupTool();
        // If a piece of arrow markup is currently selected, then update its anchor style
        const selectedArrow = await this.getSelectedArrowMarkup();
        if (selectedArrow != null) {
            selectedArrow.startLineAnchorStyle = this.startLineAnchorStyle;
            this.markupChanged.emit(selectedArrow);
        }
    }
    /**
     * @ignore
     */
    async handleEndLineAnchorStyleChanged() {
        this.updatePropsOnMarkupTool();
        // If a piece of arrow markup is currently selected, then update its anchor style
        const selectedArrow = await this.getSelectedArrowMarkup();
        if (selectedArrow != null) {
            selectedArrow.endLineAnchorStyle = this.endLineAnchorStyle;
            this.markupChanged.emit(selectedArrow);
        }
    }
    /**
     * @ignore
     */
    async handleMarkupEnd(event) {
        var _a;
        const e = event;
        const el = await this.addMarkup(e.detail);
        await new Promise((resolve) => {
            const handleRender = () => {
                el.removeEventListener('viewRendered', handleRender);
                resolve();
            };
            el.addEventListener('viewRendered', handleRender);
        });
        if (this.selectNew) {
            this.selectedMarkupId = e.detail.id;
        }
        (_a = this.getMarkupTool()) === null || _a === void 0 ? void 0 : _a.reset();
    }
    /**
     * @ignore
     */
    async handleMarkupUpdated(event) {
        const e = event;
        // Do not emit the markupChanged event if the markup was just created
        if (!e.detail.newlyCreatedMarkup) {
            this.markupChanged.emit(e.detail.markup);
        }
    }
    /**
     * @ignore
     */
    async handleMarkupPointerDown(event) {
        if (!this.disabled) {
            const el = event.target;
            const markups = await this.getMarkupElements();
            const markup = markups.find((m) => m === el);
            if ((markup === null || markup === void 0 ? void 0 : markup.id) != null && (markup === null || markup === void 0 ? void 0 : markup.id) !== '') {
                this.toSelectMarkupId = el.id;
            }
            this.pointerDownPosition = point.create(event.clientX, event.clientY);
            window.addEventListener('pointermove', this.handlePointerMove);
            window.addEventListener('pointerup', this.handlePointerUp);
        }
    }
    /**
     * @ignore
     */
    componentDidLoad() {
        this.updatePropsOnMarkupTool();
    }
    /**
     * @ignore
     */
    render() {
        return (h(Host, { key: 'a5620363230dcc56e335b0864eacf94ba8d8232c' }, h("slot", { key: '7fa77e410c679b183304f117e8c8e0a36a3ea14d', onSlotchange: this.handleSlotChange })));
    }
    appendMarkupElement(el) {
        this.updatePropsOnMarkup(el);
        this.hostEl.appendChild(el);
        this.markupAdded.emit(el);
        return el;
    }
    createArrowMarkupElement() {
        if (this.arrowTemplateId != null) {
            const element = stampTemplateWithId(window.document.body, this.arrowTemplateId, isVertexViewerArrowMarkup, () => console.warn(`Arrow template with ID ${this.arrowTemplateId} not found. Using default arrow element.`), () => console.warn(`Arrow template does not contain a vertex-viewer-markup-arrow. Using default arrow element.`));
            if (element != null) {
                return element;
            }
        }
        return document.createElement('vertex-viewer-markup-arrow');
    }
    createCircleMarkupElement() {
        if (this.circleTemplateId != null) {
            const element = stampTemplateWithId(window.document.body, this.circleTemplateId, isVertexViewerCircleMarkup, () => console.warn(`Circle template with ID ${this.circleTemplateId} not found. Using default circle element.`), () => console.warn(`Circle template does not contain a vertex-viewer-markup-circle. Using default circle element.`));
            if (element != null) {
                return element;
            }
        }
        return document.createElement('vertex-viewer-markup-circle');
    }
    createFreeformMarkupElement() {
        if (this.freeformTemplateId != null) {
            const element = stampTemplateWithId(window.document.body, this.freeformTemplateId, isVertexViewerFreeformMarkup, () => console.warn(`Freeform template with ID ${this.freeformTemplateId} not found. Using default freeform element.`), () => console.warn(`Freeform template does not contain a vertex-viewer-markup-freeform. Using default freeform element.`));
            if (element != null) {
                return element;
            }
        }
        return document.createElement('vertex-viewer-markup-freeform');
    }
    async updatePropsOnMarkups() {
        const markup = await this.getMarkupElements();
        markup.forEach((m) => this.updatePropsOnMarkup(m));
    }
    updatePropsOnMarkup(element) {
        element.viewer = this.viewer;
        element.originatingViewport = this.originatingViewport;
        element.centeringBehavior = this.centeringBehavior;
        element.offset = this.offset;
        element.scale = this.scale;
        element.classList.add('viewer-markup__markup');
    }
    updatePropsOnMarkupTool() {
        const tool = this.getMarkupTool();
        if (tool != null) {
            tool.disabled = this.disabled;
            tool.arrowTemplateId = this.arrowTemplateId;
            tool.circleTemplateId = this.circleTemplateId;
            tool.freeformTemplateId = this.freeformTemplateId;
            tool.tool = this.tool;
            tool.viewer = this.viewer;
            tool.startLineAnchorStyle = this.startLineAnchorStyle;
            tool.endLineAnchorStyle = this.endLineAnchorStyle;
            tool.originatingViewport = this.originatingViewport;
            tool.centeringBehavior = this.centeringBehavior;
            tool.offset = this.offset;
            tool.scale = this.scale;
        }
    }
    getMarkupTool() {
        return this.hostEl.querySelector('vertex-viewer-markup-tool');
    }
    async getSelectedArrowMarkup() {
        if (this.selectedMarkupId != null) {
            const selected = await this.getMarkupElement(this.selectedMarkupId);
            return isVertexViewerArrowMarkup(selected) ? selected : undefined;
        }
    }
    get hostEl() { return getElement(this); }
    static get watchers() { return {
        "selectedMarkupId": [{
                "handleSelectedMarkupIdChanged": 0
            }],
        "arrowTemplateId": [{
                "handlePropertyChanged": 0
            }],
        "circleTemplateId": [{
                "handlePropertyChanged": 0
            }],
        "freeformTemplateId": [{
                "handlePropertyChanged": 0
            }],
        "tool": [{
                "handlePropertyChanged": 0
            }],
        "viewer": [{
                "handleViewerChanged": 0
            }],
        "originatingViewport": [{
                "handleViewerChanged": 0
            }],
        "centeringBehavior": [{
                "handleViewerChanged": 0
            }],
        "offset": [{
                "handleViewerChanged": 0
            }],
        "scale": [{
                "handleViewerChanged": 0
            }],
        "disabled": [{
                "handleDisabledChanged": 0
            }],
        "startLineAnchorStyle": [{
                "handleStartLineAnchorStyleChanged": 0
            }],
        "endLineAnchorStyle": [{
                "handleEndLineAnchorStyleChanged": 0
            }]
    }; }
};
ViewerMarkup.style = viewerMarkupCss();

export { ViewerMarkup as vertex_viewer_markup };
//# sourceMappingURL=vertex-viewer-markup.entry.js.map

//# sourceMappingURL=vertex-viewer-markup.entry.js.map