// (C) 2021-2025 GoodData Corporation
import { areObjRefsEqual, isDashboardLayout, isInsightWidget, isKpiWidget, isRichTextWidget, isVisualizationSwitcherWidget, } from "@gooddata/sdk-model";
import { isExtendedDashboardLayoutWidget } from "../../../types/layoutTypes.js";
export function getWidgetCoordinates(layout, ref) {
    const itemData = getWidgetCoordinatesAndItem(layout, ref);
    if (itemData) {
        return itemData.layoutPath;
    }
    return undefined;
}
export function getWidgetsOfType(layout, types) {
    const result = [];
    for (let sectionIndex = 0; sectionIndex < layout.sections.length; sectionIndex++) {
        const section = layout.sections[sectionIndex];
        for (let itemIndex = 0; itemIndex < section.items.length; itemIndex++) {
            const item = section.items[itemIndex];
            if (item.widget?.type !== undefined && types.includes(item.widget?.type)) {
                result.push(item.widget);
            }
            if (isDashboardLayout(item.widget)) {
                result.push(...getWidgetsOfType(item.widget, types));
            }
        }
    }
    return result;
}
const processLayout = (layout, ref, path) => {
    for (let sectionIndex = 0; sectionIndex < layout.sections.length; sectionIndex++) {
        const section = layout.sections[sectionIndex];
        for (let itemIndex = 0; itemIndex < section.items.length; itemIndex++) {
            const item = section.items[itemIndex];
            const layoutPath = [
                ...path,
                {
                    sectionIndex,
                    itemIndex,
                },
            ];
            if (areObjRefsEqual(item.widget?.ref, ref)) {
                return {
                    layoutPath,
                    item,
                };
            }
            if (isDashboardLayout(item.widget)) {
                const result = processLayout(item.widget, ref, layoutPath);
                if (result !== undefined) {
                    return result;
                }
            }
        }
    }
    return undefined;
};
export function getWidgetCoordinatesAndItem(layout, ref) {
    return processLayout(layout, ref, []);
}
export function isItemWithBaseWidget(obj) {
    const widget = obj.widget;
    return (isInsightWidget(widget) ||
        isKpiWidget(widget) ||
        isRichTextWidget(widget) ||
        isVisualizationSwitcherWidget(widget) ||
        isExtendedDashboardLayoutWidget(widget));
}
export function resizeInsightWidget(size, sizeInfo) {
    const { width, height } = sizeInfo;
    const { heightAsRatio } = size;
    let { gridWidth = 0, gridHeight } = size;
    //width
    if (width.max && gridWidth > width.max) {
        gridWidth = width.max;
    }
    if (width.min && gridWidth < width.min) {
        gridWidth = width.min;
    }
    //height
    if (!heightAsRatio) {
        if (height.max && (gridHeight || 0) > height.max) {
            gridHeight = height.max;
        }
        if (height.min && (gridHeight || 0) < height.min) {
            gridHeight = height.min;
        }
    }
    return {
        gridWidth,
        gridHeight,
        heightAsRatio,
    };
}
