import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2022-2025 GoodData Corporation
import { useCallback, useEffect, useMemo, useState } from "react";
import { IntlWrapper } from "@gooddata/sdk-ui";
import { EmbedInsightDialogBase, Overlay, getDefaultEmbedTypeOptions, getHeightWithUnitsForEmbedCode, } from "@gooddata/sdk-ui-kit";
import { getWebComponentsCodeGenerator } from "../../../utils/embeddingCodeGenerator/getWebComponentsCodeGenerator.js";
import { insightViewEmbeddedCodeGenerator } from "../../../utils/embeddingInsightViewCodeGenerator/insightViewCodeGenerator.js";
import { FullVisualizationCatalog } from "../../VisualizationCatalog.js";
const INSIGHT_VIEW_PROPERTIES_LINK = "https://www.gooddata.com/docs/gooddata-ui/latest/learn/visualize_data/insightview#properties";
/**
 * @internal
 */
export function EmbedInsightDialog(props) {
    const { locale, openSaveInsightDialog, onClose, onCopyCode, showWebComponentsTab } = props;
    const { code, documentationLink, integrationDocLink, currentTab, codeOption, onTabChange, onOptionsChange, } = useEmbedInsightDialog(props);
    return (_jsx(IntlWrapper, { locale: locale, children: _jsx(ModalOverlay, { children: _jsx(EmbedInsightDialogBase, { code: code ?? "", propertiesLink: documentationLink, showWebComponentsTab: showWebComponentsTab, integrationDocLink: integrationDocLink, openSaveInsightDialog: openSaveInsightDialog, onClose: onClose, onCopyCode: onCopyCode, embedTab: currentTab, onTabChange: onTabChange, embedTypeOptions: codeOption, onOptionsChange: onOptionsChange }) }) }));
}
const useEmbedInsightDialog = (props) => {
    const { insight, backend, settings, colorPalette, theme, executionConfig, reactIntegrationDocLink, webComponentIntegrationDocLink, workspaceId, } = props;
    const [currentTab, setCurrentTab] = useState("react");
    const [codeOption, setCodeOption] = useState(getDefaultEmbedTypeOptions(currentTab));
    useEffect(() => {
        setCodeOption(getDefaultEmbedTypeOptions(currentTab));
    }, [currentTab]);
    const code = useMemo(() => {
        const isReactDefinitionCode = codeOption.type === "react" && codeOption.componentType === "definition";
        if (!insight.insight.identifier && !isReactDefinitionCode) {
            return null;
        }
        const inputBase = {
            insight,
            settings,
            backend,
            colorPalette,
            theme,
            executionConfig,
            workspaceId,
        };
        return codeOption.type === "react"
            ? generateCodeByReact({
                ...inputBase,
                codeOption: codeOption,
            })
            : generateCodeByWebComponents({
                ...inputBase,
                codeOption,
            });
    }, [codeOption, insight, settings, backend, colorPalette, theme, executionConfig, workspaceId]);
    const documentationLink = useMemo(() => {
        if (codeOption.type === "react") {
            return getLinkToPropertiesDocumentation(codeOption.componentType, insight, settings);
        }
        return getLinkToPropertiesDocumentation("definition", insight, settings);
    }, [insight, codeOption, settings]);
    const integrationDocLink = useMemo(() => {
        return currentTab === "react" ? reactIntegrationDocLink : webComponentIntegrationDocLink;
    }, [currentTab, reactIntegrationDocLink, webComponentIntegrationDocLink]);
    const onTabChange = useCallback((selectedTab) => {
        setCurrentTab(selectedTab);
    }, []);
    const onOptionsChange = useCallback((opt) => setCodeOption(opt), []);
    return {
        code,
        documentationLink,
        integrationDocLink,
        currentTab,
        codeOption,
        onTabChange,
        onOptionsChange,
    };
};
const getLinkToPropertiesDocumentation = (codeType, insight, settings) => {
    if (codeType === "definition") {
        const meta = FullVisualizationCatalog.forInsight(insight, settings?.enableNewPivotTable ?? true, settings?.enableNewGeoPushpin ?? false).getMeta();
        if (meta?.documentationUrl) {
            return `${meta?.documentationUrl}#properties`;
        }
    }
    return INSIGHT_VIEW_PROPERTIES_LINK;
};
const generateCodeByReact = (input) => {
    const { backend, codeOption, colorPalette, theme, executionConfig, insight, settings } = input;
    const height = getHeightWithUnitsForEmbedCode(codeOption);
    const generateCodeConfig = {
        context: {
            settings,
            backend,
            colorPalette,
            theme,
            executionConfig,
        },
        language: codeOption.codeType,
        height,
        omitChartProps: codeOption.displayConfiguration ? [] : ["config"],
    };
    if (codeOption.componentType === "definition") {
        const descriptor = FullVisualizationCatalog.forInsight(insight, settings?.enableNewPivotTable ?? true, settings?.enableNewGeoPushpin ?? false);
        return descriptor.getEmbeddingCode?.(insight, generateCodeConfig);
    }
    return insightViewEmbeddedCodeGenerator(insight, generateCodeConfig);
};
const generateCodeByWebComponents = (input) => {
    const { codeOption, insight, workspaceId } = input;
    const height = getHeightWithUnitsForEmbedCode(codeOption);
    return getWebComponentsCodeGenerator(workspaceId, insight, { ...codeOption, height });
};
const BUBBLE_ALIGN_POINTS = [{ align: "cc cc" }];
function ModalOverlay({ children }) {
    return (_jsx(Overlay, { alignPoints: BUBBLE_ALIGN_POINTS, isModal: true, positionType: "fixed", children: children }));
}
//# sourceMappingURL=EmbedInsightDialog.js.map