"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.KanbanStageState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _bexCore = require("@wix/bex-core");
var _mobx = require("mobx");
class KanbanStageState {
  constructor(params) {
    var _params$initialPage;
    (0, _defineProperty2.default)(this, "container", void 0);
    (0, _defineProperty2.default)(this, "itemsCollectionState", void 0);
    (0, _defineProperty2.default)(this, "name", void 0);
    (0, _defineProperty2.default)(this, "key", void 0);
    (0, _defineProperty2.default)(this, "kanbanId", void 0);
    (0, _defineProperty2.default)(this, "stage", void 0);
    (0, _defineProperty2.default)(this, "_total", void 0);
    (0, _defineProperty2.default)(this, "isRefetching", false);
    (0, _defineProperty2.default)(this, "optimisticActions", void 0);
    this.container = params.container;
    this.name = params.name;
    this.key = params.key;
    this.kanbanId = params.kanbanId;
    this.stage = params.stage;
    this._total = ((_params$initialPage = params.initialPage) == null ? void 0 : _params$initialPage.total) ?? undefined;
    this.itemsCollectionState = new _bexCore.CollectionState({
      errorMonitor: this.container.errorMonitor,
      history: this.container.history,
      lodash: this.container.lodash,
      onlineState: this.container.onlineState,
      queryCache: this.container.queryCache,
      translate: this.container.translate,
      queryName: `kanban-items-${this.kanbanId}-${this.key}`,
      fetchData: params.fetchData,
      itemKey: params.itemKey,
      itemName: params.itemName,
      filters: {
        ...params.query.filters,
        stageId: (0, _bexCore.stringFilter)({
          initialValue: this.key
        })
      },
      initialPage: params.initialPage,
      errorHandler: this.container.errorHandler,
      paginationMode: 'cursor',
      search: params.query.search
    });
    this.optimisticActions = new _bexCore.CollectionOptimisticActions({
      collection: this.itemsCollectionState,
      container: this.container
    });
    (0, _mobx.makeObservable)(this, {
      name: _mobx.observable,
      _total: _mobx.observable,
      fetchMore: _mobx.action,
      stageName: _mobx.computed,
      stageKey: _mobx.computed,
      isRefetching: _mobx.observable,
      hasError: _mobx.computed,
      setTotal: _mobx.action,
      isLoading: _mobx.computed,
      totalItems: _mobx.computed
    });
  }
  init() {
    const disposers = [this.itemsCollectionState.init({
      skipFilterListeners: true
    }), this.optimisticActions.init()];
    return () => {
      disposers.forEach(disposer => disposer == null ? void 0 : disposer());
    };
  }
  get hasError() {
    var _this$itemsCollection;
    return (_this$itemsCollection = this.itemsCollectionState.result.status) == null ? void 0 : _this$itemsCollection.isError;
  }
  items() {
    return this.itemsCollectionState.keyedItems.map(item => item.item);
  }
  itemName(item) {
    return this.itemsCollectionState.itemName(item);
  }
  itemKey(item) {
    return this.itemsCollectionState.itemKey(item);
  }
  get totalItems() {
    return this._total;
  }
  setTotal(total) {
    this._total = total;
  }
  get stageName() {
    return this.name;
  }
  get stageKey() {
    return this.key;
  }
  get isLoading() {
    return this.itemsCollectionState.initTask.status.isLoading;
  }
  fetchMore() {
    this.itemsCollectionState.fetchNextPageIfNeeded();
  }
  isFetchingMore() {
    return true;
  }
  deleteItem(item) {
    this.itemsCollectionState.optimisticActions.deleteOne(item, {
      submit: () => Promise.resolve()
    });
    (0, _mobx.runInAction)(() => {
      this.setTotal((this._total ?? 0) - 1);
    });
  }
  createItem(item) {
    this.itemsCollectionState.optimisticActions.createOne(item, {
      submit: () => Promise.resolve()
    });
    (0, _mobx.runInAction)(() => {
      this.setTotal((this._total ?? 0) + 1);
    });
  }
  showErrorToast(error) {
    var _this$container$error, _this$container$error2;
    const resolvedError = (_this$container$error = this.container.errorHandler) == null || _this$container$error.getResolvedError == null ? void 0 : _this$container$error.getResolvedError(error);
    resolvedError && ((_this$container$error2 = this.container.errorHandler) == null || _this$container$error2.showError == null ? void 0 : _this$container$error2.showError(resolvedError));
  }
  updateItem(updatedItem) {
    const allItems = this.itemsCollectionState.result.keyedItems.map(k => k.item);
    const updatedItems = allItems.map(item => this.itemKey(item) === this.itemKey(updatedItem) ? updatedItem : item);
    this.itemsCollectionState.optimisticActions.updateMany(updatedItems, {
      submit: () => Promise.resolve(updatedItems),
      keepPosition: true
    });
  }
  async refetchStage() {
    (0, _mobx.runInAction)(() => {
      this.isRefetching = true;
    });
    try {
      await this.itemsCollectionState.refreshAllPages();
    } finally {
      (0, _mobx.runInAction)(() => {
        this.isRefetching = false;
      });
    }
  }
}
exports.KanbanStageState = KanbanStageState;
//# sourceMappingURL=KanbanStageState.js.map