// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-3.0-or-later
// @source: https://gitlab.com/soapbox-pub/ditto/-/tree/f0a3877c
import{r as e}from"./rolldown-runtime-QTnfLwEv.js";import{qi as t,yt as n}from"./lucide-icons-BdRF0moN.js";import{t as r}from"./jsx-runtime-By8HlURe.js";import{t as i}from"./useQuery-rCAlChE8.js";import{o as a,r as o}from"./react-intl-eZirkCBg.js";import{t as s}from"./useCurrentUser-C6OJJlWo.js";import{t as c}from"./utils-BFSTDHdM.js";import{n as l}from"./chunk-62JRHF6Z-yJW_vgm4.js";import{t as u}from"./scroll-area-D2CmkWvj.js";import{n as d,r as f,t as p}from"./useShakespeare-MJHr565q.js";var m=e(t(),1),h=r(),g=new Map;function _(){let e=a(),{user:t}=s(),{sendStreamingMessage:r,getAvailableModels:c,isLoading:_,isAuthenticated:y}=p(),b=d(),{data:x}=i({queryKey:[`shakespeare-default-model`],queryFn:async()=>(await c()).data.sort((e,t)=>parseFloat(e.pricing.prompt)+parseFloat(e.pricing.completion)-(parseFloat(t.pricing.prompt)+parseFloat(t.pricing.completion)))[0]?.id??``,staleTime:10*6e4,enabled:!!t}),S=t?.pubkey??``,[C,w]=(0,m.useState)(()=>g.get(S)??[]),[T,E]=(0,m.useState)(``),[D,O]=(0,m.useState)(``);(0,m.useEffect)(()=>{S&&g.set(S,C)},[C,S]);let k=(0,m.useRef)(null),A=(0,m.useRef)(null),j=(0,m.useCallback)(()=>{let e=k.current?.querySelector(`[data-radix-scroll-area-viewport]`);e&&(e.scrollTop=e.scrollHeight)},[]);(0,m.useEffect)(()=>{j()},[C,D,j]);let M=(0,m.useCallback)(async()=>{let t=T.trim();if(!t||_)return;let n={role:`user`,content:t},i=[...C,n];w(i),E(``),O(``);try{let e=``;await r(i,x||`shakespeare`,t=>{e+=t,O(e)}),w(t=>[...t,{role:`assistant`,content:e}]),O(``)}catch{w(t=>[...t,{role:`assistant`,content:e.formatMessage({id:`widgets.aiChat.errorMessage`,defaultMessage:`Sorry, something went wrong. Please try again.`})}]),O(``)}},[T,_,C,r,x,e]);return!t||!y?(0,h.jsxs)(`div`,{className:`flex flex-col items-center gap-3 py-6 px-3 text-center`,children:[(0,h.jsx)(`pre`,{className:`text-xl font-mono text-primary leading-none`,children:`<[o_o]>`}),(0,h.jsx)(`p`,{className:`text-xs text-muted-foreground`,children:(0,h.jsx)(o,{id:`widgets.aiChat.loginPrompt`,defaultMessage:`Log in to chat with Dork`})})]}):b===!1?(0,h.jsxs)(`div`,{className:`flex flex-col items-center gap-3 py-6 px-3 text-center`,children:[(0,h.jsx)(`pre`,{className:`text-xl font-mono text-primary leading-none`,children:`<[o_o]>`}),(0,h.jsx)(`p`,{className:`text-xs text-muted-foreground leading-relaxed`,children:(0,h.jsx)(o,{id:`widgets.aiChat.credits`,defaultMessage:`Grab some credits on <link>Shakespeare</link> to chat with Dork.`,values:{link:e=>(0,h.jsx)(`a`,{href:`https://shakespeare.diy`,target:`_blank`,rel:`noopener noreferrer`,className:`text-primary hover:underline`,children:e})}})}),(0,h.jsx)(l,{to:`/ai-chat`,className:`text-xs font-medium text-primary hover:underline`,children:(0,h.jsx)(o,{id:`widgets.aiChat.open`,defaultMessage:`Open AI Chat`})})]}):(0,h.jsxs)(`div`,{className:`flex flex-col h-full`,children:[(0,h.jsx)(u,{ref:k,className:`flex-1 min-h-0`,children:(0,h.jsxs)(`div`,{className:`space-y-3 p-2`,children:[C.length===0&&!D&&(0,h.jsxs)(`div`,{className:`flex flex-col items-center gap-3 py-6 text-center`,children:[(0,h.jsx)(`pre`,{className:`text-xl font-mono text-primary leading-none`,children:`<[o_o]>`}),(0,h.jsx)(`p`,{className:`text-xs text-muted-foreground`,children:(0,h.jsx)(o,{id:`widgets.aiChat.emptyPrompt`,defaultMessage:`Ask me anything...`})})]}),C.map((e,t)=>(0,h.jsx)(v,{message:e},t)),D&&(0,h.jsx)(v,{message:{role:`assistant`,content:D}}),_&&!D&&(0,h.jsx)(`div`,{className:`flex gap-2 items-start`,children:(0,h.jsx)(`div`,{className:`bg-secondary rounded-xl rounded-tl-sm px-3 py-2`,children:(0,h.jsx)(f,{className:`text-[10px]`})})})]})}),(0,h.jsx)(`div`,{className:`border-t border-border p-2 space-y-1.5`,children:(0,h.jsxs)(`div`,{className:`flex items-end gap-1.5`,children:[(0,h.jsx)(`textarea`,{ref:A,value:T,onChange:e=>E(e.target.value),onKeyDown:e=>{e.key===`Enter`&&!e.shiftKey&&(e.preventDefault(),M())},placeholder:e.formatMessage({id:`widgets.aiChat.messagePlaceholder`,defaultMessage:`Message...`}),rows:1,className:`flex-1 resize-none text-sm bg-secondary/50 rounded-lg px-2.5 py-1.5 border-0 outline-none focus:ring-1 focus:ring-primary/30 placeholder:text-muted-foreground/60 min-h-[32px] max-h-[80px]`}),(0,h.jsx)(`button`,{onClick:M,disabled:!T.trim()||_||!x,className:`shrink-0 p-1.5 rounded-lg text-primary hover:bg-primary/10 disabled:opacity-30 disabled:cursor-not-allowed transition-colors`,children:(0,h.jsx)(n,{className:`size-4`})})]})})]})}function v({message:e}){let t=e.role===`user`,n=typeof e.content==`string`?e.content:e.content.map(e=>e.text??``).join(``);return(0,h.jsx)(`div`,{className:c(`flex`,t?`justify-end`:`justify-start`),children:(0,h.jsx)(`div`,{className:c(`max-w-[85%] rounded-xl px-3 py-2 text-xs leading-relaxed whitespace-pre-wrap break-words`,t?`bg-primary text-primary-foreground rounded-br-sm`:`bg-secondary text-foreground rounded-bl-sm`),children:n})})}export{_ as AIChatWidget};
// @license-end
