// (C) 2021-2026 GoodData Corporation
import { useCallback, useMemo, useState } from "react";
import { useIntl } from "react-intl";
import { isDataError } from "../../../../_staging/errors/errorPredicates.js";
import { useDashboardSelector } from "../../../../model/react/DashboardStoreProvider.js";
import { selectDashboardUserAutomationAlertsInContext } from "../../../../model/store/automations/automationsSelectors.js";
import { selectEnableAutomationManagement } from "../../../../model/store/config/configSelectors.js";
import { selectExecutionResultByRef } from "../../../../model/store/executionResults/executionResultsSelectors.js";
import { selectCanCreateAutomation } from "../../../../model/store/permissions/permissionsSelectors.js";
import { useDashboardCustomizationsContext } from "../../../dashboardContexts/DashboardCustomizationsContext.js";
import { getDefaultInsightMenuItems } from "../../insightMenu/DefaultDashboardInsightMenu/getDefaultInsightMenuItems.js";
export const useInsightMenu = (config) => {
    const { insight, widget } = config;
    const [isMenuOpen, setIsMenuOpen] = useState(false);
    const closeMenu = useCallback(() => setIsMenuOpen(false), []);
    const openMenu = useCallback(() => setIsMenuOpen(true), []);
    const { insightMenuItemsProvider } = useDashboardCustomizationsContext();
    const defaultMenuItems = useDefaultMenuItems(config, setIsMenuOpen);
    const menuItems = useMemo(() => {
        return insightMenuItemsProvider && insight
            ? insightMenuItemsProvider(insight, widget, defaultMenuItems, closeMenu, "view")
            : defaultMenuItems;
    }, [insightMenuItemsProvider, insight, widget, defaultMenuItems, closeMenu]);
    return { menuItems, isMenuOpen, openMenu, closeMenu };
};
function useDefaultMenuItems(config, setIsMenuOpen) {
    const { exportCSVEnabled, exportXLSXEnabled, exportCSVRawEnabled, isExporting, onExportCSV, onExportRawCSV, onExportXLSX, onScheduleExport, onScheduleManagementExport, onAlertingManagementOpen, onExportPdfPresentation, onExportPowerPointPresentation, onExportPngImage, onExportPdfTabular, isScheduleExportVisible, isScheduleExportManagementVisible, isAlertingVisible, alertingDisabled, scheduleExportDisabled, scheduleExportManagementDisabled, scheduleExportDisabledReason, alertingDisabledReason, widget, isExportRawVisible, isExportVisible, isExportPngImageVisible, isExportPdfTabularVisible, exportPdfPresentationDisabled, exportPowerPointPresentationDisabled, exportPngImageDisabled, exportPdfTabularDisabled, disabledReason, } = config;
    const intl = useIntl();
    const execution = useDashboardSelector(selectExecutionResultByRef(widget.ref));
    const canCreateAutomation = useDashboardSelector(selectCanCreateAutomation);
    const isAutomationManagementEnabled = useDashboardSelector(selectEnableAutomationManagement);
    const automations = useDashboardSelector(selectDashboardUserAutomationAlertsInContext(widget.localIdentifier));
    const isAlertManagementVisible = isAlertingVisible && (canCreateAutomation || automations.length > 0);
    return useMemo(() => {
        return getDefaultInsightMenuItems(intl, {
            exportCSVDisabled: !exportCSVEnabled,
            exportXLSXDisabled: !exportXLSXEnabled,
            exportCSVRawDisabled: !exportCSVRawEnabled,
            isExporting,
            scheduleExportManagementDisabled,
            scheduleExportDisabled,
            scheduleExportDisabledReason,
            isExportRawVisible,
            isExportVisible,
            isExportPngImageVisible,
            isExportPdfTabularVisible,
            isAutomationManagementEnabled,
            onExportCSV: () => {
                setIsMenuOpen(false);
                onExportCSV();
            },
            onExportRawCSV: () => {
                setIsMenuOpen(false);
                onExportRawCSV();
            },
            onExportXLSX: () => {
                setIsMenuOpen(false);
                onExportXLSX();
            },
            onScheduleExport: () => {
                setIsMenuOpen(false);
                onScheduleExport();
            },
            onScheduleManagementExport: () => {
                setIsMenuOpen(false);
                onScheduleManagementExport();
            },
            onAlertingManagementOpen: () => {
                setIsMenuOpen(false);
                onAlertingManagementOpen();
            },
            onExportPdfPresentation: () => {
                setIsMenuOpen(false);
                onExportPdfPresentation();
            },
            onExportPowerPointPresentation: () => {
                setIsMenuOpen(false);
                onExportPowerPointPresentation();
            },
            onExportPngImage: () => {
                setIsMenuOpen(false);
                onExportPngImage();
            },
            onExportPdfTabular: () => {
                setIsMenuOpen(false);
                onExportPdfTabular();
            },
            isScheduleExportVisible,
            isScheduleExportManagementVisible,
            isDataError: isDataError(execution?.error),
            isAlertingVisible,
            isAlertManagementVisible,
            alertingDisabled,
            alertingDisabledReason,
            canCreateAutomation,
            exportPdfPresentationDisabled,
            exportPowerPointPresentationDisabled,
            exportPngImageDisabled,
            exportPdfTabularDisabled,
            disabledReason,
        }, execution);
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [
        intl,
        exportCSVEnabled,
        exportXLSXEnabled,
        exportCSVRawEnabled,
        scheduleExportManagementDisabled,
        scheduleExportDisabled,
        isScheduleExportVisible,
        isScheduleExportManagementVisible,
        isAlertingVisible,
        isAlertManagementVisible,
        alertingDisabled,
        isExportVisible,
        setIsMenuOpen,
        onExportCSV,
        onExportRawCSV,
        onExportXLSX,
        onScheduleExport,
        onScheduleManagementExport,
        onExportPdfPresentation,
        onExportPowerPointPresentation,
        onExportPngImage,
        onExportPdfTabular,
        scheduleExportDisabledReason,
        alertingDisabledReason,
        canCreateAutomation,
        isExportRawVisible,
        isExportPngImageVisible,
        isExportPdfTabularVisible,
        isExporting,
        exportPdfPresentationDisabled,
        exportPowerPointPresentationDisabled,
        exportPdfTabularDisabled,
        execution,
        exportPngImageDisabled,
        disabledReason,
    ]);
}
