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 { useDashboardSelector } from "../../../../model/react/DashboardStoreProvider.js";
import { selectCanManageWorkspace } from "../../../../model/store/permissions/permissionsSelectors.js";
import { selectCurrentUser } from "../../../../model/store/user/userSelectors.js";
export function DeleteAlertConfirmDialog({ alert, onCancel, onSuccess, onError, }) {
    const effectiveBackend = useBackendStrict();
    const effectiveWorkspace = useWorkspaceStrict();
    const intl = useIntl();
    const currentUser = useDashboardSelector(selectCurrentUser);
    const canManageAutomations = useDashboardSelector(selectCanManageWorkspace);
    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 })
                        ] })),
                } }) }) }));
}
//# sourceMappingURL=DeleteAlertConfirmDialog.js.map