/*!
 * Copyright (c) 2026 Vertex Software LLC. All rights reserved.
 */
'use strict';

var index = require('./index-C_J7n4Dv.js');
var templates = require('./templates-DvjgjnlO.js');
var markup = require('./markup-BqWReWlC.js');
var utils = require('./utils-m4ihMvcS.js');
require('./bundle-BhTmftd-.js');
require('./bundle-96sYCpzz.js');
require('./_commonjsHelpers-CBbBosIU.js');

const viewerMarkupToolCss = () => `:host{position:absolute;top:0;bottom:0;left:0;right:0;pointer-events:none}:host([elevated=""]){z-index:100}`;

const ViewerMarkupTool = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.markupBegin = index.createEvent(this, "markupBegin");
        this.markupEnd = index.createEvent(this, "markupEnd");
        /**
         * The type of markup.
         *
         * This property will automatically be set when a child of a
         * `<vertex-viewer-markup>` element.
         */
        this.tool = 'arrow';
        /**
         * Disables markups.
         *
         * This property will automatically be set when a child of a
         * `<vertex-viewer-markup>` element.
         */
        this.disabled = 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;
        /**
         * Captures internal state that you want to preserve across dev refreshes, but
         * don't want to trigger a render when the state changes.
         */
        this.stateMap = {};
        this.handleMarkupInteractionBegin = () => {
            this.markupBegin.emit();
        };
        this.handleMarkupInteractionEnd = () => {
            const { markupElement } = this.stateMap;
            if (utils.isVertexViewerFreeformMarkup(markupElement)) {
                const { points, bounds } = markupElement;
                if (points != null && points.length > 0 && bounds != null) {
                    this.markupEnd.emit(new markup.FreeformMarkup({ points, bounds }));
                }
            }
            else if (utils.isVertexViewerCircleMarkup(markupElement)) {
                const { bounds } = markupElement;
                if (bounds != null) {
                    this.markupEnd.emit(new markup.CircleMarkup({ bounds }));
                }
            }
            else if (utils.isVertexViewerArrowMarkup(markupElement)) {
                const { start, end } = markupElement;
                if (start != null && end != null) {
                    this.markupEnd.emit(new markup.ArrowMarkup({ start, end }));
                }
            }
        };
    }
    async handleViewerChanged() {
        if (this.stateMap.markupElement != null) {
            this.stateMap.markupElement.viewer = this.viewer;
        }
    }
    /**
     * @ignore
     */
    handlePropertyChanged() {
        this.updateMarkupElement();
    }
    handleScalingConfigurationChanged() {
        this.updateMarkupElement();
    }
    /**
     * @ignore
     */
    componentDidLoad() {
        this.updateMarkupElement();
    }
    /**
     * Resets the state of the internally managed markup element
     * to allow for creating a new markup. This state is automatically
     * managed when this element is placed as a child of a
     * `<vertex-viewer-markup>` element.
     */
    async reset() {
        const { markupElement } = this.stateMap;
        if (utils.isVertexViewerFreeformMarkup(markupElement)) {
            markupElement.points = undefined;
            markupElement.bounds = undefined;
            markupElement.mode = 'create';
        }
        else if (utils.isVertexViewerCircleMarkup(markupElement)) {
            markupElement.bounds = undefined;
            markupElement.mode = 'create';
        }
        else if (utils.isVertexViewerArrowMarkup(markupElement)) {
            markupElement.start = undefined;
            markupElement.end = undefined;
            markupElement.mode = 'create';
        }
    }
    /**
     * @ignore
     */
    render() {
        if (!this.disabled) {
            if (this.tool === 'arrow' ||
                this.tool === 'circle' ||
                this.tool === 'freeform') {
                return (index.h(index.Host, null, index.h("slot", null)));
            }
            else {
                return index.h(index.Host, null, `Unsupported tool type '${this.tool}'.`);
            }
        }
        else {
            return index.h(index.Host, null);
        }
    }
    createArrowMarkupElement() {
        if (this.arrowTemplateId != null) {
            const element = templates.stampTemplateWithId(window.document.body, this.arrowTemplateId, utils.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 = templates.stampTemplateWithId(window.document.body, this.circleTemplateId, utils.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 = templates.stampTemplateWithId(window.document.body, this.freeformTemplateId, utils.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');
    }
    createNewMarkupElement() {
        switch (this.tool) {
            case 'arrow':
                return this.createArrowMarkupElement();
            case 'circle':
                return this.createCircleMarkupElement();
            case 'freeform':
                return this.createFreeformMarkupElement();
        }
    }
    updateMarkupElement() {
        const { markupElement } = this.stateMap;
        if (markupElement != null) {
            markupElement.remove();
            markupElement.dispose();
            markupElement.viewer = undefined;
            markupElement.removeEventListener('interactionBegin', this.handleMarkupInteractionBegin);
            markupElement.removeEventListener('interactionEnd', this.handleMarkupInteractionEnd);
        }
        if (!this.disabled) {
            const newMarkupElement = this.createNewMarkupElement();
            this.hostEl.append(newMarkupElement);
            if (this.tool === 'arrow') {
                newMarkupElement.startLineAnchorStyle = this.startLineAnchorStyle;
                newMarkupElement.endLineAnchorStyle = this.endLineAnchorStyle;
            }
            newMarkupElement.mode = 'create';
            newMarkupElement.viewer = this.viewer;
            newMarkupElement.originatingViewport = this.originatingViewport;
            newMarkupElement.centeringBehavior = this.centeringBehavior;
            newMarkupElement.offset = this.offset;
            newMarkupElement.scale = this.scale;
            newMarkupElement.addEventListener('interactionBegin', this.handleMarkupInteractionBegin);
            newMarkupElement.addEventListener('interactionEnd', this.handleMarkupInteractionEnd);
            this.stateMap.markupElement = newMarkupElement;
        }
    }
    get hostEl() { return index.getElement(this); }
    static get watchers() { return {
        "viewer": [{
                "handleViewerChanged": 0
            }],
        "arrowTemplateId": [{
                "handlePropertyChanged": 0
            }],
        "circleTemplateId": [{
                "handlePropertyChanged": 0
            }],
        "disabled": [{
                "handlePropertyChanged": 0
            }],
        "endLineAnchorStyle": [{
                "handlePropertyChanged": 0
            }],
        "freeformTemplateId": [{
                "handlePropertyChanged": 0
            }],
        "startLineAnchorStyle": [{
                "handlePropertyChanged": 0
            }],
        "tool": [{
                "handlePropertyChanged": 0
            }],
        "scale": [{
                "handleScalingConfigurationChanged": 0
            }],
        "offset": [{
                "handleScalingConfigurationChanged": 0
            }],
        "originatingViewport": [{
                "handleScalingConfigurationChanged": 0
            }],
        "centeringBehavior": [{
                "handleScalingConfigurationChanged": 0
            }]
    }; }
};
ViewerMarkupTool.style = viewerMarkupToolCss();

exports.vertex_viewer_markup_tool = ViewerMarkupTool;
//# sourceMappingURL=vertex-viewer-markup-tool.entry.cjs.js.map

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