import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2021-2026 GoodData Corporation
import { useCallback } from "react";
import cx from "classnames";
import { FormattedMessage, useIntl } from "react-intl";
import { Bubble, BubbleHoverTrigger, Dropdown, ShortenedText, UiButton, UiIcon, UiMenu, UiTooltip, } from "@gooddata/sdk-ui-kit";
import { useDrillDialogExportItems, } from "./useDrillDialogExportItems.js";
function InteractiveItemWithIcon({ item, isFocused }) {
    return (_jsxs("div", { className: cx("gd-ui-kit-menu__item", "gd-ui-kit-menu__item--size-medium", {
            "gd-ui-kit-menu__item--isFocused": isFocused,
            "gd-ui-kit-menu__item--isSelected": !!item.isSelected,
            "gd-ui-kit-menu__item--isDisabled": !!item.isDisabled,
        }), children: [
            _jsx(UiIcon, { type: item.data.icon }), _jsx(ShortenedText, { className: "gd-ui-kit-menu-item-title", ellipsisPosition: "end", children: item.stringTitle })
        ] }));
}
const dropdownAlignPoints = [
    {
        align: "tc bc",
        offset: {
            x: -10,
            y: -5,
        },
    },
];
const dropdownDisabledBubbleAlignPoints = [{ align: "tc bc" }];
export function DrillDialogExportDropdown({ exportAvailable, exportXLSXEnabled, onExportXLSX, exportCSVEnabled, exportCSVRawEnabled, exportPDFEnabled, exportPDFVisible, onExportCSV, onExportCSVRaw, onExportPDF, isExporting, isExportRawVisible, }) {
    const hasXLSXExport = exportXLSXEnabled;
    const hasCSVExport = exportCSVEnabled;
    const hasPDFExport = exportPDFEnabled && exportPDFVisible;
    const hasAnyExportFormat = hasXLSXExport || hasCSVExport || hasPDFExport;
    const isExportDisabled = !exportAvailable || !hasAnyExportFormat;
    const isDropdownDisabled = isExportDisabled && !isExportRawVisible;
    const classNames = cx("s-export-drilled-insight export-drilled-insight", {
        "is-disabled": isDropdownDisabled,
    });
    const items = useDrillDialogExportItems({
        isExporting,
        isDropdownDisabled,
        isExportRawVisible,
        isExportXLSXEnabled: exportXLSXEnabled,
        isExportCSVEnabled: exportCSVEnabled,
        isExportCSVRawEnabled: exportCSVRawEnabled,
        isExportPDFEnabled: exportPDFEnabled,
        isExportPDFVisible: exportPDFVisible,
        onExportXLSX,
        onExportCSV,
        onExportCSVRaw,
        onExportPDF,
    });
    const handleSelectItem = useCallback((item) => {
        item.data.action();
    }, []);
    const itemDataTestId = useCallback((item) => (item.type === "interactive" ? item.data.dataTestId : undefined), []);
    return (_jsx("div", { className: classNames, children: _jsx(Dropdown, { autofocusOnOpen: true, alignPoints: dropdownAlignPoints, renderBody: ({ closeDropdown, ariaAttributes }) => (_jsx("div", { className: "gd-drill-dialog-export", children: _jsx(UiMenu, { onClose: closeDropdown, ariaAttributes: ariaAttributes, onSelect: handleSelectItem, items: items, dataTestId: "s-drill-modal-export-options", itemDataTestId: itemDataTestId, InteractiveItem: DrillModalExportMenuItem }) })), renderButton: (buttonRenderProps) => {
                return isDropdownDisabled ? (_jsx("div", { className: classNames, children: _jsxs(BubbleHoverTrigger, { children: [
                            _jsx(DropdownTriggerButton, { ...buttonRenderProps, isDisabled: true }), _jsx(Bubble, { className: "bubble-primary", alignPoints: dropdownDisabledBubbleAlignPoints, children: _jsx(FormattedMessage, { id: "export_unsupported.disabled" }) })
                        ] }) })) : (_jsx(DropdownTriggerButton, { ...buttonRenderProps }));
            } }) }));
}
function DrillModalExportMenuItem(props) {
    const { item, isFocused } = props;
    const tooltip = item.data.disabledTooltip;
    return item.isDisabled && tooltip !== undefined ? (_jsxs(_Fragment, { children: [
            _jsx(UiTooltip, { triggerBy: isFocused ? [] : ["hover", "focus"], arrowPlacement: "right", optimalPlacement: true, content: tooltip, anchor: _jsx(InteractiveItemWithIcon, { ...props }) }), _jsx("div", { className: "sr-only", "aria-live": "polite", children: tooltip })
        ] })) : (_jsx(InteractiveItemWithIcon, { ...props }));
}
function DropdownTriggerButton({ toggleDropdown, buttonRef, accessibilityConfig, isDisabled, }) {
    const { formatMessage } = useIntl();
    return (_jsx(UiButton, { ref: (ref) => {
            buttonRef.current = ref;
        }, onClick: toggleDropdown, iconBefore: "download", label: formatMessage({ id: "dialogs.export.submit" }), variant: "tertiary", isDisabled: isDisabled, accessibilityConfig: {
            ...accessibilityConfig,
            ariaHaspopup: "menu",
        } }));
}
//# sourceMappingURL=DrillDialogExportDropdown.js.map