"use strict";const t=require("react/jsx-runtime"),z=require("../../hooks/useTools.cjs.js"),S=require("../../providers/Thread.cjs.js"),R=require("../../providers/ChatRuntime.cjs.js"),y=require("../../utils/utils.cjs.js"),T=require("lucide-react"),i=require("react");function E({header:u,isSidebar:h,onClose:o}){const[m,f]=i.useState(!1),[c,_]=i.useState(""),{threads:C,getThreads:x,setThreadId:b,setThreads:j,setThreadsLoading:g}=S.useThread(),{identity:p}=R.useChatRuntime(),{tool:w,userDefinedTools:N}=z.useTools();i.useEffect(()=>{typeof window>"u"||(g(!0),x().then(j).catch(console.error).finally(()=>g(!1)))},[p?.authToken]);const v=C.filter(a=>{if(!c.trim())return!0;const l=c.toLowerCase();if((a.metadata?.name||a.metadata?.threadName||a.metadata?.thread_name||"").toLowerCase().includes(l))return!0;const e=a.values;return!!(e?.messages?.[0]&&y.getContentString(e.messages[0].content).toLowerCase().includes(l))}).sort((a,l)=>{const d=new Date(a.updated_at).getTime();return new Date(l.updated_at).getTime()-d});return t.jsxs("aside",{className:`flex h-full flex-col ${h?"":"border-r"} border-white/10 border-l border-white bg-zinc w-72`,children:[t.jsxs("div",{className:"flex h-14 items-center justify-between border-b border-white/10 px-2",children:[t.jsxs("div",{className:"flex items-center gap-3",children:[o&&t.jsx("div",{className:"cursor-pointer p-2 hover:bg-white/10 rounded-md",children:t.jsx(T.PanelLeftClose,{className:"h-5 w-5 text-white/70",onClick:o})}),u&&t.jsx("div",{className:"text-white/70 text-lg font-medium",children:u.title})]}),t.jsx("div",{className:"flex items-center gap-1",children:w.map((a,l)=>{let d=a.onClick;return a.label==="Chat"?d=()=>b(null):a.label==="Search"&&(d=()=>f(!0)),a.label==="Search"&&m?null:t.jsx(k,{icon:a.icon,label:a.label,alt:a.alt,onClick:d,showText:!1},l)})})]}),m&&t.jsx("div",{className:"p-2 mr-2",children:t.jsx("input",{type:"text",placeholder:"Search chats...",value:c,onChange:a=>_(a.target.value),onBlur:()=>{c.trim()||f(!1)},autoFocus:!0,className:"w-full bg-zinc-800 text-white/80 text-sm px-3 py-2 rounded-md outline-none focus:ring-1 focus:ring-zinc-600 placeholder:text-white/40"})}),t.jsx("div",{className:"flex-1 flex flex-col p-2 mr-2 overflow-hidden",children:t.jsxs(t.Fragment,{children:[N.map((a,l)=>t.jsx(k,{icon:a.icon,label:a.label,alt:a.alt,onClick:a.onClick,showText:!0},l)),t.jsx("div",{className:"flex-1 space-y-1 overflow-y-auto overflow-x-hidden thread-scrollbar",children:t.jsx(L,{threads:v})})]})})]})}function k({icon:u,label:h,alt:o,onClick:m,showText:f=!0}){return t.jsxs("button",{className:`flex items-center gap-3 rounded-md px-2 py-2
        text-white/70 hover:bg-white/10 cursor-pointer`,onClick:m,title:o||h,children:[u,f&&t.jsx("span",{className:"text-md font-medium",children:h})]})}function L({threads:u,onThreadClick:h}){const{threadId:o,setThreadId:m,deleteThread:f,updateThread:c}=S.useThread(),[_,C]=i.useState(null),[x,b]=i.useState(null),[j,g]=i.useState(null),[p,w]=i.useState(""),N=i.useRef(null),v=i.useRef(null);i.useEffect(()=>{const e=s=>{N.current&&!N.current.contains(s.target)&&b(null)};return x&&document.addEventListener("mousedown",e),()=>{document.removeEventListener("mousedown",e)}},[x]),i.useEffect(()=>{j&&v.current&&(v.current.focus(),v.current.select())},[j]);const a=async e=>{const s=e.values,n=e.metadata?.name||e.metadata?.threadName||e.metadata?.thread_name||(s?.messages?.[0]?y.getContentString(s.messages[0].content):e.thread_id);if(p.trim()&&p.trim()!==n)try{await c(e.thread_id,{...e.metadata,name:p.trim()})}catch(r){console.error("Failed to rename thread:",r),alert("Failed to rename thread. Please try again.")}g(null),w("")},l=()=>{g(null),w("")},d=async(e,s)=>{b(null);try{switch(e){case"rename":{const n=s.values,r=s.metadata?.name||s.metadata?.threadName||s.metadata?.thread_name||(n?.messages?.[0]?y.getContentString(n.messages[0].content):s.thread_id);w(r),g(s.thread_id);break}case"share":{const n=`${window.location.origin}${window.location.pathname}?thread=${s.thread_id}`;await navigator.clipboard.writeText(n),alert("Thread link copied to clipboard!");break}case"archive":{await c(s.thread_id,{...s.metadata,archived:!0});break}case"delete":{confirm("Are you sure you want to delete this thread? This action cannot be undone.")&&await f(s.thread_id);break}}}catch(n){console.error(`Failed to ${e} thread:`,n),alert(`Failed to ${e} thread. Please try again.`)}};return t.jsx("div",{className:"flex h-full w-full flex-col items-start justify-start gap-2",children:u.map(e=>{let s=e.metadata?.name||e.metadata?.threadName||e.metadata?.thread_name;if(!s&&typeof e.values=="object"&&e.values&&"messages"in e.values&&Array.isArray(e.values.messages)&&e.values.messages?.length>0){const r=e.values.messages[0];s=y.getContentString(r.content)}s||(s=e.thread_id);const n=j===e.thread_id;return t.jsxs("div",{className:`relative w-full rounded-md group ${e.thread_id===o?"bg-zinc-700/60":"hover:bg-zinc-800"}`,onMouseEnter:()=>C(e.thread_id),onMouseLeave:()=>C(null),children:[t.jsxs("div",{className:"flex items-center gap-1 px-2 py-1",children:[n?t.jsx("input",{ref:v,type:"text",value:p,onChange:r=>w(r.target.value),onKeyDown:r=>{r.key==="Enter"?a(e):r.key==="Escape"&&l()},onBlur:()=>a(e),className:"flex-1 bg-zinc-700 text-white/80 text-sm px-2 py-1 rounded outline-none focus:ring-1 focus:ring-zinc-500"}):t.jsxs("div",{className:"flex-1 flex items-center justify-between min-w-0",children:[t.jsx("button",{className:"flex-1 items-start justify-start text-left font-normal min-w-0",onClick:r=>{r.preventDefault(),h?.(e.thread_id),e.thread_id!==o&&m(e.thread_id)},children:t.jsx("p",{className:"truncate text-ellipsis text-sm text-white/80",children:s})}),t.jsx("span",{className:"flex-shrink-0 text-xs text-white/40 ml-2",children:y.formatRelativeTime(e.updated_at)})]}),!n&&_===e.thread_id&&t.jsx("button",{className:"flex-shrink-0 p-2 rounded-md hover:bg-zinc-700",onClick:r=>{r.stopPropagation(),b(x===e.thread_id?null:e.thread_id)},children:t.jsx(T.MoreVertical,{className:"h-4 w-4 text-white/60"})})]}),x===e.thread_id&&t.jsxs("div",{ref:N,className:"absolute right-2 top-8 z-50 w-48 rounded-md bg-zinc-800 border border-white/10 shadow-lg py-1",children:[t.jsxs("button",{className:"w-full flex items-center gap-2 px-3 py-2 text-sm text-white/80 hover:bg-zinc-700",onClick:()=>d("rename",e),children:[t.jsx(T.Pencil,{className:"h-4 w-4"}),"Rename"]}),t.jsxs("button",{className:"w-full flex items-center gap-2 px-3 py-2 text-sm text-white/80 hover:bg-zinc-700",onClick:()=>d("share",e),children:[t.jsx(T.Share2,{className:"h-4 w-4"}),"Share"]}),t.jsx("div",{className:"my-1 border-t border-white/10"}),t.jsxs("button",{className:"w-full flex items-center gap-2 px-3 py-2 text-sm text-red-400 hover:bg-zinc-700",onClick:()=>d("delete",e),children:[t.jsx(T.Trash2,{className:"h-4 w-4"}),"Delete"]})]})]},e.thread_id)})})}module.exports=E;
//# sourceMappingURL=ThreadHistory.cjs.js.map
