"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.GridSectionsState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _mobx = require("mobx");
class GridSectionsState {
  constructor(params) {
    (0, _defineProperty2.default)(this, "groupBy", void 0);
    (0, _defineProperty2.default)(this, "renderSection", void 0);
    (0, _defineProperty2.default)(this, "toolbar", void 0);
    (0, _defineProperty2.default)(this, "collection", void 0);
    (0, _defineProperty2.default)(this, "parentState", void 0);
    this.collection = params.collection;
    this.toolbar = params.toolbar;
    this.groupBy = params.groupBy;
    this.renderSection = params.renderSection;
    this.parentState = params.parentState;
    (0, _mobx.makeObservable)(this, {
      sections: _mobx.computed,
      groupBy: _mobx.observable.ref,
      renderSection: _mobx.observable.ref
    });
  }
  get sections() {
    const {
      collection,
      groupBy,
      renderSection
    } = this;
    if (!groupBy) {
      return [];
    }
    const sectionItemsMap = {};
    const orderedSections = [];
    collection.result.items.forEach(item => {
      const sectionId = groupBy(item);
      if (!sectionItemsMap[sectionId]) {
        sectionItemsMap[sectionId] = [];
        orderedSections.push(sectionId);
      }
      sectionItemsMap[sectionId].push(item);
    });
    let currentIndex = 0;
    return orderedSections.map((sectionId, sectionIndex) => {
      const sectionItems = sectionItemsMap[sectionId];
      const section = renderSection ? renderSection(sectionId, sectionItems) : {
        title: sectionId
      };
      const range = {
        start: currentIndex,
        length: sectionItems.length
      };
      currentIndex += sectionItems.length;
      return {
        sectionId,
        sectionIndex,
        section,
        range
      };
    });
  }
}
exports.GridSectionsState = GridSectionsState;
//# sourceMappingURL=GridSectionsState.js.map