"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseMessage = void 0;
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = tslib_1.__importDefault(require("react"));
const icons_1 = require("@gravity-ui/icons");
const uikit_1 = require("@gravity-ui/uikit");
const messages_1 = require("../../../types/messages.js");
const actionUtils_1 = require("../../../utils/actionUtils.js");
const cn_1 = require("../../../utils/cn.js");
const ActionButton_1 = require("../../atoms/ActionButton/index.js");
const ChatDate_1 = require("../../atoms/ChatDate/index.js");
const ButtonGroup_1 = require("../ButtonGroup/index.js");
const i18n_1 = require("./i18n/index.js");
require("./BaseMessage.css");
const b = (0, cn_1.block)('base-message');
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(messages_1.BaseMessageActionType);
        if (!actionType || !knownTypes.includes(actionType)) {
            return '';
        }
        try {
            return (0, i18n_1.i18n)(`action-tooltip-${actionType}`);
        }
        catch (_a) {
            return '';
        }
    };
    const hasActions = actions && actions.length > 0;
    const showFooter = hasActions || Boolean(extraInfo);
    return ((0, jsx_runtime_1.jsxs)("div", { className: b({ variant, 'btn-hover': showActionsOnHover }, className), "data-qa": qa, children: [children, showFooter && ((0, jsx_runtime_1.jsxs)("div", { className: b('actions', { reverse: variant !== 'user' }), children: [showTimestamp ? (0, jsx_runtime_1.jsx)(ChatDate_1.ChatDate, { date: timestamp, format: "HH:mm", showTime: true }) : null, hasActions && ((0, jsx_runtime_1.jsx)(ButtonGroup_1.ButtonGroup, { children: actions === null || actions === void 0 ? void 0 : actions.map((action, index) => {
                            if (!(0, actionUtils_1.isActionConfig)(action)) {
                                return (0, jsx_runtime_1.jsx)(react_1.default.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 = tslib_1.__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 = (0, jsx_runtime_1.jsx)(uikit_1.Icon, { size: 16, data: defaultIcon });
                            }
                            else if (action.label) {
                                buttonContent = action.label;
                            }
                            else {
                                buttonContent = action.actionType;
                            }
                            return ((0, jsx_runtime_1.jsx)(ActionButton_1.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] }))] }));
};
exports.BaseMessage = BaseMessage;
function getDefaultIcon(actionType, userRating) {
    switch (actionType) {
        case messages_1.BaseMessageActionType.Copy:
            return icons_1.Copy;
        case messages_1.BaseMessageActionType.Edit:
            return icons_1.Pencil;
        case messages_1.BaseMessageActionType.Retry:
            return icons_1.ArrowRotateLeft;
        case messages_1.BaseMessageActionType.Delete:
            return icons_1.TrashBin;
        case messages_1.BaseMessageActionType.Like:
            return userRating === 'like' ? icons_1.ThumbsUpFill : icons_1.ThumbsUp;
        case messages_1.BaseMessageActionType.Dislike:
            return userRating === 'dislike' ? icons_1.ThumbsDownFill : icons_1.ThumbsDown;
        default:
            return undefined;
    }
}
//# sourceMappingURL=index.js.map
