// (C) 2019-2026 GoodData Corporation
import { invariant } from "ts-invariant";
import { InsightWidgetBuilder, KpiWidgetBuilder, resolveValueOrUpdateCallback, } from "@gooddata/sdk-backend-base";
import { isDashboardLayout, isDashboardLayoutItem, isInsightWidget, isInsightWidgetDefinition, isKpiWidget, isKpiWidgetDefinition, } from "@gooddata/sdk-model";
/**
 * @alpha
 */
export class DashboardLayoutItemBuilder {
    setSection;
    getItemFacade;
    itemIndex;
    constructor(setSection, getItemFacade, itemIndex) {
        this.setSection = setSection;
        this.getItemFacade = getItemFacade;
        this.itemIndex = itemIndex;
    }
    /**
     * Creates an instance of DashboardLayoutItemBuilder for particular layout item.
     *
     * @param sectionBuilder - section builder
     * @param itemIndex - item to modify
     */
    static for(sectionBuilder, itemIndex) {
        invariant(isDashboardLayoutItem(sectionBuilder.facade().item(itemIndex)?.raw()), `Provided data must be IDashboardLayoutItem! ${itemIndex} ${sectionBuilder
            .facade()
            .item(itemIndex)}`);
        return new DashboardLayoutItemBuilder((section) => sectionBuilder.setSection(section), () => sectionBuilder.facade().item(itemIndex), itemIndex);
    }
    size(valueOrUpdateCallback) {
        this.setItem((item) => ({
            ...item,
            size: resolveValueOrUpdateCallback(valueOrUpdateCallback, item.size),
        }));
        return this;
    }
    widget(valueOrUpdateCallback) {
        this.setItem((item) => ({
            ...item,
            widget: resolveValueOrUpdateCallback(valueOrUpdateCallback, item.widget),
        }));
        return this;
    }
    setItem(valueOrUpdateCallback) {
        this.setSection((section) => {
            const updatedItems = [...section.items];
            updatedItems[this.itemIndex] = resolveValueOrUpdateCallback(valueOrUpdateCallback, this.build());
            return {
                ...section,
                items: updatedItems,
            };
        });
        return this;
    }
    modify(modifications) {
        modifications(this, this.facade());
        return this;
    }
    build() {
        return this.facade().raw();
    }
    facade() {
        return this.getItemFacade();
    }
    newInsightWidget(insight, create = (v) => v) {
        this.widget(create(InsightWidgetBuilder.forNew(insight)).build());
        return this;
    }
    modifyInsightWidget(modify) {
        const content = this.facade().widget();
        invariant(isInsightWidgetDefinition(content) || isInsightWidget(content), "Content of the item is not a kpi widget.");
        this.widget(modify(InsightWidgetBuilder.for(content)).build());
        return this;
    }
    newKpiWidget(measure, create = (v) => v) {
        this.widget(create(KpiWidgetBuilder.forNew(measure)).build());
        return this;
    }
    modifyKpiWidget(modify) {
        const content = this.facade().widget();
        invariant(isKpiWidgetDefinition(content) || isKpiWidget(content), "Content of the item is not a kpi widget.");
        const builder = KpiWidgetBuilder.for(content);
        this.widget(modify(builder).build());
        return this;
    }
    modifyLayoutWidget(modify, dashboardLayoutBuilderConstructor) {
        const content = this.facade().widget();
        invariant(isDashboardLayout(content), "Content of the item is not a layout widget.");
        const itemPath = this.getItemFacade().index();
        const builder = dashboardLayoutBuilderConstructor(content, itemPath);
        this.widget(modify(builder).build());
        return this;
    }
}
//# sourceMappingURL=item.js.map