import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2022-2025 GoodData Corporation
import { useCallback, useState } from "react";
import { FormattedMessage } from "react-intl";
import { LoadingSpinner, useIdPrefixed, useListWithActionsKeyboardNavigation } from "@gooddata/sdk-ui-kit";
import { useTheme } from "@gooddata/sdk-ui-theme-provider";
import { Alert } from "./Alert.js";
const getItemAdditionalActions = () => {
    return ["dropdown", "item"];
};
export function Alerts({ isLoading, alerts, onDelete, onEdit, onPause, noAlertsMessageId }) {
    const theme = useTheme();
    const handleEdit = useCallback((alert) => () => onEdit(alert), [onEdit]);
    const handleTogglePause = useCallback((alert) => () => onPause(alert, alert.alert?.trigger.state !== "PAUSED"), [onPause]);
    const handleDelete = useCallback((alert) => () => onDelete(alert), [onDelete]);
    const [dropdownOpenAlertId, setDropdownOpenAlertId] = useState(null);
    const handleToggleDropdown = useCallback((alert) => (desiredState) => {
        setDropdownOpenAlertId((prev) => {
            const nextState = desiredState ?? !prev;
            return nextState ? alert.id : null;
        });
    }, []);
    const { focusedAction, focusedItem, onKeyboardNavigation } = useListWithActionsKeyboardNavigation({
        items: alerts,
        getItemAdditionalActions,
        actionHandlers: {
            selectItem: handleEdit,
            item: handleEdit,
            dropdown: (alert) => () => {
                handleToggleDropdown(alert)(true);
            },
        },
        isNestedList: true,
    });
    const listId = useIdPrefixed("AlertsList");
    if (isLoading) {
        return (_jsx("div", { className: "gd-loading-equalizer-wrap gd-notifications-channels-message", children: _jsx("div", { className: "gd-loading-equalizer gd-loading-equalizer-fade", children: _jsx(LoadingSpinner, { className: "large gd-loading-equalizer-spinner", color: theme?.palette?.complementary?.c9 }) }) }));
    }
    if (alerts.length === 0) {
        return (_jsx("div", { className: "gd-notifications-channels-message s-no-alerts-message", children: _jsx(FormattedMessage, { id: noAlertsMessageId, values: { br: _jsx("br", {}) } }) }));
    }
    return (_jsx("div", { onKeyDown: onKeyboardNavigation, tabIndex: 0, className: "gd-alerts-list__items gd-alerts-list--dialog", id: listId, children: alerts.map((alert) => (_jsx(Alert, { alert: alert, focusedAction: alert === focusedItem ? focusedAction : undefined, onDelete: handleDelete(alert), onEdit: handleEdit(alert), onTogglePause: handleTogglePause(alert), listId: listId, isDropdownOpen: dropdownOpenAlertId === alert.id, onToggleDropdown: handleToggleDropdown(alert), isSubtitleVisible: true }, alert.id))) }));
}
//# sourceMappingURL=AlertsList.js.map