"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.TagsCollectionState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _mobx = require("mobx");
var _TagsModalState = require("./TagsModalState");
var _bexCore = require("@wix/bex-core");
var _TagsAssignButton = require("../components/TagsAssignButton");
var _CollectionManageTagsModal = require("../components/Tags/ManageTags/CollectionManageTagsModal");
var _TagsColumn = require("../components/TagsColumn/TagsColumn");
var _tagsFilterBuilder = require("../components/TagsFilter/tagsFilterBuilder");
var _TagsBulkAssignPopoverState = require("./TagsBulkAssignPopoverState");
var _CollectionToolbarFilters = require("../components/CollectionToolbarFilters");
class TagsCollectionState {
  get featuredPageActions() {
    return [this.Tags.manageTagsPageAction];
  }
  constructor({
    entityTypeName,
    bulkUpdateTags,
    asyncBulkUpdateTags,
    toolbar,
    columnConfig,
    getTags,
    assignButtonConfig,
    props,
    Tags
  }) {
    (0, _defineProperty2.default)(this, "toolbar", void 0);
    (0, _defineProperty2.default)(this, "tagsManagementState", void 0);
    (0, _defineProperty2.default)(this, "tagsModalState", void 0);
    (0, _defineProperty2.default)(this, "tagsBulkAssignPopoverState", void 0);
    (0, _defineProperty2.default)(this, "initTask", new _bexCore.TaskState());
    (0, _defineProperty2.default)(this, "props", void 0);
    (0, _defineProperty2.default)(this, "Tags", void 0);
    (0, _defineProperty2.default)(this, "components", void 0);
    (0, _defineProperty2.default)(this, "_columnConfig", void 0);
    (0, _defineProperty2.default)(this, "_getTags", void 0);
    this.toolbar = toolbar;
    this.props = props;
    this.Tags = Tags;
    this.tagsManagementState = new _bexCore.TagsManagementState({
      fqdn: {
        ...toolbar.collection.fqdn
      },
      container: toolbar.container
    });
    this.tagsModalState = new _TagsModalState.TagsModalState({
      tagsManagementState: this.tagsManagementState,
      entityTypeName
    });
    this.tagsBulkAssignPopoverState = new _TagsBulkAssignPopoverState.TagsBulkAssignPopoverState({
      tagsManagementState: this.tagsManagementState,
      collectionState: toolbar.collection,
      tagsModalState: this.tagsModalState,
      bulkUpdateTags,
      asyncBulkUpdateTags,
      getTags,
      assignButtonConfig
    });
    this._columnConfig = columnConfig;
    this._getTags = getTags;
    this.components = {
      AssignButton: _TagsAssignButton.TagsAssignButton,
      ManageModal: _CollectionManageTagsModal.CollectionManageTagsModal,
      CollectionToolbarFilters: _CollectionToolbarFilters.CollectionToolbarFilters
    };
    (0, _mobx.makeObservable)(this, {
      init: _mobx.action,
      openManageTagsModal: _mobx.action
    });
  }
  init() {
    const disposers = [this.tagsManagementState.init(), this.tagsBulkAssignPopoverState.init(), this.tagsModalState.init(), this.toolbar.collection.query.addFilterIfNeeded('tags', (0, _bexCore.idNameArrayFilter)({
      name: 'tags'
    }))];
    this.initTask.runOnce(async () => {
      await Promise.all([this.tagsManagementState.initTask.status.promise, this.toolbar.container.initTask.status.promise]);
      this.addTagsColumn();
    });
    return () => {
      disposers.forEach(d => d == null ? void 0 : d());
    };
  }
  buildTagsFilterWrapper() {
    return (0, _tagsFilterBuilder.buildTagsFilterElement)(this.toolbar);
  }
  addTagsColumn() {
    var _this$toolbar;
    const tagsColumn = (0, _TagsColumn.buildTagsColumn)((_this$toolbar = this.toolbar) == null ? void 0 : _this$toolbar.container, this._columnConfig);
    const userTagsColumn = this.toolbar._userColumns.find(column => column.id === tagsColumn.id);
    if (!userTagsColumn) {
      this.toolbar.addFeatureColumn(tagsColumn);
    }
  }
  openManageTagsModal() {
    this.tagsModalState.openManageTagsModal({
      origin: 'external'
    });
  }
}
exports.TagsCollectionState = TagsCollectionState;
//# sourceMappingURL=TagsCollectionState.js.map