"use strict";

require("core-js/modules/es.error.cause.js");
exports.__esModule = true;
var _menu2 = require("../../../plugins/contextMenu/menu");
var _object = require("../../../helpers/object");
var _array = require("../../../helpers/array");
var _element = require("../../../helpers/dom/element");
var C = _interopRequireWildcard(require("../../../i18n/constants"));
var _predefinedItems = require("../../../plugins/contextMenu/predefinedItems");
var _base = require("./_base");
var _a11y = require("../../../helpers/a11y");
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 _interopRequireWildcard(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 = {}; 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; }
function _classPrivateMethodInitSpec(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); }
function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); }
function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
function _classPrivateFieldSet(receiver, privateMap, value) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); _classApplyDescriptorSet(receiver, descriptor, value); return value; }
function _classApplyDescriptorSet(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 _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return fn; }
function _classPrivateFieldGet(receiver, privateMap) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); return _classApplyDescriptorGet(receiver, descriptor); }
function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to " + action + " private field on non-instance"); } return privateMap.get(receiver); }
function _classApplyDescriptorGet(receiver, descriptor) { if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; }
/**
 * @private
 * @class SelectUI
 */
var _menu = /*#__PURE__*/new WeakMap();
var _items = /*#__PURE__*/new WeakMap();
var _caption = /*#__PURE__*/new WeakMap();
var _captionElement = /*#__PURE__*/new WeakMap();
var _dropdown = /*#__PURE__*/new WeakMap();
var _onMenuSelect = /*#__PURE__*/new WeakSet();
var _onMenuClosed = /*#__PURE__*/new WeakSet();
var _onClick = /*#__PURE__*/new WeakSet();
class SelectUI extends _base.BaseUI {
  static get DEFAULTS() {
    return (0, _object.clone)({
      className: 'htUISelect',
      wrapIt: false,
      tabIndex: -1
    });
  }

  /**
   * Instance of {@link Menu}.
   *
   * @type {Menu}
   */

  constructor(hotInstance, options) {
    super(hotInstance, (0, _object.extend)(SelectUI.DEFAULTS, options));
    /**
     * On element click listener.
     *
     * @private
     */
    _classPrivateMethodInitSpec(this, _onClick);
    /**
     * On menu closed listener.
     */
    _classPrivateMethodInitSpec(this, _onMenuClosed);
    /**
     * On menu selected listener.
     *
     * @param {object} command Selected item.
     */
    _classPrivateMethodInitSpec(this, _onMenuSelect);
    _classPrivateFieldInitSpec(this, _menu, {
      writable: true,
      value: null
    });
    /**
     * List of available select options.
     *
     * @type {Array}
     */
    _classPrivateFieldInitSpec(this, _items, {
      writable: true,
      value: []
    });
    /**
     * The reference to the BaseUI instance of the caption.
     *
     * @type {BaseUI}
     */
    _classPrivateFieldInitSpec(this, _caption, {
      writable: true,
      value: void 0
    });
    /**
     * The reference to the table caption element.
     *
     * @type {HTMLTableCaptionElement}
     */
    _classPrivateFieldInitSpec(this, _captionElement, {
      writable: true,
      value: void 0
    });
    /**
     * The reference to the BaseUI instance of the dropdown.
     *
     * @type {BaseUI}
     */
    _classPrivateFieldInitSpec(this, _dropdown, {
      writable: true,
      value: void 0
    });
    this.registerHooks();
  }

  /**
   * Gets the instance of the Menu.
   *
   * @returns {Menu}
   */
  getMenu() {
    return _classPrivateFieldGet(this, _menu);
  }

  /**
   * Register all necessary hooks.
   */
  registerHooks() {
    this.addLocalHook('click', () => _classPrivateMethodGet(this, _onClick, _onClick2).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) {
    _classPrivateFieldSet(this, _items, this.translateNames(items));
    if (_classPrivateFieldGet(this, _menu)) {
      _classPrivateFieldGet(this, _menu).setMenuItems(_classPrivateFieldGet(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();
    _classPrivateFieldSet(this, _menu, new _menu2.Menu(this.hot, {
      className: 'htSelectUI htFiltersConditionsMenu',
      keepInViewport: false,
      standalone: true,
      container: this.options.menuContainer
    }));
    _classPrivateFieldGet(this, _menu).setMenuItems(_classPrivateFieldGet(this, _items));
    const caption = new _base.BaseUI(this.hot, {
      className: 'htUISelectCaption'
    });
    const dropdown = new _base.BaseUI(this.hot, {
      className: 'htUISelectDropdown'
    });
    _classPrivateFieldSet(this, _caption, caption);
    _classPrivateFieldSet(this, _captionElement, caption.element);
    _classPrivateFieldSet(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));
    _classPrivateFieldGet(this, _menu).addLocalHook('select', command => _classPrivateMethodGet(this, _onMenuSelect, _onMenuSelect2).call(this, command));
    _classPrivateFieldGet(this, _menu).addLocalHook('afterClose', () => _classPrivateMethodGet(this, _onMenuClosed, _onMenuClosed2).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 = _classPrivateFieldGet(this, _menu).hot.getTranslatedPhrase(C.FILTERS_CONDITIONS_NONE);
    }
    _classPrivateFieldGet(this, _captionElement).textContent = conditionName;
    super.update();
  }

  /**
   * Open select dropdown menu with available options.
   */
  openOptions() {
    const rect = this.element.getBoundingClientRect();
    if (_classPrivateFieldGet(this, _menu)) {
      _classPrivateFieldGet(this, _menu).open();
      _classPrivateFieldGet(this, _menu).setPosition({
        left: this.hot.isLtr() ? rect.left - 5 : rect.left - 31,
        top: rect.top - 1,
        width: rect.width,
        height: rect.height
      });
      _classPrivateFieldGet(this, _menu).getNavigator().toFirstItem();
      _classPrivateFieldGet(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 (_classPrivateFieldGet(this, _menu)) {
      _classPrivateFieldGet(this, _menu).close();
    }
  }

  /**
   * Focus element.
   */
  focus() {
    if (this.isBuilt()) {
      this.element.focus();
    }
  }
  /**
   * Destroy instance.
   */
  destroy() {
    if (_classPrivateFieldGet(this, _menu)) {
      _classPrivateFieldGet(this, _menu).destroy();
      _classPrivateFieldSet(this, _menu, null);
    }
    if (_classPrivateFieldGet(this, _caption)) {
      _classPrivateFieldGet(this, _caption).destroy();
    }
    if (_classPrivateFieldGet(this, _dropdown)) {
      _classPrivateFieldGet(this, _dropdown).destroy();
    }
    super.destroy();
  }
}
exports.SelectUI = SelectUI;
function _onMenuSelect2(command) {
  if (command.name !== _predefinedItems.SEPARATOR) {
    this.options.value = command;
    this.update();
    this.runLocalHooks('select', this.options.value);
  }
}
function _onMenuClosed2() {
  this.runLocalHooks('afterClose');
}
function _onClick2() {
  this.openOptions();
}