"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.CustomColumnsDragAndDropState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _bexCore = require("@wix/bex-core");
var _sortable = require("@wix/wix-style-react-incubator/dnd-kit/sortable");
var _util = require("@wix/bex-core/util");
var _DragAndDrop = require("../components/DragAndDrop");
var _mobx = require("mobx");
var _RectState = require("./RectState");
var _indexOfCompareFn = require("./indexOfCompareFn");
class CustomColumnsDragAndDropState {
  get _dnd() {
    var _this$collectionDragA;
    return (_this$collectionDragA = this.collectionDragAndDropState) == null ? void 0 : _this$collectionDragA.dnd;
  }
  constructor(params) {
    (0, _defineProperty2.default)(this, "container", void 0);
    (0, _defineProperty2.default)(this, "customColumns", void 0);
    (0, _defineProperty2.default)(this, "dataSource", void 0);
    (0, _defineProperty2.default)(this, "columnsActions", void 0);
    (0, _defineProperty2.default)(this, "components", void 0);
    (0, _defineProperty2.default)(this, "collectionDragAndDropState", null);
    (0, _defineProperty2.default)(this, "forceRenderIndexes", undefined);
    (0, _defineProperty2.default)(this, "reportBi", () => {
      // report columns re-order BI here
    });
    (0, _defineProperty2.default)(this, "listRect", void 0);
    const {
      customColumns,
      dataSource,
      components
    } = params;
    this.components = components;
    this.container = customColumns.container;
    this.listRect = new _RectState.RectState({
      container: customColumns.container
    });
    this.customColumns = customColumns;
    this.dataSource = dataSource;
    this.columnsActions = new _bexCore.CollectionOptimisticActions({
      container: this.container,
      collection: this.customColumns.columnsCollection
    });
    this.collectionDragAndDropState = new _DragAndDrop.CollectionDragAndDropState({
      state: this,
      a11yContainer: params.modalsContainerRef,
      container: this.container
    });
    (0, _mobx.makeObservable)(this, {
      _onCustomColumnsReorder: _mobx.action.bound,
      containerRect: _mobx.computed,
      orderedColumns: _mobx.computed,
      orderedColumnsByReorderDisabled: _mobx.computed
    });
  }
  get orderedColumnsByReorderDisabled() {
    return this.dataSource.columns.reduce((state, column) => {
      column.reorderDisabled ? state.reorderDisabled.push(column) : state.regular.push(column);
      return state;
    }, {
      regular: [],
      reorderDisabled: []
    });
  }
  get orderedColumns() {
    const {
      dataSource
    } = this;
    const {
      selectedColumns: {
        value: selectedColumns
      }
    } = dataSource;
    const {
      reorderDisabled,
      regular
    } = this.orderedColumnsByReorderDisabled;
    return [...reorderDisabled, ...regular.sort((0, _indexOfCompareFn.indexOfCompareFn)(selectedColumns, c => c.id))];
  }
  get collection() {
    return this.customColumns.columnsCollection;
  }
  get containerRect() {
    const {
      listRect
    } = this;
    if (listRect.rect.left) {
      return {
        ...listRect.rect
      };
    }
    return undefined;
  }
  _onCustomColumnsReorder(columnId, overColumnId) {
    const {
      dataSource: {
        selectedColumns
      }
    } = this;
    const columnsArray = selectedColumns.value;
    const reorderedColumnsArray = (0, _sortable.arrayMove)(columnsArray, columnsArray.findIndex(c => c.id === columnId), columnsArray.findIndex(c => c.id === overColumnId));
    selectedColumns.refresh(reorderedColumnsArray);
  }
  init() {
    const {
      columnsActions
    } = this;
    const disposers = [columnsActions.init(), (0, _util.addEventListener)(columnsActions.events, 'updated', async () => {
      const {
        customColumns: {
          columnsCollection
        }
      } = this;
      await columnsCollection.refreshAllPages();
    })];
    return () => {
      disposers.forEach(disposer => disposer == null ? void 0 : disposer());
    };
  }
}
exports.CustomColumnsDragAndDropState = CustomColumnsDragAndDropState;
//# sourceMappingURL=CustomColumnsDragAndDropState.js.map