"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.ViewsLocalSearchState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _wordStartsWithMatch = require("../utils/wordStartsWithMatch");
var _viewsDropdownRenderHelpers = require("../components/CollectionViewsDropdown/viewsDropdownRenderHelpers");
var _mobx = require("mobx");
var _ViewsLocalSearchBIReporter = require("./ViewsLocalSearchBIReporter");
class ViewsLocalSearchState {
  get container() {
    return this.views.container;
  }
  get collection() {
    return this.views.collection;
  }
  constructor(params) {
    (0, _defineProperty2.default)(this, "views", void 0);
    (0, _defineProperty2.default)(this, "bi", void 0);
    (0, _defineProperty2.default)(this, "scheduleLocalSearch", void 0);
    (0, _defineProperty2.default)(this, "inputValue", '');
    // collection.query.search is for server side search - so we use this simple variable for local search
    (0, _defineProperty2.default)(this, "localSearchValue", '');
    (0, _defineProperty2.default)(this, "setInputValueAndScheduleSearch", (0, _mobx.action)(value => {
      this.inputValue = value;
      this.scheduleLocalSearch();
    }));
    (0, _defineProperty2.default)(this, "clearSearch", (0, _mobx.action)(() => {
      this.inputValue = '';
      this._setLocalSearchValue();
    }));
    (0, _defineProperty2.default)(this, "_setLocalSearchValue", (0, _mobx.action)((value = this.inputValue) => {
      this.localSearchValue = value;
    }));
    (0, _defineProperty2.default)(this, "_search", (0, _mobx.action)(() => {
      this._setLocalSearchValue();
      this.bi.onSearch();
    }));
    this.views = params.views;
    this.scheduleLocalSearch = this.container.lodash.debounce(this._search, 300);
    this.bi = new _ViewsLocalSearchBIReporter.ViewsLocalSearchBIReporter({
      state: this
    });
    (0, _mobx.makeObservable)(this, {
      inputValue: _mobx.observable.ref,
      localSearchValue: _mobx.observable.ref,
      categoriesWithViews: _mobx.computed,
      filteredCategoriesWithViews: _mobx.computed
    });
  }
  init() {
    return this.bi.init();
  }
  get filteredCategoriesWithViews() {
    const {
      localSearchValue,
      categoriesWithViews
    } = this;
    const {
      viewsWithoutCategory,
      categories
    } = categoriesWithViews;
    const q = localSearchValue.trim();
    if (!q.length) {
      return {
        viewsWithoutCategory,
        categories
      };
    }
    const startsWith = (0, _wordStartsWithMatch.wordStartsWithMatch)(q);
    const viewPredicate = view => startsWith((0, _viewsDropdownRenderHelpers.getViewName)({
      view,
      viewsState: this.views
    }));
    const transformCategory = category => {
      if (startsWith((0, _viewsDropdownRenderHelpers.getCategoryName)({
        category,
        viewsState: this.views
      }))) {
        return category;
      }
      return {
        ...category,
        views: category.views.filter(viewPredicate)
      };
    };
    return {
      viewsWithoutCategory: viewsWithoutCategory.filter(viewPredicate),
      categories: categories.map(transformCategory).filter(c => c.views.length)
    };
  }
  get categoriesWithViews() {
    const {
      collection: {
        result: {
          items
        }
      },
      views: {
        basicCategories
      }
    } = this;
    const viewsWithoutCategory = [];
    const categoriesMap = new Map([]);
    for (const view of items) {
      if (!view.categoryId) {
        viewsWithoutCategory.push(view);
      } else {
        let category = categoriesMap.get(view.categoryId);
        if (!category) {
          category = {
            ...basicCategories[view.categoryId],
            views: []
          };
          categoriesMap.set(view.categoryId, category);
        }
        category.views.push(view);
      }
    }
    return {
      categories: Array.from(categoriesMap.values()),
      viewsWithoutCategory
    };
  }
}
exports.ViewsLocalSearchState = ViewsLocalSearchState;
//# sourceMappingURL=ViewsLocalSearchState.js.map