"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MessageListQa = void 0;
exports.MessageList = MessageList;
const jsx_runtime_1 = require("react/jsx-runtime");
const hooks_1 = require("../../../hooks/index.js");
const utils_1 = require("../../../utils/index.js");
const cn_1 = require("../../../utils/cn.js");
const IntersectionContainer_1 = require("../../atoms/IntersectionContainer/index.js");
const Loader_1 = require("../../atoms/Loader/index.js");
const ActionPopup_1 = require("../../molecules/ActionPopup/index.js");
const RatingBlock_1 = require("../../molecules/RatingBlock/RatingBlock.js");
const AssistantMessage_1 = require("../AssistantMessage/index.js");
const UserMessage_1 = require("../UserMessage/index.js");
const ErrorAlert_1 = require("./ErrorAlert.js");
const usePopup_1 = require("./usePopup.js");
require("./MessageList.css");
const b = (0, cn_1.block)('message-list');
var MessageListQa;
(function (MessageListQa) {
    MessageListQa["Root"] = "message-list";
    MessageListQa["Messages"] = "message-list-messages";
})(MessageListQa || (exports.MessageListQa = MessageListQa = {}));
function MessageList({ messages, messageRendererRegistry, transformOptions, shouldParseIncompleteMarkdown, showActionsOnHover, showTimestamp, showAvatar, userActions, assistantActions, userExtraInfo: UserExtraInfo, assistantExtraInfo: AssistantExtraInfo, loaderStatuses = ['submitted', 'streaming_loading'], className, qa, status, errorMessage, onRetry, hasPreviousMessages = false, onLoadPreviousMessages, ratingBlockProps, actionPopupProps, }) {
    var _a, _b;
    const isStreaming = status === 'streaming' || status === 'streaming_loading';
    const isSubmitted = status === 'submitted';
    const showLoader = status && loaderStatuses.includes(status);
    // Use popup hook for managing popup state
    const { popupState, handleActionPopup, handlePopupOpenChange, showActionPopup } = (0, usePopup_1.usePopup)();
    const { containerRef } = (0, hooks_1.useSmartScroll)({
        isStreaming: isStreaming || isSubmitted,
        messagesCount: messages.length,
        status,
    });
    // Preserve scroll position when older messages are loaded
    (0, hooks_1.useScrollPreservation)(containerRef, messages.length);
    const renderMessage = (message, index) => {
        if ((0, utils_1.isUserMessage)(message)) {
            const actions = (0, utils_1.resolveMessageActions)(message, userActions);
            return ((0, jsx_runtime_1.jsx)(UserMessage_1.UserMessage, { content: message.content, actions: actions, extraInfo: UserExtraInfo ? (0, jsx_runtime_1.jsx)(UserExtraInfo, { message: message }) : undefined, timestamp: message.timestamp, format: message.format, avatarUrl: message.avatarUrl, images: message.images, fileAttachments: message.fileAttachments, transformOptions: transformOptions, shouldParseIncompleteMarkdown: shouldParseIncompleteMarkdown, showActionsOnHover: showActionsOnHover, showTimestamp: showTimestamp, showAvatar: showAvatar, onActionPopup: (action, anchor) => handleActionPopup(message, action, anchor) }, message.id || `message-${index}`));
        }
        if ((0, utils_1.isAssistantMessage)(message)) {
            const isLastMessage = index === messages.length - 1;
            const isNotCompleted = isSubmitted || isStreaming;
            const showActions = !(isLastMessage && isNotCompleted);
            // Don't show assistantActions for messages with ONLY thinking content
            // For mixed content (thinking + text), actions are shown and copy entire message
            const actions = showActions && !(0, utils_1.hasOnlyThinkingContent)(message.content)
                ? (0, utils_1.resolveMessageActions)(message, assistantActions)
                : undefined;
            return ((0, jsx_runtime_1.jsx)(AssistantMessage_1.AssistantMessage, { content: message.content, actions: actions, extraInfo: AssistantExtraInfo ? (0, jsx_runtime_1.jsx)(AssistantExtraInfo, { message: message }) : undefined, timestamp: message.timestamp, id: message.id, messageRendererRegistry: messageRendererRegistry, transformOptions: transformOptions, shouldParseIncompleteMarkdown: shouldParseIncompleteMarkdown, showActionsOnHover: showActionsOnHover, showTimestamp: showTimestamp, userRating: message.userRating, onActionPopup: (action, anchor) => handleActionPopup(message, action, anchor) }, message.id || `message-${index}`));
        }
        return null;
    };
    return ((0, jsx_runtime_1.jsxs)("div", { ref: containerRef, className: b(null, className), "data-qa": qa !== null && qa !== void 0 ? qa : MessageListQa.Root, children: [hasPreviousMessages && ((0, jsx_runtime_1.jsx)(IntersectionContainer_1.IntersectionContainer, { onIntersect: onLoadPreviousMessages, className: b('load-trigger'), children: (0, jsx_runtime_1.jsx)(Loader_1.Loader, { view: "loading" }) })), (0, jsx_runtime_1.jsx)("div", { className: b('messages'), "data-qa": qa ? `${qa}-messages` : MessageListQa.Messages, children: messages.map(renderMessage) }), showLoader && (0, jsx_runtime_1.jsx)(Loader_1.Loader, { className: b('loader') }), status === 'error' && ((0, jsx_runtime_1.jsx)(ErrorAlert_1.ErrorAlert, { className: b('error-alert'), onRetry: onRetry, errorMessage: errorMessage })), ratingBlockProps && ratingBlockProps.visible !== false && ((0, jsx_runtime_1.jsx)(RatingBlock_1.RatingBlock, Object.assign({}, ratingBlockProps, { className: b('rating-block', ratingBlockProps.className) }))), showActionPopup && ((_a = popupState.actionConfig) === null || _a === void 0 ? void 0 : _a.popup) && ((0, jsx_runtime_1.jsx)(ActionPopup_1.ActionPopup, { open: popupState.open, onOpenChange: handlePopupOpenChange, anchorElement: popupState.anchorElement, title: (actionPopupProps === null || actionPopupProps === void 0 ? void 0 : actionPopupProps.title) || popupState.title || undefined, subtitle: (actionPopupProps === null || actionPopupProps === void 0 ? void 0 : actionPopupProps.subtitle) || popupState.subtitle || undefined, placement: (actionPopupProps === null || actionPopupProps === void 0 ? void 0 : actionPopupProps.placement) || popupState.actionConfig.popup.placement, className: actionPopupProps === null || actionPopupProps === void 0 ? void 0 : actionPopupProps.className, qa: (_b = actionPopupProps === null || actionPopupProps === void 0 ? void 0 : actionPopupProps.qa) !== null && _b !== void 0 ? _b : (qa ? `${qa}-action-popup` : 'action-popup'), children: popupState.content }))] }));
}
//# sourceMappingURL=MessageList.js.map
