/*!
 * 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 { WalkModeController } from "../../lib/walk-mode/controller";
import { WalkInteractionHandler } from "../../lib/walk-mode/interactions";
import { WalkModeModel } from "../../lib/walk-mode/model";
/**
 * The `<vertex-viewer-walk-mode-tool>` allows for additional interactions
 * intended for walking through a model instead of orbiting a model.
 */
export class ViewerWalkModeTool {
    constructor() {
        /**
         * The `WalkModeModel` responsible for tracking configuration and emitting
         * events for interaction handlers to respond to.
         */
        this.model = new WalkModeModel();
        /**
         * Determines whether the interaction handlers for this tool should respond to events.
         * When set to `true`, the default viewer interaction mode will be overridden to use the
         * `pivot` camera interaction type, keyboard controls for movement will be added, and
         * setting the `teleportMode` will enable the tool.
         *
         * Defaults to `true`.
         */
        this.enabled = true;
        this.stateMap = {};
    }
    /**
     * @ignore
     */
    componentWillLoad() {
        this.setupController();
        this.setupInteractionHandler();
    }
    /**
     * @ignore
     */
    async componentDidLoad() {
        await this.ensureTeleportToolConfigured();
        this.updateTeleportTool();
        await this.setPivotInteractionMode();
    }
    /**
     * @ignore
     */
    connectedCallback() {
        this.setupInteractionHandler();
        this.setPivotInteractionMode();
    }
    /**
     * @ignore
     */
    disconnectedCallback() {
        this.clearInteractionHandler();
        this.resetInteractionMode();
    }
    /**
     * @ignore
     */
    handleEnabledChanged() {
        var _a;
        (_a = this.controller) === null || _a === void 0 ? void 0 : _a.setEnabled(this.enabled);
        this.toggleInteractionMode();
    }
    /**
     * @ignore
     */
    handleViewerChanged() {
        this.clearInteractionHandler();
        this.setupInteractionHandler();
        this.updateTeleportTool();
        this.toggleInteractionMode();
    }
    /**
     * @ignore
     */
    handleTeleportModeChanged() {
        this.updateTeleportTool();
    }
    /**
     * @ignore
     */
    handleControllerChanged() {
        this.updateTeleportTool();
        this.controllerChanged.emit(this.controller);
    }
    /**
     * @ignore
     */
    handleModelChanged() {
        this.setupController();
        this.clearInteractionHandler();
        this.setupInteractionHandler();
    }
    render() {
        return (h(Host, { key: 'b5a66d1f87a5d1eed77092c0dc1ff295587b3d01' }, h("slot", { key: '91809a5501b34953821955c791bc0a497765209f', name: "teleport-tool", onSlotchange: async () => {
                await this.ensureTeleportToolConfigured();
                this.updateTeleportTool();
            } })));
    }
    setupController() {
        if (this.controller == null) {
            this.controller = new WalkModeController(this.model);
            this.controllerChanged.emit(this.controller);
        }
        else {
            this.controller.updateModel(this.model);
        }
    }
    clearInteractionHandler() {
        var _a, _b;
        (_a = this.interactionHandlerDisposable) === null || _a === void 0 ? void 0 : _a.dispose();
        this.interactionHandlerDisposable = undefined;
        (_b = this.interactionHandler) === null || _b === void 0 ? void 0 : _b.dispose();
        this.interactionHandler = undefined;
    }
    async setupInteractionHandler() {
        var _a;
        if (this.interactionHandler == null) {
            this.interactionHandler = new WalkInteractionHandler(this.model);
            this.interactionHandlerDisposable =
                await ((_a = this.viewer) === null || _a === void 0 ? void 0 : _a.registerInteractionHandler(this.interactionHandler));
        }
    }
    async ensureTeleportToolConfigured() {
        var _a, _b, _c;
        if (this.hostEl != null) {
            const slotted = (_b = (_a = this.hostEl) === null || _a === void 0 ? void 0 : _a.querySelector('vertex-viewer-teleport-tool[slot="teleport-tool"]')) !== null && _b !== void 0 ? _b : undefined;
            const slottedTeleportTool = (slotted === null || slotted === void 0 ? void 0 : slotted.tagName) === 'VERTEX-VIEWER-TELEPORT-TOOL'
                ? slotted
                : undefined;
            if (slottedTeleportTool != null) {
                this.stateMap.teleportTool = slottedTeleportTool;
            }
            else {
                this.stateMap.teleportTool = document.createElement('vertex-viewer-teleport-tool');
                this.stateMap.teleportTool.slot = 'teleport-tool';
                (_c = this.hostEl) === null || _c === void 0 ? void 0 : _c.appendChild(this.stateMap.teleportTool);
            }
        }
    }
    updateTeleportTool() {
        if (this.stateMap.teleportTool != null) {
            this.stateMap.teleportTool.viewer = this.viewer;
            this.stateMap.teleportTool.controller = this.controller;
            this.stateMap.teleportTool.model = this.model;
            this.stateMap.teleportTool.mode = this.teleportMode;
        }
    }
    toggleInteractionMode() {
        if (this.enabled) {
            this.setPivotInteractionMode();
        }
        else {
            this.resetInteractionMode();
        }
    }
    async setPivotInteractionMode() {
        var _a;
        const baseInteractionHandler = await ((_a = this.viewer) === null || _a === void 0 ? void 0 : _a.getBaseInteractionHandler());
        if (this.enabled && baseInteractionHandler != null) {
            const interactionType = baseInteractionHandler.getPrimaryInteractionType();
            this.stateMap.previousPrimaryInteractionType =
                interactionType === 'pivot'
                    ? this.stateMap.previousPrimaryInteractionType
                    : interactionType;
            baseInteractionHandler.setPrimaryInteractionType('pivot');
        }
    }
    async resetInteractionMode() {
        var _a;
        const baseInteractionHandler = await ((_a = this.viewer) === null || _a === void 0 ? void 0 : _a.getBaseInteractionHandler());
        if (baseInteractionHandler != null &&
            baseInteractionHandler.getPrimaryInteractionType() === 'pivot' &&
            this.stateMap.previousPrimaryInteractionType != null) {
            baseInteractionHandler.setPrimaryInteractionType(this.stateMap.previousPrimaryInteractionType);
            this.stateMap.previousPrimaryInteractionType = undefined;
        }
    }
    static get is() { return "vertex-viewer-walk-mode-tool"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() {
        return {
            "$": ["viewer-walk-mode-tool.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["viewer-walk-mode-tool.css"]
        };
    }
    static get properties() {
        return {
            "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
            },
            "controller": {
                "type": "unknown",
                "mutable": true,
                "complexType": {
                    "original": "WalkModeController",
                    "resolved": "WalkModeController | undefined",
                    "references": {
                        "WalkModeController": {
                            "location": "import",
                            "path": "../../lib/walk-mode/controller",
                            "id": "src/lib/walk-mode/controller.ts::WalkModeController",
                            "referenceLocation": "WalkModeController"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "The `WalkModeController` responsible for controlling `KeyBinding`s and excluded\nelements, as well as updating the `WalkModeModel` with various configuration\nsettings."
                },
                "getter": false,
                "setter": false
            },
            "model": {
                "type": "unknown",
                "mutable": true,
                "complexType": {
                    "original": "WalkModeModel",
                    "resolved": "WalkModeModel",
                    "references": {
                        "WalkModeModel": {
                            "location": "import",
                            "path": "../../lib/walk-mode/model",
                            "id": "src/lib/walk-mode/model.ts::WalkModeModel",
                            "referenceLocation": "WalkModeModel"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The `WalkModeModel` responsible for tracking configuration and emitting\nevents for interaction handlers to respond to."
                },
                "getter": false,
                "setter": false,
                "defaultValue": "new WalkModeModel()"
            },
            "teleportMode": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "ViewerTeleportMode",
                    "resolved": "\"teleport\" | \"teleport-and-align\" | \"teleport-toward\" | undefined",
                    "references": {
                        "ViewerTeleportMode": {
                            "location": "import",
                            "path": "../../lib/walk-mode/model",
                            "id": "src/lib/walk-mode/model.ts::ViewerTeleportMode",
                            "referenceLocation": "ViewerTeleportMode"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "The type of teleportation to perform when clicking. This value is passed through\nto a `<vertex-viewer-teleport-tool>`'s mode attribute.\n\n`teleport` - the camera's `position` is moved to the location of the hit result\nconstrained by the plane represented by the camera's current `position` and `up`\nvectors.\n\n`teleport-and-align` - the camera's `position`, `lookAt`, and `up` vectors are updated\nto align to the plane represented by the hit result's position and normal.\n\n`teleport-toward` - the camera's `position` is moved a fixed distance toward the location of the\nhit result constrained by the plane represented by the camera's current `position` and `up`\nvectors.\n\n`undefined` - no teleportation will occur when clicking.\n\nDefaults to `undefined`."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "teleport-mode"
            },
            "enabled": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Determines whether the interaction handlers for this tool should respond to events.\nWhen set to `true`, the default viewer interaction mode will be overridden to use the\n`pivot` camera interaction type, keyboard controls for movement will be added, and\nsetting the `teleportMode` will enable the tool.\n\nDefaults to `true`."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "enabled",
                "defaultValue": "true"
            }
        };
    }
    static get events() {
        return [{
                "method": "controllerChanged",
                "name": "controllerChanged",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Event emitted when the `WalkModeController` associated with this tool changes."
                },
                "complexType": {
                    "original": "WalkModeController",
                    "resolved": "WalkModeController",
                    "references": {
                        "WalkModeController": {
                            "location": "import",
                            "path": "../../lib/walk-mode/controller",
                            "id": "src/lib/walk-mode/controller.ts::WalkModeController",
                            "referenceLocation": "WalkModeController"
                        }
                    }
                }
            }];
    }
    static get elementRef() { return "hostEl"; }
    static get watchers() {
        return [{
                "propName": "enabled",
                "methodName": "handleEnabledChanged"
            }, {
                "propName": "viewer",
                "methodName": "handleViewerChanged"
            }, {
                "propName": "teleportMode",
                "methodName": "handleTeleportModeChanged"
            }, {
                "propName": "controller",
                "methodName": "handleControllerChanged"
            }, {
                "propName": "model",
                "methodName": "handleModelChanged"
            }];
    }
}
//# sourceMappingURL=viewer-walk-mode-tool.js.map
