import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2022-2026 GoodData Corporation
import { useCallback } from "react";
import cx from "classnames";
import { FormattedMessage, useIntl } from "react-intl";
import { Bubble, BubbleHoverTrigger, IconInsight, IconSimplifiedDashboard, IconWarning, SELECT_ITEM_ACTION, ShortenedText, } from "@gooddata/sdk-ui-kit";
import { useTheme } from "@gooddata/sdk-ui-theme-provider";
import { useDashboardSelector } from "../../../../../model/react/DashboardStoreProvider.js";
import { selectCanManageWorkspace } from "../../../../../model/store/permissions/permissionsSelectors.js";
import { selectCurrentUser } from "../../../../../model/store/user/userSelectors.js";
import { gdColorNegative, gdColorStateBlank } from "../../../../constants/colors.js";
import { isVisualisationAutomation } from "../../../shared/utils/automationUtils.js";
import { useScheduleValidation } from "../../DefaultScheduledEmailDialog/hooks/useScheduleValidation.js";
const ICON_TOOLTIP_ALIGN_POINTS = [
    { align: "cr cl", offset: { x: 0, y: 0 } },
    { align: "cl cr", offset: { x: 0, y: 0 } },
];
const TEXT_TOOLTIP_ALIGN_POINTS = [
    { align: "tc bc", offset: { x: 0, y: 0 } },
    { align: "bc tc", offset: { x: 0, y: 0 } },
];
export function ScheduledEmail({ scheduledEmail, onDelete, onEdit, notificationChannels, focusedAction, }) {
    const theme = useTheme();
    const currentUser = useDashboardSelector(selectCurrentUser);
    const canManageWorkspace = useDashboardSelector(selectCanManageWorkspace);
    const canEdit = canManageWorkspace || (currentUser && currentUser.login === scheduledEmail.createdBy?.login);
    const { isValid } = useScheduleValidation(scheduledEmail);
    const intl = useIntl();
    const cronDescription = scheduledEmail.schedule?.cronDescription;
    const webhookTitle = notificationChannels.find((channel) => channel.id === scheduledEmail.notificationChannel)?.title;
    const dashboardTitle = scheduledEmail.exportDefinitions?.find((def) => def.title)?.title;
    const isWidget = isVisualisationAutomation(scheduledEmail);
    const iconColor = theme?.palette?.complementary?.c6 ?? gdColorStateBlank;
    const iconColorError = theme?.palette?.error?.base ?? gdColorNegative;
    const iconComponent = isValid ? (isWidget ? (_jsx(IconInsight, { width: 16, height: 16, color: iconColor })) : (_jsx(IconSimplifiedDashboard, { width: 19, height: 19, color: iconColor }))) : (_jsx(IconWarning, { width: 16, height: 16, color: iconColorError }));
    const subtitle = [cronDescription, webhookTitle, dashboardTitle].filter(Boolean).join(" • ");
    const handleClick = useCallback(() => {
        onEdit(scheduledEmail);
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [scheduledEmail]);
    return (_jsxs("div", { className: cx("gd-notifications-channel", "s-scheduled-email", {
            editable: canEdit,
            "gd-schedule-email__item--isFocused": !!focusedAction,
            "gd-schedule-email__item--isFocusedSelectItem": focusedAction === SELECT_ITEM_ACTION,
        }), children: [
            _jsxs("div", { className: "gd-notifications-channel-content", onClick: canEdit ? handleClick : undefined, children: [
                    _jsx("div", { className: cx("gd-notifications-channel-icon", {
                            "gd-notifications-channel-icon-invalid": !isValid,
                        }), children: iconComponent }), _jsxs("div", { className: "gd-notifications-channel-text-content", children: [
                            _jsx("div", { className: "gd-notifications-channel-title", children: _jsx("strong", { children: _jsx(ShortenedText, { className: "gd-notifications-channel-shortened-text", tooltipAlignPoints: TEXT_TOOLTIP_ALIGN_POINTS, children: scheduledEmail.title ??
                                            intl.formatMessage({ id: "dialogs.schedule.email.title.placeholder" }) }) }) }), _jsx("div", { children: _jsx("span", { className: "gd-notifications-channel-subtitle", children: _jsx(ShortenedText, { className: "gd-notifications-channel-shortened-text", tooltipAlignPoints: TEXT_TOOLTIP_ALIGN_POINTS, children: subtitle }) }) })
                        ] })
                ] }), _jsx("div", { className: "gd-notifications-channel-delete", children: _jsxs(BubbleHoverTrigger, { showDelay: 0, hideDelay: 0, children: [
                        _jsx("span", { className: cx("gd-notifications-channel-delete-icon s-scheduled-email-delete-icon", {
                                "gd-schedule-email__item__button--isFocused": focusedAction === "delete",
                            }), onClick: () => onDelete(scheduledEmail) }), _jsx(Bubble, { className: "bubble-primary", alignPoints: ICON_TOOLTIP_ALIGN_POINTS, children: _jsx(FormattedMessage, { id: "dialogs.schedule.management.delete" }) })
                    ] }) })
        ] }));
}
