import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2024-2026 GoodData Corporation
import { useCallback } from "react";
import { compact } from "lodash-es";
import { FormattedMessage, defineMessages, useIntl } from "react-intl";
import { ConfirmDialog } from "@gooddata/sdk-ui-kit";
import { eagerRemoveSectionItemByWidgetRef } from "../../model/commands/layout.js";
import { useDashboardDispatch, useDashboardSelector } from "../../model/react/DashboardStoreProvider.js";
import { dispatchAndWaitFor } from "../../model/store/_infra/dispatchAndWaitFor.js";
import { selectDashboardUserAutomationAlertsInContext, selectDashboardUserAutomationSchedulesInContext, } from "../../model/store/automations/automationsSelectors.js";
import { selectEnableAlerting, selectEnableScheduling } from "../../model/store/config/configSelectors.js";
import { selectAnalyticalWidgetByRef } from "../../model/store/tabs/layout/layoutSelectors.js";
import { uiActions } from "../../model/store/ui/index.js";
import { selectIsWidgetDeleteDialogOpen, selectWidgetDeleteDialogWidgetRef, } from "../../model/store/ui/uiSelectors.js";
const deleteMessages = defineMessages({
    headline: {
        id: "deleteWidgetDialog.headline",
    },
    objects: {
        id: "deleteWidgetDialog.objectsMessage",
    },
    alerts: {
        id: "deleteWidgetDialog.alerts",
    },
    schedules: {
        id: "deleteWidgetDialog.schedules",
    },
    cancel: {
        id: "cancel",
    },
    delete: {
        id: "delete",
    },
});
/**
 * @internal
 */
export function useWidgetDeleteDialogProps() {
    const dispatch = useDashboardDispatch();
    const onCancel = useCallback(() => dispatch(uiActions.closeWidgetDeleteDialog()), [dispatch]);
    const widgetRef = useDashboardSelector(selectWidgetDeleteDialogWidgetRef);
    const widget = useDashboardSelector(selectAnalyticalWidgetByRef(widgetRef));
    const onDelete = useCallback(() => {
        if (widget) {
            void dispatchAndWaitFor(dispatch, eagerRemoveSectionItemByWidgetRef(widget.ref)).finally(() => {
                dispatch(uiActions.closeWidgetDeleteDialog());
            });
        }
    }, [dispatch, widget]);
    const isVisible = useDashboardSelector(selectIsWidgetDeleteDialogOpen);
    const isSchedulingEnabled = useDashboardSelector(selectEnableScheduling);
    const isAlertingEnabled = useDashboardSelector(selectEnableAlerting);
    const alerts = useDashboardSelector(selectDashboardUserAutomationAlertsInContext(widget?.localIdentifier));
    const schedules = useDashboardSelector(selectDashboardUserAutomationSchedulesInContext(widget?.localIdentifier));
    return {
        isVisible,
        showAlertsMessage: isAlertingEnabled && alerts.length > 0,
        showSchedulesMessage: isSchedulingEnabled && schedules.length > 0,
        onCancel,
        onDelete,
        widget,
    };
}
/**
 * @internal
 */
export function DefaultWidgetDeleteDialog({ isVisible, showAlertsMessage, showSchedulesMessage, onDelete, onCancel, widget, }) {
    const intl = useIntl();
    if (!isVisible || !widget) {
        return null;
    }
    const widgetTitle = widget.title;
    const messages = compact([
        showAlertsMessage && deleteMessages.alerts,
        showSchedulesMessage && deleteMessages.schedules,
    ]);
    return (_jsx(ConfirmDialog, { onCancel: onCancel, onSubmit: onDelete, isPositive: false, className: "s-dialog s-delete_widget_dialog", headline: intl.formatMessage(deleteMessages.headline), cancelButtonText: intl.formatMessage(deleteMessages.cancel), submitButtonText: intl.formatMessage(deleteMessages.delete), children: _jsxs("div", { children: [
                _jsx(FormattedMessage, { id: deleteMessages.objects.id, values: { title: widgetTitle } }), _jsx("ul", { className: "gd-delete-dialog-objects-list", children: messages.map((message) => (_jsx("li", { children: _jsx(FormattedMessage, { ...message }) }, message.id))) })
            ] }) }));
}
