/*!
 * Copyright (c) 2026 Vertex Software LLC. All rights reserved.
 */
import { 
// eslint-disable-next-line @typescript-eslint/no-unused-vars
h, Host, } from "@stencil/core";
import { PinController } from "../../lib/pins/controller";
import { PinsInteractionHandler } from "../../lib/pins/interactions";
import { PinModel, } from "../../lib/pins/model";
import { getMarkupBoundingClientRect } from "../viewer-markup/dom";
export class ViewerPinTool {
    constructor() {
        /**
         * The model that contains the entities and outcomes from performing pin annotations
         */
        this.pinModel = new PinModel();
        /**
         * The type of pin.
         *
         * This property will automatically be set.
         */
        this.tool = 'pin-icon';
        /**
         * The mode of the pin tool
         */
        this.mode = 'view';
        this.pins = [];
        this.stateMap = {};
        this.handleSetProjectionMatrix = () => {
            var _a, _b;
            this.projectionViewMatrix =
                (_b = (_a = this.viewer) === null || _a === void 0 ? void 0 : _a.frame) === null || _b === void 0 ? void 0 : _b.scene.camera.projectionViewMatrix;
        };
    }
    /**
     * @ignore
     */
    watchModeChange() {
        var _a;
        (_a = this.pinController) === null || _a === void 0 ? void 0 : _a.setToolMode(this.mode);
        this.setupInteractionHandler();
        if (this.mode === 'edit') {
            this.setFeatureMaps();
            this.setCameraAndKeyboardControls();
        }
        else {
            this.resetFeatureMaps();
            this.resetCameraAndKeyboardControls();
        }
    }
    /**
     * @ignore
     */
    watchTypeChange() {
        var _a;
        (_a = this.pinController) === null || _a === void 0 ? void 0 : _a.setToolType(this.tool);
        this.setupInteractionHandler();
    }
    /**
     * @ignore
     */
    watchAccentColorChange() {
        var _a;
        (_a = this.pinController) === null || _a === void 0 ? void 0 : _a.setAccentColor(this.accentColor);
        this.setupInteractionHandler();
    }
    /**
     * @ignore
     */
    watchPrimaryColorChange() {
        var _a;
        (_a = this.pinController) === null || _a === void 0 ? void 0 : _a.setPrimaryColor(this.primaryColor);
        this.setupInteractionHandler();
    }
    /**
     * @ignore
     */
    watchPinsChange() {
        if (this.pins.length > 0) {
            this.setDepthBuffers();
            this.setFeatureMaps();
        }
        else {
            this.resetDepthBuffers();
            this.resetFeatureMaps();
        }
    }
    /**
     * @ignore
     */
    connectedCallback() {
        this.setupInteractionHandler();
        this.setDepthBuffers();
        this.setFeatureMaps();
        this.setCameraAndKeyboardControls();
    }
    /**
     * @ignore
     */
    componentWillLoad() {
        this.updateViewport();
        this.setupController();
        this.setupInteractionHandler();
        this.setDepthBuffers();
        this.setFeatureMaps();
        this.setCameraAndKeyboardControls();
        this.pinModel.onEntitiesChanged((entities) => {
            this.pins = entities;
        });
        this.pinModel.onSelectionChange((selectedId) => {
            this.selectedPinId = selectedId;
        });
        this.handleViewerChanged(this.viewer, undefined);
    }
    componentDidLoad() {
        this.resizeObserver = new ResizeObserver(() => this.updateViewport());
        this.resizeObserver.observe(this.hostEl);
    }
    /**
     * @ignore
     */
    disconnectedCallback() {
        this.clearInteractionHandler();
        this.clearModelListeners();
        this.resetDepthBuffers();
        this.resetFeatureMaps();
        this.resetCameraAndKeyboardControls();
    }
    /**
     * @ignore
     */
    handleViewerChanged(newViewer, oldViewer) {
        this.setupInteractionHandler();
        this.setDepthBuffers();
        this.setFeatureMaps();
        this.setCameraAndKeyboardControls();
        if (oldViewer != null) {
            oldViewer.removeEventListener('frameDrawn', this.handleSetProjectionMatrix);
        }
        if (newViewer != null) {
            newViewer.addEventListener('frameDrawn', this.handleSetProjectionMatrix);
        }
        this.handleSetProjectionMatrix();
    }
    render() {
        return (h(Host, { key: '311442b0cc3d62c485b81526fde10692b560af93' }, h("vertex-viewer-dom-renderer", { key: 'e4fe9d022a717db444590a6c2d2239d233bbe2ce', viewer: this.viewer, drawMode: "2d" }, this.pins.map((pin) => {
            return (h("vertex-viewer-pin-group", { id: `pin-group-${pin.id}`, "data-is-dom-group-element": true, pin: pin, elementBounds: this.elementBounds, pinModel: this.pinModel, pinController: this.pinController, projectionViewMatrix: this.projectionViewMatrix, selected: this.selectedPinId === pin.id }));
        }))));
    }
    setupController() {
        const attributes = this.accentColor || this.primaryColor
            ? {
                accentColor: this.accentColor,
                primaryColor: this.primaryColor,
            }
            : undefined;
        this.pinController = new PinController(this.pinModel, this.mode, this.tool, attributes);
    }
    clearInteractionHandler() {
        var _a;
        (_a = this.registeredInteractionHandler) === null || _a === void 0 ? void 0 : _a.then((handler) => handler.dispose());
        this.registeredInteractionHandler = undefined;
    }
    setupInteractionHandler() {
        var _a;
        const hostStyles = window.getComputedStyle(this.hostEl);
        const xOffset = hostStyles
            .getPropertyValue('--viewer-pin-tool-initial-label-offset-x')
            .trim();
        const xOffsetAsNumber = Number.isFinite(Number.parseInt(xOffset))
            ? Number.parseInt(xOffset)
            : 20;
        const yOffset = hostStyles
            .getPropertyValue('--viewer-pin-tool-initial-label-offset-y')
            .trim();
        const yOffsetAsNumber = Number.isFinite(Number.parseInt(yOffset))
            ? Number.parseInt(yOffset)
            : 20;
        this.clearInteractionHandler();
        if (this.pinController != null) {
            this.registeredInteractionHandler =
                (_a = this.viewer) === null || _a === void 0 ? void 0 : _a.registerInteractionHandler(new PinsInteractionHandler(this.pinController, xOffsetAsNumber, yOffsetAsNumber));
        }
    }
    clearModelListeners() {
        var _a, _b, _c;
        (_a = this.onEntitiesChangedHandler) === null || _a === void 0 ? void 0 : _a.dispose();
        this.onEntitiesChangedHandler = undefined;
        (_b = this.onEntityAddedHandler) === null || _b === void 0 ? void 0 : _b.dispose();
        this.onEntityAddedHandler = undefined;
        (_c = this.resizeObserver) === null || _c === void 0 ? void 0 : _c.disconnect();
    }
    updateViewport() {
        this.elementBounds = getMarkupBoundingClientRect(this.hostEl);
    }
    setDepthBuffers() {
        if (this.pins.length > 0 &&
            this.viewer != null &&
            this.viewer.depthBuffers == null) {
            this.stateMap.shouldClearDepthBuffers = true;
            this.viewer.depthBuffers = 'final';
        }
    }
    resetDepthBuffers() {
        if (this.stateMap.shouldClearDepthBuffers && this.viewer != null) {
            this.viewer.depthBuffers = undefined;
            this.stateMap.shouldClearDepthBuffers = undefined;
        }
    }
    setFeatureMaps() {
        if ((this.pins.length > 0 || this.mode === 'edit') &&
            this.viewer != null &&
            this.viewer.featureMaps == null) {
            this.stateMap.shouldClearFeatureMaps = true;
            this.viewer.featureMaps = 'final';
        }
    }
    resetFeatureMaps() {
        if (this.stateMap.shouldClearFeatureMaps && this.viewer != null) {
            this.viewer.featureMaps = undefined;
            this.stateMap.shouldClearFeatureMaps = undefined;
        }
    }
    setCameraAndKeyboardControls() {
        if (this.mode === 'edit' && this.viewer != null) {
            // Check camera controls
            if (this.viewer.cameraControls) {
                this.stateMap.shouldClearCameraControls = true;
                this.viewer.cameraControls = false;
            }
            // Check keyboard controls
            if (this.viewer.keyboardControls) {
                this.stateMap.shouldClearKeyboardControls = true;
                this.viewer.keyboardControls = false;
            }
        }
    }
    resetCameraAndKeyboardControls() {
        if (this.viewer != null) {
            if (this.stateMap.shouldClearCameraControls) {
                this.viewer.cameraControls = true;
                this.stateMap.shouldClearCameraControls = undefined;
            }
            if (this.stateMap.shouldClearKeyboardControls) {
                this.viewer.keyboardControls = true;
                this.stateMap.shouldClearKeyboardControls = undefined;
            }
        }
    }
    static get is() { return "vertex-viewer-pin-tool"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() {
        return {
            "$": ["viewer-pin-tool.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["viewer-pin-tool.css"]
        };
    }
    static get properties() {
        return {
            "pinController": {
                "type": "unknown",
                "mutable": true,
                "complexType": {
                    "original": "PinController",
                    "resolved": "PinController | undefined",
                    "references": {
                        "PinController": {
                            "location": "import",
                            "path": "../../lib/pins/controller",
                            "id": "src/lib/pins/controller.ts::PinController",
                            "referenceLocation": "PinController"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "The controller that is responsible for drawing pins and updating the model"
                },
                "getter": false,
                "setter": false
            },
            "pinModel": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "PinModel",
                    "resolved": "PinModel",
                    "references": {
                        "PinModel": {
                            "location": "import",
                            "path": "../../lib/pins/model",
                            "id": "src/lib/pins/model.ts::PinModel",
                            "referenceLocation": "PinModel"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The model that contains the entities and outcomes from performing pin annotations"
                },
                "getter": false,
                "setter": false,
                "defaultValue": "new PinModel()"
            },
            "viewer": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "HTMLVertexViewerElement",
                    "resolved": "HTMLVertexViewerElement | undefined",
                    "references": {
                        "HTMLVertexViewerElement": {
                            "location": "global",
                            "id": "global::HTMLVertexViewerElement"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "The viewer that this component is bound to. This is automatically assigned\nif added to the light-dom of a parent viewer element."
                },
                "getter": false,
                "setter": false
            },
            "tool": {
                "type": "string",
                "mutable": true,
                "complexType": {
                    "original": "ViewerPinToolType",
                    "resolved": "\"pin-icon\" | \"pin-text\"",
                    "references": {
                        "ViewerPinToolType": {
                            "location": "import",
                            "path": "../../lib/pins/model",
                            "id": "src/lib/pins/model.ts::ViewerPinToolType",
                            "referenceLocation": "ViewerPinToolType"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The type of pin.\n\nThis property will automatically be set."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "tool",
                "defaultValue": "'pin-icon'"
            },
            "mode": {
                "type": "string",
                "mutable": true,
                "complexType": {
                    "original": "ViewerPinToolMode",
                    "resolved": "\"edit\" | \"view\"",
                    "references": {
                        "ViewerPinToolMode": {
                            "location": "import",
                            "path": "../../lib/pins/model",
                            "id": "src/lib/pins/model.ts::ViewerPinToolMode",
                            "referenceLocation": "ViewerPinToolMode"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The mode of the pin tool"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "mode",
                "defaultValue": "'view'"
            },
            "primaryColor": {
                "type": "string",
                "mutable": true,
                "complexType": {
                    "original": "Color.Color | string | undefined",
                    "resolved": "Color | string | undefined",
                    "references": {
                        "Color": {
                            "location": "import",
                            "path": "@vertexvis/utils",
                            "id": "../utils/dist/index.d.ts::\"/home/runner/work/vertex-web-sdk/vertex-web-sdk/packages/utils/dist/color\"",
                            "referenceLocation": "Color"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The primary color for new pins. Setting this will override the primary template color, and will be used for any new\npins created with this `vertex-viewer-pin-tool`. This styling applies to pin anchors, and borders, etc."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "primary-color"
            },
            "accentColor": {
                "type": "string",
                "mutable": true,
                "complexType": {
                    "original": "Color.Color | string | undefined",
                    "resolved": "Color | string | undefined",
                    "references": {
                        "Color": {
                            "location": "import",
                            "path": "@vertexvis/utils",
                            "id": "../utils/dist/index.d.ts::\"/home/runner/work/vertex-web-sdk/vertex-web-sdk/packages/utils/dist/color\"",
                            "referenceLocation": "Color"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The accent color for new pins. Setting this will override the accent template color, and will be used for any new\npins created with this `vertex-viewer-pin-tool`. This styling applies to some background colors, etc"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "accent-color"
            }
        };
    }
    static get states() {
        return {
            "pins": {},
            "selectedPinId": {},
            "elementBounds": {},
            "projectionViewMatrix": {}
        };
    }
    static get elementRef() { return "hostEl"; }
    static get watchers() {
        return [{
                "propName": "mode",
                "methodName": "watchModeChange"
            }, {
                "propName": "tool",
                "methodName": "watchTypeChange"
            }, {
                "propName": "accentColor",
                "methodName": "watchAccentColorChange"
            }, {
                "propName": "primaryColor",
                "methodName": "watchPrimaryColorChange"
            }, {
                "propName": "pins",
                "methodName": "watchPinsChange"
            }, {
                "propName": "viewer",
                "methodName": "handleViewerChanged"
            }];
    }
}
//# sourceMappingURL=viewer-pin-tool.js.map
