import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2020-2026 GoodData Corporation
import { useMemo } from "react";
import { isDashboardWidget } from "@gooddata/sdk-model";
import { extendedWidgetDebugStr } from "../../../model/types/layoutTypes.js";
import { isInitialPlaceholderWidget, isInsightPlaceholderWidget, isLoadingPlaceholderWidget, } from "../../../widgets/placeholders/types.js";
import { useDashboardComponentsContext } from "../../dashboardContexts/DashboardComponentsContext.js";
import { LoadingDashboardPlaceholderWidget } from "../../dragAndDrop/draggableWidget/LoadingDashboardWidget.js";
import { EmptyDashboardDropZone as FlexibleEmptyDashboardDropZone } from "../../flexibleLayout/dragAndDrop/draggableWidget/EmptyDashboardDropZone.js";
import { DefaultDashboardWidget } from "./DefaultDashboardWidget.js";
function BadWidgetType() {
    return _jsx("div", { children: "Missing renderer" });
}
function MissingWidget() {
    return _jsx("div", { children: "Missing widget" });
}
/**
 * @internal
 */
export function DashboardWidget(props) {
    const { WidgetComponentProvider, InsightWidgetComponentSet } = useDashboardComponentsContext();
    const { widget, 
    // @ts-expect-error Don't expose index prop on public interface (we need it only for css class for KD tests)
    index, } = props;
    const WidgetComponent = useMemo(() => {
        // TODO: we need to get rid of this; the widget being optional at this point is the problem; the parent
        //  components (or possibly the model) should deal with layout items that have no valid widgets associated
        //  and thus short-circuit.
        if (!widget) {
            console.warn("Attempting render an undefined widget.");
            return MissingWidget;
        }
        const Component = WidgetComponentProvider(widget);
        // the default WidgetComponentProvider always returns something, DefaultDashboardWidget by default
        if (Component && Component !== DefaultDashboardWidget) {
            return Component;
        }
        function InitialPlaceholder() {
            return _jsx(FlexibleEmptyDashboardDropZone, {});
        }
        if (isInitialPlaceholderWidget(widget)) {
            return InitialPlaceholder;
        }
        if (isLoadingPlaceholderWidget(widget)) {
            return LoadingDashboardPlaceholderWidget;
        }
        if (isInsightPlaceholderWidget(widget) && InsightWidgetComponentSet.creating) {
            return InsightWidgetComponentSet.creating.CreatingPlaceholderComponent;
        }
        if (isDashboardWidget(widget)) {
            return DefaultDashboardWidget;
        }
        console.warn(`Unable to render widget ${extendedWidgetDebugStr(widget)}`);
        return BadWidgetType;
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [widget]);
    return (_jsx(WidgetComponent, { ...props, 
        // @ts-expect-error Don't expose index prop on public interface (we need it only for css class for KD tests)
        index: index }));
}
//# sourceMappingURL=DashboardWidget.js.map