import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2019-2026 GoodData Corporation
import { useCallback, useState } from "react";
import cx from "classnames";
import { FormattedMessage, defineMessages, useIntl } from "react-intl";
import { Bubble, BubbleHoverTrigger, Button, DialogBase, ShortenedText, Typography, UiAutofocus, UiButton, useId, } from "@gooddata/sdk-ui-kit";
import { useDashboardSelector } from "../../../../../model/react/DashboardStoreProvider.js";
import { selectEnableExportToPdfTabular, selectSettings, } from "../../../../../model/store/config/configSelectors.js";
import { selectCanExportTabular } from "../../../../../model/store/permissions/permissionsSelectors.js";
import { DrillDialogExportDropdownResolver } from "./DrillDialogExportDropdownResolver.js";
import { getTitleWithBreadcrumbs } from "./getTitleWithBreadcrumbs.js";
const tooltipAlignPoints = [{ align: "cc tc", offset: { x: -20, y: 10 } }];
const bubbleAlignPoints = [{ align: "bc tc", offset: { x: -5, y: -5 } }];
const messages = defineMessages({
    showAsTable: {
        id: "controlButtons.asTable",
    },
    showAsOriginal: {
        id: "controlButtons.asOriginal",
    },
});
export function DrillDialog({ insight, widget, insightTitle, breadcrumbs, onCloseDialog, onBackButtonClick, isBackButtonVisible, children, enableDrillDescription, exportAvailable, exportXLSXEnabled, exportCSVEnabled, exportCSVRawEnabled, exportPDFEnabled, exportPDFVisible, onExportCSV, onExportXLSX, onExportCSVRaw, onExportPDF, isLoading, isExporting, isExportRawVisible, accessibilityConfig = {}, initialFocus, isShowAsTableVisible, isWidgetAsTable, onShowAsTable, focusCheckFn, }) {
    const [announcementText, setAnnouncementText] = useState("");
    const canExport = useDashboardSelector(selectCanExportTabular);
    const enablePdfTabularExport = useDashboardSelector(selectEnableExportToPdfTabular);
    const settings = useDashboardSelector(selectSettings);
    const isAccessibilityModeEnabled = settings.enableAccessibilityMode === true;
    const titleWithBreadcrumbs = getTitleWithBreadcrumbs(insightTitle, breadcrumbs);
    const titleElementId = useId();
    const { formatMessage } = useIntl();
    const showAsTableButtonMessage = isWidgetAsTable ? messages.showAsOriginal : messages.showAsTable;
    const handleShowAsTableClick = useCallback(() => {
        // Announce what state we're changing TO (opposite of current state)
        const message = isWidgetAsTable
            ? formatMessage({ id: "controlButtons.announcement.switchedToOriginal" })
            : formatMessage({ id: "controlButtons.announcement.switchedToTable" });
        setAnnouncementText(message);
        onShowAsTable();
    }, [isWidgetAsTable, formatMessage, onShowAsTable]);
    return (_jsx(UiAutofocus, { refocusKey: titleWithBreadcrumbs, children: _jsxs(DialogBase, { submitOnEnterKey: false, className: cx("gd-drill-modal-dialog s-drill-modal-dialog", {
                "gd-ff-drill-description": enableDrillDescription,
            }), onClose: onCloseDialog, displayCloseButton: true, accessibilityConfig: { ...accessibilityConfig, isModal: true, titleElementId }, initialFocus: initialFocus, returnFocusAfterClose: true, shouldCloseOnEscape: true, autofocusOnOpen: true, focusCheckFn: focusCheckFn, children: [
                _jsxs("div", { className: cx("gd-dialog-header gd-dialog-header-with-border gd-drill-modal-dialog-header", {
                        "gd-ff-drill-description": enableDrillDescription,
                    }), children: [isBackButtonVisible ? (_jsxs(BubbleHoverTrigger, { children: [
                                _jsx(Button, { className: "gd-button-primary gd-button-icon-only gd-icon-navigateleft s-drill-reset-button gd-drill-reset-button", onClick: onBackButtonClick, accessibilityConfig: {
                                        ariaLabel: formatMessage({ id: "drillModal.backToTop" }),
                                    } }), _jsx(Bubble, { className: "bubble-primary", alignPoints: bubbleAlignPoints, children: _jsx(FormattedMessage, { id: "drillModal.backToTop", tagName: "span" }) })
                            ] })) : null, _jsx(Typography, { tagName: "h3", className: "gd-drill-title s-drill-title", id: titleElementId, children: _jsx(ShortenedText, { tooltipAlignPoints: tooltipAlignPoints, tooltipVisibleOnMouseOver: false, children: titleWithBreadcrumbs }) })
                    ] }), _jsx("div", { className: cx("gd-drill-modal-dialog-content visualization", {
                        "gd-ff-drill-description": enableDrillDescription,
                    }), children: _jsx("div", { className: "gd-drill-modal-dialog-content-base", children: children }) }), canExport || isShowAsTableVisible ? (_jsxs("div", { className: "gd-drill-modal-dialog-footer gd-drill-modal-dialog-footer-with-border s-drill-modal-dialog-footer", children: [isShowAsTableVisible ? (_jsx(UiButton, { variant: "tertiary", onClick: handleShowAsTableClick, iconBefore: isWidgetAsTable ? "visualization" : "table", label: formatMessage(showAsTableButtonMessage) })) : null, canExport ? (_jsx(DrillDialogExportDropdownResolver, { insight: insight, widget: widget, exportAvailable: exportAvailable, exportXLSXEnabled: exportXLSXEnabled, exportCSVEnabled: exportCSVEnabled, exportCSVRawEnabled: exportCSVRawEnabled, exportPDFEnabled: exportPDFEnabled, exportPDFVisible: exportPDFVisible ||
                                (isWidgetAsTable && enablePdfTabularExport && !isAccessibilityModeEnabled), onExportXLSX: onExportXLSX, onExportCSV: onExportCSV, onExportCSVRaw: onExportCSVRaw, onExportPDF: onExportPDF, isLoading: isLoading, isExporting: isExporting, isExportRawVisible: isExportRawVisible })) : null] })) : null, _jsx("div", { className: "sr-only", "aria-live": "polite", "aria-atomic": "true", role: "status", children: announcementText })
            ] }) }));
}
//# sourceMappingURL=DrillDialog.js.map