import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useScrollPreservation, useSmartScroll } from "../../../hooks/index.js";
import { hasOnlyThinkingContent, isAssistantMessage, isUserMessage, resolveMessageActions, } from "../../../utils/index.js";
import { block } from "../../../utils/cn.js";
import { IntersectionContainer } from "../../atoms/IntersectionContainer/index.js";
import { Loader } from "../../atoms/Loader/index.js";
import { ActionPopup } from "../../molecules/ActionPopup/index.js";
import { RatingBlock } from "../../molecules/RatingBlock/RatingBlock.js";
import { AssistantMessage } from "../AssistantMessage/index.js";
import { UserMessage } from "../UserMessage/index.js";
import { ErrorAlert } from "./ErrorAlert.js";
import { usePopup } from "./usePopup.js";
import "./MessageList.css";
const b = block('message-list');
export var MessageListQa;
(function (MessageListQa) {
    MessageListQa["Root"] = "message-list";
    MessageListQa["Messages"] = "message-list-messages";
})(MessageListQa || (MessageListQa = {}));
export 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 } = usePopup();
    const { containerRef } = useSmartScroll({
        isStreaming: isStreaming || isSubmitted,
        messagesCount: messages.length,
        status,
    });
    // Preserve scroll position when older messages are loaded
    useScrollPreservation(containerRef, messages.length);
    const renderMessage = (message, index) => {
        if (isUserMessage(message)) {
            const actions = resolveMessageActions(message, userActions);
            return (_jsx(UserMessage, { content: message.content, actions: actions, extraInfo: UserExtraInfo ? _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 (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 && !hasOnlyThinkingContent(message.content)
                ? resolveMessageActions(message, assistantActions)
                : undefined;
            return (_jsx(AssistantMessage, { content: message.content, actions: actions, extraInfo: AssistantExtraInfo ? _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 (_jsxs("div", { ref: containerRef, className: b(null, className), "data-qa": qa !== null && qa !== void 0 ? qa : MessageListQa.Root, children: [hasPreviousMessages && (_jsx(IntersectionContainer, { onIntersect: onLoadPreviousMessages, className: b('load-trigger'), children: _jsx(Loader, { view: "loading" }) })), _jsx("div", { className: b('messages'), "data-qa": qa ? `${qa}-messages` : MessageListQa.Messages, children: messages.map(renderMessage) }), showLoader && _jsx(Loader, { className: b('loader') }), status === 'error' && (_jsx(ErrorAlert, { className: b('error-alert'), onRetry: onRetry, errorMessage: errorMessage })), ratingBlockProps && ratingBlockProps.visible !== false && (_jsx(RatingBlock, Object.assign({}, ratingBlockProps, { className: b('rating-block', ratingBlockProps.className) }))), showActionPopup && ((_a = popupState.actionConfig) === null || _a === void 0 ? void 0 : _a.popup) && (_jsx(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
