"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.TableSectionsState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _mobx = require("mobx");
const SECTION_PREFIX = '$$$wix-patterns-sections$$$-';
const INVISIBLE_ROW_ID = `${SECTION_PREFIX}invisible`;
class TableSectionsState {
  constructor(table, renderSection) {
    (0, _defineProperty2.default)(this, "table", void 0);
    (0, _defineProperty2.default)(this, "renderSection", void 0);
    this.table = table;
    this.renderSection = renderSection;
    (0, _mobx.makeObservable)(this, {
      sectionsKeyedItems: _mobx.computed,
      sectionCounts: _mobx.computed
    });
  }
  generateSectionId(firstItemId) {
    return `${SECTION_PREFIX}${firstItemId}`;
  }
  get computeSectionData() {
    const counts = new Map();
    const keyedItems = [];
    if (this.table.keyedItems.length === 0) {
      return {
        counts,
        keyedItems
      };
    }

    // Add invisible row for design purposes
    keyedItems.push({
      id: INVISIBLE_ROW_ID,
      key: INVISIBLE_ROW_ID,
      item: this.table.keyedItems[0].item,
      index: 0,
      pageIndex: 0,
      indexWithinPage: 0
    });
    let currentSectionId = null;
    let currentSectionIndex = 0;
    let currentHeaderId = null;
    let count = 0;
    for (const keyedItem of this.table.keyedItems) {
      const section = this.renderSection(keyedItem.item);
      const sectionId = section.id;
      if (sectionId !== currentSectionId) {
        // Store count for previous section
        if (currentHeaderId) {
          counts.set(currentHeaderId, {
            count,
            isComplete: true
          });
        }

        // Start new section
        currentSectionId = sectionId;
        currentHeaderId = this.generateSectionId(`${sectionId}@${currentSectionIndex}`);
        currentSectionIndex++;
        count = 1;

        // Add section header to keyed items
        const sectionKeyedItem = {
          id: currentHeaderId,
          key: currentHeaderId,
          item: keyedItem.item,
          index: keyedItems.length,
          pageIndex: 0,
          indexWithinPage: keyedItems.length
        };
        keyedItems.push(sectionKeyedItem);
      } else {
        count++;
      }

      // Add the actual data item
      keyedItems.push(keyedItem);
    }

    // Handle last section
    if (currentHeaderId) {
      const isLastSectionComplete = this.table.collection.fetchedLastPage;
      counts.set(currentHeaderId, {
        count,
        isComplete: isLastSectionComplete
      });
    }
    return {
      counts,
      keyedItems
    };
  }
  get sectionCounts() {
    return this.computeSectionData.counts;
  }
  get sectionsKeyedItems() {
    return this.computeSectionData.keyedItems;
  }
  getSectionWithCount(sectionKeyedItem) {
    const section = this.renderSection(sectionKeyedItem.item);
    const headerId = sectionKeyedItem.id;
    const sectionData = headerId ? this.sectionCounts.get(headerId) || {
      count: 0,
      isComplete: true
    } : {
      count: 0,
      isComplete: true
    };
    return {
      ...section,
      count: sectionData.count,
      hasMore: !sectionData.isComplete
    };
  }
  isSectionRow(keyedItem) {
    return keyedItem.id.startsWith(SECTION_PREFIX);
  }
  isInvisibleRow(keyedItem) {
    return keyedItem.id === INVISIBLE_ROW_ID;
  }
}
exports.TableSectionsState = TableSectionsState;
//# sourceMappingURL=TableSectionsState.js.map