"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.TagsBulkAssignSyncState = exports.MAX_SELECTED_ENTITIES_FOR_BULK_ASSIGN_SYNC = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _bi = require("@wix/bex-core/bi");
const MAX_SELECTED_ENTITIES_FOR_BULK_ASSIGN_SYNC = exports.MAX_SELECTED_ENTITIES_FOR_BULK_ASSIGN_SYNC = 100;
class TagsBulkAssignSyncState {
  constructor(params) {
    (0, _defineProperty2.default)(this, "collectionState", void 0);
    (0, _defineProperty2.default)(this, "optimisticActions", void 0);
    (0, _defineProperty2.default)(this, "container", void 0);
    (0, _defineProperty2.default)(this, "tagsBIReporter", void 0);
    (0, _defineProperty2.default)(this, "bulkUpdateTags", void 0);
    (0, _defineProperty2.default)(this, "getTags", void 0);
    (0, _defineProperty2.default)(this, "_reportUndoBi", () => {
      this.tagsBIReporter.reportBi((0, _bi.cairoUndoClicked)({
        actionName: 'Save',
        changedAttributes: 'Tags Assignment'
      }));
    });
    const {
      collectionState,
      optimisticActions,
      container,
      tagsBIReporter,
      getTags,
      bulkUpdateTags
    } = params;
    this.collectionState = collectionState;
    this.optimisticActions = optimisticActions;
    this.container = container;
    this.tagsBIReporter = tagsBIReporter;
    this.getTags = getTags;
    this.bulkUpdateTags = bulkUpdateTags;
  }
  get canUseBulkSync() {
    const {
      allSelectedItemsFetched,
      selectedCountOrTotal
    } = this.collectionState.bulkSelect;
    return allSelectedItemsFetched && selectedCountOrTotal <= MAX_SELECTED_ENTITIES_FOR_BULK_ASSIGN_SYNC;
  }
  async applyChanges({
    newAssignedTagIds,
    newUnassignedTagIds,
    searchValue
  }) {
    this._reportApplyBi({
      newAssignedTagIds,
      newUnassignedTagIds,
      searchValue
    });
    const items = this._calcOptimisticItems({
      newAssignedTagIds,
      newUnassignedTagIds
    });
    const entityIds = this.collectionState.bulkSelect.selectedIds;
    this.optimisticActions.updateMany(items, {
      keepPosition: true,
      submit: async () => {
        return this.bulkUpdateTags({
          entityIds,
          assignTags: {
            privateTags: {
              tagIds: newAssignedTagIds
            }
          },
          unassignTags: {
            privateTags: {
              tagIds: newUnassignedTagIds
            }
          }
        });
      },
      successToast: {
        biName: 'cairo-update-bulk-assign-tags-success',
        message: this.container.translate('cairo.tags.applyChanges.toast.success')
      },
      showUndoToast: true,
      errorToast: () => ({
        biName: 'cairo-update-bulk-assign-tags-error',
        message: this.container.translate('cairo.tags.collectionPage.toast.update.error')
      }),
      onUndo: this._reportUndoBi
    });
  }
  _calculateUpdatedTagIds({
    item,
    getTags,
    newAssignedTagIds,
    newUnassignedTagIds
  }) {
    var _tags$privateTags;
    const tags = item.tags ?? (getTags == null ? void 0 : getTags(item));
    const currentTagIds = (tags == null || (_tags$privateTags = tags.privateTags) == null ? void 0 : _tags$privateTags.tagIds) || [];
    const updatedTagIds = Array.from(new Set([...currentTagIds, ...newAssignedTagIds].filter(tagId => !newUnassignedTagIds.includes(tagId))));
    return updatedTagIds;
  }
  _calcOptimisticItems({
    newAssignedTagIds,
    newUnassignedTagIds
  }) {
    return this.collectionState.result.items.map(item => {
      const selectedIds = this.collectionState.bulkSelect.selectedIds;
      const itemKey = this.collectionState.itemKey(item);
      if (!selectedIds.includes(itemKey)) {
        return item;
      }
      const updatedTagIds = this._calculateUpdatedTagIds({
        newAssignedTagIds,
        newUnassignedTagIds,
        item,
        getTags: this.getTags
      });
      return {
        ...item,
        tags: {
          privateTags: {
            tagIds: updatedTagIds
          }
        }
      };
    });
  }
  _reportApplyBi({
    newAssignedTagIds,
    newUnassignedTagIds,
    searchValue
  }) {
    let actionName = 'Both';
    if (newAssignedTagIds.length === 0) {
      actionName = 'Remove';
    } else if (newUnassignedTagIds.length === 0) {
      actionName = 'Assign';
    }
    const {
      selectedIds: selectedItemsIds
    } = this.collectionState.bulkSelect;
    this.tagsBIReporter.reportBi((0, _bi.cairoAssignTagsToEntities)({
      actionName,
      isFromSearch: searchValue !== '',
      itemIds: JSON.stringify(selectedItemsIds),
      numItems: selectedItemsIds.length,
      assignedTags: JSON.stringify(newAssignedTagIds),
      unAssignedTags: JSON.stringify(newUnassignedTagIds),
      origin: 'Assign Tags Modal'
    }));
  }
}
exports.TagsBulkAssignSyncState = TagsBulkAssignSyncState;
//# sourceMappingURL=TagsBulkAssignSyncState.js.map