/**
 * @license
 * Copyright Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { BaseAdapter } from '../core/base/base-adapter';
import { MENU_CONSTANTS } from './menu-constants';
import { ListDropdown } from '../list-dropdown';
import { LIST_CONSTANTS, LIST_ITEM_CONSTANTS } from '../list';
export class MenuAdapter extends BaseAdapter {
    constructor(component) {
        super(component);
        this._childMenus = new Map();
    }
    getDropdownElement() {
        var _a, _b;
        return (_b = (_a = this._listDropdown) === null || _a === void 0 ? void 0 : _a.dropdownElement) !== null && _b !== void 0 ? _b : null;
    }
    hasTargetElement() {
        return !!this._targetElement;
    }
    initializeTarget() {
        this._targetElement = this._component.querySelector(MENU_CONSTANTS.selectors.TOGGLE);
        if (!this._targetElement) {
            this._targetElement = this._component.firstElementChild;
        }
    }
    initializeAccessibility(id) {
        if (!this._targetElement) {
            return;
        }
        this._targetElement.setAttribute('aria-atomic', 'true');
        this._targetElement.setAttribute('aria-live', 'assertive');
        this._targetElement.setAttribute('aria-haspopup', 'true');
        this._targetElement.setAttribute('aria-expanded', 'false');
        if (!this._targetElement.hasAttribute('aria-label')) {
            this._targetElement.setAttribute('aria-label', this._targetElement.textContent || '');
        }
    }
    addTargetListener(event, callback, bubbles = false) {
        if (this._targetElement) {
            this._targetElement.addEventListener(event, callback, bubbles);
        }
    }
    removeTargetListener(event, callback) {
        if (this._targetElement) {
            this._targetElement.removeEventListener(event, callback);
        }
    }
    attachMenu(config) {
        if (this._listDropdown || !this._targetElement) {
            return;
        }
        this._listDropdown = new ListDropdown(this._targetElement, config);
        this._listDropdown.open();
        this._targetElement.setAttribute('aria-expanded', 'true');
        this._targetElement.setAttribute('aria-controls', `list-dropdown-popup-${config.id}`);
    }
    setOptions(options) {
        var _a;
        (_a = this._listDropdown) === null || _a === void 0 ? void 0 : _a.setOptions(options);
    }
    destroyListDropdown() {
        var _a;
        (_a = this._listDropdown) === null || _a === void 0 ? void 0 : _a.destroy();
        this._listDropdown = undefined;
    }
    async detachMenu() {
        var _a, _b, _c, _d, _e;
        (_a = this._targetElement) === null || _a === void 0 ? void 0 : _a.removeAttribute('aria-activedescendant');
        (_b = this._targetElement) === null || _b === void 0 ? void 0 : _b.removeAttribute('aria-expanded');
        (_c = this._targetElement) === null || _c === void 0 ? void 0 : _c.removeAttribute('aria-controls');
        await ((_d = this._listDropdown) === null || _d === void 0 ? void 0 : _d.close());
        (_e = this._listDropdown) === null || _e === void 0 ? void 0 : _e.destroy();
        this._listDropdown = undefined;
    }
    setActiveOption(index) {
        var _a;
        (_a = this._listDropdown) === null || _a === void 0 ? void 0 : _a.activateOption(index);
    }
    activateFirstOption() {
        var _a;
        (_a = this._listDropdown) === null || _a === void 0 ? void 0 : _a.activateFirstOption();
    }
    setCascadeTargetInactive() {
        const listItem = this._targetElement;
        if (listItem === null || listItem === void 0 ? void 0 : listItem.active) {
            listItem.active = false;
        }
    }
    getActiveOptionIndex() {
        var _a, _b;
        return (_b = (_a = this._listDropdown) === null || _a === void 0 ? void 0 : _a.getActiveOptionIndex()) !== null && _b !== void 0 ? _b : -1;
    }
    focusTarget() {
        var _a;
        (_a = this._targetElement) === null || _a === void 0 ? void 0 : _a.focus();
    }
    isTargetFocused() {
        return document.activeElement === this._targetElement;
    }
    updateActiveDescendant(id) {
        if (!this._targetElement) {
            return;
        }
        if (id) {
            this._targetElement.setAttribute('aria-activedescendant', id);
        }
        else {
            this._targetElement.removeAttribute('aria-activedescendant');
        }
    }
    addDropdownListener(type, listener) {
        if (!this._listDropdown || !this._listDropdown.dropdownElement) {
            return;
        }
        this._listDropdown.dropdownElement.addEventListener(type, listener);
    }
    isOwnElement(el) {
        if (!this._targetElement || !this._listDropdown || !this._listDropdown.dropdownElement) {
            return false;
        }
        return this._targetElement.contains(el) || !!this._listDropdown.dropdownElement.contains(el);
    }
    propagateKey(key) {
        var _a;
        (_a = this._listDropdown) === null || _a === void 0 ? void 0 : _a.handleKey(key);
    }
    proxyKeyboardEventToChild(evt, id) {
        if (!this.getDropdownElement()) {
            return;
        }
        const openMenu = this._getOpenChildMenu(id);
        if (openMenu) {
            openMenu.propagateKeyEvent(evt);
        }
    }
    toggleChildMenu(index, open) {
        const listItems = this._getListItems();
        const listItem = listItems[index];
        const childMenu = listItem.parentElement;
        if (childMenu && childMenu.tagName.toLowerCase() === MENU_CONSTANTS.elementName) {
            if (open === undefined) {
                childMenu.open = !childMenu.open;
            }
            else {
                childMenu.open = open;
            }
            childMenu.activateFirstOption();
        }
    }
    createChildMenu(index, parentValue, openCb, closeCb, selectCb) {
        const menu = document.createElement('forge-menu');
        menu.style.display = 'block';
        // Set listeners to notify the parent menu when anything important happens within a child menu that needs to be propagated to the root menu
        menu.addEventListener(MENU_CONSTANTS.events.OPEN, () => {
            this._childMenus.set(index, menu);
            openCb(index);
        });
        menu.addEventListener(MENU_CONSTANTS.events.CLOSE, () => {
            this._childMenus.delete(index);
            closeCb(index);
        });
        menu.addEventListener(MENU_CONSTANTS.events.SELECT, (evt) => {
            evt.stopPropagation();
            selectCb(Object.assign(Object.assign({}, evt.detail), { parentValue }));
        });
        return menu;
    }
    closeOtherChildMenus(excludeIndex) {
        const menusToRemove = [];
        this._childMenus.forEach((menu, index) => {
            if (index !== excludeIndex) {
                menu.open = false;
                menusToRemove.push(index);
            }
        });
        menusToRemove.forEach(index => this._childMenus.delete(index));
    }
    setSelectedValues(values) {
        var _a;
        (_a = this._listDropdown) === null || _a === void 0 ? void 0 : _a.setSelectedValues(values);
    }
    _getOpenChildMenu(id) {
        if (!this.getDropdownElement()) {
            return null;
        }
        const list = this._getOwnList(id);
        return list ? list.querySelector(`${MENU_CONSTANTS.elementName}[${MENU_CONSTANTS.attributes.OPEN}]`) : null;
    }
    _getOwnList(id) {
        const popup = this.getDropdownElement();
        if (!popup) {
            return null;
        }
        return popup.querySelector(`${LIST_CONSTANTS.elementName}[id="list-dropdown-list-${id}"]`);
    }
    _getListItems() {
        const popup = this.getDropdownElement();
        if (!popup) {
            return [];
        }
        const listElement = popup.querySelector(LIST_CONSTANTS.elementName);
        if (listElement) {
            return Array.from(listElement.querySelectorAll(LIST_ITEM_CONSTANTS.elementName));
        }
        return [];
    }
}
