"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.GridBaseState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _mobx = require("mobx");
var _RectState = require("./RectState");
var _ResponsiveColumnsState = require("./ResponsiveColumnsState");
class GridBaseState {
  get _dnd() {
    var _this$collectionDragA;
    return (_this$collectionDragA = this.collectionDragAndDropState) == null ? void 0 : _this$collectionDragA.dnd;
  }
  constructor(params) {
    var _params$range, _params$range2;
    (0, _defineProperty2.default)(this, "toolbar", void 0);
    (0, _defineProperty2.default)(this, "container", void 0);
    (0, _defineProperty2.default)(this, "collection", void 0);
    (0, _defineProperty2.default)(this, "_containerRectState", void 0);
    (0, _defineProperty2.default)(this, "_responsiveColumnsState", void 0);
    (0, _defineProperty2.default)(this, "parentState", void 0);
    (0, _defineProperty2.default)(this, "_sizePreset", void 0);
    (0, _defineProperty2.default)(this, "layoutType", void 0);
    (0, _defineProperty2.default)(this, "trailingItemsCount", 0);
    (0, _defineProperty2.default)(this, "minItemWidth", 196);
    (0, _defineProperty2.default)(this, "smallCardWidthThreshold", 200);
    (0, _defineProperty2.default)(this, "preset", 'title');
    (0, _defineProperty2.default)(this, "_draggable", 'none');
    (0, _defineProperty2.default)(this, "_localLimit", void 0);
    (0, _defineProperty2.default)(this, "_localStart", void 0);
    (0, _defineProperty2.default)(this, "forceRenderIndexes", undefined);
    (0, _defineProperty2.default)(this, "collectionDragAndDropState", null);
    (0, _defineProperty2.default)(this, "onAddItemClick", origin => {
      this.toolbar.toolbarBIReporter.onAddItemClick(origin);
    });
    (0, _defineProperty2.default)(this, "onAddItemActionComplete", async items => {
      return this.toolbar.onAddItemActionComplete(items);
    });
    this.container = params.container;
    this.collection = params.collection;
    this.parentState = params.parentState;
    this.layoutType = params.layoutType ?? 'grid';
    this._containerRectState = new _RectState.RectState({
      container: this.container
    });
    this._draggable = params.draggable ?? 'none';
    this.toolbar = params.toolbar;
    this._responsiveColumnsState = new _ResponsiveColumnsState.ResponsiveColumnsState({
      gridState: this,
      minItemWidth: this.minItemWidth,
      rectState: this._containerRectState
    });
    this._sizePreset = params.sizePreset;
    this._localLimit = (_params$range = params.range) == null ? void 0 : _params$range.length;
    this._localStart = ((_params$range2 = params.range) == null ? void 0 : _params$range2.start) ?? 0;
    (0, _mobx.makeObservable)(this, {
      trailingItemsCount: _mobx.observable.ref,
      forceRenderIndexes: _mobx.observable.ref,
      preset: _mobx.observable.ref,
      _draggable: _mobx.observable.ref,
      draggable: _mobx.computed,
      itemsToRenderCount: _mobx.computed,
      itemsToRender: _mobx.computed,
      rowCount: _mobx.computed,
      isInteracting: _mobx.computed,
      calcGridProps: _mobx.computed,
      columnsCount: _mobx.computed,
      _sizeProps: _mobx.computed,
      itemKeyRecyclerOverride: _mobx.computed,
      _localLimit: _mobx.observable.ref,
      _localStart: _mobx.observable.ref,
      itemsContentWidth: _mobx.computed,
      containerPaddingTop: _mobx.computed,
      gap: _mobx.computed,
      _sizePreset: _mobx.observable.ref,
      layoutType: _mobx.observable.ref
    });
  }
  get sizePreset() {
    return this.parentState._sizePreset ?? this._sizePreset;
  }
  get imagePlacement() {
    return this.sizePreset.imagePlacement;
  }
  get gap() {
    const {
      layoutType
    } = this;
    if (layoutType === 'list') {
      return 12;
    }
    return 24;
  }
  get containerPaddingTop() {
    const {
      layoutType
    } = this;
    if (layoutType === 'list') {
      return 0;
    }
    return this.toolbar.contentPadding;
  }
  get aspectRatio() {
    return this.sizePreset.aspectRatio;
  }
  get footerHeights() {
    return this.sizePreset.footerHeights;
  }
  get itemsContentWidth() {
    return this._responsiveColumnsState.itemsContentWidth;
  }
  get draggable() {
    var _this$_dnd;
    if ((_this$_dnd = this._dnd) != null && _this$_dnd.isDisabled) {
      return 'none';
    }
    return this._draggable;
  }
  get window() {
    return this.container.window;
  }
  get itemsToRenderCount() {
    const {
      collection,
      trailingItemsCount,
      _localLimit,
      _localStart
    } = this;
    const {
      result: {
        keyedItems
      }
    } = collection;
    const itemsCount = _localLimit != null ? Math.min(_localLimit, keyedItems.length) : keyedItems.length;
    return collection.fetchedLastPage || _localLimit != null && _localStart + _localLimit < keyedItems.length ? itemsCount + trailingItemsCount : itemsCount - Math.floor(itemsCount % this.columnsCount);
  }
  get reportBi() {
    return this.toolbar.reportBi;
  }
  get columnsCount() {
    return this.calcGridProps.columnCount;
  }
  get containerRect() {
    return this.toolbar.containerRect;
  }
  get rowCount() {
    const {
      itemsToRenderCount,
      columnsCount
    } = this;
    return Math.ceil(itemsToRenderCount / columnsCount);
  }
  get isInteracting() {
    var _this$_dnd2, _this$_dnd3;
    return ((_this$_dnd2 = this._dnd) == null ? void 0 : _this$_dnd2.isActive) || ((_this$_dnd3 = this._dnd) == null ? void 0 : _this$_dnd3.isDropping);
  }
  get itemsToRender() {
    const {
      itemsToRenderCount
    } = this;
    return new Array(itemsToRenderCount).fill(null);
  }
  getItemRenderTypeAt(index) {
    const {
      collection,
      itemsToRenderCount,
      _localStart
    } = this;
    const {
      result: {
        keyedItems
      }
    } = collection;
    if (index >= itemsToRenderCount) {
      return null;
    }
    const indexInCollection = index + _localStart;
    if (indexInCollection >= keyedItems.length) {
      return 'trailing';
    }
    return keyedItems[indexInCollection];
  }
  get errorMonitor() {
    return this.container.errorMonitor;
  }
  get _sizeProps() {
    const {
      aspectRatio,
      preset,
      draggable,
      _responsiveColumnsState: {
        gap,
        columnCount: responsiveColumnCount,
        itemsContentWidth: width,
        widthWithLastColumnGap
      },
      layoutType
    } = this;
    const columnCount = layoutType === 'list' ? 1 : responsiveColumnCount;
    const widthWithoutGaps = width - (columnCount - 1) * gap;
    const columnWidth = widthWithoutGaps / columnCount;
    const cardFooterSize = 'small';
    const cardFooterHeight = this.footerHeights[preset][draggable][cardFooterSize];
    const cardTopImageHeight = columnWidth * aspectRatio;
    const rowHeight = cardTopImageHeight + (cardFooterHeight > 0 ? cardFooterHeight + 1 : 0);
    const columnWidthIncludingGap = widthWithLastColumnGap / columnCount;
    const rowHeightIncludingGap = rowHeight + gap;
    return {
      columnCount,
      columnWidth: () => columnWidth,
      columnWidthIncludingGap: () => columnWidthIncludingGap,
      rowHeight: () => rowHeight,
      rowHeightFixed: rowHeight,
      cardTopImageHeight,
      rowHeightIncludingGap: () => rowHeightIncludingGap,
      cardFooterHeight,
      cardFooterSize
    };
  }
  get calcGridProps() {
    const {
      toolbar: {
        itemsContentHeight
      },
      _sizeProps: {
        rowHeightFixed
      }
    } = this;
    const overscanCount = Math.round(itemsContentHeight / rowHeightFixed * 1.5);
    return {
      ...this._sizeProps,
      overscanCount
    };
  }
  get getCollectionSnapshot() {
    return this.toolbar.getCollectionSnapshot;
  }
  get itemKeyRecyclerOverride() {
    const {
      collection
    } = this;
    const {
      result: {
        keyedItems
      }
    } = collection;
    return index => {
      var _keyedItems$index;
      return ((_keyedItems$index = keyedItems[index]) == null ? void 0 : _keyedItems$index.key) ?? index;
    };
  }
  get dragAndDropCategories() {
    return this.toolbar.dragAndDropCategories;
  }
  get listAttributes() {
    var _this$_dnd4;
    return {
      ...((_this$_dnd4 = this._dnd) == null || _this$_dnd4.nullAnnouncements.listAttributes == null ? void 0 : _this$_dnd4.nullAnnouncements.listAttributes()),
      role: 'list'
    };
  }
  _subscribe() {
    const {
      parentState: {
        _subGrids
      }
    } = this;
    if (!_subGrids) {
      return;
    }
    _subGrids.push(this);
    return () => {
      const i = _subGrids.indexOf(this);
      i !== -1 && _subGrids.splice(i, 1);
    };
  }
  init() {
    return this._subscribe();
  }
}
exports.GridBaseState = GridBaseState;
//# sourceMappingURL=GridBaseState.js.map