import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { getClassNames } from '@websolutespa/bom-core';
import { useLabel } from '@websolutespa/bom-mixer-hooks';
import { useLlm } from '@websolutespa/bom-mixer-llm';
import React, { useEffect, useMemo, useRef } from 'react';
import { chunksToUIChunks } from '../../../utils';
import style from './page-thread.module.scss';
export const PageThread = ()=>{
    const ref = useRef(null);
    const label = useLabel();
    const { ChunkWrapper, Feedback, PageLoading, Stream } = useLlm((state)=>state.components);
    const enableFeedback = useLlm((state)=>state.app?.contents.enableFeedback);
    const messages = useLlm((state)=>state.messages);
    const message = useLlm((state)=>state.message);
    const streaming = useLlm((state)=>state.streaming);
    const hasValidChunks = useLlm((state)=>(state.chunks || []).reduce((p, c)=>{
            return p || ![
                'header',
                'log',
                'info',
                'end'
            ].includes(c.type);
        }, false));
    useEffect(()=>{
        window.scrollTo({
            top: 0
        });
    /*
    if (ref.current) {
      ref.current.scrollIntoView();
    }
    */ }, [
        message,
        hasValidChunks
    ]);
    const classNames = getClassNames(style.thread);
    // console.log('message', message);
    const parsedMessage = useMemo(()=>{
        if (message) {
            return {
                ...message,
                chunks: chunksToUIChunks(message.chunks)
            };
        }
        return undefined;
    }, [
        message
    ]);
    return /*#__PURE__*/ _jsx("section", {
        ref: ref,
        className: classNames,
        children: hasValidChunks ? /*#__PURE__*/ _jsx(Stream, {
            className: getClassNames(style.message, '-assistant'),
            index: messages.length
        }) : streaming ? /*#__PURE__*/ _jsx(PageLoading, {}) : /*#__PURE__*/ _jsx(_Fragment, {
            children: parsedMessage && /*#__PURE__*/ _jsxs("div", {
                className: getClassNames(style.message, `-${parsedMessage.role}`),
                role: "region",
                "aria-label": parsedMessage.role === 'user' ? label('llm.question') : label('llm.answer'),
                children: [
                    parsedMessage.chunks.map((chunk, c)=>/*#__PURE__*/ _jsx(ChunkWrapper, {
                            className: getClassNames(style.messageInner, `-${chunk.type}`),
                            item: chunk,
                            chunkIndex: c,
                            index: 0,
                            total: messages.length,
                            role: parsedMessage.role
                        }, `llm-message-${parsedMessage.messageId}-chunk-${c}`)),
                    enableFeedback && /*#__PURE__*/ _jsx(Feedback, {
                        message: parsedMessage
                    })
                ]
            }, `llm-message-${parsedMessage.messageId}-0`)
        })
    });
};

//# sourceMappingURL=page-thread.js.map