import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2025-2026 GoodData Corporation
import { useMemo } from "react";
import { useIntl } from "react-intl";
import { UiIcon, useToastMessage } from "@gooddata/sdk-ui-kit";
import { AutomationIconTooltip } from "./AutomationIconTooltip.js";
import { bem } from "../../notificationsPanel/bem.js";
import { AUTOMATION_ICON_CONFIGS } from "../constants.js";
import { formatAutomationSubtitle, formatCellValue } from "../format.js";
import { messages } from "../messages.js";
const { e } = bem("gd-ui-ext-automation-icon-tooltip");
export function AutomationIcon({ type, automation, state, timezone }) {
    const intl = useIntl();
    const { addSuccess } = useToastMessage();
    const failedHeaderText = useMemo(() => {
        if (!automation) {
            return "";
        }
        if (automation.schedule) {
            return intl.formatMessage(messages.automationIconTooltipHeaderSchedule);
        }
        return intl.formatMessage(messages.automationIconTooltipHeaderAlert);
    }, [automation, intl]);
    if (!type) {
        return null;
    }
    const props = state === "PAUSED" ? AUTOMATION_ICON_CONFIGS[`${type}${state}`] : AUTOMATION_ICON_CONFIGS[type];
    if (type === "FAILED") {
        const traceId = automation?.lastRun?.traceId;
        const status = automation?.lastRun?.errorMessage;
        const onCopyTraceId = () => {
            void navigator.clipboard.writeText(traceId);
            addSuccess(messages.messageAutomationIconTooltipTraceIdCopied);
        };
        return (_jsx(AutomationIconTooltip, { header: failedHeaderText, align: "bc tc", sections: [
                {
                    header: intl.formatMessage(messages.automationIconTooltipStatus),
                    content: formatCellValue(status),
                },
                {
                    header: intl.formatMessage(messages.automationIconTooltipTraceId),
                    content: formatCellValue(traceId),
                    icon: "copy",
                    onIconClick: onCopyTraceId,
                },
            ], children: _jsx(UiIcon, { ...props, layout: "block" }) }));
    }
    if (type === "automationDetails" && automation) {
        const subtitle = formatCellValue(formatAutomationSubtitle(automation, intl));
        return (_jsx(AutomationIconTooltip, { header: formatCellValue(automation.title), align: "cr cl", sections: [
                ...(automation.created
                    ? [
                        {
                            header: intl.formatMessage(messages.automationIconTooltipStartsOn),
                            content: (_jsx(TooltipStartsOnSection, { automation: automation, timezone: timezone })),
                        },
                    ]
                    : []),
                {
                    header: intl.formatMessage(messages.automationIconTooltipRepeats),
                    content: subtitle,
                },
                ...(automation.recipients?.length
                    ? [
                        {
                            header: intl.formatMessage(messages.columnRecipients),
                            content: _jsx(TooltipRecipientsList, { automation: automation }),
                        },
                    ]
                    : []),
            ], children: _jsx(UiIcon, { ...props, layout: "block" }) }));
    }
    return _jsx(UiIcon, { ...props });
}
function TooltipRecipientsList({ automation }) {
    return (_jsx("div", { className: e("recipients-list"), children: automation.recipients?.map((recipient) => (_jsx("span", { className: e("recipient"), children: recipient.name }, recipient.id))) }));
}
function TooltipStartsOnSection({ automation, timezone, }) {
    const [date, time] = formatCellValue(automation.created, "date", timezone)?.split(" ") ?? [];
    return (_jsxs("div", { className: e("starts-on"), children: [_jsx("span", { children: date }), _jsx("span", { children: `${time} ${timezone}` })] }));
}
//# sourceMappingURL=AutomationIcon.js.map