// (C) 2021-2025 GoodData Corporation
import { v4 as uuidv4 } from "uuid";
import { idRef, isDashboardLayoutItem, isInsightWidget, isInsightWidgetDefinition, isKpiWidgetDefinition, isWidget, } from "@gooddata/sdk-model";
import { isCustomWidgetDefinition, } from "../types/layoutTypes.js";
export function extractInsightRefsFromItems(items) {
    const result = [];
    items.forEach((item) => {
        if (isInsightWidget(item.widget)) {
            result.push(item.widget.insight);
        }
    });
    return result;
}
const TemporaryIdentityPrefix = "@@GDC.DASH.TEMP";
function generateTemporaryIdentityForWidget() {
    const id = `${TemporaryIdentityPrefix}-${uuidv4()}`;
    return {
        ref: idRef(id),
        identifier: id,
        uri: id,
    };
}
/**
 * Tests whether the provided object is/has a temporary identity. A temporary identity is used for those
 * objects which are not yet persisted however need to be reference-able from the dashboard itself.
 *
 * @param obj - object to test
 * @internal
 */
export function isTemporaryIdentity(obj) {
    return obj.identifier.startsWith(TemporaryIdentityPrefix);
}
/**
 * Adds temporary identity to all insight and KPI widget definitions found within the provided dashboard item
 * definitions.
 *
 * Having an identity for each widget is important for the dashboard; many places both internal and through
 * public API for different use-cases rely on identification of widget by reference.
 *
 * This function will map the input items so that the result never contains bare insight widget definitions
 * or kpi widget definitions (which do not have the identity).
 *
 * The identity assigned to those widgets is temporary one. It is a specially formatted identifier combined
 * with UUID. You can use {@link isTemporaryIdentity} function to test whether object has temporary
 * identity. When the dashboard gets saved, this temporary identity will change to permanent one that
 * will be assigned by the backend.
 *
 * @param items - items to map, will not be modified
 * @returns new array with the necessary items mapped to widgets with temporary identity.
 * @internal
 */
export function addTemporaryIdentityToWidgets(items) {
    return items.map((item) => {
        if (!isDashboardLayoutItem(item)) {
            return item;
        }
        if (isInsightWidgetDefinition(item.widget) ||
            isKpiWidgetDefinition(item.widget) ||
            isCustomWidgetDefinition(item.widget)) {
            const temporaryIdentity = generateTemporaryIdentityForWidget();
            return {
                ...item,
                widget: {
                    ...item.widget,
                    ...temporaryIdentity,
                },
            };
        }
        return item;
    });
}
/**
 * Safely gets the title from an ExtendedDashboardWidget.
 *
 * @param widget - The widget to get title from
 * @param fallbackTitle - Title to return if widget doesn't have a title property
 * @returns The widget title if available, otherwise the fallback title
 * @public
 */
export function getWidgetTitle(widget, fallbackTitle = "") {
    if (!widget) {
        return fallbackTitle;
    }
    // Check if it's a regular widget (IWidget) which has title
    if (isWidget(widget)) {
        return widget.title;
    }
    // Fallback for any other cases
    return fallbackTitle;
}
//# sourceMappingURL=dashboardItemUtils.js.map