"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "SelectUI", {
    enumerable: true,
    get: function() {
        return SelectUI;
    }
});
const _menu = require("../../../plugins/contextMenu/menu");
const _object = require("../../../helpers/object");
const _array = require("../../../helpers/array");
const _element = require("../../../helpers/dom/element");
const _constants = /*#__PURE__*/ _interop_require_wildcard(require("../../../i18n/constants"));
const _predefinedItems = require("../../../plugins/contextMenu/predefinedItems");
const _base = require("./_base");
const _a11y = require("../../../helpers/a11y");
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);
}
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
var /**
   * Instance of {@link Menu}.
   *
   * @type {Menu}
   */ _menu1 = /*#__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();
class SelectUI extends _base.BaseUI {
    static get DEFAULTS() {
        return (0, _object.clone)({
            className: 'htUISelect',
            wrapIt: false,
            tabIndex: -1
        });
    }
    /**
   * Gets the instance of the Menu.
   *
   * @returns {Menu}
   */ getMenu() {
        return _class_private_field_get(this, _menu1);
    }
    /**
   * 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, _menu1)) {
            _class_private_field_get(this, _menu1).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) {
        (0, _array.arrayEach)(items, (item)=>{
            item.name = this.translateIfPossible(item.name);
        });
        return items;
    }
    /**
   * Build DOM structure.
   */ build() {
        super.build();
        _class_private_field_set(this, _menu1, new _menu.Menu(this.hot, {
            className: 'htSelectUI htFiltersConditionsMenu',
            keepInViewport: false,
            standalone: true,
            container: this.options.menuContainer
        }));
        _class_private_field_get(this, _menu1).setMenuItems(_class_private_field_get(this, _items));
        const caption = new _base.BaseUI(this.hot, {
            className: 'htUISelectCaption'
        });
        const dropdown = new _base.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) {
            (0, _element.setAttribute)(dropdown.element, [
                (0, _a11y.A11Y_HIDDEN)()
            ]);
            (0, _element.setAttribute)(this._element, [
                (0, _a11y.A11Y_LISTBOX)()
            ]);
        }
        (0, _array.arrayEach)([
            caption,
            dropdown
        ], (element)=>this._element.appendChild(element.element));
        _class_private_field_get(this, _menu1).addLocalHook('select', (command)=>_class_private_method_get(this, _onMenuSelect, onMenuSelect).call(this, command));
        _class_private_field_get(this, _menu1).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, _menu1).hot.getTranslatedPhrase(_constants.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, _menu1)) {
            _class_private_field_get(this, _menu1).open();
            _class_private_field_get(this, _menu1).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, _menu1).getNavigator().toFirstItem();
            _class_private_field_get(this, _menu1).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, _menu1)) {
            _class_private_field_get(this, _menu1).close();
        }
    }
    /**
   * Focus element.
   */ focus() {
        if (this.isBuilt()) {
            this.element.focus();
        }
    }
    /**
   * Destroy instance.
   */ destroy() {
        if (_class_private_field_get(this, _menu1)) {
            _class_private_field_get(this, _menu1).destroy();
            _class_private_field_set(this, _menu1, 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, (0, _object.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, _menu1, {
            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, _menu1, null), _class_private_field_set(this, _items, []);
        this.registerHooks();
    }
}
function onMenuSelect(command) {
    if (command.name !== _predefinedItems.SEPARATOR) {
        this.options.value = command;
        this.update();
        this.runLocalHooks('select', this.options.value);
    }
}
function onMenuClosed() {
    this.runLocalHooks('afterClose');
}
function onClick() {
    this.openOptions();
}
