// (C) 2019-2026 GoodData Corporation
import { isEqual } from "lodash-es";
import { areObjRefsEqual, isDashboardLayout, isDashboardLayoutItem, isInsightWidget, isInsightWidgetDefinition, isKpiWidget, isKpiWidgetDefinition, isObjRef, isWidget, isWidgetDefinition, } from "@gooddata/sdk-model";
import { areLayoutPathsEqual, updateItemIndex } from "../../../layout/coordinates.js";
import { determineWidthForScreen } from "../../../layout/sizing.js";
/**
 * @alpha
 */
export class DashboardLayoutItemFacade {
    sectionFacade;
    item;
    itemIndex;
    constructor(sectionFacade, item, itemIndex) {
        this.sectionFacade = sectionFacade;
        this.item = item;
        this.itemIndex = itemIndex;
    }
    static for(sectionFacade, item, index) {
        return new DashboardLayoutItemFacade(sectionFacade, item, index);
    }
    raw() {
        return this.item;
    }
    section() {
        return this.sectionFacade;
    }
    index() {
        return this.itemIndex;
    }
    indexIs(index) {
        return areLayoutPathsEqual(this.index(), index);
    }
    isFirstInSection() {
        return this.indexIs(updateItemIndex(this.itemIndex, 0));
    }
    isLastInSection() {
        return this.indexIs(updateItemIndex(this.itemIndex, this.sectionFacade.items().count() - 1));
    }
    isLastInRow(screen) {
        const rows = this.section().items().asGridRows(screen);
        return rows.some((rowItems) => areLayoutPathsEqual(rowItems[rowItems.length - 1].index(), this.index()));
    }
    widget() {
        return this.item.widget;
    }
    ref() {
        if (isObjRef(this.item.widget)) {
            return this.item.widget;
        }
        return undefined;
    }
    widgetEquals(widget) {
        return isEqual(this.widget(), widget);
    }
    widgetIs(widget) {
        return this.widget() === widget;
    }
    isEmpty() {
        const widget = this.widget();
        return widget === null || widget === undefined;
    }
    size() {
        return this.item.size;
    }
    sizeForScreen(screen) {
        return this.size()[screen];
    }
    sizeForScreenWithFallback(screen) {
        return {
            gridWidth: determineWidthForScreen(screen, this.size()),
            gridHeight: this.sizeForScreen(screen)?.gridHeight,
        };
    }
    hasSizeForScreen(screen) {
        const size = this.sizeForScreen(screen);
        return !(size === null || size === undefined);
    }
    testRaw(pred) {
        return pred(this.raw());
    }
    test(pred) {
        return pred(this);
    }
    isWidgetItem() {
        return isWidget(this.widget());
    }
    isWidgetDefinitionItem() {
        return isWidgetDefinition(this.widget());
    }
    isKpiWidgetItem() {
        return isKpiWidget(this.widget());
    }
    isKpiWidgetDefinitionItem() {
        return isKpiWidgetDefinition(this.widget());
    }
    isInsightWidgetItem() {
        return isInsightWidget(this.widget());
    }
    isInsightWidgetDefinitionItem() {
        return isInsightWidgetDefinition(this.widget());
    }
    isLayoutItem() {
        return isDashboardLayout(this.widget());
    }
    isCustomItem() {
        return !isDashboardLayoutItem(this.widget());
    }
    isWidgetItemWithRef(ref) {
        if (this.isWidgetItem()) {
            return areObjRefsEqual(this.item.widget?.ref, ref);
        }
        return false;
    }
    isWidgetItemWithInsightRef(ref) {
        if (this.isInsightWidgetItem() || this.isInsightWidgetDefinitionItem()) {
            return areObjRefsEqual(this.item.widget?.insight, ref);
        }
        return false;
    }
    isWidgetItemWithKpiRef(ref) {
        if (this.isKpiWidgetItem() || this.isKpiWidgetDefinitionItem()) {
            return areObjRefsEqual(this.item.widget?.ref, ref);
        }
        return false;
    }
}
//# sourceMappingURL=item.js.map