"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.TagsSearchState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _TagsManagementState = require("./TagsManagementState");
var _mobx = require("mobx");
class TagsSearchState {
  constructor({
    tagsManagementState
  }) {
    (0, _defineProperty2.default)(this, "tagsManagementState", void 0);
    (0, _defineProperty2.default)(this, "searchValue", '');
    (0, _defineProperty2.default)(this, "setSearchValue", newSearchValue => {
      this.searchValue = newSearchValue;
      if (this.tagsManagementState.shouldAllowTagsPagination) {
        this.tagsManagementState.collection.search(newSearchValue.substring(0, _TagsManagementState.TAG_MAX_LENGTH));
      }
    });
    this.tagsManagementState = tagsManagementState;
    (0, _mobx.makeObservable)(this, {
      searchValue: _mobx.observable,
      setSearchValue: _mobx.action,
      items: _mobx.computed,
      hasSameTagAsSearch: _mobx.computed
    });
  }
  get items() {
    if (this.tagsManagementState.shouldAllowTagsPagination) {
      return this.tagsManagementState.items;
    }
    if (!this.searchValue) {
      return this.tagsManagementState.items;
    }
    const filtered = this.tagsManagementState.items.filter(tag => tag.name.toLowerCase().includes(this.searchValue.toLowerCase()));
    if (filtered.length === 0 && this.searchValue.length > _TagsManagementState.TAG_MAX_LENGTH) {
      const truncated = this.tagsManagementState.items.find(tag => tag.name.toLowerCase() === this.searchValue.substring(0, _TagsManagementState.TAG_MAX_LENGTH).toLowerCase());
      if (truncated) {
        return [truncated];
      }
    }
    return filtered;
  }
  get hasSameTagAsSearch() {
    const term = this.searchValue.substring(0, _TagsManagementState.TAG_MAX_LENGTH).toLowerCase();
    return this.tagsManagementState.items.some(tag => tag.name.toLowerCase() === term);
  }
}
exports.TagsSearchState = TagsSearchState;
//# sourceMappingURL=TagsSearchState.js.map