import { computed, makeObservable, observable } from 'mobx';
export class GridSectionsState {
    constructor(params) {
        this.collection = params.collection;
        this.toolbar = params.toolbar;
        this.groupBy = params.groupBy;
        this.renderSection = params.renderSection;
        this.parentState = params.parentState;
        makeObservable(this, {
            sections: computed,
            groupBy: observable.ref,
            renderSection: 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 };
        });
    }
}
//# sourceMappingURL=GridSectionsState.js.map