import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { FormattedMessage, useIntl } from "react-intl";
import { convertError, useBackendStrict, useWorkspaceStrict } from "@gooddata/sdk-ui";
import { ConfirmDialog } from "@gooddata/sdk-ui-kit";
import { useAlertingManagementDialogContext } from "../../../contexts/AlertingManagementDialogContext.js";
export function DeleteAlertConfirmDialog({ alert, onCancel, onSuccess, onError, }) {
    const effectiveBackend = useBackendStrict();
    const effectiveWorkspace = useWorkspaceStrict();
    const intl = useIntl();
    const { canManageWorkspace: canManageAutomations, currentUser } = useAlertingManagementDialogContext();
    const handleDeleteAlert = async () => {
        try {
            const alertCreatorId = alert.createdBy?.login;
            const currentUserId = currentUser?.login;
            const isAlertCreatedByCurrentUser = !!alertCreatorId && !!currentUserId && alertCreatorId === currentUserId;
            const automationService = effectiveBackend.workspace(effectiveWorkspace).automations();
            // If alert is created by current user, or user has permissions to manage automations, delete it, otherwise unsubscribe
            const deleteMethod = canManageAutomations || isAlertCreatedByCurrentUser
                ? automationService.deleteAutomation.bind(automationService)
                : automationService.unsubscribeAutomation.bind(automationService);
            await deleteMethod(alert.id);
            onSuccess?.(alert);
        }
        catch (err) {
            onError?.(convertError(err));
        }
    };
    return (_jsx(ConfirmDialog, { displayCloseButton: true, isPositive: false, headline: intl.formatMessage({ id: "dialogs.alerting.management.delete.dialog" }), cancelButtonText: intl.formatMessage({ id: "cancel" }), submitButtonText: intl.formatMessage({ id: "delete" }), onSubmit: () => {
            void handleDeleteAlert();
        }, onClose: onCancel, onCancel: onCancel, className: "gd-notifications-channel-delete-dialog s-alert-delete-dialog", containerClassName: "gd-notifications-channel-delete-dialog-overlay", children: _jsx("span", { className: "s-alert-delete-dialog-content", children: _jsx(FormattedMessage, { id: "dialogs.alerting.management.delete.dialog.confirm", values: {
                    b: (chunks) => (_jsxs("span", { className: "gd-notifications-channel-delete-dialog-text", children: [chunks, " ",
                            _jsx("strong", { children: alert.title })
                        ] })),
                } }) }) }));
}
