import{u as B,d as N,r as t,e as s,D as R,f as G,g as H,B as g,S,T as b,C as M,h as V,i as W,k as z,l as U,P as u,n as Y,M as K,E as Q,V as X,o as Z,I as _,p as $,q as ss,s as O,t as y,v as ts}from"./index-_6SmYgWK.js";import{A as es}from"./AssistantDialog-CjH6GuIO.js";import{A as as}from"./assistant_empty-BAOmXGQM.js";import"./v4-CtRu48qb.js";const w=({show:h,dialogProps:a,onCancel:x,onAssistantSelected:C,setError:c})=>{const p=document.getElementById("portal"),n=B(N.getAllAvailableAssistants),[i,A]=t.useState(""),[m,f]=t.useState([]),[l,j]=t.useState("");t.useEffect(()=>()=>{A(""),f([]),j("")},[a]),t.useEffect(()=>{if(n.data&&n.data.length){const o=[];for(let d=0;d<n.data.length;d+=1)o.push({label:n.data[d].name,name:n.data[d].id,description:n.data[d].instructions});f(o)}},[n.data]),t.useEffect(()=>{n.error&&c&&c(n.error)},[n.error]);const I=h?s.jsxs(R,{fullWidth:!0,maxWidth:"xs",open:h,onClose:x,"aria-labelledby":"alert-dialog-title","aria-describedby":"alert-dialog-description",children:[s.jsx(G,{sx:{fontSize:"1rem"},id:"alert-dialog-title",children:a.title}),s.jsxs(H,{children:[s.jsxs(g,{sx:{p:2},children:[s.jsx(S,{sx:{position:"relative"},direction:"row",children:s.jsxs(b,{variant:"overline",children:["OpenAI Credential",s.jsx("span",{style:{color:"red"},children:" *"})]})}),s.jsx(M,{data:i?{credential:i}:{},inputParam:{label:"Connect Credential",name:"credential",type:"credential",credentialNames:["openAIApi"]},onSelect:o=>{A(o),o&&n.request(o)}},i)]}),i&&s.jsxs(g,{sx:{p:2},children:[s.jsx(S,{sx:{position:"relative"},direction:"row",children:s.jsxs(b,{variant:"overline",children:["Assistants",s.jsx("span",{style:{color:"red"},children:" *"})]})}),s.jsx(V,{name:l,options:m,onSelect:o=>j(o),value:l??"choose an option"})]})]}),l&&s.jsx(W,{children:s.jsx(z,{variant:"contained",onClick:()=>C(l,i),children:"Load"})})]}):null;return U.createPortal(I,p)};w.propTypes={show:u.bool,dialogProps:u.object,onCancel:u.func,onAssistantSelected:u.func,setError:u.func};const ls=()=>{var E;const h=Y(),a=B(N.getAllAssistants),[x,C]=t.useState(!0),[c,p]=t.useState(null),[n,i]=t.useState(!1),[A,m]=t.useState({}),[f,l]=t.useState(!1),[j,I]=t.useState({}),o=()=>{I({title:"Load Existing Assistant"}),l(!0)},[d,L]=t.useState(""),k=e=>{L(e.target.value)},T=(e,r)=>{l(!1),D(e,r)},D=(e,r)=>{m({title:"Add New Assistant",type:"ADD",cancelButtonName:"Cancel",confirmButtonName:"Add",selectedOpenAIAssistantId:e,credential:r}),i(!0)},q=e=>{m({title:"Edit Assistant",type:"EDIT",cancelButtonName:"Cancel",confirmButtonName:"Save",data:e}),i(!0)},F=()=>{i(!1),a.request("OPENAI")};function J(e){const r=JSON.parse(e.details);return r&&r.name&&r.name.toLowerCase().indexOf(d.toLowerCase())>-1}return t.useEffect(()=>{a.request("OPENAI")},[]),t.useEffect(()=>{C(a.loading)},[a.loading]),t.useEffect(()=>{a.error&&p(a.error)},[a.error]),s.jsxs(s.Fragment,{children:[s.jsx(K,{children:c?s.jsx(Q,{error:c}):s.jsxs(S,{flexDirection:"column",sx:{gap:3},children:[s.jsxs(X,{isBackButton:!0,onSearchChange:k,search:!0,searchPlaceholder:"Search Assistants",title:"OpenAI Assistant",description:"Create assistants using OpenAI Assistant API",onBack:()=>h(-1),children:[s.jsx(Z,{permissionId:"assistants:create",variant:"outlined",onClick:o,startIcon:s.jsx(_,{}),sx:{borderRadius:2,height:40},children:"Load"}),s.jsx($,{permissionId:"assistants:create",variant:"contained",sx:{borderRadius:2,height:40},onClick:D,startIcon:s.jsx(ss,{}),children:"Add"})]}),x?s.jsxs(g,{display:"grid",gridTemplateColumns:"repeat(3, 1fr)",gap:O,children:[s.jsx(y,{variant:"rounded",height:160}),s.jsx(y,{variant:"rounded",height:160}),s.jsx(y,{variant:"rounded",height:160})]}):s.jsx(g,{display:"grid",gridTemplateColumns:"repeat(3, 1fr)",gap:O,children:a.data&&((E=a.data)==null?void 0:E.filter(J).map((e,r)=>{var v,P;return s.jsx(ts,{data:{name:(v=JSON.parse(e.details))==null?void 0:v.name,description:(P=JSON.parse(e.details))==null?void 0:P.instructions,iconSrc:e.iconSrc},onClick:()=>q(e)},r)}))}),!x&&(!a.data||a.data.length===0)&&s.jsxs(S,{sx:{alignItems:"center",justifyContent:"center"},flexDirection:"column",children:[s.jsx(g,{sx:{p:2,height:"auto"},children:s.jsx("img",{style:{objectFit:"cover",height:"20vh",width:"auto"},src:as,alt:"AssistantEmptySVG"})}),s.jsx("div",{children:"No OpenAI Assistants Added Yet"})]})]})}),s.jsx(w,{show:f,dialogProps:j,onCancel:()=>l(!1),onAssistantSelected:T,setError:p}),s.jsx(es,{show:n,dialogProps:A,onCancel:()=>i(!1),onConfirm:F,setError:p})]})};export{ls as default};
