import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2024-2026 GoodData Corporation
import { useCallback, useMemo, useRef, useState } from "react";
import cx from "classnames";
import { useIntl } from "react-intl";
import { insightVisualizationType, widgetTitle, } from "@gooddata/sdk-model";
import { useDashboardSelector } from "../../../model/react/DashboardStoreProvider.js";
import { useDashboardScheduledEmails } from "../../../model/react/useDasboardScheduledEmails/useDashboardScheduledEmails.js";
import { useDashboardAlerts } from "../../../model/react/useDashboardAlerting/useDashboardAlerts.js";
import { selectSettings } from "../../../model/store/config/configSelectors.js";
import { selectInsightsMap } from "../../../model/store/insights/insightsSelectors.js";
import { useDashboardComponentsContext } from "../../dashboardContexts/DashboardComponentsContext.js";
import { DashboardItem } from "../../presentationComponents/DashboardItems/DashboardItem.js";
import { DashboardItemVisualization } from "../../presentationComponents/DashboardItems/DashboardItemVisualization.js";
import { getVisTypeCssClass } from "../../presentationComponents/DashboardItems/utils.js";
import { useInsightExport } from "../common/useInsightExport.js";
import { InsightWidgetDescriptionTrigger } from "../description/InsightWidgetDescriptionTrigger.js";
import { supportsShowAsTable } from "../insight/insightToTable.js";
import { ShowAsTableButton } from "../showAsTableButton/ShowAsTableButton.js";
import { useShowAsTable } from "../showAsTableButton/useShowAsTable.js";
import { useAlertingAndScheduling } from "../widget/InsightWidget/useAlertingAndScheduling.js";
import { useInsightMenu } from "../widget/InsightWidget/useInsightMenu.js";
import { VisualizationSwitcherNavigationHeader } from "../widget/VisualizationSwitcherWidget/VisualizationSwitcherNavigationHeader.js";
import { AllVisualizationsDashInsights } from "./AllVisualizationsDashInsights.js";
import { useExecutionProgress } from "./useExecutionProgress.js";
/**
 * @internal
 */
export function ViewModeDashboardVisualizationSwitcher({ widget, activeVisualizationId: initialActiveVisualizationId, onError, onExportReady, onLoadingChanged, screen, exportData, }) {
    const [activeVisualizationId, setActiveVisualizationId] = useState(initialActiveVisualizationId);
    const activeVisualization = widget.visualizations.find((visualization) => visualization.identifier === activeVisualizationId) ??
        widget.visualizations[0];
    const insights = useDashboardSelector(selectInsightsMap);
    const insight = activeVisualization ? insights.get(activeVisualization.insight) : undefined;
    if (!activeVisualization || !insight) {
        return _jsx(ViewModeDashboardVisualizationSwitcherEmpty, {});
    }
    else {
        return (_jsx(ViewModeDashboardVisualizationSwitcherContent, { widget: widget, insight: insight, activeVisualization: activeVisualization, screen: screen, onError: onError, onExportReady: onExportReady, onLoadingChanged: onLoadingChanged, onActiveVisualizationChange: setActiveVisualizationId, exportData: exportData }));
    }
}
export function ViewModeDashboardVisualizationSwitcherEmpty() {
    return _jsx("div", { className: "gd-visualization-switcher-widget-empty-content" });
}
export function ViewModeDashboardVisualizationSwitcherContent({ widget, insight, activeVisualization, onActiveVisualizationChange, screen, onError, onExportReady, onLoadingChanged, exportData, }) {
    const intl = useIntl();
    const visType = insightVisualizationType(insight);
    const settings = useDashboardSelector(selectSettings);
    const { showOthers } = useExecutionProgress();
    const { ref: widgetRef } = activeVisualization;
    const { exportCSVEnabled, exportXLSXEnabled, exportCSVRawEnabled, isExportRawVisible, isExportVisible, isExportPngImageVisible, isExportPdfTabularVisible, isExporting, onExportRawCSV, onExportCSV, onExportXLSX, onExportPdfPresentation, onExportPngImage, onExportPowerPointPresentation, onExportPdfTabular, exportPdfPresentationDisabled, exportPowerPointPresentationDisabled, exportPngImageDisabled, exportPdfTabularDisabled, disabledReason, } = useInsightExport({
        widgetRef,
        title: widgetTitle(activeVisualization) || intl.formatMessage({ id: "export.defaultTitle" }),
        insight,
        widget: activeVisualization,
    });
    const { onScheduleEmailingOpen, onScheduleEmailingManagementOpen, isScheduledEmailingVisible, isScheduledManagementEmailingVisible, } = useDashboardScheduledEmails();
    const { onAlertingManagementOpen: onAlertingManagementOpenAction } = useDashboardAlerts();
    const onScheduleExport = useCallback(() => {
        onScheduleEmailingOpen(activeVisualization);
    }, [onScheduleEmailingOpen, activeVisualization]);
    const onScheduleManagementExport = useCallback(() => {
        onScheduleEmailingManagementOpen(activeVisualization);
    }, [onScheduleEmailingManagementOpen, activeVisualization]);
    const onAlertingManagementOpen = useCallback(() => {
        onAlertingManagementOpenAction(activeVisualization);
    }, [onAlertingManagementOpenAction, activeVisualization]);
    const { isAlertingVisible, alertingDisabled, alertingDisabledReason, scheduleExportDisabled, scheduleExportManagementDisabled, scheduleExportDisabledReason, } = useAlertingAndScheduling({
        widget: activeVisualization,
        insight,
    });
    const { closeMenu, isMenuOpen, menuItems, openMenu } = useInsightMenu({
        insight,
        widget: activeVisualization,
        exportCSVEnabled,
        exportXLSXEnabled,
        exportCSVRawEnabled,
        isExportRawVisible,
        isExportVisible,
        isExportPngImageVisible,
        isExportPdfTabularVisible,
        isExporting,
        onExportCSV,
        onExportXLSX,
        onExportRawCSV,
        onScheduleExport,
        onScheduleManagementExport,
        onAlertingManagementOpen,
        onExportPdfPresentation,
        onExportPowerPointPresentation,
        onExportPngImage,
        onExportPdfTabular,
        isScheduleExportVisible: isScheduledEmailingVisible,
        isScheduleExportManagementVisible: isScheduledManagementEmailingVisible,
        isAlertingVisible,
        alertingDisabled,
        alertingDisabledReason,
        scheduleExportDisabled,
        scheduleExportManagementDisabled,
        scheduleExportDisabledReason,
        exportPdfPresentationDisabled,
        exportPowerPointPresentationDisabled,
        exportPngImageDisabled,
        exportPdfTabularDisabled,
        disabledReason,
    });
    const toggleMenu = useCallback(() => {
        if (isMenuOpen) {
            closeMenu();
        }
        else {
            openMenu();
        }
    }, [isMenuOpen, closeMenu, openMenu]);
    const { InsightMenuButtonComponentProvider, InsightMenuComponentProvider } = useDashboardComponentsContext();
    const InsightMenuButtonComponent = useMemo(() => InsightMenuButtonComponentProvider(insight, activeVisualization), [InsightMenuButtonComponentProvider, insight, activeVisualization]);
    const InsightMenuComponent = useMemo(() => InsightMenuComponentProvider(insight, activeVisualization), [InsightMenuComponentProvider, insight, activeVisualization]);
    const accessibilityWidgetDescription = settings.enableDescriptions
        ? activeVisualization.configuration?.description?.source === "widget" || !insight
            ? activeVisualization.description
            : insight.insight.summary
        : "";
    const { isWidgetAsTable, toggleWidgetAsTable } = useShowAsTable(activeVisualization);
    const widgetContainerRef = useRef(null);
    return (_jsx(DashboardItem, { as: "div", className: cx("type-visualization", "gd-dashboard-view-widget", getVisTypeCssClass(activeVisualization.type, visType)), screen: screen, ref: widgetContainerRef, description: accessibilityWidgetDescription, children: _jsx(DashboardItemVisualization, { isExport: !!exportData, renderHeadline: (clientHeight, clientWidth) => (_jsx(VisualizationSwitcherNavigationHeader, { widget: widget, clientHeight: clientHeight, clientWidth: clientWidth, activeVisualization: activeVisualization, onActiveVisualizationChange: (activeVisualizationId) => onActiveVisualizationChange(activeVisualizationId), exportData: exportData?.title })), renderBeforeVisualization: () => (_jsxs("div", { className: "gd-absolute-row", children: [settings?.enableDescriptions ? (_jsx(InsightWidgetDescriptionTrigger, { insight: insight, widget: activeVisualization, screen: screen })) : null, (() => {
                        const visType = insightVisualizationType(insight);
                        if (supportsShowAsTable(visType)) {
                            return (_jsx(ShowAsTableButton, { widget: activeVisualization, isWidgetAsTable: isWidgetAsTable, onClick: toggleWidgetAsTable }));
                        }
                        return null;
                    })(), _jsx(InsightMenuButtonComponent, { insight: insight, widget: activeVisualization, isOpen: isMenuOpen, onClick: toggleMenu, items: menuItems })
                ] })), renderAfterContent: () => {
                if (!isMenuOpen) {
                    return null;
                }
                return (_jsx(InsightMenuComponent, { insight: insight, widget: activeVisualization, isOpen: isMenuOpen, onClose: closeMenu, items: menuItems }));
            }, children: ({ clientHeight, clientWidth }) => (_jsx(AllVisualizationsDashInsights, { visualizations: widget.visualizations, activeVisualization: activeVisualization, showOthers: showOthers, onExportReady: onExportReady, onError: onError, onLoadingChanged: onLoadingChanged, clientHeight: clientHeight, clientWidth: clientWidth, exportData: exportData?.widget })) }) }));
}
//# sourceMappingURL=ViewModeDashboardVisualizationSwitcher.js.map