// (C) 2022-2026 GoodData Corporation
import { useEffect } from "react";
import { convertError } from "@gooddata/sdk-ui";
import { useAlertingManagementDialogContext } from "../../../contexts/AlertingManagementDialogContext.js";
export function PauseAlertRunner({ alert, pause, onSuccess, onError }) {
    const { pauseAlert, resumeAlert } = useAlertingManagementDialogContext();
    useEffect(() => {
        if (!alert) {
            return;
        }
        if (pause) {
            const alertToPause = {
                ...alert,
                alert: { ...alert.alert, trigger: { ...alert.alert?.trigger, state: "PAUSED" } },
            };
            pauseAlert(alertToPause)
                .then((updatedAlert) => onSuccess?.(updatedAlert, true))
                .catch((err) => onError?.(convertError(err), true));
        }
        else {
            const alertToResume = {
                ...alert,
                alert: { ...alert.alert, trigger: { ...alert.alert?.trigger, state: "ACTIVE" } },
            };
            resumeAlert(alertToResume)
                .then((updatedAlert) => onSuccess?.(updatedAlert, false))
                .catch((err) => onError?.(convertError(err), false));
        }
    }, [alert, onError, onSuccess, pause, pauseAlert, resumeAlert]);
    return null;
}
