function _check_private_redeclaration(obj, privateCollection) {
    if (privateCollection.has(obj)) {
        throw new TypeError("Cannot initialize the same private elements twice on an object");
    }
}
function _class_apply_descriptor_get(receiver, descriptor) {
    if (descriptor.get) {
        return descriptor.get.call(receiver);
    }
    return descriptor.value;
}
function _class_apply_descriptor_set(receiver, descriptor, value) {
    if (descriptor.set) {
        descriptor.set.call(receiver, value);
    } else {
        if (!descriptor.writable) {
            throw new TypeError("attempted to set read only private field");
        }
        descriptor.value = value;
    }
}
function _class_extract_field_descriptor(receiver, privateMap, action) {
    if (!privateMap.has(receiver)) {
        throw new TypeError("attempted to " + action + " private field on non-instance");
    }
    return privateMap.get(receiver);
}
function _class_private_field_get(receiver, privateMap) {
    var descriptor = _class_extract_field_descriptor(receiver, privateMap, "get");
    return _class_apply_descriptor_get(receiver, descriptor);
}
function _class_private_field_init(obj, privateMap, value) {
    _check_private_redeclaration(obj, privateMap);
    privateMap.set(obj, value);
}
function _class_private_field_set(receiver, privateMap, value) {
    var descriptor = _class_extract_field_descriptor(receiver, privateMap, "set");
    _class_apply_descriptor_set(receiver, descriptor, value);
    return value;
}
function _class_private_method_get(receiver, privateSet, fn) {
    if (!privateSet.has(receiver)) {
        throw new TypeError("attempted to get private field on non-instance");
    }
    return fn;
}
function _class_private_method_init(obj, privateSet) {
    _check_private_redeclaration(obj, privateSet);
    privateSet.add(obj);
}
import { Menu } from '../../../plugins/contextMenu/menu/index.mjs';
import { clone, extend } from '../../../helpers/object.mjs';
import { arrayEach } from '../../../helpers/array.mjs';
import { setAttribute } from '../../../helpers/dom/element.mjs';
import * as C from '../../../i18n/constants.mjs';
import { SEPARATOR } from '../../../plugins/contextMenu/predefinedItems/index.mjs';
import { BaseUI } from './_base.mjs';
import { A11Y_HIDDEN, A11Y_LISTBOX } from '../../../helpers/a11y.mjs';
var /**
   * Instance of {@link Menu}.
   *
   * @type {Menu}
   */ _menu = /*#__PURE__*/ new WeakMap(), /**
   * List of available select options.
   *
   * @type {Array}
   */ _items = /*#__PURE__*/ new WeakMap(), /**
   * The reference to the BaseUI instance of the caption.
   *
   * @type {BaseUI}
   */ _caption = /*#__PURE__*/ new WeakMap(), /**
   * The reference to the table caption element.
   *
   * @type {HTMLTableCaptionElement}
   */ _captionElement = /*#__PURE__*/ new WeakMap(), /**
   * The reference to the BaseUI instance of the dropdown.
   *
   * @type {BaseUI}
   */ _dropdown = /*#__PURE__*/ new WeakMap(), /**
   * On menu selected listener.
   *
   * @param {object} command Selected item.
   */ _onMenuSelect = /*#__PURE__*/ new WeakSet(), /**
   * On menu closed listener.
   */ _onMenuClosed = /*#__PURE__*/ new WeakSet(), /**
   * On element click listener.
   *
   * @private
   */ _onClick = /*#__PURE__*/ new WeakSet();
/**
 * @private
 * @class SelectUI
 */ export class SelectUI extends BaseUI {
    static get DEFAULTS() {
        return clone({
            className: 'htUISelect',
            wrapIt: false,
            tabIndex: -1
        });
    }
    /**
   * Gets the instance of the Menu.
   *
   * @returns {Menu}
   */ getMenu() {
        return _class_private_field_get(this, _menu);
    }
    /**
   * Register all necessary hooks.
   */ registerHooks() {
        this.addLocalHook('click', ()=>_class_private_method_get(this, _onClick, onClick).call(this));
    }
    /**
   * Set options which can be selected in the list.
   *
   * @param {Array} items Array of objects with required keys `key` and `name`.
   */ setItems(items) {
        _class_private_field_set(this, _items, this.translateNames(items));
        if (_class_private_field_get(this, _menu)) {
            _class_private_field_get(this, _menu).setMenuItems(_class_private_field_get(this, _items));
        }
    }
    /**
   * Translate names of menu items.
   *
   * @param {Array} items Array of objects with required keys `key` and `name`.
   * @returns {Array} Items with translated `name` keys.
   */ translateNames(items) {
        arrayEach(items, (item)=>{
            item.name = this.translateIfPossible(item.name);
        });
        return items;
    }
    /**
   * Build DOM structure.
   */ build() {
        super.build();
        _class_private_field_set(this, _menu, new Menu(this.hot, {
            className: 'htSelectUI htFiltersConditionsMenu',
            keepInViewport: false,
            standalone: true,
            container: this.options.menuContainer
        }));
        _class_private_field_get(this, _menu).setMenuItems(_class_private_field_get(this, _items));
        const caption = new BaseUI(this.hot, {
            className: 'htUISelectCaption'
        });
        const dropdown = new BaseUI(this.hot, {
            className: 'htUISelectDropdown'
        });
        _class_private_field_set(this, _caption, caption);
        _class_private_field_set(this, _captionElement, caption.element);
        _class_private_field_set(this, _dropdown, dropdown);
        if (this.hot.getSettings().ariaTags) {
            setAttribute(dropdown.element, [
                A11Y_HIDDEN()
            ]);
            setAttribute(this._element, [
                A11Y_LISTBOX()
            ]);
        }
        arrayEach([
            caption,
            dropdown
        ], (element)=>this._element.appendChild(element.element));
        _class_private_field_get(this, _menu).addLocalHook('select', (command)=>_class_private_method_get(this, _onMenuSelect, onMenuSelect).call(this, command));
        _class_private_field_get(this, _menu).addLocalHook('afterClose', ()=>_class_private_method_get(this, _onMenuClosed, onMenuClosed).call(this));
        this.update();
    }
    /**
   * Update DOM structure.
   */ update() {
        if (!this.isBuilt()) {
            return;
        }
        let conditionName;
        if (this.options.value) {
            conditionName = this.options.value.name;
        } else {
            conditionName = _class_private_field_get(this, _menu).hot.getTranslatedPhrase(C.FILTERS_CONDITIONS_NONE);
        }
        _class_private_field_get(this, _captionElement).textContent = conditionName;
        super.update();
    }
    /**
   * Open select dropdown menu with available options.
   */ openOptions() {
        const rect = this.element.getBoundingClientRect();
        if (_class_private_field_get(this, _menu)) {
            _class_private_field_get(this, _menu).open();
            _class_private_field_get(this, _menu).setPosition({
                left: this.hot.isLtr() ? rect.left - 5 : rect.left - 31,
                top: rect.top - 1,
                width: rect.width,
                height: rect.height
            });
            _class_private_field_get(this, _menu).getNavigator().toFirstItem();
            _class_private_field_get(this, _menu).getKeyboardShortcutsCtrl().addCustomShortcuts([
                {
                    keys: [
                        [
                            'Tab'
                        ],
                        [
                            'Shift',
                            'Tab'
                        ]
                    ],
                    callback: (event)=>{
                        this.closeOptions();
                        this.runLocalHooks('tabKeydown', event);
                    }
                },
                {
                    keys: [
                        [
                            'Control/Meta',
                            'A'
                        ]
                    ],
                    callback: ()=>false
                }
            ]);
        }
    }
    /**
   * Close select dropdown menu.
   */ closeOptions() {
        if (_class_private_field_get(this, _menu)) {
            _class_private_field_get(this, _menu).close();
        }
    }
    /**
   * Focus element.
   */ focus() {
        if (this.isBuilt()) {
            this.element.focus();
        }
    }
    /**
   * Destroy instance.
   */ destroy() {
        if (_class_private_field_get(this, _menu)) {
            _class_private_field_get(this, _menu).destroy();
            _class_private_field_set(this, _menu, null);
        }
        if (_class_private_field_get(this, _caption)) {
            _class_private_field_get(this, _caption).destroy();
        }
        if (_class_private_field_get(this, _dropdown)) {
            _class_private_field_get(this, _dropdown).destroy();
        }
        super.destroy();
    }
    constructor(hotInstance, options){
        super(hotInstance, extend(SelectUI.DEFAULTS, options)), _class_private_method_init(this, _onMenuSelect), _class_private_method_init(this, _onMenuClosed), _class_private_method_init(this, _onClick), _class_private_field_init(this, _menu, {
            writable: true,
            value: void 0
        }), _class_private_field_init(this, _items, {
            writable: true,
            value: void 0
        }), _class_private_field_init(this, _caption, {
            writable: true,
            value: void 0
        }), _class_private_field_init(this, _captionElement, {
            writable: true,
            value: void 0
        }), _class_private_field_init(this, _dropdown, {
            writable: true,
            value: void 0
        }), _class_private_field_set(this, _menu, null), _class_private_field_set(this, _items, []);
        this.registerHooks();
    }
}
function onMenuSelect(command) {
    if (command.name !== SEPARATOR) {
        this.options.value = command;
        this.update();
        this.runLocalHooks('select', this.options.value);
    }
}
function onMenuClosed() {
    this.runLocalHooks('afterClose');
}
function onClick() {
    this.openOptions();
}
