import{a as e,d as t,t as n}from"./hooks.module-BrXTvcfS.js";import{t as r}from"./jsxRuntime.module-Cao1NCKs.js";import{a as i,i as a,t as o}from"./ChatMessage-BURGqoUt.js";t();function s(e,t){l(e);for(let n of t){if(!n.highlights?.length)continue;let t=a(e,n.id);if(t)for(let e of n.highlights){let n=i(t,e);n&&u(n,e.id)}}}var c=`chat-highlight`;function l(e){for(let t of e.querySelectorAll(`mark.${c}`)){let e=t.parentNode;e&&(t.replaceWith(...t.childNodes),e.normalize())}}function u(e,t){for(let n of d(e)){let r=n===e.startContainer?e.startOffset:0,i=n===e.endContainer?e.endOffset:n.data.length;if(i<=r)continue;let a=n;i<a.data.length&&a.splitText(i),r>0&&(a=a.splitText(r));let o=a.ownerDocument.createElement(`mark`);o.className=c,o.dataset.highlightId=t,a.replaceWith(o),o.appendChild(a)}}function d(e){let t=e.commonAncestorContainer;if(t.nodeType===Node.TEXT_NODE)return[t];let n=t.ownerDocument?.createTreeWalker(t,NodeFilter.SHOW_TEXT);if(!n)return[];let r=[],i=!1;for(let t=n.nextNode();t;t=n.nextNode()){let n=t;if(n===e.startContainer&&(i=!0),i&&r.push(n),n===e.endContainer)break}return r}function f({messages:t}){let i=n(null);return e(()=>{i.current&&s(i.current,t)},[t]),r(`div`,{className:`llm-chat-preview`,ref:i,children:t.map(e=>r(o,{message:e},e.id))})}export{f as default};