// (C) 2019-2026 GoodData Corporation
import { useCallback } from "react";
import { omit } from "lodash-es";
import { isAllValuesAttributeFilter, isNoopAllTimeDateFilter, } from "@gooddata/sdk-model";
import { useDashboardSelector } from "../../../../model/react/DashboardStoreProvider.js";
import { selectEnableAutomationFilterContext } from "../../../../model/store/config/configSelectors.js";
import { useCreateAlert } from "./useCreateAlert.js";
import { useUpdateAlert } from "./useUpdateAlert.js";
/**
 * @internal
 * @param onCreateSuccess - callback to be called when alert is created successfully
 * @param onCreateError - callback to be called when alert creation fails
 * @param onUpdateSuccess - callback to be called when alert is updated successfully
 * @param onUpdateError - callback to be called when alert update fails
 * @param onPauseSuccess - callback to be called when alert is paused successfully
 * @param onPauseError - callback to be called when alert pausing fails
 * @param onResumeSuccess - callback to be called when alert is resumed successfully
 * @param onResumeError - callback to be called when alert resuming fails
 */
export function useSaveAlertToBackend({ onCreateSuccess, onCreateError, onUpdateSuccess, onUpdateError, onPauseSuccess, onPauseError, onResumeSuccess, onResumeError, }) {
    const enableAutomationFilterContext = useDashboardSelector(selectEnableAutomationFilterContext);
    const alertCreator = useCreateAlert({
        onSuccess: (alert) => onCreateSuccess?.(alert),
        onError: onCreateError,
    });
    const handleCreateAlert = useCallback((alert) => {
        const sanitizedAlert = sanitizeAutomation(alert, enableAutomationFilterContext);
        alertCreator.create(sanitizedAlert);
    }, [alertCreator, enableAutomationFilterContext]);
    const alertUpdater = useUpdateAlert({
        onSuccess: onUpdateSuccess,
        onError: onUpdateError,
    });
    const handleUpdateAlert = useCallback((alert) => {
        const sanitizedAlert = sanitizeAutomation(alert, enableAutomationFilterContext);
        alertUpdater.save(sanitizedAlert);
    }, [alertUpdater, enableAutomationFilterContext]);
    const alertPauser = useUpdateAlert({
        onSuccess: onPauseSuccess,
        onError: onPauseError,
    });
    const handlePauseAlert = useCallback((alert) => {
        alertPauser.save(sanitizeAutomation(alert, enableAutomationFilterContext));
    }, [alertPauser, enableAutomationFilterContext]);
    const alertResumer = useUpdateAlert({
        onSuccess: onResumeSuccess,
        onError: onResumeError,
    });
    const handleResumeAlert = useCallback((alert) => {
        alertResumer.save(sanitizeAutomation(alert, enableAutomationFilterContext));
    }, [alertResumer, enableAutomationFilterContext]);
    const isSavingAlert = alertCreator.creationStatus === "running" ||
        alertUpdater.savingStatus === "running" ||
        alertPauser.savingStatus === "running" ||
        alertResumer.savingStatus === "running";
    return { handleCreateAlert, handleUpdateAlert, handlePauseAlert, handleResumeAlert, isSavingAlert };
}
function sanitizeAutomation(automationToSave, enableAutomationFilterContext) {
    let automation = {
        ...automationToSave,
    };
    // We want to omit the cronDescription as it is a variable created on backend that cannot
    // be overriden and BE has hard time handling it with each PUT
    if (automation.schedule) {
        automation.schedule = omit(automation.schedule, ["cronDescription"]);
    }
    if (automation.alert?.execution?.filters) {
        automation = {
            ...automation,
            alert: {
                ...automation.alert,
                execution: {
                    ...automation.alert.execution,
                    filters: removeNoopFiltersFromAlertFilters(automation.alert.execution.filters, enableAutomationFilterContext),
                },
            },
        };
    }
    return automation;
}
/**
 * Strip noop filters that have no effect on execution and should not appear in alert email notifications.
 * - "All values" attribute filters are always stripped.
 * - "All time" date filters are stripped only for the legacy path (new path handles this in getAppliedWidgetFilters).
 */
function removeNoopFiltersFromAlertFilters(filters, enableAutomationFilterContext) {
    return filters.filter((filter) => {
        if (isAllValuesAttributeFilter(filter)) {
            return false;
        }
        if (!enableAutomationFilterContext && isNoopAllTimeDateFilter(filter)) {
            return false;
        }
        return true;
    });
}
//# sourceMappingURL=useSaveAlertToBackend.js.map