import{r as e}from"./rolldown-runtime-CpWojdLp.js";import{n as t,t as n}from"./jsx-runtime-B6e-FjZo.js";var r=e(t(),1),i=n();function a({agentId:e,title:t,placeholder:n,starters:a,seedQuestion:s,seedAnswer:c,offlineMessage:l,priceCard:u,intent:d=null,seedAsAssistantOnly:f=!1,handleRef:p,tryToken:m=null,tryIntro:h,tryStarters:g}){let[_,v]=(0,r.useState)(null),[y,b]=(0,r.useState)(null),[x,S]=(0,r.useState)(()=>m&&h?[{role:`assistant`,text:h}]:f?[{role:`assistant`,text:`${s} ${c}`.trim()}]:[{role:`user`,text:s},{role:`assistant`,text:c}]),[C,w]=(0,r.useState)(``),[T,E]=(0,r.useState)(!1),[D,O]=(0,r.useState)(!0),[k,A]=(0,r.useState)(!0),[j,M]=(0,r.useState)(null),N=(0,r.useRef)(null),P=(0,r.useRef)(!1);(0,r.useEffect)(()=>{N.current&&(N.current.scrollTop=N.current.scrollHeight)},[x,T]);let F=(0,r.useCallback)(async()=>{if(m)return{jwt:``,conversationId:``};if(_&&y)return{jwt:_,conversationId:y};if(!e)throw Error(`no_agent`);let t=`prism-anon`,n=null;try{n=window.localStorage.getItem(t)}catch{n=null}let r=await fetch(`/api/v1/widget/init`,{method:`POST`,headers:{"Content-Type":`application/json`,Accept:`application/json`},credentials:`omit`,body:JSON.stringify({agent_id:e,page_url:window.location.href,page_title:document.title,referrer:document.referrer||null,anon_id:n})});if(!r.ok)throw Error(`init_${r.status}`);let i=await r.json();try{i.data.anonymous_id&&window.localStorage.setItem(t,i.data.anonymous_id)}catch{}return v(i.data.jwt),b(i.data.conversation_id),{jwt:i.data.jwt,conversationId:i.data.conversation_id}},[e,y,_,m]),I=(0,r.useRef)(x);(0,r.useEffect)(()=>{I.current=x},[x]);let L=(0,r.useCallback)(async(e,t,n)=>{let r=m?await fetch(`/api/v1/widget/try-now/stream`,{method:`POST`,headers:{"Content-Type":`application/json`,Accept:`text/event-stream`},body:JSON.stringify({token:m,message:n,history:I.current.slice(-10).map(e=>({role:e.role,text:e.text}))})}):await fetch(`/api/v1/widget/messages/stream`,{method:`POST`,headers:{"Content-Type":`application/json`,Accept:`text/event-stream`,Authorization:`Bearer ${e}`},body:JSON.stringify({message:n,page_context:{title:document.title,url:window.location.href}})});if(!r.ok||!r.body)throw Error(`stream_${r.status}`);let i=r.body.getReader(),a=new TextDecoder,s=``,c=``;S(e=>[...e,{role:`assistant`,text:``}]);let l=()=>{let e;for(;(e=s.indexOf(`

`))!==-1;){let t=s.slice(0,e);s=s.slice(e+2);let n=o(t);if(n){if(n.name===`token`){let e=typeof n.data?.t==`string`&&n.data.t||typeof n.data?.text==`string`&&n.data.text||``;e&&(c+=e,S(e=>{let t=[...e];return t[t.length-1]={role:`assistant`,text:c},t}))}if(n.name===`done`){let e=typeof n.data?.text==`string`&&n.data.text||``;e&&(c=e,S(e=>{let t=[...e];return t[t.length-1]={role:`assistant`,text:c},t}))}n.name===`error`&&(c=typeof n.data?.message==`string`&&n.data.message||`Stream error.`,S(e=>{let t=[...e];return t[t.length-1]={role:`assistant`,text:c},t}))}}};for(;;){let{value:e,done:t}=await i.read();if(t){s+=`

`,l();break}s+=a.decode(e,{stream:!0}),l()}},[m]),R=(0,r.useCallback)(async e=>{let t=(e??C).trim();if(!(!t||T)){M(null),O(!1),A(!1),S(e=>[...e,{role:`user`,text:t}]),w(``),E(!0);try{let e=await F();await L(e.jwt,e.conversationId,t)}catch{M(l),S(e=>[...e,{role:`assistant`,text:l}])}finally{E(!1)}}},[T,F,C,l,L]);return(0,r.useEffect)(()=>{m||!e||P.current||(P.current=!0,F().catch(()=>{}))},[e,F,m]),(0,r.useEffect)(()=>{if(p)return p.current={ask:e=>{R(e)}},()=>{p.current=null}},[p,R]),(0,i.jsxs)(`div`,{className:`chat`,role:`dialog`,"aria-label":`${t} conversation`,children:[(0,i.jsxs)(`div`,{className:`chat-hd`,children:[(0,i.jsxs)(`div`,{className:`chat-title`,children:[(0,i.jsx)(`span`,{className:`dot`}),` `,t]}),(0,i.jsx)(`span`,{className:`chat-live`,children:m?`Your site`:e?`Live`:`Preview`})]}),(0,i.jsxs)(`div`,{className:`chat-body`,ref:N,children:[x.map((e,t)=>(0,i.jsx)(`div`,{className:`bbl ${e.role===`user`?`bbl-u`:`bbl-b`}`,children:e.text||`…`},t)),T&&(0,i.jsx)(`div`,{className:`bbl bbl-b`,children:(0,i.jsxs)(`div`,{className:`typing`,children:[(0,i.jsx)(`span`,{}),(0,i.jsx)(`span`,{}),(0,i.jsx)(`span`,{})]})}),k&&d&&(0,i.jsxs)(`div`,{className:`intent-card`,children:[(0,i.jsxs)(`div`,{className:`intent-left`,children:[(0,i.jsx)(`span`,{className:`intent-ic`,children:(0,i.jsxs)(`svg`,{width:`14`,height:`14`,viewBox:`0 0 24 24`,fill:`none`,stroke:`currentColor`,strokeWidth:`2`,children:[(0,i.jsx)(`circle`,{cx:`12`,cy:`12`,r:`9`}),(0,i.jsx)(`circle`,{cx:`12`,cy:`12`,r:`3`})]})}),(0,i.jsxs)(`div`,{children:[(0,i.jsx)(`span`,{className:`intent-label`,children:`Detected intent:`}),(0,i.jsx)(`span`,{className:`intent-value`,children:d})]})]}),(0,i.jsxs)(`div`,{className:`intent-right`,children:[(0,i.jsxs)(`span`,{children:[(0,i.jsx)(`svg`,{width:`12`,height:`12`,viewBox:`0 0 24 24`,fill:`none`,stroke:`currentColor`,strokeWidth:`2.5`,children:(0,i.jsx)(`path`,{d:`M5 12l5 5L20 7`})}),`High intent`]}),(0,i.jsxs)(`span`,{children:[(0,i.jsx)(`svg`,{width:`12`,height:`12`,viewBox:`0 0 24 24`,fill:`none`,stroke:`currentColor`,strokeWidth:`2.5`,children:(0,i.jsx)(`path`,{d:`M5 12l5 5L20 7`})}),`Active 2m 18s`]})]})]}),D&&u&&(0,i.jsxs)(`div`,{className:`price-mini price-mini-h`,children:[(0,i.jsxs)(`div`,{className:`price-mini-left`,children:[(0,i.jsxs)(`p`,{className:`price-mini-desc`,children:[`The `,u.plan,` plan includes everything you need to scale your business.`]}),(0,i.jsx)(`ul`,{children:u.features.map(e=>(0,i.jsxs)(`li`,{children:[(0,i.jsxs)(`svg`,{width:`16`,height:`16`,viewBox:`0 0 24 24`,fill:`none`,stroke:`currentColor`,strokeWidth:`2.5`,children:[(0,i.jsx)(`circle`,{cx:`12`,cy:`12`,r:`11`,fill:`rgba(140,91,255,0.12)`,stroke:`none`}),(0,i.jsx)(`path`,{d:`M7 12l3 3 7-7`})]}),(0,i.jsx)(`span`,{children:e})]},e))})]}),(0,i.jsxs)(`div`,{className:`price-mini-right`,children:[(0,i.jsx)(`div`,{className:`plan`,children:u.plan}),(0,i.jsxs)(`div`,{className:`amt`,children:[u.price,(0,i.jsx)(`small`,{children:u.interval?`/${u.interval}`:``})]})]})]})]}),(()=>{let t=m&&g?g:a,n=T||!e&&!m;return t.length>0?(0,i.jsx)(`div`,{className:`chat-suggest`,children:t.slice(0,4).map(e=>(0,i.jsx)(`button`,{type:`button`,onClick:()=>R(e),disabled:n,children:e},e))}):null})(),(0,i.jsxs)(`form`,{className:`chat-form`,onSubmit:e=>{e.preventDefault(),R()},"aria-label":n,children:[(0,i.jsx)(`input`,{placeholder:n,value:C,onChange:e=>w(e.target.value),disabled:T||!e&&!m}),(0,i.jsx)(`button`,{type:`submit`,disabled:T||!e&&!m,"aria-label":`Send`,className:`chat-send`,children:(0,i.jsxs)(`svg`,{width:`16`,height:`16`,viewBox:`0 0 24 24`,fill:`none`,stroke:`currentColor`,strokeWidth:`2`,strokeLinecap:`round`,strokeLinejoin:`round`,children:[(0,i.jsx)(`path`,{d:`M22 2L11 13`}),(0,i.jsx)(`path`,{d:`M22 2l-7 20-4-9-9-4z`})]})})]}),j?(0,i.jsx)(`div`,{style:{padding:`8px 16px 12px`,fontSize:11,color:`var(--muted)`},children:j}):null]})}function o(e){let t=`message`,n=[];for(let r of e.split(`
`))r.startsWith(`event:`)?t=r.slice(6).trim():r.startsWith(`data:`)&&n.push(r.slice(5).trim());if(n.length===0)return{name:t,data:{}};let r=n.join(`
`);try{return{name:t,data:JSON.parse(r)}}catch{return{name:t,data:{text:r}}}}export{a as default};