// (C) 2022-2026 GoodData Corporation
import { useCallback } from "react";
import { useToastMessage } from "@gooddata/sdk-ui-kit";
import { messages } from "../../../locales.js";
import { selectEnableAutomationManagement } from "../../store/config/configSelectors.js";
import { selectDashboardRef } from "../../store/meta/metaSelectors.js";
import { selectIsScheduleEmailManagementDialogContext } from "../../store/ui/uiSelectors.js";
import { useDashboardSelector } from "../DashboardStoreProvider.js";
import { useDashboardAutomations } from "../useDashboardAutomations/useDashboardAutomations.js";
import { useDashboardScheduledEmailsCommands } from "./useDashboardScheduledEmailsCommands.js";
/**
 * @internal
 */
export const useDashboardScheduledEmailsManagementDialog = ({ setShouldReturnToManagementDialog, }) => {
    const { addSuccess, addError } = useToastMessage();
    const dashboardRef = useDashboardSelector(selectDashboardRef);
    const enableAutomationManagement = useDashboardSelector(selectEnableAutomationManagement);
    const managementDialogContext = useDashboardSelector(selectIsScheduleEmailManagementDialogContext);
    const { closeScheduleEmailingDialog, closeScheduleEmailingManagementDialog, openScheduleEmailingDialog, openScheduleEmailingManagementDialog, } = useDashboardScheduledEmailsCommands();
    const { refreshAutomations, refreshAutomationManagementItems } = useDashboardAutomations();
    const handleRefreshAutomations = useCallback(() => {
        if (enableAutomationManagement) {
            refreshAutomationManagementItems();
        }
        else {
            refreshAutomations();
        }
    }, [enableAutomationManagement, refreshAutomations, refreshAutomationManagementItems]);
    /*
     * exports and scheduling are not available when rendering a dashboard that is not persisted.
     * this can happen when a new dashboard is created and is being edited.
     *
     * the setup of menu items available in the menu needs to reflect this.
     */
    const defaultOnScheduleEmailingManagement = useCallback((widget) => {
        if (!dashboardRef) {
            return;
        }
        openScheduleEmailingManagementDialog(widget, "dashboard");
    }, [dashboardRef, openScheduleEmailingManagementDialog]);
    // Open / Close
    const onScheduleEmailingManagementOpen = useCallback((widget) => {
        openScheduleEmailingManagementDialog(widget, "widget");
    }, [openScheduleEmailingManagementDialog]);
    const onScheduleEmailingManagementClose = useCallback(() => {
        closeScheduleEmailingManagementDialog();
        setShouldReturnToManagementDialog(false);
    }, [closeScheduleEmailingManagementDialog, setShouldReturnToManagementDialog]);
    // Loading
    const onScheduleEmailingManagementLoadingError = useCallback(() => {
        closeScheduleEmailingManagementDialog();
        addError(messages.scheduleManagementLoadError);
    }, [closeScheduleEmailingManagementDialog, addError]);
    // Create
    const onScheduleEmailingManagementAdd = useCallback((targetWidget) => {
        const contextWidget = managementDialogContext.widgetRef
            ? { ref: managementDialogContext.widgetRef }
            : undefined;
        const widget = contextWidget ?? targetWidget;
        if (enableAutomationManagement) {
            openScheduleEmailingDialog({ widget });
        }
        else {
            setShouldReturnToManagementDialog(true);
            closeScheduleEmailingManagementDialog();
            openScheduleEmailingDialog({ widget });
        }
    }, [
        enableAutomationManagement,
        managementDialogContext,
        setShouldReturnToManagementDialog,
        closeScheduleEmailingManagementDialog,
        openScheduleEmailingDialog,
    ]);
    // Edit
    const onScheduleEmailingManagementEdit = useCallback((schedule, widget) => {
        if (enableAutomationManagement) {
            openScheduleEmailingDialog({ widget, schedule });
        }
        else {
            setShouldReturnToManagementDialog(true);
            closeScheduleEmailingManagementDialog();
            openScheduleEmailingDialog({ widget, schedule });
        }
    }, [
        enableAutomationManagement,
        closeScheduleEmailingManagementDialog,
        openScheduleEmailingDialog,
        setShouldReturnToManagementDialog,
    ]);
    // Delete
    const onScheduleEmailingManagementDeleteSuccess = useCallback(() => {
        closeScheduleEmailingDialog();
        addSuccess(messages.scheduleEmailDeleteSuccess);
        handleRefreshAutomations();
    }, [addSuccess, closeScheduleEmailingDialog, handleRefreshAutomations]);
    const onScheduleEmailingManagementDeleteError = useCallback(() => {
        closeScheduleEmailingDialog();
        closeScheduleEmailingManagementDialog();
        addError(messages.scheduleManagementDeleteError);
    }, [closeScheduleEmailingDialog, closeScheduleEmailingManagementDialog, addError]);
    return {
        defaultOnScheduleEmailingManagement,
        onScheduleEmailingManagementOpen,
        onScheduleEmailingManagementClose,
        onScheduleEmailingManagementLoadingError,
        onScheduleEmailingManagementAdd,
        onScheduleEmailingManagementEdit,
        onScheduleEmailingManagementDeleteSuccess,
        onScheduleEmailingManagementDeleteError,
    };
};
