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

var index = require('./index-C_J7n4Dv.js');
var model = require('./model-BrVcVbnP.js');
var bundle = require('./bundle-BhTmftd-.js');
require('./bundle-96sYCpzz.js');
require('./_commonjsHelpers-CBbBosIU.js');

function targetIsElement(target) {
    return target instanceof Element;
}

class WalkInteractionHandler {
    constructor(model$1) {
        this.model = model$1;
        this.pressed = {};
        this.handleKeyUp = async (event) => {
            const key = event.key.toLocaleLowerCase();
            this.pressed = Object.keys(this.pressed)
                .filter((k) => k !== key)
                .reduce((pressed, k) => ({ ...pressed, [k]: true }), {});
            if (Object.keys(this.pressed).length === 0 && this.interval != null) {
                this.endInteraction();
            }
        };
        this.endInteraction = async () => {
            var _a;
            this.clearInterval();
            await ((_a = this.api) === null || _a === void 0 ? void 0 : _a.endInteraction());
        };
        this.handleKeyDown = this.handleKeyDown.bind(this);
        this.handleKeyUp = this.handleKeyUp.bind(this);
        this.handleEnabledChange = this.handleEnabledChange.bind(this);
        this.restartInteraction = this.restartInteraction.bind(this);
        this.updateCamera = this.updateCamera.bind(this);
        this.handlers = {
            [model.ViewerWalkModeOperation.MOVE_DOWN]: this.moveDown.bind(this),
            [model.ViewerWalkModeOperation.MOVE_UP]: this.moveUp.bind(this),
            [model.ViewerWalkModeOperation.PIVOT_DOWN]: this.pivotDown.bind(this),
            [model.ViewerWalkModeOperation.PIVOT_LEFT]: this.pivotLeft.bind(this),
            [model.ViewerWalkModeOperation.PIVOT_RIGHT]: this.pivotRight.bind(this),
            [model.ViewerWalkModeOperation.PIVOT_UP]: this.pivotUp.bind(this),
            [model.ViewerWalkModeOperation.WALK_BACKWARD]: this.walkBackward.bind(this),
            [model.ViewerWalkModeOperation.WALK_FORWARD]: this.walkForward.bind(this),
            [model.ViewerWalkModeOperation.WALK_LEFT]: this.walkLeft.bind(this),
            [model.ViewerWalkModeOperation.WALK_RIGHT]: this.walkRight.bind(this),
        };
        this.enabledChangeDisposable = this.model.onEnabledChange(this.handleEnabledChange);
        this.keyBindingsChangeDisposable = this.model.onKeyBindingsChange(this.restartInteraction);
        this.configurationChangeDisposable = this.model.onConfigurationChange(this.restartInteraction);
    }
    dispose() {
        var _a, _b, _c;
        this.disable();
        (_a = this.enabledChangeDisposable) === null || _a === void 0 ? void 0 : _a.dispose();
        (_b = this.keyBindingsChangeDisposable) === null || _b === void 0 ? void 0 : _b.dispose();
        (_c = this.configurationChangeDisposable) === null || _c === void 0 ? void 0 : _c.dispose();
        window.removeEventListener('keydown', this.handleKeyDown);
        window.removeEventListener('keyup', this.handleKeyUp);
    }
    initialize(_, api) {
        this.api = api;
        this.handleEnabledChange(this.model.getEnabled());
    }
    enable() {
        this.disable();
        window.addEventListener('keydown', this.handleKeyDown);
        window.addEventListener('keyup', this.handleKeyUp);
    }
    disable() {
        window.removeEventListener('keydown', this.handleKeyDown);
        window.removeEventListener('keyup', this.handleKeyUp);
    }
    handleKeyDown(event) {
        const key = event.key.toLowerCase();
        const exclude = targetIsElement(event.target) &&
            this.model.isElementExcluded(event.target);
        if (!event.repeat && !exclude) {
            this.pressed = { ...this.pressed, [key]: true };
            this.tryBeginInteraction();
        }
    }
    handleEnabledChange(enabled) {
        if (enabled) {
            this.enable();
        }
        else {
            this.disable();
        }
    }
    restartInteraction() {
        this.clearInterval();
        this.tryBeginInteraction();
    }
    tryBeginInteraction() {
        if (Object.keys(this.pressed).length > 0 && this.someOperationMatches()) {
            this.beginInteraction();
        }
    }
    beginInteraction() {
        var _a, _b;
        if (!((_a = this.api) === null || _a === void 0 ? void 0 : _a.isInteracting())) {
            (_b = this.api) === null || _b === void 0 ? void 0 : _b.beginInteraction();
        }
        if (this.interval == null) {
            this.interval = setInterval(this.updateCamera, this.model.getKeyboardRepeatInterval());
        }
    }
    clearInterval() {
        if (this.interval != null) {
            clearInterval(this.interval);
            this.interval = undefined;
        }
    }
    updateCamera() {
        Object.keys(this.handlers).forEach((key) => {
            const op = key;
            if (this.model.operationMatches(op, this.pressed)) {
                this.handlers[op]();
            }
        });
    }
    someOperationMatches() {
        return Object.keys(this.handlers).some((op) => this.model.operationMatches(op, this.pressed));
    }
    pivotLeft() {
        var _a;
        (_a = this.api) === null || _a === void 0 ? void 0 : _a.pivotCamera(0, this.model.getKeyboardPivotDegrees());
    }
    pivotRight() {
        var _a;
        (_a = this.api) === null || _a === void 0 ? void 0 : _a.pivotCamera(0, -this.model.getKeyboardPivotDegrees());
    }
    pivotUp() {
        var _a;
        (_a = this.api) === null || _a === void 0 ? void 0 : _a.pivotCamera(-this.model.getKeyboardPivotDegrees(), 0);
    }
    pivotDown() {
        var _a;
        (_a = this.api) === null || _a === void 0 ? void 0 : _a.pivotCamera(this.model.getKeyboardPivotDegrees(), 0);
    }
    walkForward() {
        var _a;
        (_a = this.api) === null || _a === void 0 ? void 0 : _a.walk(bundle.vector3.scale(this.relativeWalkSpeed(), bundle.vector3.forward()));
    }
    walkBackward() {
        var _a;
        (_a = this.api) === null || _a === void 0 ? void 0 : _a.walk(bundle.vector3.scale(this.relativeWalkSpeed(), bundle.vector3.back()));
    }
    walkLeft() {
        var _a;
        (_a = this.api) === null || _a === void 0 ? void 0 : _a.walk(bundle.vector3.scale(this.relativeWalkSpeed(), bundle.vector3.left()));
    }
    walkRight() {
        var _a;
        (_a = this.api) === null || _a === void 0 ? void 0 : _a.walk(bundle.vector3.scale(this.relativeWalkSpeed(), bundle.vector3.right()));
    }
    moveUp() {
        var _a;
        (_a = this.api) === null || _a === void 0 ? void 0 : _a.walk(bundle.vector3.scale(this.relativeWalkSpeed(), bundle.vector3.down()));
    }
    moveDown() {
        var _a;
        (_a = this.api) === null || _a === void 0 ? void 0 : _a.walk(bundle.vector3.scale(this.relativeWalkSpeed(), bundle.vector3.up()));
    }
    relativeWalkSpeed() {
        return this.model.getKeyboardWalkSpeed() / 500;
    }
}

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

const ViewerWalkModeTool = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.controllerChanged = index.createEvent(this, "controllerChanged");
        /**
         * The `WalkModeModel` responsible for tracking configuration and emitting
         * events for interaction handlers to respond to.
         */
        this.model = new model.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 (index.h(index.Host, { key: 'b5a66d1f87a5d1eed77092c0dc1ff295587b3d01' }, index.h("slot", { key: '91809a5501b34953821955c791bc0a497765209f', name: "teleport-tool", onSlotchange: async () => {
                await this.ensureTeleportToolConfigured();
                this.updateTeleportTool();
            } })));
    }
    setupController() {
        if (this.controller == null) {
            this.controller = new model.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;
        }
    }
    get hostEl() { return index.getElement(this); }
    static get watchers() { return {
        "enabled": [{
                "handleEnabledChanged": 0
            }],
        "viewer": [{
                "handleViewerChanged": 0
            }],
        "teleportMode": [{
                "handleTeleportModeChanged": 0
            }],
        "controller": [{
                "handleControllerChanged": 0
            }],
        "model": [{
                "handleModelChanged": 0
            }]
    }; }
};
ViewerWalkModeTool.style = viewerWalkModeToolCss();

exports.vertex_viewer_walk_mode_tool = ViewerWalkModeTool;
//# sourceMappingURL=vertex-viewer-walk-mode-tool.entry.cjs.js.map

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