// (C) 2019-2026 GoodData Corporation
import { invariant } from "ts-invariant";
import { asLayoutItemPath } from "../../../layout/coordinates.js";
import { DASHBOARD_LAYOUT_GRID_COLUMNS_COUNT } from "../config.js";
import { DashboardLayoutItemFacade } from "./item.js";
/**
 * @alpha
 */
export class DashboardLayoutItemsFacade {
    sectionFacade;
    itemFacades;
    constructor(sectionFacade, itemFacades) {
        this.sectionFacade = sectionFacade;
        this.itemFacades = itemFacades;
    }
    static for(sectionFacade, items) {
        const itemFacades = items.map((column, index) => DashboardLayoutItemFacade.for(sectionFacade, column, asLayoutItemPath(sectionFacade.index(), index)));
        return new DashboardLayoutItemsFacade(sectionFacade, itemFacades);
    }
    raw() {
        return this.itemFacades.map((item) => item.raw());
    }
    item(index) {
        return this.itemFacades[index];
    }
    all() {
        return this.itemFacades;
    }
    asGridRows(screen) {
        const renderedRows = [];
        const parentWidth = this.sectionFacade.layout().size()?.gridWidth ?? DASHBOARD_LAYOUT_GRID_COLUMNS_COUNT;
        let currentRowWidth = 0;
        let currentRow = [];
        this.itemFacades.forEach((itemFacade, index) => {
            const itemSize = itemFacade.sizeForScreen(screen);
            invariant(itemSize, `Item size for ${screen} screen is not defined.`);
            if (currentRowWidth + itemSize.gridWidth > parentWidth) {
                if (index !== 0) {
                    renderedRows.push(currentRow);
                }
                currentRow = [];
                currentRowWidth = 0;
            }
            currentRow.push(itemFacade);
            currentRowWidth = currentRowWidth + itemSize.gridWidth;
        });
        if (currentRow.length > 0) {
            renderedRows.push(currentRow);
        }
        return renderedRows;
    }
    flatMap(callback) {
        return this.itemFacades.flatMap(callback);
    }
    count() {
        return this.itemFacades.length;
    }
    map(callback) {
        return this.itemFacades.map(callback);
    }
    reduce(callback, initialValue) {
        return this.itemFacades.reduce(callback, initialValue);
    }
    every(pred) {
        return this.itemFacades.every(pred);
    }
    some(pred) {
        return this.itemFacades.some(pred);
    }
    find(pred) {
        return this.itemFacades.find(pred);
    }
    filter(pred) {
        return this.itemFacades.filter(pred);
    }
}
//# sourceMappingURL=items.js.map