/**
 * @license
 * Copyright Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { matchesSelectors } from '@tylertech/forge-core';
import { BaseAdapter } from '../core/base';
import { KEYBOARD_SHORTCUT_CONSTANTS } from './keyboard-shortcut-constants';
export class KeyboardShortcutAdapter extends BaseAdapter {
    constructor(component) {
        super(component);
        this.component = component;
    }
    hasTargetElement() {
        return !!this._targetElement;
    }
    destroy() {
        this._targetElement = null;
    }
    /**
     * Adds an event listener to the target element.
     * @param type The event type.
     * @param listener The event listener.
     */
    addTargetEventListener(type, listener, capture) {
        var _a;
        (_a = this._targetElement) === null || _a === void 0 ? void 0 : _a.addEventListener(type, listener, { capture });
    }
    /**
     * Removes an event listener from the target element.
     * @param type The event type.
     * @param listener The event listener.
     */
    removeTargetEventListener(type, listener, capture) {
        var _a;
        (_a = this._targetElement) === null || _a === void 0 ? void 0 : _a.removeEventListener(type, listener, { capture });
    }
    /**
     * Sets the host element's display to none
     */
    setHostStyles() {
        this.component.style.display = 'none';
    }
    /**
     * Sets the target element based on the provided CSS selector.
     * @param {string | undefined} selector The target element selector.
     * @param {boolean} global Whether to target the document element.
     */
    setTargetElement(selector, global) {
        if (global) {
            const doc = this._component.ownerDocument || document;
            this._targetElement = doc.documentElement;
            return;
        }
        if (selector) {
            if (this._component.parentElement) {
                if (matchesSelectors(this._component.parentElement, selector)) {
                    this._targetElement = this._component.parentElement;
                    return;
                }
                this._targetElement = this._component.parentElement.querySelector(selector);
                return;
            }
        }
        else {
            let sibling = this._component.previousElementSibling;
            while (sibling) {
                // Ignore <forge-tooltip> elements
                if (!matchesSelectors(sibling, KEYBOARD_SHORTCUT_CONSTANTS.selectors.TOOLTIP)) {
                    this._targetElement = sibling;
                    return;
                }
                sibling = sibling.previousElementSibling;
            }
            this._targetElement = this._component.parentElement;
            return;
        }
    }
}
