"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.MultiLevelSortingStateDragAndDrop = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _bexCore = require("@wix/bex-core");
var _bi = require("@wix/bex-core/bi");
var _sortable = require("@wix/wix-style-react-incubator/dnd-kit/sortable");
var _events = _interopRequireDefault(require("events"));
var _DragAndDrop = require("../components/DragAndDrop");
var _mobx = require("mobx");
class MultiLevelSortingStateDragAndDrop {
  constructor(params) {
    (0, _defineProperty2.default)(this, "multiLevelSortingState", void 0);
    (0, _defineProperty2.default)(this, "multiLevelSortingActions", void 0);
    (0, _defineProperty2.default)(this, "collectionDragAndDropState", null);
    (0, _defineProperty2.default)(this, "forceRenderIndexes", undefined);
    (0, _defineProperty2.default)(this, "events", new _events.default());
    const {
      multiLevelSortingState
    } = params;
    this.multiLevelSortingState = multiLevelSortingState;
    this.multiLevelSortingActions = new _bexCore.CollectionOptimisticActions({
      container: this.container,
      collection: this.collection
    });
    this.collectionDragAndDropState = new _DragAndDrop.CollectionDragAndDropState({
      state: this,
      a11yContainer: params.modalsContainerRef,
      container: this.multiLevelSortingState.container
    });
    (0, _mobx.makeObservable)(this, {
      forceRenderIndexes: _mobx.observable.ref
    });
  }
  get _dnd() {
    var _this$collectionDragA;
    return (_this$collectionDragA = this.collectionDragAndDropState) == null ? void 0 : _this$collectionDragA.dnd;
  }
  get reportBi() {
    return this.multiLevelSortingState.table.reportBi;
  }
  get container() {
    return this.multiLevelSortingState.container;
  }
  get collection() {
    return this.multiLevelSortingState.sortingCollection;
  }
  _onSortReorder(fromColumnId, overColumnId) {
    const {
      orderedSortableColumns
    } = this.multiLevelSortingState;
    const indexBefore = orderedSortableColumns.findIndex(({
      columnId
    }) => columnId === fromColumnId);
    const indexAfter = orderedSortableColumns.findIndex(({
      columnId
    }) => columnId === overColumnId);
    const newOrder = (0, _sortable.arrayMove)(orderedSortableColumns, indexBefore, indexAfter);
    this.events.emit('beforeReorder');
    orderedSortableColumns.replace(newOrder);
    this.events.emit('afterReorder', {
      indexBefore,
      indexAfter,
      columnId: fromColumnId
    });
  }
  init() {
    const {
      reportBi,
      events,
      multiLevelSortingState
    } = this;
    const disposers = [this.multiLevelSortingActions.init(), (0, _bexCore.addEventListener)(events, 'beforeReorder', () => {
      const prevSortOrder = JSON.stringify(multiLevelSortingState.currentSortOrder);
      events.once('afterReorder', ({
        indexBefore,
        indexAfter,
        columnId
      }) => {
        const newSortOrder = JSON.stringify(multiLevelSortingState.currentSortOrder);
        const numSortingColumns = multiLevelSortingState.orderedSortableColumns.length;
        reportBi((0, _bexCore.withoutDefaults)(_bi.cairoReorderingSortColumns)({
          columnIndexBefore: indexBefore,
          columnIndexAfter: indexAfter,
          numSortingColumns,
          sortingColumn: columnId,
          newSortOrder,
          prevSortOrder
        }));
      });
    })];
    return () => {
      disposers.forEach(disposer => disposer == null ? void 0 : disposer());
    };
  }
}
exports.MultiLevelSortingStateDragAndDrop = MultiLevelSortingStateDragAndDrop;
//# sourceMappingURL=MultiLevelSortingDragAndDrop.js.map