"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.CustomColumnsPanelStateBase = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _bexCore = require("@wix/bex-core");
var _mobx = require("mobx");
class CustomColumnsPanelStateBase {
  constructor(params) {
    (0, _defineProperty2.default)(this, "container", void 0);
    (0, _defineProperty2.default)(this, "columnsCollection", void 0);
    (0, _defineProperty2.default)(this, "select", new _bexCore.SelectState({
      keyGetter: column => column.id
    }));
    (0, _defineProperty2.default)(this, "searchValue", '');
    (0, _defineProperty2.default)(this, "onColumnClick", void 0);
    (0, _defineProperty2.default)(this, "setSearchValue", (0, _mobx.action)(value => {
      this.searchValue = value;
      this.columnsCollection.refreshAllPages();
    }));
    (0, _defineProperty2.default)(this, "onColumnCheckboxToggle", (0, _mobx.action)(column => {
      const {
        select,
        onColumnClick
      } = this;
      select.toggle(column);
      onColumnClick == null || onColumnClick(column, select.isChecked(column.id));
    }));
    this.container = params.container;
    this.columnsCollection = new _bexCore.CollectionState({
      ...this.container,
      events: {},
      queryName: params.queryName,
      fetchData: async () => {
        const items = this.filteredColumns.filter(column => column.hiddenFromCustomColumnsSelection !== true);
        return {
          items
        };
      },
      filters: {},
      itemKey: item => item.id,
      itemName: item => item.name
    });
  }
  initBaseObservables() {
    (0, _mobx.makeObservable)(this, {
      searchValue: _mobx.observable,
      setSearchValue: _mobx.action.bound,
      orderedColumns: _mobx.computed,
      filteredColumns: _mobx.computed
    });
  }
  get filteredColumns() {
    const {
      orderedColumns,
      searchValue
    } = this;
    if (!searchValue.trim()) {
      return orderedColumns;
    }
    const searchLower = searchValue.toLowerCase();
    return orderedColumns.filter(column => {
      const name = column.name || (typeof column.title === 'string' ? column.title : column.id);
      return name.toLowerCase().includes(searchLower);
    });
  }
}
exports.CustomColumnsPanelStateBase = CustomColumnsPanelStateBase;
//# sourceMappingURL=CustomColumnsPanelStateBase.js.map