import { __rest } from "tslib";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import React from 'react';
import { ArrowRotateLeft, Copy as CopyIcon, Pencil, ThumbsDown, ThumbsDownFill, ThumbsUp, ThumbsUpFill, TrashBin, } from '@gravity-ui/icons';
import { Icon } from '@gravity-ui/uikit';
import { BaseMessageActionType } from "../../../types/messages.js";
import { isActionConfig } from "../../../utils/actionUtils.js";
import { block } from "../../../utils/cn.js";
import { ActionButton } from "../../atoms/ActionButton/index.js";
import { ChatDate } from "../../atoms/ChatDate/index.js";
import { ButtonGroup } from "../ButtonGroup/index.js";
import { i18n } from "./i18n/index.js";
import "./BaseMessage.css";
const b = block('base-message');
export const BaseMessage = (props) => {
    const { className, qa, showActionsOnHover, actions, extraInfo, children, role: variant, showTimestamp, timestamp = '', userRating, onActionPopup, } = props;
    // Get tooltip text for action
    const getTooltipText = (actionType) => {
        const knownTypes = Object.values(BaseMessageActionType);
        if (!actionType || !knownTypes.includes(actionType)) {
            return '';
        }
        try {
            return i18n(`action-tooltip-${actionType}`);
        }
        catch (_a) {
            return '';
        }
    };
    const hasActions = actions && actions.length > 0;
    const showFooter = hasActions || Boolean(extraInfo);
    return (_jsxs("div", { className: b({ variant, 'btn-hover': showActionsOnHover }, className), "data-qa": qa, children: [children, showFooter && (_jsxs("div", { className: b('actions', { reverse: variant !== 'user' }), children: [showTimestamp ? _jsx(ChatDate, { date: timestamp, format: "HH:mm", showTime: true }) : null, hasActions && (_jsx(ButtonGroup, { children: actions === null || actions === void 0 ? void 0 : actions.map((action, index) => {
                            if (!isActionConfig(action)) {
                                return _jsx(React.Fragment, { children: action }, index);
                            }
                            const tooltipText = getTooltipText(action.actionType);
                            const defaultIcon = getDefaultIcon(action.actionType, userRating);
                            // Determine tooltip title
                            let tooltipTitle;
                            if (action.label) {
                                tooltipTitle = action.label;
                            }
                            else if (tooltipText && action.actionType !== 'custom') {
                                tooltipTitle = tooltipText;
                            }
                            // Exclude custom props that shouldn't be passed to DOM
                            const { actionType: _actionType, popup: _popup, icon: _icon, label: _label, children: actionChildren, onClick: actionOnClick } = action, buttonProps = __rest(action, ["actionType", "popup", "icon", "label", "children", "onClick"]);
                            // Determine button content; children takes precedence over icon
                            let buttonContent;
                            if (actionChildren) {
                                buttonContent = actionChildren;
                            }
                            else if (action.icon) {
                                buttonContent = action.icon;
                            }
                            else if (defaultIcon) {
                                buttonContent = _jsx(Icon, { size: 16, data: defaultIcon });
                            }
                            else if (action.label) {
                                buttonContent = action.label;
                            }
                            else {
                                buttonContent = action.actionType;
                            }
                            return (_jsx(ActionButton, Object.assign({}, buttonProps, { tooltipTitle: tooltipTitle, view: action.view || 'flat-secondary', onClick: (e) => {
                                    onActionPopup === null || onActionPopup === void 0 ? void 0 : onActionPopup(action, e.currentTarget);
                                    actionOnClick === null || actionOnClick === void 0 ? void 0 : actionOnClick(e);
                                }, children: buttonContent }), action.actionType || index));
                        }) })), extraInfo] }))] }));
};
function getDefaultIcon(actionType, userRating) {
    switch (actionType) {
        case BaseMessageActionType.Copy:
            return CopyIcon;
        case BaseMessageActionType.Edit:
            return Pencil;
        case BaseMessageActionType.Retry:
            return ArrowRotateLeft;
        case BaseMessageActionType.Delete:
            return TrashBin;
        case BaseMessageActionType.Like:
            return userRating === 'like' ? ThumbsUpFill : ThumbsUp;
        case BaseMessageActionType.Dislike:
            return userRating === 'dislike' ? ThumbsDownFill : ThumbsDown;
        default:
            return undefined;
    }
}
//# sourceMappingURL=index.js.map
