"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.ReorderModeState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _bexCore = require("@wix/bex-core");
var _mobx = require("mobx");
var _events = require("events");
var _bi = require("@wix/bex-core/bi");
class ReorderModeState {
  constructor(params) {
    (0, _defineProperty2.default)(this, "discardChangesModal", new _bexCore.ConditionalModalState());
    (0, _defineProperty2.default)(this, "feature", 'Drag and Drop');
    (0, _defineProperty2.default)(this, "origin", 'Table Toolbar');
    (0, _defineProperty2.default)(this, "toolbar", void 0);
    (0, _defineProperty2.default)(this, "container", void 0);
    (0, _defineProperty2.default)(this, "_isReorderModeSubmitted", false);
    (0, _defineProperty2.default)(this, "pendingPromise", null);
    (0, _defineProperty2.default)(this, "closeSidePanel", void 0);
    (0, _defineProperty2.default)(this, "events", new _events.EventEmitter());
    this.toolbar = params.toolbar;
    this.container = params.container;
    this.closeSidePanel = params.closeSidePanel;
    (0, _mobx.makeObservable)(this, {
      isReorderModeToolbarVisible: _mobx.computed,
      isReorderModePending: _mobx.computed,
      _isReorderModeSubmitted: _mobx.observable.ref,
      _setIsReorderModeSubmitted: _mobx.action
    });
    this.init();
  }
  init() {
    const disposers = [(0, _mobx.reaction)(() => this.moves.length, async () => {
      if (this.moves.length === 0) {
        this.pendingPromise = null;
        this._setIsReorderModeSubmitted(false);
      } else {
        var _this$closeSidePanel;
        (_this$closeSidePanel = this.closeSidePanel) == null || _this$closeSidePanel.call(this);
      }
    })];
    return () => {
      disposers.forEach(disposer => disposer == null ? void 0 : disposer());
    };
  }
  get optimisticActions() {
    return this.toolbar.collection._optimisticActions;
  }
  get patches() {
    var _this$optimisticActio;
    return ((_this$optimisticActio = this.optimisticActions) == null ? void 0 : _this$optimisticActio.state.patches) ?? [];
  }
  get dnd() {
    return this.toolbar._dnd;
  }
  get moves() {
    const {
      toolbar: {
        _dnd
      }
    } = this;
    if (_dnd == null) {
      return [];
    }
    return this.patches.filter(patch => patch.move !== undefined && !patch.isStale).map(({
      move
    }) => _dnd._createSimpleDragEndEvent(move)) ?? [];
  }
  get isReorderModeToolbarVisible() {
    return this.moves.length > 0 && !this._isReorderModeSubmitted;
  }
  get isReorderModePending() {
    return this.moves.length > 0 && this.pendingPromise !== null;
  }
  get snapshot() {
    var _this$optimisticActio2;
    return ((_this$optimisticActio2 = this.optimisticActions) == null ? void 0 : _this$optimisticActio2.collection.result.pages) ?? [];
  }
  get onSubmit() {
    if (this.pendingPromise) {
      return this.pendingPromise;
    }
    const {
      events
    } = this;
    return this.pendingPromise = new Promise((resolve, reject) => {
      events.once('submitSuccess', resolve);
      events.once('submitError', reject);
    });
  }
  _showSuccessToast() {
    var _this$container$showT, _this$container;
    (_this$container$showT = (_this$container = this.container).showToast) == null || _this$container$showT.call(_this$container, {
      type: 'SUCCESS',
      message: this.container.translate('cairo.dragAndDrop.reorderMode.toast.success'),
      biName: 'reorder-mode-submit-success'
    });
  }
  _reportApplyButtonClickedBiEvent() {
    this.dnd.reportBi((0, _bexCore.withoutDefaults)(_bi.cairoApplyButtonClicked)({
      numOfActions: this.moves.length,
      feature: this.feature,
      origin: this.origin
    }));
  }
  _reportDiscardChangesBeforeApplyBiEvent() {
    this.dnd.reportBi((0, _bexCore.withoutDefaults)(_bi.cairoDiscardChangesBeforeApply)({
      numOfActions: this.moves.length,
      feature: this.feature,
      origin: this.origin
    }));
  }
  reportLearnMoreClickedBiEvent() {
    this.dnd.reportBi((0, _bexCore.withoutDefaults)(_bi.cairoLearnMore)({
      origin: this.origin
    }));
  }
  async onBulkCancel() {
    this.discardChangesModal.open(null);
    const {
      result
    } = await this.discardChangesModal.modal.promise();
    if (result === 'confirmed') {
      var _this$optimisticActio3;
      this._reportDiscardChangesBeforeApplyBiEvent();
      this.events.emit('submitError', new Error((_this$optimisticActio3 = this.optimisticActions) == null ? void 0 : _this$optimisticActio3.reorderModeCanceledMessage));
    }
  }
  async onBulkSubmit() {
    this._reportApplyButtonClickedBiEvent();
    try {
      var _this$dnd$onBulkSubmi, _this$dnd;
      this._setIsReorderModeSubmitted(true);
      await ((_this$dnd$onBulkSubmi = (_this$dnd = this.dnd).onBulkSubmit) == null ? void 0 : _this$dnd$onBulkSubmi.call(_this$dnd, this.moves, this.snapshot));
      this._showSuccessToast();
      this.events.emit('submitSuccess');
    } catch (error) {
      this.events.emit('submitError', error);
      this.pendingPromise = null;
    }
  }
  _setIsReorderModeSubmitted(submitted) {
    this._isReorderModeSubmitted = submitted;
  }
}
exports.ReorderModeState = ReorderModeState;
//# sourceMappingURL=ReorderModeState.js.map