import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2022-2026 GoodData Corporation
import { useCallback, useState } from "react";
import { buildAutomationUrl, navigate, useWorkspace } from "@gooddata/sdk-ui";
import { useDashboardSelector } from "../../../model/react/DashboardStoreProvider.js";
import { selectEnableAutomationManagement, selectExternalRecipient, selectIsEmbedded, } from "../../../model/store/config/configSelectors.js";
import { selectDashboardId } from "../../../model/store/meta/metaSelectors.js";
import { DeleteAlertConfirmDialog } from "./components/DeleteAlertConfirmDialog.js";
import { PauseAlertRunner } from "./components/PauseAlertRunner.js";
import { DefaultAlertingManagementDialogContentBasic } from "./DefaultAlertingManagementDialogContentBasic.js";
import { DefaultAlertingManagementDialogContentEnhanced } from "./DefaultAlertingManagementDialogContentEnhanced.js";
/**
 * @alpha
 */
export function DefaultAlertingManagementDialogNew({ onPauseSuccess, onPauseError, onEdit, onAdd, onDeleteSuccess, onDeleteError, onClose, isLoadingAlertingData, automations, }) {
    const [alertToDelete, setAlertToDelete] = useState(null);
    const [alertToPause, setAlertToPause] = useState(null);
    const workspace = useWorkspace();
    const enableAutomationManagement = useDashboardSelector(selectEnableAutomationManagement);
    const dashboardId = useDashboardSelector(selectDashboardId);
    const isEmbedded = useDashboardSelector(selectIsEmbedded);
    const externalRecipientOverride = useDashboardSelector(selectExternalRecipient);
    const handleAlertDeleteOpen = useCallback((alert) => {
        setAlertToDelete(alert);
    }, []);
    const handleAlertDeleteClose = useCallback(() => {
        setAlertToDelete(null);
    }, []);
    const handleAlertEdit = useCallback((alert) => {
        if (enableAutomationManagement && alert.dashboard?.id !== dashboardId) {
            navigate(buildAutomationUrl({
                workspaceId: workspace,
                dashboardId: alert.dashboard?.id,
                automationId: alert.id,
                isEmbedded,
                queryParams: externalRecipientOverride
                    ? { recipient: externalRecipientOverride }
                    : undefined,
            }));
            return;
        }
        onEdit?.(alert);
    }, [onEdit, enableAutomationManagement, dashboardId, workspace, isEmbedded, externalRecipientOverride]);
    const handleAlertPause = useCallback((alert, pause) => {
        setAlertToPause([alert, pause]);
    }, []);
    const handleAlertDeleteSuccess = (alert) => {
        onDeleteSuccess?.(alert);
        handleAlertDeleteClose();
    };
    const handleAlertPauseSuccess = (alert, pause) => {
        onPauseSuccess?.(alert, pause);
        setAlertToPause(null);
    };
    const handleAlertPauseError = (err, pause) => {
        onPauseError?.(err, pause);
        setAlertToPause(null);
    };
    return (_jsxs(_Fragment, { children: [enableAutomationManagement ? (_jsx(DefaultAlertingManagementDialogContentEnhanced, { onAdd: onAdd, onClose: onClose, onEdit: handleAlertEdit })) : (_jsx(DefaultAlertingManagementDialogContentBasic, { onClose: onClose, onDelete: handleAlertDeleteOpen, onEdit: handleAlertEdit, onPause: handleAlertPause, isLoadingAlertingData: isLoadingAlertingData, automations: automations })), alertToDelete ? (_jsx(DeleteAlertConfirmDialog, { alert: alertToDelete, onCancel: handleAlertDeleteClose, onSuccess: handleAlertDeleteSuccess, onError: onDeleteError })) : null, alertToPause ? (_jsx(PauseAlertRunner, { alert: alertToPause[0], pause: alertToPause[1], onSuccess: handleAlertPauseSuccess, onError: handleAlertPauseError })) : null] }));
}
//# sourceMappingURL=DefaultAlertingManagementDialogNew.js.map