@adens/wanie 1.2.4 → 1.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/web/.next/BUILD_ID +1 -1
- package/web/.next/build-manifest.json +3 -3
- package/web/.next/required-server-files.json +2 -2
- package/web/.next/server/middleware-build-manifest.js +1 -1
- package/web/.next/server/pages/404.html +1 -1
- package/web/.next/server/pages/500.html +1 -1
- package/web/.next/server/pages/crm.html +1 -1
- package/web/.next/server/pages/dashboard.html +1 -1
- package/web/.next/server/pages/index.html +1 -1
- package/web/.next/static/{QQ1ysyWPzWQomE__NhSkk → GT6CVS519M7idO182p2AN}/_buildManifest.js +1 -1
- package/web/.next/static/chunks/pages/{dashboard-59b9d8a866dc0f3a.js → dashboard-dcfa88a488dcbd51.js} +1 -1
- package/web/.next/trace +2 -2
- /package/web/.next/static/{QQ1ysyWPzWQomE__NhSkk → GT6CVS519M7idO182p2AN}/_ssgManifest.js +0 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[724],{6998:(e,t,a)=>{(window.__NEXT_P=window.__NEXT_P||[]).push(["/dashboard",function(){return a(9042)}])},9042:(e,t,a)=>{"use strict";a.r(t),a.d(t,{default:()=>X});var s=a(6514),n=a(5834),i=a(6225),l=a(2025),r=a(2309),o=a(644),d=a(848);function c(e){var t,a,i,l,c,x;let{open:m,chat:u,onClose:h}=e,p=(0,o.C)(e=>e.token),[f,g]=(0,n.useState)(""),[b,w]=(0,n.useState)(""),[y,v]=(0,n.useState)(""),[j,N]=(0,n.useState)(!1),[k,C]=(0,n.useState)(!1),[S,A]=(0,n.useState)(""),T=(0,n.useRef)(null);(0,n.useEffect)(()=>{var e,t,a;m&&(g((null==u||null==(e=u.contact)?void 0:e.displayName)||""),w((null==u||null==(t=u.contact)?void 0:t.avatarUrl)||""),v((null==u||null==(a=u.contact)?void 0:a.persona)||""),A(""))},[m,u]);let E=String((null==u||null==(t=u.contact)?void 0:t.externalId)||""),P=E.startsWith("openwa:assistant"),I="openwa:assistant"===E;async function R(e){var t,a;let s=null==(t=e.target.files)?void 0:t[0];if(s){if(!p)return alert("Authentication required.");A(s.name),C(!0);try{let e=new FormData;e.append("file",s);let t=await (0,r.n)("/api/media",{method:"POST",token:p,formData:e}),n="".concat((0,r.h)(),"/").concat(t.mediaFile.relativePath);if(P)if(I){let e=(await (0,r.n)("/api/assistant",{method:"PUT",token:p,body:{avatarUrl:n}})).assistant||{};w(e.avatarUrl||n);let t=(o.C.getState().chats||[]).map(t=>{var a;return(null==(a=t.contact)?void 0:a.externalId)==="openwa:assistant"?{...t,contact:{...t.contact,avatarUrl:e.avatarUrl||n}}:t});o.C.setState({chats:t})}else{let e=(await (0,r.n)("/api/contacts/".concat(u.contact.id),{method:"PUT",token:p,body:{avatarUrl:n}})).chat;if(e&&e.id){let t=(o.C.getState().chats||[]).map(t=>t.id===e.id?e:t);o.C.setState({chats:t}),w((null==(a=e.contact)?void 0:a.avatarUrl)||n)}else w(n)}else w(n)}catch(e){alert(e.message||"Failed to upload avatar")}finally{C(!1),T.current&&(T.current.value="")}}}async function O(e){if(e.preventDefault(),!P)return h();if(!p)return alert("Authentication required.");N(!0);try{let e={displayName:f,persona:y,avatarUrl:b};if(I){let t=(await (0,r.n)("/api/assistant",{method:"PUT",token:p,body:e})).assistant||{},a=(o.C.getState().chats||[]).map(e=>{var a;return(null==(a=e.contact)?void 0:a.externalId)==="openwa:assistant"?{...e,contact:{...e.contact,displayName:t.displayName,avatarUrl:t.avatarUrl,persona:t.persona}}:e});o.C.setState({chats:a}),h()}else{let t=(await (0,r.n)("/api/contacts/".concat(u.contact.id),{method:"PUT",token:p,body:e})).chat;if(t&&t.id){let e=(o.C.getState().chats||[]).map(e=>e.id===t.id?t:e);o.C.setState({chats:e})}h()}}catch(e){alert(e.message||"Failed to save assistant")}finally{N(!1)}}return m?(0,s.jsx)("div",{className:"fixed inset-0 z-50 flex items-center justify-center bg-black/70 px-4 py-6 backdrop-blur-sm",children:(0,s.jsxs)("div",{className:"flex max-h-[calc(100vh-3rem)] w-full max-w-md flex-col overflow-hidden rounded-[20px] border border-white/10 bg-[#161717] shadow-[0_28px_90px_rgba(0,0,0,0.45)]",children:[(0,s.jsxs)("div",{className:"flex shrink-0 items-center justify-between border-b border-white/10 bg-[#161717]/95 px-5 py-4 backdrop-blur-sm",children:[(0,s.jsx)("h3",{className:"text-lg font-semibold text-white",children:"Profile"}),(0,s.jsx)("button",{type:"button",className:"rounded-full bg-[#2e2f2f] px-3 py-1 text-sm text-white/70",onClick:h,children:"Close"})]}),(0,s.jsxs)("div",{className:"min-h-0 flex-1 overflow-y-auto px-5 py-4",children:[(0,s.jsxs)("div",{className:"flex items-center gap-3",children:[(0,s.jsx)("div",{className:"h-16 w-16 shrink-0 rounded-2xl overflow-hidden bg-[#2e2f2f]",children:b?(0,s.jsx)("img",{src:b,alt:f,className:"h-full w-full object-cover"}):(0,s.jsx)("div",{className:"flex h-full w-full items-center justify-center text-white",children:(f||"?").slice(0,2).toUpperCase()})}),(0,s.jsxs)("div",{className:"flex-1",children:[(0,s.jsx)("p",{className:"text-sm text-white/60",children:null==u||null==(a=u.contact)?void 0:a.externalId}),(0,s.jsx)("p",{className:"mt-1 font-medium text-white",children:null==u||null==(i=u.contact)?void 0:i.displayName})]})]}),(0,s.jsx)("div",{className:"mt-4",children:P?(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)("label",{className:"block text-xs text-white/50",children:"Display name"}),(0,s.jsx)("input",{className:"mt-1 w-full rounded-[10px] bg-[#2e2f2f] px-3 py-2 text-sm text-white outline-none",value:f,onChange:e=>g(e.target.value)}),(0,s.jsx)("label",{className:"mt-3 block text-xs text-white/50",children:"Avatar"}),(0,s.jsxs)("div",{className:"mt-2 rounded-[18px] border border-white/10 bg-[linear-gradient(135deg,rgba(255,255,255,0.06),rgba(255,255,255,0.02))] p-3",children:[(0,s.jsxs)("div",{className:"flex items-center gap-4",children:[(0,s.jsxs)("div",{className:"relative h-20 w-20 shrink-0 overflow-hidden rounded-[20px] border border-white/10 bg-[#2e2f2f] shadow-[0_12px_30px_rgba(0,0,0,0.25)]",children:[b?(0,s.jsx)("img",{src:b,alt:f,className:"h-full w-full object-cover"}):(0,s.jsx)("div",{className:"flex h-full w-full items-center justify-center text-white/70",children:(0,s.jsx)(d.TbK,{className:"h-8 w-8"})}),(0,s.jsx)("div",{className:"absolute inset-x-0 bottom-0 bg-black/55 px-2 py-1 text-center text-[10px] font-medium uppercase tracking-[0.18em] text-white/80",children:"Preview"})]}),(0,s.jsxs)("div",{className:"min-w-0 flex-1",children:[(0,s.jsx)("p",{className:"text-sm font-medium text-white",children:"Update avatar image"}),(0,s.jsx)("p",{className:"mt-1 text-xs leading-5 text-white/50",children:"Pilih gambar persegi agar hasil avatar lebih rapi. Format umum seperti JPG, PNG, dan WEBP didukung."}),(0,s.jsxs)("div",{className:"mt-3 flex flex-wrap items-center gap-2",children:[(0,s.jsxs)("button",{type:"button",className:"inline-flex items-center gap-2 rounded-full bg-white px-4 py-2 text-sm font-semibold text-[#111b21] transition hover:bg-white/90 disabled:cursor-not-allowed disabled:opacity-60",onClick:()=>{var e;return null==(e=T.current)?void 0:e.click()},disabled:k||j,children:[(0,s.jsx)(d.E10,{className:"h-4 w-4"}),k?"Uploading...":"Choose Image"]}),S?(0,s.jsxs)("div",{className:"inline-flex max-w-full items-center gap-2 rounded-full border border-emerald-400/20 bg-emerald-400/10 px-3 py-2 text-xs text-emerald-100",children:[(0,s.jsx)(d.mU9,{className:"h-4 w-4 shrink-0"}),(0,s.jsx)("span",{className:"truncate",children:S})]}):null]})]})]}),(0,s.jsx)("input",{ref:T,type:"file",accept:"image/*",onChange:R,className:"hidden"}),(0,s.jsx)("div",{className:"mt-3 rounded-2xl border border-dashed border-white/10 bg-black/20 px-3 py-3",children:(0,s.jsxs)("div",{className:"flex items-center justify-between gap-3",children:[(0,s.jsxs)("div",{className:"min-w-0",children:[(0,s.jsx)("p",{className:"truncate text-sm font-medium text-white/85",children:k?"Uploading avatar image":S||"Belum ada file baru yang dipilih"}),(0,s.jsx)("p",{className:"mt-1 text-xs text-white/45",children:k?"Avatar akan diperbarui otomatis setelah upload selesai.":b?"Avatar saat ini tetap dipakai sampai Anda memilih gambar baru.":"Klik tombol di atas untuk memilih gambar avatar."})]}),(0,s.jsx)("div",{className:"rounded-full bg-white/5 p-2 text-white/55",children:(0,s.jsx)(d.TbK,{className:"h-5 w-5"})})]})})]}),(0,s.jsx)("label",{className:"mt-3 block text-xs text-white/50",children:"Persona (system prompt)"}),(0,s.jsx)("textarea",{className:"mt-1 w-full rounded-[10px] bg-[#2e2f2f] px-3 py-2 text-sm text-white outline-none",rows:4,value:y,onChange:e=>v(e.target.value)})]}):(0,s.jsxs)("div",{className:"mt-3 space-y-2 text-sm text-white/50",children:[(0,s.jsxs)("p",{children:[(0,s.jsx)("strong",{children:"Name:"})," ",null==u||null==(l=u.contact)?void 0:l.displayName]}),(0,s.jsxs)("p",{children:[(0,s.jsx)("strong",{children:"External ID:"})," ",null==u||null==(c=u.contact)?void 0:c.externalId]}),(null==u||null==(x=u.contact)?void 0:x.lastMessageAt)?(0,s.jsxs)("p",{children:[(0,s.jsx)("strong",{children:"Last message:"})," ",new Date(u.contact.lastMessageAt).toLocaleString()]}):null]})})]}),(0,s.jsxs)("div",{className:"flex shrink-0 items-center justify-end gap-3 border-t border-white/10 bg-[#161717]/95 px-5 py-4 backdrop-blur-sm",children:[(0,s.jsx)("button",{type:"button",className:"rounded-2xl bg-white/5 px-4 py-2 text-sm font-semibold text-white/75 transition hover:bg-white/10",onClick:h,children:"Close"}),P?(0,s.jsx)("button",{type:"button",className:"rounded-2xl bg-brand-500 px-4 py-2 text-sm font-semibold text-[#10251a]",onClick:O,disabled:j,children:j?"Saving...":"Save"}):null]})]})}):null}var x=a(8944),m=a(5642),u=a(2493),h=a(4982);function p(e){let{inline:t,className:a,children:i,...l}=e,[r,o]=(0,n.useState)(!1),d=(function e(t){return"string"==typeof t||"number"==typeof t?String(t):Array.isArray(t)?t.map(e).join(""):t&&"object"==typeof t&&t.props&&t.props.children?e(t.props.children):""})(i).replace(/\n$/,""),c=String(a||"").replace(/^language-/,"").trim(),x=async()=>{if(d)try{var e;if("undefined"!=typeof navigator&&(null==(e=navigator.clipboard)?void 0:e.writeText))await navigator.clipboard.writeText(d);else{if("undefined"==typeof document)return;let e=document.createElement("textarea");e.value=d,e.setAttribute("readonly",""),e.style.position="absolute",e.style.left="-9999px",document.body.appendChild(e),e.select(),document.execCommand("copy"),document.body.removeChild(e)}o(!0),window.setTimeout(()=>o(!1),1600)}catch(e){o(!1)}};return t?(0,s.jsx)("code",{className:"rounded-md bg-black/30 px-1.5 py-0.5 font-mono text-[0.92em] text-emerald-100",...l,children:d}):(0,s.jsxs)("div",{className:"relative my-3 overflow-hidden rounded-2xl bg-black/35",children:[(0,s.jsxs)("div",{className:"flex items-center justify-between gap-3 border-b border-white/8 px-3 py-2",children:[(0,s.jsx)("span",{className:"min-w-0 truncate font-mono text-[11px] uppercase tracking-[0.18em] text-white/45",children:c||"code"}),(0,s.jsx)("button",{type:"button",className:"rounded-full border border-white/10 bg-white/5 px-2.5 py-1 text-[11px] font-medium text-white/75 transition hover:bg-white/10 hover:text-white",onClick:x,children:r?"Copied":"Copy"})]}),(0,s.jsx)("pre",{className:"overflow-x-auto p-3 text-xs leading-6 text-emerald-50",children:(0,s.jsx)("code",{className:a,...l,children:i})})]})}function f(e){let{content:t}=e;return(0,s.jsx)("div",{className:"message-markdown text-sm leading-6 text-white/88",children:(0,s.jsx)(m.oz,{rehypePlugins:[[u.A,{detect:!1,ignoreMissing:!0}]],remarkPlugins:[h.A],components:{code:p,a:e=>{let{href:t,children:a,...n}=e;return(0,s.jsx)("a",{href:t,target:"_blank",rel:"noreferrer",className:"text-emerald-200 underline decoration-emerald-300/40 underline-offset-4 transition hover:text-emerald-100",...n,children:a})},table:e=>{let{children:t,...a}=e;return(0,s.jsx)("div",{className:"overflow-x-auto",children:(0,s.jsx)("table",{className:"min-w-full border-collapse",...a,children:t})})}},children:String(t||"")})})}function g(e){return e?new Date(e).toLocaleString():"-"}function b(e){var t,a,i,l;let{terminalId:d,fallbackBody:c,onReplaceTerminalId:x}=e,m=(0,o.C)(e=>e.token),u=(0,o.C)(e=>e.terminalRecordsById[d]),h=(0,o.C)(e=>e.upsertTerminalRecord),[p,f]=(0,n.useState)(!1),[b,w]=(0,n.useState)(!1);(0,n.useEffect)(()=>{let e=!0;if(d&&m&&!u)return(async()=>{f(!0);try{let t=await (0,r.n)("/api/terminal/".concat(d),{token:m});e&&(null==t?void 0:t.item)&&h(t.item)}catch(e){}finally{e&&f(!1)}})(),()=>{e=!1}},[d,m,u,h]);let y=(null==u?void 0:u.status)||(p?"loading":"unknown"),v=(0,n.useMemo)(()=>"completed"===y?"bg-emerald-500/15 text-emerald-200 border-emerald-400/20":"failed"===y?"bg-red-500/15 text-red-200 border-red-400/20":"pending"===y?"bg-amber-500/15 text-amber-200 border-amber-400/20":"running"===y?"bg-sky-500/15 text-sky-200 border-sky-400/20":"denied"===y?"bg-zinc-500/20 text-zinc-200 border-white/10":"bg-white/10 text-white/75 border-white/10",[y]),j=async e=>{let t=e||d;if(!t||!m)return null;let a=await (0,r.n)("/api/terminal/".concat(t),{token:m});return(null==a?void 0:a.item)?(h(a.item),a.item):null},N=async()=>{if(d){w(!0);try{await (0,r.n)("/api/terminal/".concat(d,"/approve"),{method:"POST",token:m}),await j(d)}finally{w(!1)}}},k=async()=>{if(d){w(!0);try{await (0,r.n)("/api/terminal/".concat(d,"/deny"),{method:"POST",token:m}),await j(d)}finally{w(!1)}}},C=async()=>{if(d){w(!0);try{let e=await (0,r.n)("/api/terminal/".concat(d,"/rerun"),{method:"POST",token:m});(null==e?void 0:e.id)&&(await j(e.id),"function"==typeof x&&x(e.id))}finally{w(!1)}}};return(0,s.jsxs)("div",{className:"mb-2 rounded-2xl border border-white/10 bg-black/20 p-3",children:[(0,s.jsxs)("div",{className:"flex items-start justify-between gap-3",children:[(0,s.jsxs)("div",{className:"min-w-0 flex-1",children:[(0,s.jsx)("p",{className:"text-xs uppercase tracking-[0.2em] text-white/35",children:"Terminal"}),(0,s.jsx)("p",{className:"mt-1 break-words text-sm font-medium text-white/90",children:(null==u?void 0:u.command)||c||"Terminal command"})]}),(0,s.jsx)("span",{className:"rounded-full border px-2.5 py-1 text-[11px] font-medium capitalize ".concat(v),children:y})]}),(0,s.jsxs)("div",{className:"mt-3 grid gap-2 text-xs text-white/45",children:[(0,s.jsxs)("div",{children:["Requested: ",g(null==u?void 0:u.requestedAt)]}),(null==u?void 0:u.executedAt)?(0,s.jsxs)("div",{children:["Executed: ",g(u.executedAt)]}):null]}),(null==u||null==(t=u.result)?void 0:t.stdout)||(null==u||null==(a=u.result)?void 0:a.stderr)?(0,s.jsxs)("div",{className:"mt-3 space-y-2",children:[(null==u||null==(i=u.result)?void 0:i.stdout)?(0,s.jsx)("pre",{className:"max-h-28 overflow-auto rounded-xl bg-black/35 p-3 text-xs text-emerald-100/90",children:u.result.stdout}):null,(null==u||null==(l=u.result)?void 0:l.stderr)?(0,s.jsx)("pre",{className:"max-h-28 overflow-auto rounded-xl bg-black/35 p-3 text-xs text-red-100/90",children:u.result.stderr}):null]}):null,(0,s.jsxs)("div",{className:"mt-3 flex flex-wrap items-center justify-end gap-2",children:[(0,s.jsx)("button",{type:"button",className:"rounded-full bg-yellow-600 px-3 py-1.5 text-xs font-semibold text-white disabled:opacity-50",onClick:C,disabled:b||p,children:"Rerun"}),"pending"===y?(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)("button",{type:"button",className:"rounded-full bg-emerald-600 px-3 py-1.5 text-xs font-semibold text-white disabled:opacity-50",onClick:N,disabled:b||p,children:"Approve"}),(0,s.jsx)("button",{type:"button",className:"rounded-full bg-red-600 px-3 py-1.5 text-xs font-semibold text-white disabled:opacity-50",onClick:k,disabled:b||p,children:"Deny"})]}):null]})]})}function w(e){let{isOpen:t,onClose:a,message:i,onReply:l,onDelete:r,onForward:o,isOutbound:c,triggerRef:x}=e,m=(0,n.useRef)(null),[u,h]=(0,n.useState)({top:0,left:0,transformOrigin:"top-right"});return((0,n.useEffect)(()=>{function e(e){m.current&&!m.current.contains(e.target)&&(null==x?void 0:x.current)&&!x.current.contains(e.target)&&a()}if(t)return document.addEventListener("mousedown",e),()=>document.removeEventListener("mousedown",e)},[t,a,x]),(0,n.useEffect)(()=>{if(!t||!(null==x?void 0:x.current)||!(null==m?void 0:m.current))return;let e=()=>{let e=x.current,t=m.current,a=e.getBoundingClientRect(),s=t.getBoundingClientRect(),n=window.innerWidth,i=window.innerHeight,l=s.height||180,r=0,o="top-right",d=n-a.right,c=a.left;d>=232?(r=a.width+8,o="top-left"):c>=232?(r=-232,o="top-right"):(r=a.width+8,o="top-left");let u=i-a.bottom,p=a.top;h({top:u>=l+8?a.height+8:p>=l+8?-(l+8):a.height+8,left:r,transformOrigin:o})};return e(),window.addEventListener("resize",e),()=>window.removeEventListener("resize",e)},[t,x]),t)?(0,s.jsxs)("div",{ref:m,style:{position:"absolute",top:"".concat(u.top,"px"),left:"".concat(u.left,"px"),transformOrigin:u.transformOrigin},className:"z-50 w-56 rounded-2xl border border-white/10 bg-[#1a1b1b] shadow-[0_16px_32px_rgba(0,0,0,0.4)]",children:[(0,s.jsxs)("button",{type:"button",onClick:a,className:"flex w-full items-center gap-3 border-b border-white/10 px-4 py-3 text-sm font-medium text-white transition hover:bg-white/5",children:[(0,s.jsx)("span",{children:"←"}),(0,s.jsx)("span",{children:"Back"})]}),(0,s.jsxs)("div",{className:"space-y-0",children:[(0,s.jsxs)("button",{type:"button",onClick:()=>{l(),a()},className:"flex w-full items-center gap-3 px-4 py-3 text-sm text-white/80 transition hover:bg-white/5 hover:text-white",children:[(0,s.jsx)("span",{children:(0,s.jsx)(d.EdT,{className:"w-4 h-4"})}),(0,s.jsx)("span",{children:"Reply"})]}),c&&(0,s.jsxs)("button",{type:"button",onClick:()=>{r(),a()},className:"flex w-full items-center gap-3 px-4 py-3 text-sm text-white/80 transition hover:bg-red-500/10 hover:text-red-300",children:[(0,s.jsx)("span",{children:(0,s.jsx)(d.b6i,{className:"w-4 h-4"})}),(0,s.jsx)("span",{children:"Delete"})]}),(0,s.jsxs)("button",{type:"button",onClick:()=>{o(),a()},className:"flex w-full items-center gap-3 px-4 py-3 text-sm text-white/80 transition hover:bg-white/5 hover:text-white",children:[(0,s.jsx)("span",{children:(0,s.jsx)(d.RZ7,{className:"w-4 h-4"})}),(0,s.jsx)("span",{children:"Forward"})]})]})]}):null}function y(e){var t;let{media:a,onClose:i}=e,[l,r]=(0,n.useState)(1);if((0,n.useEffect)(()=>{let e=e=>{"Escape"===e.key&&i(),("+"===e.key||"="===e.key)&&(e.preventDefault(),r(e=>Math.min(e+.2,3))),"-"===e.key&&(e.preventDefault(),r(e=>Math.max(e-.2,.5)))};return window.addEventListener("keydown",e),()=>window.removeEventListener("keydown",e)},[i]),!a)return null;let o=a.mediaUrl||(a.relativePath?"".concat(null==(t=getApiBaseUrl)?void 0:t(),"/")+a.relativePath:""),c=a.mimeType||"",x=a.originalName||"media",m=void 0!==a.isImage?a.isImage:c.startsWith("image/")&&"image/webp"!==c,u=async()=>{try{let e=await fetch(o),t=await e.blob(),a=window.URL.createObjectURL(t),s=document.createElement("a");s.href=a,s.download=x,document.body.appendChild(s),s.click(),document.body.removeChild(s),window.URL.revokeObjectURL(a)}catch(e){console.error("Download failed:",e),alert("Download failed: "+e.message)}};return(0,s.jsx)("div",{className:"fixed inset-0 z-50 flex items-center justify-center bg-black/80 backdrop-blur-sm",onClick:i,children:(0,s.jsxs)("div",{className:"relative max-h-[90vh] max-w-[90vw] rounded-2xl bg-black shadow-2xl",onClick:e=>e.stopPropagation(),children:[(0,s.jsx)("button",{type:"button",onClick:i,className:"absolute right-6 top-6 z-10 rounded-full bg-black/60 w-10 h-10 flex items-center justify-center transition hover:bg-black/80 text-white",title:"Close (Esc)",children:(0,s.jsx)(d.m6K,{className:"w-5 h-5"})}),(0,s.jsxs)("button",{type:"button",onClick:u,className:"absolute bottom-6 right-6 z-10 rounded-lg bg-black/60 px-4 py-2 text-sm font-medium text-white transition hover:bg-black/80 flex items-center gap-2",title:"Download",children:[(0,s.jsx)(d.E5v,{className:"w-4 h-4"}),"Download"]}),(0,s.jsx)("div",{className:"flex items-center justify-center overflow-auto p-4",children:m?(0,s.jsx)("img",{src:o,alt:x,className:"max-h-[calc(90vh-80px)] max-w-[calc(90vw-32px)] rounded-xl object-contain",style:{transform:"scale(".concat(l,")"),transition:"transform 0.2s ease-in-out"}}):c.startsWith("video/")?(0,s.jsx)("video",{controls:!0,autoPlay:!0,className:"max-h-[calc(90vh-80px)] max-w-[calc(90vw-32px)] rounded-xl",children:(0,s.jsx)("source",{src:o,type:c})}):c.startsWith("audio/")?(0,s.jsx)("audio",{controls:!0,autoPlay:!0,className:"w-full max-w-[500px]",children:(0,s.jsx)("source",{src:o,type:c})}):null}),m&&(0,s.jsxs)("div",{className:"absolute bottom-6 left-6 flex items-center gap-2 rounded-lg bg-black/60 px-3 py-2",children:[(0,s.jsx)("button",{type:"button",onClick:()=>r(e=>Math.max(e-.2,.5)),className:"rounded p-1 transition hover:bg-black/80 text-white flex items-center justify-center",title:"Zoom out",children:(0,s.jsx)(d.ak5,{className:"w-4 h-4"})}),(0,s.jsxs)("span",{className:"min-w-[40px] text-center text-sm text-white",children:[Math.round(100*l),"%"]}),(0,s.jsx)("button",{type:"button",onClick:()=>r(e=>Math.min(e+.2,3)),className:"rounded p-1 transition hover:bg-black/80 text-white flex items-center justify-center",title:"Zoom in",children:(0,s.jsx)(d.jgn,{className:"w-4 h-4"})}),(0,s.jsx)("div",{className:"h-4 w-px bg-white/20"}),(0,s.jsx)("button",{type:"button",onClick:()=>r(1),className:"px-2 py-1 text-xs transition hover:bg-black/80 text-white",title:"Reset zoom",children:"Reset"})]})]})})}let v={smileys:{label:"\uD83D\uDE0A",emojis:["\uD83D\uDE00","\uD83D\uDE03","\uD83D\uDE04","\uD83D\uDE01","\uD83D\uDE06","\uD83D\uDE05","\uD83E\uDD23","\uD83D\uDE02","\uD83D\uDE42","\uD83D\uDE43","\uD83D\uDE09","\uD83D\uDE0A","\uD83D\uDE07","\uD83E\uDD70","\uD83D\uDE0D","\uD83E\uDD29","\uD83D\uDE18","\uD83D\uDE17","\uD83D\uDE1A","\uD83D\uDE19","\uD83E\uDD72","\uD83D\uDE0B","\uD83D\uDE1B","\uD83D\uDE1C","\uD83E\uDD2A","\uD83D\uDE0C","\uD83D\uDE14","\uD83D\uDE11","\uD83D\uDE10","\uD83D\uDE10","\uD83D\uDE11","\uD83D\uDE12","\uD83D\uDE41","☹️","\uD83D\uDE32","\uD83D\uDE1E","\uD83D\uDE16","\uD83D\uDE22","\uD83D\uDE2D","\uD83D\uDE24","\uD83D\uDE20","\uD83D\uDE21","\uD83E\uDD2C","\uD83D\uDE08","\uD83D\uDC7F","\uD83D\uDC80","☠️","\uD83D\uDCA9","\uD83E\uDD21","\uD83D\uDC79","\uD83D\uDC7A","\uD83D\uDC7B","\uD83D\uDC7D","\uD83D\uDC7E","\uD83E\uDD16"]},gestures:{label:"\uD83D\uDC4B",emojis:["\uD83D\uDC4B","\uD83E\uDD1A","\uD83D\uDD90️","✋","\uD83D\uDD96","\uD83D\uDC4C","\uD83E\uDD0C","\uD83E\uDD0F","✌️","\uD83E\uDD1E","\uD83E\uDEF0","\uD83E\uDD1F","\uD83E\uDD18","\uD83E\uDD19","\uD83D\uDC4D","\uD83D\uDC4E","✊","\uD83D\uDC4A","\uD83E\uDD1B","\uD83E\uDD1C","\uD83D\uDC4F","\uD83D\uDE4C","\uD83D\uDC50","\uD83E\uDD32","\uD83E\uDD1D","\uD83E\uDD1C","\uD83E\uDD1B","\uD83E\uDEF1","\uD83E\uDEF2","\uD83D\uDC85","\uD83D\uDC42","\uD83D\uDC43","\uD83E\uDDE0","\uD83E\uDEC0","\uD83E\uDEC1","\uD83E\uDDB7","\uD83E\uDDB4","\uD83D\uDC40","\uD83D\uDC41️","\uD83D\uDC45","\uD83D\uDC44"]},hearts:{label:"❤️",emojis:["❤️","\uD83E\uDDE1","\uD83D\uDC9B","\uD83D\uDC9A","\uD83D\uDC99","\uD83D\uDC9C","\uD83D\uDDA4","\uD83E\uDD0D","\uD83E\uDD0E","\uD83D\uDC94","\uD83D\uDC95","\uD83D\uDC9E","\uD83D\uDC93","\uD83D\uDC97","\uD83D\uDC96","\uD83D\uDC98","\uD83D\uDC9D","\uD83D\uDC9F","\uD83D\uDC8C","\uD83D\uDCA4","\uD83D\uDC8B"]},hand:{label:"\uD83D\uDC4C",emojis:["\uD83D\uDC4C","\uD83E\uDD0C","\uD83E\uDD0F","✌️","\uD83E\uDD1E","\uD83E\uDEF0","\uD83E\uDD1F","\uD83E\uDD18","\uD83E\uDD19","\uD83D\uDC4D","\uD83D\uDC4E","✊","\uD83D\uDC4A","\uD83E\uDD1B","\uD83E\uDD1C","\uD83D\uDC4F","\uD83D\uDE4C","\uD83D\uDC50","\uD83E\uDD32","\uD83E\uDD1D"]},animals:{label:"\uD83D\uDC36",emojis:["\uD83D\uDC36","\uD83D\uDC31","\uD83D\uDC2D","\uD83D\uDC39","\uD83D\uDC30","\uD83E\uDD8A","\uD83D\uDC3B","\uD83D\uDC3C","\uD83D\uDC28","\uD83D\uDC2F","\uD83E\uDD81","\uD83D\uDC2E","\uD83D\uDC37","\uD83D\uDC38","\uD83D\uDC35","\uD83D\uDE48","\uD83D\uDE49","\uD83D\uDE4A","\uD83D\uDC12","\uD83D\uDC14","\uD83D\uDC27","\uD83D\uDC26","\uD83D\uDC24","\uD83D\uDC23","\uD83D\uDC25","\uD83E\uDD86","\uD83E\uDD85","\uD83E\uDD89","\uD83E\uDD87","\uD83D\uDC3A","\uD83D\uDC17","\uD83D\uDC34","\uD83E\uDD84","\uD83D\uDC1D","\uD83E\uDEB1","\uD83D\uDC1B","\uD83E\uDD8B","\uD83D\uDC0C","\uD83D\uDC1E","\uD83D\uDC1C","\uD83E\uDEB0","\uD83E\uDD9F"]},food:{label:"\uD83C\uDF4E",emojis:["\uD83C\uDF4E","\uD83C\uDF4A","\uD83C\uDF4B","\uD83C\uDF4C","\uD83C\uDF49","\uD83C\uDF47","\uD83C\uDF53","\uD83E\uDED0","\uD83C\uDF48","\uD83C\uDF52","\uD83C\uDF51","\uD83E\uDD6D","\uD83C\uDF4D","\uD83E\uDD65","\uD83E\uDD51","\uD83C\uDF46","\uD83C\uDF45","\uD83C\uDF36️","\uD83C\uDF3D","\uD83E\uDD52","\uD83E\uDD6C","\uD83E\uDD66","\uD83E\uDDC4","\uD83E\uDDC5","\uD83C\uDF44","\uD83E\uDD5C","\uD83C\uDF30","\uD83C\uDF5E","\uD83E\uDD50","\uD83E\uDD56","\uD83E\uDD68","\uD83E\uDD6F","\uD83E\uDD5E","\uD83E\uDDC7","\uD83E\uDD5A","\uD83C\uDF73","\uD83E\uDDC8","\uD83E\uDD5E","\uD83E\uDD53","\uD83E\uDD54","\uD83C\uDF5F","\uD83C\uDF57","\uD83C\uDF56","\uD83C\uDF2D","\uD83C\uDF54","\uD83C\uDF5F","\uD83C\uDF55"]},travel:{label:"\uD83C\uDFA1",emojis:["✈️","\uD83D\uDEEB","\uD83D\uDEEC","\uD83D\uDEE9️","\uD83D\uDCBA","\uD83D\uDEF0️","\uD83D\uDE81","\uD83D\uDE82","\uD83D\uDE83","\uD83D\uDE84","\uD83D\uDE85","\uD83D\uDE86","\uD83D\uDE87","\uD83D\uDE88","\uD83D\uDE89","\uD83D\uDE8A","\uD83D\uDE9D","\uD83D\uDE9E","\uD83D\uDE8B","\uD83D\uDE8C","\uD83D\uDE8D","\uD83D\uDE8E","\uD83D\uDE90","\uD83D\uDE91","\uD83D\uDE92","\uD83D\uDE93","\uD83D\uDE94","\uD83D\uDE95","\uD83D\uDE96","\uD83D\uDE97","\uD83D\uDE98","\uD83D\uDE99","\uD83D\uDE9A","\uD83D\uDE9B","\uD83D\uDE9C","\uD83C\uDFCE️","\uD83C\uDFCD️","\uD83D\uDEF5","\uD83E\uDDAF","\uD83E\uDDBD","\uD83E\uDDBC","\uD83D\uDEFA","\uD83D\uDEB2","\uD83D\uDEF4","\uD83D\uDEF9","\uD83D\uDEFC","\uD83D\uDEDE","\uD83D\uDE8F","⛽"]},activities:{label:"⚽",emojis:["⚽","⚾","\uD83E\uDD4E","\uD83C\uDFBE","\uD83C\uDFC0","\uD83C\uDFD0","\uD83C\uDFC8","\uD83C\uDFC9","\uD83E\uDD4F","\uD83C\uDFB3","\uD83C\uDFD3","\uD83C\uDFF8","\uD83C\uDFD2","\uD83C\uDFD1","\uD83E\uDD4D","\uD83C\uDFCF","\uD83E\uDD45","⛳","⛸️","\uD83C\uDFA3","\uD83C\uDFBD","\uD83C\uDFBF","⛷️","\uD83C\uDFC2","\uD83E\uDE82","\uD83E\uDD3C","\uD83E\uDD38","⛹️","\uD83E\uDD3A","\uD83E\uDD3E","\uD83C\uDFCC️","\uD83C\uDFC7","\uD83E\uDDD8","\uD83C\uDFC4","\uD83C\uDFCA","\uD83E\uDD3D","\uD83D\uDEA3","\uD83E\uDDD7","\uD83D\uDEB4","\uD83D\uDEB5","\uD83E\uDD39","\uD83C\uDFAA"]}};function j(e){let{isOpen:t,onClose:a,onEmojiSelect:i,triggerRef:l}=e,r=(0,n.useRef)(null),o=(0,n.useRef)(null);return((0,n.useEffect)(()=>{function e(e){r.current&&!r.current.contains(e.target)&&(null==l?void 0:l.current)&&!l.current.contains(e.target)&&a()}if(t)return document.addEventListener("mousedown",e),()=>document.removeEventListener("mousedown",e)},[t,a,l]),t)?(0,s.jsxs)("div",{ref:r,className:"relative mb-2 w-80 rounded-2xl border border-white/10 bg-[#1a1b1b] shadow-[0_16px_32px_rgba(0,0,0,0.4)]",children:[(0,s.jsxs)("div",{className:"flex h-12 items-center border-b border-white/10 px-3",children:[(0,s.jsx)("h3",{className:"flex-1 text-sm font-semibold text-white",children:"Emoji"}),(0,s.jsx)("button",{type:"button",onClick:a,className:"flex h-6 w-6 items-center justify-center rounded-full text-white/60 transition hover:bg-white/10 hover:text-white",title:"Close emoji picker",children:(0,s.jsx)(d.m6K,{className:"w-4 h-4"})})]}),(0,s.jsx)("div",{ref:o,className:"max-h-64 overflow-y-auto p-3",children:(0,s.jsx)("div",{className:"space-y-3",children:Object.entries(v).map(e=>{let[t,n]=e;return(0,s.jsxs)("div",{children:[(0,s.jsx)("div",{className:"mb-2 text-xs font-semibold text-white/50",children:n.label}),(0,s.jsx)("div",{className:"grid grid-cols-8 gap-2",children:n.emojis.map((e,n)=>(0,s.jsx)("button",{type:"button",onClick:()=>{i(e),a()},className:"flex h-8 w-8 items-center justify-center rounded-lg text-lg transition hover:bg-white/10",title:e,children:e},"".concat(t,"-").concat(n)))})]},t)})})})]}):null}function N(){return(0,s.jsx)("div",{className:"flex justify-start",children:(0,s.jsxs)("div",{className:"max-w-[72%] space-y-2 rounded-[18px] bg-[#2e2f2f] px-4 py-3",children:[(0,s.jsx)("div",{className:"h-4 w-48 animate-pulse rounded bg-white/10"}),(0,s.jsx)("div",{className:"h-4 w-40 animate-pulse rounded bg-white/10"}),(0,s.jsxs)("div",{className:"mt-3 flex justify-between",children:[(0,s.jsx)("div",{className:"h-3 w-12 animate-pulse rounded bg-white/5"}),(0,s.jsx)("div",{className:"h-3 w-8 animate-pulse rounded bg-white/5"})]})]})})}function k(){return(0,s.jsx)("div",{className:"space-y-3",children:Array.from({length:5}).map((e,t)=>(0,s.jsx)(N,{},t))})}function C(){return(0,s.jsxs)("div",{className:"flex items-center gap-3 border-b border-white/5 px-3 py-3 transition hover:bg-white/5 cursor-pointer",children:[(0,s.jsx)("div",{className:"h-12 w-12 animate-pulse rounded-2xl bg-white/10"}),(0,s.jsxs)("div",{className:"flex-1 min-w-0",children:[(0,s.jsx)("div",{className:"h-4 w-32 animate-pulse rounded bg-white/10 mb-2"}),(0,s.jsx)("div",{className:"h-3 w-48 animate-pulse rounded bg-white/5"})]}),(0,s.jsx)("div",{className:"h-3 w-10 animate-pulse rounded bg-white/5"})]})}function S(){return(0,s.jsx)("div",{children:Array.from({length:8}).map((e,t)=>(0,s.jsx)(C,{},t))})}function A(){return(0,s.jsxs)("svg",{className:"h-5 w-5 animate-spin",xmlns:"http://www.w3.org/2000/svg",fill:"none",viewBox:"0 0 24 24",children:[(0,s.jsx)("circle",{className:"opacity-25",cx:"12",cy:"12",r:"10",stroke:"currentColor",strokeWidth:"4"}),(0,s.jsx)("path",{className:"opacity-75",fill:"currentColor",d:"M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"})]})}function T(e){return e?new Intl.DateTimeFormat("id-ID",{hour:"2-digit",minute:"2-digit"}).format(new Date(e)):""}function E(e){var t,a,s;let n=null==(t=e.outboundDelivery)?void 0:t.status;if("failed"===n)return"Failed";if("canceled"===n)return"Canceled";if("queued"===n||"sending"===n)return"Sending";let i=null==(s=e.statuses)||null==(a=s[e.statuses.length-1])?void 0:a.status;return i&&"outbound"===e.direction?"read"===i?"Read":"delivered"===i?"Delivered":"Sent":""}function P(e){var t;return e?e.body?e.body:(null==(t=e.mediaFile)?void 0:t.originalName)?e.mediaFile.originalName:"Attachment":""}function I(e){let{src:t,label:a}=e;return t?(0,s.jsx)("img",{src:t,alt:a,className:"h-11 w-11 rounded-2xl object-cover"}):(0,s.jsx)("div",{className:"flex h-11 w-11 items-center justify-center rounded-2xl bg-[#2e2f2f] text-sm font-semibold text-white",children:String(a||"?").slice(0,2).toUpperCase()})}function R(e){let{mediaUrl:t,originalName:a,createdAt:i}=e,[l,r]=(0,n.useState)(120),[o,c]=(0,n.useState)(!1);return((0,n.useEffect)(()=>{let e=new Date(i).getTime(),t=()=>{let t=120-Math.floor((Date.now()-e)/1e3);t<=0?(r(0),c(!0)):(r(t),c(!1))};t();let a=setInterval(t,1e3);return()=>clearInterval(a)},[i]),o)?(0,s.jsxs)("div",{className:"flex h-[450px] w-[450px] flex-col items-center justify-center rounded-2xl bg-[#2e2f2f] p-6 text-center shadow-lg border border-white/5",children:[(0,s.jsx)(d.joZ,{className:"mb-4 text-5xl text-white/20 animate-spin-slow"}),(0,s.jsx)("p",{className:"mb-4 font-medium text-white/60",children:"QR Code Expired"}),(0,s.jsx)("button",{onClick:()=>window.location.reload(),className:"rounded-full bg-emerald-500 px-6 py-2 text-sm font-semibold text-white shadow-md hover:bg-emerald-600 transition",children:"Refresh to Get New QR"})]}):(0,s.jsxs)("div",{className:"relative h-[450px] w-[450px] overflow-hidden rounded-2xl bg-white p-6 shadow-lg",children:[(0,s.jsx)("img",{src:t,alt:a,className:"h-full w-full object-contain"}),(0,s.jsx)("div",{className:"absolute bottom-4 right-4 flex items-center gap-2 rounded-full bg-black/70 px-3 py-1 text-xs font-bold text-white backdrop-blur-sm",children:(0,s.jsxs)("span",{className:l<=10?"text-red-400 animate-pulse":"",children:["Expires in ",l,"s"]})})]})}function O(e){return e&&e.startsWith("image/")&&"image/webp"!==e}let _=(0,n.forwardRef)(function(e,t){var a,i;let{chat:l,messages:m,chats:u,typingState:h,loading:p,messagesLoading:g,loadingOlder:v,hasMoreMessages:N,messageQuery:C,onMessageQueryChange:S,onLoadOlder:_,onSendMessage:D,onSendMedia:L,onTyping:W,onDeleteMessage:F,onForwardMessage:M,onOpenContacts:U,onOpenCrm:q,onOpenSettings:B,onLogout:z}=e,[K,H]=(0,n.useState)(""),[Q,G]=(0,n.useState)(!1),[Z,Y]=(0,n.useState)(!1),[J,V]=(0,n.useState)(null),[X,$]=(0,n.useState)(null),[ee,et]=(0,n.useState)(""),[ea,es]=(0,n.useState)(!!C),[en,ei]=(0,n.useState)(0),[el,er]=(0,n.useState)(null),[eo,ed]=(0,n.useState)(null),[ec,ex]=(0,n.useState)(null),[em,eu]=(0,n.useState)(!1),[eh,ep]=(0,n.useState)([]),[ef,eg]=(0,n.useState)(!1),[eb,ew]=(0,n.useState)({}),ey=(0,o.C)(e=>e.updateMessage),ev=(0,o.C)(e=>e.typingByChat),ej=h||((null==l?void 0:l.id)?ev[null==l?void 0:l.id]:null),eN=(0,n.useRef)(null),ek=(0,n.useRef)(null),eC=(0,n.useRef)(null),eS=(0,n.useRef)(null),eA=(0,n.useRef)(!1),eT=(0,n.useRef)(0),eE=(0,n.useRef)(null),eP=(0,n.useRef)(null),eI=(0,n.useRef)(null),eR=(0,n.useRef)(null),[eO,e_]=(0,n.useState)(!1),eD=(0,n.useRef)(null),eL=(0,n.useRef)(null),[eW,eF]=(0,n.useState)(!1),eM=(0,n.useRef)(null),eU=(0,n.useRef)(null),[eq,eB]=(0,n.useState)([]),[ez,eK]=(0,n.useState)(!1),[eH,eQ]=(0,n.useState)({}),[eG,eZ]=(0,n.useState)(""),[eY,eJ]=(0,n.useState)(""),[eV,eX]=(0,n.useState)(""),[e$,e0]=(0,n.useState)(!1);(0,n.useEffect)(()=>{let e=e=>{em&&eR.current&&!eR.current.contains(e.target)&&!eI.current.contains(e.target)&&eu(!1),eO&&eL.current&&!eL.current.contains(e.target)&&!eD.current.contains(e.target)&&e_(!1),eW&&eU.current&&!eU.current.contains(e.target)&&!eM.current.contains(e.target)&&eF(!1)};return document.addEventListener("mousedown",e),()=>{document.removeEventListener("mousedown",e)}},[em,eO,eW]);let e1=[...(null==l||null==(a=l.contact)?void 0:a.externalId)&&("openwa:assistant"===l.contact.externalId||String(l.contact.externalId).startsWith("openwa:assistant")||String(l.contact.externalId).endsWith(":assistant"))?[{label:"New Assistant Chat",message:"/new",icon:"✨"}]:[],{label:"Add WhatsApp Device",message:"Please help me add a new WhatsApp device",icon:"\uD83D\uDCF1"},{label:"Integrate Telegram",message:"How can I remote Wanie via Telegram?",icon:"\uD83E\uDD16"},{label:"Latest Messages",message:"Show me the latest messages from my WhatsApp chats",icon:"\uD83D\uDCAC"},{label:"Create Coding Project",message:"I want to create a new coding project in the workspace. Help me scaffold it.",icon:"\uD83D\uDE80"},{label:"Register New Tool",message:"I want to register a new external tool/API to your capabilities.",icon:"\uD83D\uDEE0️"},{label:"Setup LLM Provider",message:"Help me setup an LLM Provider (OpenAI/Anthropic/Ollama)",icon:"\uD83E\uDDE0"},{label:"Create API Key",message:"I want to create a new API Key",icon:"\uD83D\uDD11"},{label:"Check Workspace",message:"Check the contents of my workspace folder",icon:"\uD83D\uDCC1"},{label:"Reset Password",message:"/reset_password",icon:"\uD83D\uDD12"},{label:"Help / Capabilities",message:"What are your capabilities as an AI Assistant?",icon:"❓"}],e2=async e=>{e_(!1),H(""),G(!0);try{await D({body:e,replyToId:null}),W(!1)}catch(t){console.error("Failed to send shortcut message:",t),H(e)}finally{G(!1)}},e5=(0,n.useMemo)(()=>{let e=String(C||"").trim().toLowerCase();return e?m.map((t,a)=>{var s,n;return[t.body,t.sender,null==(s=t.replyTo)?void 0:s.body,null==(n=t.mediaFile)?void 0:n.originalName].filter(Boolean).some(t=>t.toLowerCase().includes(e))?a:-1}).filter(e=>-1!==e):[]},[C,m]);(0,n.useMemo)(()=>m,[C,m]);let e3=u.filter(e=>e.id!==(null==l?void 0:l.id)),e4=async()=>{if(eh.length>0)return void await e9();K.trim()&&await e8()},e6=async e=>{e.preventDefault(),await e4()},e8=async()=>{if(K.trim()){G(!0);try{await D({body:K.trim(),replyToId:(null==J?void 0:J.id)||null}),H(""),V(null),W(!1)}finally{G(!1)}}},e7=async e=>{let t=e.target.files;if(!t||0===t.length)return;let a=[];for(let e of t){let t=null,s=e.type||"";s.startsWith("image/")?t=URL.createObjectURL(e):s.startsWith("video/")&&(t=URL.createObjectURL(e)),a.push({file:e,name:e.name,size:e.size,type:s,preview:t})}ep(e=>[...e,...a]),e.target.value=""},e9=async()=>{if(0===eh.length)return e8();let e=[...eh];G(!0),Y(!0),H(""),V(null),ep([]),W(!1);try{for(let t=0;t<e.length;t++){let a=e[t],s=t===e.length-1?K.trim():"";await L({file:a.file,caption:s})}for(let t of e||[])(null==t?void 0:t.preview)&&URL.revokeObjectURL(t.preview)}catch(t){throw H(K),V(J),ep(e),t}finally{G(!1),Y(!1)}},te=(0,o.C)(e=>e.token),tt=(0,o.C)(e=>e.setActiveChat),ta=(0,o.C)(e=>e.upsertChat),ts=(0,o.C)(e=>e.defaultAiProviderId),tn=(0,o.C)(e=>e.defaultAiModel),ti=(0,o.C)(e=>e.setDefaultAiProvider),tl=(0,o.C)(e=>e.setDefaultAiModel),[tr,to]=(0,n.useState)(!1);(0,n.useEffect)(()=>{if(!eW)return;let e=!0;return eK(!0),eZ(ts||""),eJ(tn||""),eX(""),(async()=>{if(!te){e&&eK(!1);return}try{let t=await (0,r.n)("/api/ai-providers",{token:te});if(!e)return;eB(t.providers||[])}catch(e){}finally{e&&eK(!1)}})(),()=>{e=!1}},[eW,te,ts,tn]),(0,n.useEffect)(()=>{if(!eG||eH[eG]||!te)return;let e=!0;return(async()=>{try{let t=await (0,r.n)("/api/ai-providers/".concat(eG,"/models"),{token:te});if(!e)return;eQ(e=>({...e,[eG]:t.models||[]}))}catch(e){}})(),()=>{e=!1}},[eG,te,eH]);let td=async()=>{if(eG){e0(!0);try{await ti(eG);let e=eY||eV.trim()||null;await tl(e),eF(!1)}catch(e){}finally{e0(!1)}}},tc=e=>{eX(e)},tx=async(e,t)=>{if(!te)return alert("Not authenticated");ew(e=>({...e,[t]:!0}));try{let t=await fetch("".concat((0,r.h)(),"/api/sessions/").concat(e,"/confirm-scan"),{method:"POST",headers:{Authorization:"Bearer ".concat(te),"Content-Type":"application/json"}}),a=await t.json().catch(()=>({}));if(!t.ok)throw Error(a.error||"Request failed");alert("Scan confirmed; waiting for device to connect.")}catch(e){alert(e.message||"Failed to confirm scan")}finally{ew(e=>({...e,[t]:!1}))}},tm=async e=>{ee&&(await M(e,ee),$(null),et(""))},tu=e=>{var t;if(0===e5.length)return;let a=e5[e],s=document.querySelector('[data-message-id="'.concat(null==(t=m[a])?void 0:t.id,'"]'));s&&eC.current&&(eC.current.scrollTop=s.offsetTop-eC.current.offsetTop)};return((0,n.useEffect)(()=>{C&&e5.length>0&&(ei(0),tu(0))},[C]),(0,n.useImperativeHandle)(t,()=>({focusComposer(){var e;null==(e=eN.current)||e.focus()}}),[]),(0,n.useEffect)(()=>{let e=eN.current;e&&(e.style.height="0px",e.style.height="".concat(Math.min(e.scrollHeight,128),"px"))},[K]),(0,n.useEffect)(()=>{if(ea){var e;null==(e=ek.current)||e.focus()}},[ea]),(0,n.useEffect)(()=>{eA.current=!0,eT.current=0},[null==l?void 0:l.id]),(0,n.useEffect)(()=>{if(!(null==l?void 0:l.id))return;let e=function(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"smooth";eC.current&&eC.current.scrollTo({top:eC.current.scrollHeight,behavior:e})};if(eA.current){if(!m.length){e(),eA.current=!1;return}let t=setTimeout(()=>{requestAnimationFrame(()=>{requestAnimationFrame(()=>{requestAnimationFrame(()=>{e(),eA.current=!1})})})},300);return()=>clearTimeout(t)}let t=m.length,a=eT.current;if(eT.current=t,t>a&&!g){let t=setTimeout(()=>{e("smooth")},50);return()=>clearTimeout(t)}},[null==l?void 0:l.id,m.length,g]),p)?(0,s.jsx)("div",{className:"flex flex-1 items-center justify-center text-white/50",children:"Loading dashboard..."}):l?(0,s.jsxs)("section",{className:"flex min-w-0 flex-1 flex-col bg-[#161717] text-white",children:[(0,s.jsxs)("header",{className:"flex h-[78px] shrink-0 items-center justify-between gap-4 bg-[#161717] px-6 py-3",children:[(0,s.jsxs)("div",{className:"flex min-w-0 items-center gap-3 cursor-pointer",onClick:()=>eg(!0),children:[(0,s.jsx)(I,{src:l.contact.avatarUrl,label:l.contact.displayName}),(0,s.jsxs)("div",{className:"min-w-0",children:[(0,s.jsxs)("div",{className:"flex min-w-0 items-center gap-2",children:[(0,s.jsx)("h2",{className:"truncate font-semibold text-white",children:l.contact.displayName}),(0,s.jsx)(x.zd,{chat:l,compact:!0})]}),(0,s.jsx)("p",{className:"text-sm text-white/40",children:(null==ej?void 0:ej.isTyping)?"".concat(ej.name," is typing..."):"".concat((0,x.qh)(l)," chat synced locally")})]})]}),(0,s.jsxs)("div",{className:"flex items-center gap-2",children:[ea||C?(0,s.jsxs)("div",{className:"flex items-center gap-2 rounded-[22px] bg-[#2e2f2f] px-4 py-2",children:[(0,s.jsx)("input",{ref:ek,className:"w-[180px] border-none bg-transparent text-sm text-white outline-none placeholder:text-white/30",placeholder:"Search messages...",value:C,onChange:e=>S(e.target.value)}),e5.length>0&&(0,s.jsxs)("span",{className:"text-xs text-white/60",children:[en+1,"/",e5.length]}),e5.length>0&&(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)("button",{type:"button",title:"Previous result","aria-label":"Previous result",className:"text-sm leading-none text-white/55 transition hover:text-white",onClick:()=>{if(0===e5.length)return;let e=(en-1+e5.length)%e5.length;ei(e),tu(e)},children:"↑"}),(0,s.jsx)("button",{type:"button",title:"Next result","aria-label":"Next result",className:"text-sm leading-none text-white/55 transition hover:text-white",onClick:()=>{if(0===e5.length)return;let e=(en+1)%e5.length;ei(e),tu(e)},children:"↓"})]}),(0,s.jsx)("button",{type:"button",title:"Close search","aria-label":"Close search",className:"text-sm leading-none text-white/55 transition hover:text-white",onClick:()=>{S(""),es(!1)},children:(0,s.jsx)(d.m6K,{className:"w-4 h-4"})})]}):null,(0,s.jsx)("button",{type:"button",title:"Search","aria-label":"Search",className:"flex h-10 w-10 items-center justify-center rounded-full bg-[#2e2f2f] text-base leading-none text-white transition hover:bg-[#3a3b3b]",onClick:()=>es(!0),children:(0,s.jsx)(d.gZ7,{className:"w-5 h-5"})}),(0,s.jsx)("button",{type:"button",title:"New chat","aria-label":"New chat",className:"flex h-10 w-10 items-center justify-center rounded-full bg-[#2e2f2f] text-lg leading-none text-white transition hover:bg-[#3a3b3b]",onClick:U,children:(0,s.jsx)(d.jgn,{className:"w-5 h-5"})}),(0,s.jsxs)("button",{type:"button",title:"Open Dashboard CRM","aria-label":"Open Dashboard CRM",className:"relative flex h-10 items-center justify-center gap-2 overflow-hidden rounded-full bg-amber-400 px-3 text-sm font-semibold text-[#1d1600] shadow-[0_0_14px_rgba(251,191,36,0.28)] ring-1 ring-amber-100/70 transition hover:bg-amber-300 focus:outline-none focus:ring-2 focus:ring-amber-200",onClick:q,children:[(0,s.jsx)(d.Itr,{className:"relative h-5 w-5"}),(0,s.jsx)("span",{className:"relative whitespace-nowrap",children:"Dashboard CRM"})]}),(0,s.jsx)("button",{type:"button",title:"New Assistant","aria-label":"New Assistant",className:"flex h-10 w-10 items-center justify-center rounded-full bg-[#2e2f2f] text-sm leading-none text-white transition hover:bg-[#3a3b3b]",onClick:async()=>{if(!te)return alert("Not authenticated");if(confirm("Start a new Assistant conversation?")){to(!0);try{let e=await fetch("".concat((0,r.h)(),"/api/assistant/sessions"),{method:"POST",headers:{Authorization:"Bearer ".concat(te),"Content-Type":"application/json"},body:JSON.stringify({})}),t=await e.json().catch(()=>({}));if(!e.ok)throw Error(t.error||"Request failed");let a=t.chat;a&&a.id&&(ta(a),tt(a.id))}catch(e){alert(e.message||"Failed to create assistant session")}finally{to(!1)}}},disabled:tr,children:tr?"...":"AI"}),(0,s.jsx)("button",{type:"button",title:"Settings","aria-label":"Settings",className:"flex h-10 w-10 items-center justify-center rounded-full bg-[#2e2f2f] text-base leading-none text-white transition hover:bg-[#3a3b3b]",onClick:B,children:(0,s.jsx)(d.hgV,{className:"w-5 h-5"})}),(0,s.jsx)("button",{type:"button",title:"Logout","aria-label":"Logout",className:"flex h-10 w-10 items-center justify-center rounded-full bg-[#2e2f2f] text-base leading-none text-white transition hover:bg-[#3a3b3b]",onClick:z,children:(0,s.jsx)(d.EyH,{className:"w-5 h-5"})})]})]}),(0,s.jsx)(c,{open:ef,chat:l,onClose:()=>eg(!1)}),(0,s.jsxs)("div",{ref:eC,className:"flex-1 overflow-y-auto bg-[#161717] px-8 py-5",children:[(0,s.jsx)("div",{className:"mb-5 flex justify-center",children:(0,s.jsx)("button",{type:"button",className:"rounded-full bg-[#2e2f2f] px-4 py-2 text-xs font-medium text-white/60 transition hover:text-white disabled:opacity-40",onClick:_,disabled:!N||v,children:v?"Loading...":N?"Load older messages":"All messages loaded"})}),g?(0,s.jsx)(k,{}):null,(0,s.jsxs)("div",{className:"space-y-3",children:[(function(e){let t=[],a=null;for(let n=0;n<e.length;n++){var s;let i=e[n],l=String((null==(s=i.mediaFile)?void 0:s.mimeType)||""),r=i.mediaFile&&O(l);r&&null===a?a={type:"image-group",messages:[i],direction:i.direction,startTime:new Date(i.createdAt).getTime()}:r&&a&&"image-group"===a.type&&a.direction===i.direction&&new Date(i.createdAt).getTime()-a.startTime<=12e4?a.messages.push(i):(a&&(t.push(a),a=null),r||t.push({type:"single",message:i}))}return a&&t.push(a),t})(m).map((e,t)=>{if("image-group"===e.type){let a=e.messages[0],n="outbound"===a.direction,i=e.messages.map(e=>e.body).filter(Boolean).join("\n");return(0,s.jsx)("div",{className:"flex ".concat(n?"justify-end":"justify-start"),children:(0,s.jsxs)("div",{className:"max-w-[72%] rounded-[18px] px-4 py-3 shadow-[0_16px_32px_rgba(0,0,0,0.18)] transition-colors relative ".concat(n?"bg-[#144d37]":"bg-[#2e2f2f]"),children:[function(e,t){let a=e.messages.map(e=>e.mediaFile).filter(Boolean);if(0===a.length)return null;if(1===a.length){let n=a[0],i=e.messages[0],l="".concat((0,r.h)(),"/").concat(n.relativePath);return"whatsapp-qr.png"===String(n.originalName||"").toLowerCase()||String(n.fileName||"").toLowerCase().startsWith("qr-")?(0,s.jsx)("div",{className:"mb-2",children:(0,s.jsx)(R,{mediaUrl:l,originalName:n.originalName,createdAt:i.createdAt})}):(0,s.jsx)("img",{src:l,alt:n.originalName,className:"mb-2 h-24 w-24 cursor-pointer rounded-2xl object-cover",onClick:()=>t({mediaUrl:l,relativePath:n.relativePath,mimeType:n.mimeType,originalName:n.originalName,isImage:!0})})}return(0,s.jsx)("div",{className:"mb-2 grid grid-cols-2 gap-1",children:a.map((e,a)=>{let n="".concat((0,r.h)(),"/").concat(e.relativePath);return(0,s.jsx)("img",{src:n,alt:e.originalName,className:"h-32 w-32 cursor-pointer rounded-lg object-cover",onClick:()=>t({mediaUrl:n,relativePath:e.relativePath,mimeType:e.mimeType,originalName:e.originalName,isImage:!0})},a)})})}(e,e=>ex(e)),i?(0,s.jsx)("p",{className:"mt-3 whitespace-pre-wrap text-sm leading-6 text-white/88",children:i}):null,(0,s.jsxs)("div",{className:"mt-3 flex items-center justify-end gap-2 text-[11px] text-white/35",children:[(0,s.jsx)("span",{children:T(a.createdAt)}),n?(0,s.jsx)("span",{children:E(a)}):null]})]})},"group-".concat(t))}{var a,n;let t=e.message,i="outbound"===t.direction,o=m.indexOf(t),c=C&&e5.includes(o),x=c&&e5[en]===o,u=function(e){let t=String((null==e?void 0:e.externalMessageId)||"");return t.startsWith("terminal:")&&t.slice(9)||null}(t),h=i&&(null==(n=t.outboundDelivery)?void 0:n.status)==="failed";return(0,s.jsx)("div",{"data-message-id":t.id,className:"flex ".concat(i?"justify-end":"justify-start"),children:(0,s.jsxs)("div",{className:"max-w-[72%] rounded-[18px] px-4 py-3 shadow-[0_16px_32px_rgba(0,0,0,0.18)] transition-colors relative ".concat(x?"ring-2 ring-brand-500 "+(i?"bg-[#1a5f41]":"bg-[#3a4a4a]"):c?"ring-1 ring-brand-500/50 "+(i?"bg-[#144d37]":"bg-[#2e2f2f]"):h?"bg-red-950/80 ring-1 ring-red-400/30":i?"bg-[#144d37]":"bg-[#2e2f2f]"),onMouseEnter:()=>er(t.id),onMouseLeave:()=>er(null),children:[t.replyTo?(0,s.jsxs)("div",{className:"mb-2 rounded-2xl border-l-4 border-brand-500 bg-white/[0.04] px-3 py-2 text-xs text-white/55",children:[(0,s.jsx)("span",{className:"font-semibold text-white",children:"outbound"===t.replyTo.direction?"Anda":l.contact.displayName}),(0,s.jsx)("p",{className:"mt-1 truncate",children:P(t.replyTo)})]}):null,t.mediaFile&&function(e,t){if(!e.mediaFile)return null;let a="".concat((0,r.h)(),"/").concat(e.mediaFile.relativePath),n=String(e.mediaFile.mimeType||""),i="sticker"===e.type||"image/webp"===n,l="whatsapp-qr.png"===String(e.mediaFile.originalName||"").toLowerCase()||String(e.mediaFile.fileName||"").toLowerCase().startsWith("qr-");return i?(0,s.jsx)("a",{href:a,target:"_blank",rel:"noreferrer",className:"mb-2 inline-flex overflow-hidden rounded-2xl bg-transparent",children:(0,s.jsx)("img",{src:a,alt:e.mediaFile.originalName,className:"h-36 w-36 object-contain drop-shadow-sm"})}):O(n)?l?(0,s.jsx)("div",{className:"mb-2",children:(0,s.jsx)(R,{mediaUrl:a,originalName:e.mediaFile.originalName,createdAt:e.createdAt})}):(0,s.jsx)("img",{src:a,alt:e.mediaFile.originalName,className:"mb-2 max-h-[320px] w-full cursor-pointer rounded-2xl object-contain",onClick:()=>t&&t({mediaUrl:a,relativePath:e.mediaFile.relativePath,mimeType:e.mediaFile.mimeType,originalName:e.mediaFile.originalName,isImage:!0})}):n.startsWith("video/")?(0,s.jsx)("video",{controls:!0,className:"mb-2 max-h-[320px] w-full rounded-2xl bg-black",children:(0,s.jsx)("source",{src:a,type:n})}):n.startsWith("audio/")?(0,s.jsx)("audio",{controls:!0,className:"mb-2 w-full",children:(0,s.jsx)("source",{src:a,type:n})}):(0,s.jsx)("a",{href:a,target:"_blank",rel:"noreferrer",className:"mb-2 inline-flex rounded-xl bg-white/10 px-3 py-2 text-sm font-medium text-white underline-offset-2 hover:underline",children:e.mediaFile.originalName})}(t,e=>ex(e)),t.mediaFile&&t.sender&&String(t.sender).startsWith("session:")&&String(t.sender).endsWith(":assistant")&&("image"===t.type||String(t.mediaFile.mimeType||"").startsWith("image/"))&&(0,s.jsx)("div",{className:"mt-2",children:(0,s.jsx)("button",{type:"button",className:"rounded-2xl bg-brand-500 px-3 py-2 text-sm font-semibold text-[#10251a]",onClick:()=>{let e=String(t.sender).split(":")[1]||null;e&&tx(e,t.id)},disabled:!!eb[t.id],children:eb[t.id]?"Confirming...":"I scanned this QR"})}),u?(0,s.jsx)(b,{terminalId:u,fallbackBody:t.body,onReplaceTerminalId:e=>{ey({...t,externalMessageId:"terminal:".concat(e)})}}):null,!u&&t.body?(0,s.jsx)(f,{content:t.body}):null,(0,s.jsxs)("div",{className:"mt-3 flex items-center justify-end gap-3 text-[11px] text-white/35",children:[(0,s.jsx)("span",{children:T(t.createdAt)}),i?(0,s.jsx)("span",{className:h?"font-semibold text-red-200":"",children:E(t)}):null]}),h&&(null==(a=t.outboundDelivery)?void 0:a.lastError)?(0,s.jsx)("p",{className:"mt-2 text-[11px] leading-5 text-red-100/75",children:t.outboundDelivery.lastError}):null,el===t.id&&(0,s.jsxs)("div",{className:"absolute right-2 top-2 flex items-center gap-1",children:[(0,s.jsx)("button",{type:"button",ref:eP,className:"flex h-8 w-8 items-center justify-center rounded-full bg-[#2e2f2f] text-white/60 transition hover:bg-[#3a3b3b] hover:text-white",onClick:()=>ed(e=>e===t.id?null:t.id),title:"More options",children:(0,s.jsx)(d.a95,{className:"w-5 h-5"})}),(0,s.jsx)(w,{isOpen:eo===t.id,onClose:()=>ed(null),message:t,onReply:()=>V(t),onDelete:()=>F(t.id),onForward:()=>{$(e=>e===t.id?null:t.id),et("")},isOutbound:i,triggerRef:eP})]}),X===t.id?(0,s.jsxs)("div",{className:"mt-3 flex flex-wrap gap-2 rounded-2xl bg-white/[0.04] p-3",children:[(0,s.jsxs)("select",{className:"min-w-[200px] flex-1 rounded-xl border border-white/10 bg-[#0b141a] px-3 py-2 text-sm text-white outline-none",value:ee,onChange:e=>et(e.target.value),children:[(0,s.jsx)("option",{value:"",children:"Select target chat"}),e3.map(e=>(0,s.jsx)("option",{value:e.id,children:e.contact.displayName},e.id))]}),(0,s.jsx)("button",{type:"button",className:"rounded-xl bg-brand-500 px-4 py-2 text-sm font-semibold text-[#10251a]",onClick:()=>tm(t.id),children:"Send"})]}):null]})},t.id)}}),(null==ej?void 0:ej.isTyping)&&(0,s.jsx)("div",{className:"flex justify-start",children:(0,s.jsx)("div",{className:"max-w-[72%] rounded-[18px] bg-[#2e2f2f] px-4 py-3 shadow-[0_16px_32px_rgba(0,0,0,0.18)]",children:(0,s.jsxs)("div",{className:"flex items-center gap-2 text-sm text-white/60 italic",children:[(0,s.jsxs)("span",{children:[ej.name," is thinking..."]}),(0,s.jsxs)("div",{className:"flex gap-1",children:[(0,s.jsx)("span",{className:"h-1 w-1 animate-bounce rounded-full bg-white/40 delay-0"}),(0,s.jsx)("span",{className:"h-1 w-1 animate-bounce rounded-full bg-white/40 delay-150"}),(0,s.jsx)("span",{className:"h-1 w-1 animate-bounce rounded-full bg-white/40 delay-300"})]})]})})}),(0,s.jsx)("div",{ref:eS})]})]}),(0,s.jsxs)("form",{className:"shrink-0 bg-[#161717] px-6 py-3",onSubmit:e6,children:[J?(0,s.jsxs)("div",{className:"mb-3 flex items-start justify-between rounded-2xl bg-[#2e2f2f] px-4 py-3",children:[(0,s.jsxs)("div",{className:"min-w-0",children:[(0,s.jsx)("p",{className:"text-xs uppercase tracking-[0.22em] text-brand-100",children:"Reply"}),(0,s.jsx)("p",{className:"mt-1 truncate text-sm text-white/55",children:P(J)})]}),(0,s.jsx)("button",{type:"button",className:"text-sm text-white/45 hover:text-white",onClick:()=>V(null),children:(0,s.jsx)(d.m6K,{className:"w-4 h-4"})})]}):null,eh.length>0&&(0,s.jsx)("div",{className:"mb-3 flex flex-wrap gap-2 rounded-2xl bg-white/[0.04] p-3",children:eh.map((e,t)=>{var a;return(0,s.jsxs)("div",{className:"relative",children:[e.preview&&e.type.startsWith("image/")?(0,s.jsx)("img",{src:e.preview,alt:e.name,className:"h-16 w-16 rounded-lg object-cover"}):e.preview&&e.type.startsWith("video/")?(0,s.jsx)("video",{src:e.preview,className:"h-16 w-16 rounded-lg object-cover"}):(0,s.jsx)("div",{className:"flex h-16 w-16 items-center justify-center rounded-lg bg-[#2e2f2f] text-sm font-medium text-white/60",children:(null==(a=e.name.split(".").pop())?void 0:a.toUpperCase())||"FILE"}),(0,s.jsx)("button",{type:"button",onClick:()=>{ep(e=>{let a=[...e],s=a[t];return s.preview&&URL.revokeObjectURL(s.preview),a.splice(t,1),a})},className:"absolute -right-2 -top-2 flex h-6 w-6 items-center justify-center rounded-full bg-red-500 text-xs text-white transition hover:bg-red-600",children:(0,s.jsx)(d.m6K,{className:"w-4 h-4"})})]},t)})}),(0,s.jsxs)("div",{className:"flex items-center gap-2 relative",children:[(0,s.jsxs)("label",{className:"flex h-10 w-10 shrink-0 cursor-pointer items-center justify-center rounded-full bg-[#2e2f2f] text-[24px] leading-none text-white/60 transition hover:bg-[#3a3b3b] hover:text-white",children:[(0,s.jsx)(d.jgn,{className:"w-5 h-5"}),(0,s.jsx)("input",{ref:eE,type:"file",className:"hidden",onChange:e7,multiple:!0,accept:"image/*,video/*,audio/*,.pdf,.doc,.docx,.xls,.xlsx,.txt"})]}),(0,s.jsx)("button",{type:"button",ref:eI,className:"flex h-10 w-10 shrink-0 items-center justify-center rounded-full bg-[#2e2f2f] text-[20px] transition hover:bg-[#3a3b3b]",onClick:()=>eu(!em),title:"Emoji",children:(0,s.jsx)(d.aUC,{className:"w-5 h-5"})}),em&&(0,s.jsx)("div",{ref:eR,className:"absolute bottom-full left-0 z-50",children:(0,s.jsx)(j,{isOpen:em,onClose:()=>eu(!1),onEmojiSelect:e=>{let t=eN.current;if(!t)return;let a=t.selectionStart||0,s=t.selectionEnd||0;H(K.slice(0,a)+e+K.slice(s)),setTimeout(()=>{t.focus(),t.setSelectionRange(a+e.length,a+e.length)},0),eu(!1)},triggerRef:eI})}),(0,s.jsx)("button",{type:"button",ref:eD,className:"flex h-10 w-10 shrink-0 items-center justify-center rounded-full transition ".concat(eO?"bg-brand-500 text-[#10251a] ring-2 ring-emerald-400/60 shadow-[0_0_0_8px_rgba(16,37,26,0.12)]":"bg-[#2e2f2f] text-white/60 hover:bg-[#3a3b3b] hover:text-white ring-1 ring-emerald-400/30 animate-pulse"),onClick:()=>e_(!eO),title:"Shortcuts",children:(0,s.jsx)(d.vSZ,{className:"w-5 h-5"})}),eO&&(0,s.jsxs)("div",{ref:eL,className:"absolute bottom-full left-12 z-50 mb-2 w-64 overflow-hidden rounded-2xl bg-[#2e2f2f] p-1 shadow-2xl ring-1 ring-white/10",children:[(0,s.jsx)("div",{className:"px-3 py-2 text-[11px] font-bold uppercase tracking-wider text-white/30",children:"AI Shortcuts"}),(0,s.jsx)("div",{className:"max-h-64 overflow-y-auto",children:e1.map((e,t)=>(0,s.jsxs)("button",{type:"button",className:"flex w-full items-center gap-3 px-3 py-2.5 text-left text-sm text-white/90 transition hover:bg-white/5 first:rounded-t-xl last:rounded-b-xl",onClick:()=>e2(e.message),children:[(0,s.jsx)("span",{className:"text-lg",children:e.icon}),(0,s.jsx)("span",{className:"flex-1 font-medium",children:e.label})]},t))})]}),(0,s.jsx)("button",{type:"button",ref:eM,className:"flex h-10 w-10 shrink-0 items-center justify-center rounded-full transition ".concat(eW?"bg-emerald-500 text-white ring-2 ring-emerald-300/70 shadow-[0_0_0_8px_rgba(23,163,70,0.18)]":"bg-orange-500 text-white hover:bg-orange-600 ring-1 ring-orange-300/30"),onClick:()=>eF(!eW),title:"AI Provider settings",children:(0,s.jsx)(d.BtR,{className:"w-5 h-5"})}),eW&&(0,s.jsxs)("div",{ref:eU,className:"absolute bottom-full left-20 z-50 mb-2 w-72 overflow-hidden rounded-2xl bg-[#2e2f2f] p-3 shadow-2xl ring-1 ring-white/10",children:[(0,s.jsx)("div",{className:"px-2 pb-2 text-[11px] font-bold uppercase tracking-wider text-white/30",children:"AI Provider & Model"}),ez?(0,s.jsx)("div",{className:"rounded-2xl bg-[#161717] px-4 py-4 text-center text-sm text-white/50",children:"Loading providers..."}):0===eq.length?(0,s.jsx)("div",{className:"rounded-2xl bg-[#161717] px-4 py-4 text-sm text-white/60",children:"No AI providers configured. Open settings to add one."}):(0,s.jsxs)("div",{className:"space-y-3",children:[(0,s.jsxs)("div",{children:[(0,s.jsx)("label",{className:"mb-1 block text-xs uppercase tracking-[0.22em] text-white/40",children:"Provider"}),(0,s.jsxs)("select",{className:"w-full rounded-[14px] bg-[#161717] px-3 py-2 text-sm text-white outline-none",value:eG,onChange:e=>{eZ(e.target.value),eJ(""),eX("")},children:[(0,s.jsx)("option",{value:"",children:"Select provider"}),eq.map(e=>(0,s.jsxs)("option",{value:e.id,children:[e.name," (",e.provider,")"]},e.id))]})]}),eG?(0,s.jsxs)("div",{children:[(0,s.jsx)("label",{className:"mb-1 block text-xs uppercase tracking-[0.22em] text-white/40",children:"Model"}),(null==(i=eH[eG])?void 0:i.length)?(0,s.jsxs)(s.Fragment,{children:[(0,s.jsxs)("select",{className:"w-full rounded-[14px] bg-[#161717] px-3 py-2 text-sm text-white outline-none",value:eY,onChange:e=>{eJ(e.target.value),eX("")},children:[(0,s.jsx)("option",{value:"",children:"Select model"}),eH[eG].map(e=>(0,s.jsx)("option",{value:e.id,children:e.name||e.id},e.id)),(0,s.jsx)("option",{value:"manual-model",children:"Manual model"})]}),"manual-model"===eY?(0,s.jsx)("input",{className:"mt-2 w-full rounded-[14px] bg-[#161717] px-3 py-2 text-sm text-white outline-none",placeholder:"Enter model id",value:eV,onChange:e=>tc(e.target.value)}):null]}):(0,s.jsx)("input",{className:"w-full rounded-[14px] bg-[#161717] px-3 py-2 text-sm text-white outline-none",placeholder:"Enter model id",value:eV,onChange:e=>tc(e.target.value)})]}):null,(0,s.jsx)("button",{type:"button",className:"w-full rounded-[14px] bg-brand-500 px-4 py-2 text-sm font-semibold text-[#10251a] transition hover:bg-brand-600 disabled:opacity-60",onClick:td,disabled:!eG||e$,children:e$?"Saving...":"Set AI provider"})]})]}),(0,s.jsx)("div",{className:"flex flex-1 items-center rounded-[22px] bg-[#2e2f2f] px-4 py-2",children:(0,s.jsx)("textarea",{ref:eN,rows:1,className:"min-h-[20px] w-full resize-none overflow-y-auto border-none bg-transparent px-1 py-0.5 text-sm leading-5 text-white outline-none placeholder:text-white/30 disabled:opacity-60",placeholder:"Type a message",value:K,onChange:e=>{H(e.target.value),W(!!e.target.value)},onKeyDown:e=>{"Enter"===e.key&&!e.shiftKey&&(e.preventDefault(),Q||Z||e4())},disabled:Q||Z})}),(0,s.jsx)("button",{type:"submit",className:"flex h-10 w-10 items-center justify-center rounded-full bg-brand-500 text-sm font-semibold leading-none text-[#10251a] transition hover:bg-brand-600 disabled:cursor-not-allowed disabled:opacity-60",disabled:Q||Z,children:Q||Z?(0,s.jsx)(A,{}):(0,s.jsx)(d.KpL,{className:"w-5 h-5"})})]})]}),ec&&(0,s.jsx)(y,{media:ec,onClose:()=>ex(null)})]}):(0,s.jsx)("div",{className:"flex flex-1 items-center justify-center bg-[#161717] px-8 text-center text-white/50",children:(0,s.jsxs)("div",{children:[(0,s.jsx)("p",{className:"text-lg font-medium text-white",children:"No chat selected"}),(0,s.jsx)("p",{className:"mt-3 max-w-md text-sm leading-7 text-white/45",children:"Start a new conversation from the contact selector to begin chatting."}),(0,s.jsx)("button",{type:"button",className:"mt-5 rounded-full bg-brand-500 px-5 py-3 text-sm font-semibold text-[#10251a]",onClick:U,children:"New chat"})]})})});var D=a(1826);let L="1.2.4";function W(e){let{src:t,label:a}=e;return t?(0,s.jsx)("img",{src:t,alt:a,className:"h-12 w-12 shrink-0 rounded-2xl object-cover"}):(0,s.jsx)("div",{className:"flex h-12 w-12 shrink-0 items-center justify-center rounded-2xl bg-[#2e2f2f] text-sm font-semibold text-white",children:String(a||"?").split(" ").map(e=>e[0]).join("").slice(0,2).toUpperCase()})}function F(e){let{chats:t,activeChatId:a,onSelectChat:i,currentUser:l,loading:r,query:o,onQueryChange:c,onTogglePin:m,onDeleteChat:u}=e,h=String(o||"").trim().toLowerCase(),[p,f]=(0,n.useState)(null),g=t.find(e=>e.contact&&"openwa:assistant"===e.contact.externalId);(0,n.useEffect)(()=>{function e(e){if(p)try{let t=e.target.closest('[data-menu-button="'.concat(p,'"]')),a=e.target.closest('[data-menu-id="'.concat(p,'"]'));t||a||f(null)}catch(e){f(null)}}function t(e){"Escape"===e.key&&f(null)}return document.addEventListener("mousedown",e),document.addEventListener("touchstart",e),document.addEventListener("keydown",t),()=>{document.removeEventListener("mousedown",e),document.removeEventListener("touchstart",e),document.removeEventListener("keydown",t)}},[p]);let b=t.filter(e=>e.contact&&"openwa:assistant"!==e.contact.externalId).filter(e=>!h||[e.contact.displayName,e.contact.lastMessagePreview,e.title].filter(Boolean).some(e=>e.toLowerCase().includes(h)));return(0,s.jsxs)("aside",{className:"flex h-full w-[360px] shrink-0 flex-col bg-[#161717]",children:[(0,s.jsxs)("div",{className:"px-5 py-5",children:[(0,s.jsx)("div",{className:"flex items-center justify-center",children:(0,s.jsxs)("div",{className:"flex h-12 items-center gap-2",children:[(0,s.jsx)(D.F,{variant:"long",alt:"Wanie",className:"h-full"}),L?(0,s.jsxs)("span",{className:"rounded-full bg-white/5 px-2 py-1 text-[11px] font-semibold text-white/45",children:["v",L]}):null]})}),(0,s.jsx)("div",{className:"mt-4 rounded-[22px] bg-[#2e2f2f] px-3 py-2",children:(0,s.jsx)("input",{className:"w-full border-none bg-transparent text-sm text-white outline-none placeholder:text-white/30",placeholder:"Search conversation",value:o,onChange:e=>c(e.target.value)})})]}),(0,s.jsxs)("div",{className:"flex-1 overflow-y-auto px-3 py-3",children:[r?(0,s.jsx)(S,{}):null,r||0!==b.length?null:(0,s.jsx)("p",{className:"px-3 py-4 text-sm leading-6 text-white/40",children:"No synced conversations yet. Connect your device to load chats."}),(0,s.jsxs)("div",{className:"space-y-2",children:[g?(0,s.jsxs)("div",{className:"relative",children:[(0,s.jsxs)("button",{type:"button",className:"flex w-full items-start gap-3 rounded-[16px] px-4 py-3 pr-12 text-left transition "+(g.id===a?"bg-[#2e2f2f]":"bg-transparent hover:bg-white/[0.05]"),onClick:()=>i(g.id),children:[(0,s.jsx)(W,{src:g.contact.avatarUrl,label:g.contact.displayName}),(0,s.jsx)("div",{className:"min-w-0 flex-1",children:(0,s.jsxs)("div",{className:"grid grid-cols-[minmax(0,1fr)_auto] items-start gap-x-3",children:[(0,s.jsx)("h3",{className:"min-w-0 truncate font-medium text-white",children:g.contact.displayName}),(0,s.jsx)("div",{className:"w-[62px] shrink-0 text-right",children:(0,s.jsx)("span",{className:"block truncate text-[11px] text-white/35",children:g.contact.lastMessageAt||g.updatedAt?new Intl.DateTimeFormat("en-US",{hour:"2-digit",minute:"2-digit"}).format(new Date(g.contact.lastMessageAt||g.updatedAt)):""})}),(0,s.jsx)("p",{className:"mt-1 min-w-0 truncate text-sm text-white/42",children:g.contact.persona?g.contact.persona:g.contact.lastMessagePreview||"No messages yet"}),(0,s.jsx)("div",{className:"mt-1 w-[62px] shrink-0"})]})})]}),(0,s.jsxs)("div",{className:"absolute right-2 top-3 flex items-start gap-1",children:[(0,s.jsx)("button",{type:"button",onClick:e=>{e.stopPropagation(),f(p===g.id?null:g.id)},"data-menu-button":g.id,className:"text-white/40 hover:text-white/70","aria-label":"Open chat menu",children:(0,s.jsx)(d.a95,{className:"w-5 h-5"})}),p===g.id?(0,s.jsxs)("div",{"data-menu-id":g.id,className:"absolute right-0 z-50 mt-2 w-40 rounded-md bg-[#0b141a] border border-white/6 p-1 shadow-lg",children:[(0,s.jsx)("button",{type:"button",className:"w-full text-left px-3 py-2 text-sm text-white hover:bg-white/[0.03]",onClick:e=>{e.stopPropagation(),"function"==typeof m&&m(g.id,!!g.pinnedAt),f(null)},children:g.pinnedAt?"Unpin":"Pin"}),(0,s.jsx)("button",{type:"button",className:"w-full text-left px-3 py-2 text-sm text-red-400 hover:bg-white/[0.03]",onClick:e=>{if(e.stopPropagation(),!confirm("Delete this conversation?"))return void f(null);"function"==typeof u&&u(g.id),f(null)},children:"Delete"})]}):null]})]},g.id):null,b.map(e=>{var t;return(0,s.jsxs)("div",{className:"relative",children:[(0,s.jsxs)("button",{type:"button",className:"flex w-full items-start gap-3 rounded-[16px] px-4 py-3 pr-12 text-left transition "+(e.id===a?"bg-[#2e2f2f]":"bg-transparent hover:bg-white/[0.05]"),onClick:()=>i(e.id),children:[(0,s.jsx)(W,{src:e.contact.avatarUrl,label:e.contact.displayName}),(0,s.jsx)("div",{className:"min-w-0 flex-1",children:(0,s.jsxs)("div",{className:"grid grid-cols-[minmax(0,1fr)_auto] items-start gap-x-3",children:[(0,s.jsxs)("div",{className:"flex min-w-0 items-center gap-2",children:[(0,s.jsx)("h3",{className:"min-w-0 truncate font-medium text-white",children:e.contact.displayName}),(0,s.jsx)(x.zd,{chat:e,compact:!0})]}),(0,s.jsx)("div",{className:"flex w-[76px] shrink-0 items-center justify-end gap-2",children:(0,s.jsx)("span",{className:"block min-w-0 truncate text-[11px] text-white/35",children:(t=e.contact.lastMessageAt||e.updatedAt)?new Intl.DateTimeFormat("en-US",{hour:"2-digit",minute:"2-digit"}).format(new Date(t)):""})}),(0,s.jsx)("p",{className:"mt-1 min-w-0 truncate text-sm text-white/42",children:e.contact.lastMessagePreview||"No messages yet"}),(0,s.jsx)("div",{className:"mt-1 flex w-[76px] shrink-0 justify-end",children:e.contact.unreadCount?(0,s.jsx)("span",{className:"flex h-5 min-w-5 items-center justify-center rounded-full bg-brand-500 px-1.5 text-[11px] font-bold text-[#10251a]",children:e.contact.unreadCount}):null})]})})]}),(0,s.jsxs)("div",{className:"absolute right-2 top-3 flex items-start gap-1",children:[(0,s.jsx)("button",{type:"button",onClick:t=>{t.stopPropagation(),f(p===e.id?null:e.id)},"data-menu-button":e.id,className:"text-white/40 hover:text-white/70","aria-label":"Open chat menu",children:(0,s.jsx)(d.a95,{className:"w-5 h-5"})}),p===e.id?(0,s.jsxs)("div",{"data-menu-id":e.id,className:"absolute right-0 z-50 mt-2 w-40 rounded-md bg-[#0b141a] border border-white/6 p-1 shadow-lg",children:[(0,s.jsx)("button",{type:"button",className:"w-full text-left px-3 py-2 text-sm text-white hover:bg-white/[0.03]",onClick:t=>{t.stopPropagation(),"function"==typeof m&&m(e.id,!!e.pinnedAt),f(null)},children:e.pinnedAt?"Unpin":"Pin"}),(0,s.jsx)("button",{type:"button",className:"w-full text-left px-3 py-2 text-sm text-red-400 hover:bg-white/[0.03]",onClick:t=>{if(t.stopPropagation(),!confirm("Delete this conversation?"))return void f(null);"function"==typeof u&&u(e.id),f(null)},children:"Delete"})]}):null]})]},e.id)})]})]})]})}function M(e){let{src:t,label:a}=e;return t?(0,s.jsx)("img",{src:t,alt:a,className:"h-11 w-11 rounded-2xl object-cover"}):(0,s.jsx)("div",{className:"flex h-11 w-11 items-center justify-center rounded-2xl bg-[#2e2f2f] text-sm font-semibold text-white",children:String(a||"?").split(" ").map(e=>e[0]).join("").slice(0,2).toUpperCase()})}function U(e){let{contacts:t,loading:a,open:n,query:i,onQueryChange:l,onStartChat:r,onClose:o,startingContactId:c}=e,x=String(i||"").trim().toLowerCase(),m=t.filter(e=>!x||[e.displayName,e.externalId,e.lastMessagePreview].filter(Boolean).some(e=>e.toLowerCase().includes(x)));return(0,s.jsx)("aside",{className:"flex h-full shrink-0 overflow-hidden bg-[#161717] transition-[width,opacity] duration-300 ".concat(n?"w-[320px] opacity-100":"w-0 opacity-0"),children:(0,s.jsxs)("div",{className:"flex h-full w-[320px] min-w-[320px] flex-col transition-transform duration-300 ".concat(n?"translate-x-0":"translate-x-8"),children:[(0,s.jsxs)("div",{className:"px-5 py-5",children:[(0,s.jsxs)("div",{className:"flex items-start justify-between gap-3",children:[(0,s.jsxs)("div",{children:[(0,s.jsx)("p",{className:"text-[11px] uppercase tracking-[0.24em] text-white/35",children:"Contacts"}),(0,s.jsx)("h2",{className:"mt-2 text-lg font-semibold text-white",children:"Start new message"})]}),(0,s.jsx)("button",{type:"button",className:"flex h-8 w-8 items-center justify-center rounded-full bg-[#2e2f2f] text-xs leading-none text-white/60 transition hover:bg-[#3a3b3b] hover:text-white",onClick:o,title:"Close contacts","aria-label":"Close contacts",children:(0,s.jsx)(d.m6K,{className:"w-4 h-4"})})]}),(0,s.jsx)("div",{className:"mt-4 rounded-[22px] bg-[#2e2f2f] px-3 py-2",children:(0,s.jsx)("input",{className:"w-full border-none bg-transparent text-sm text-white outline-none placeholder:text-white/30",placeholder:"Search contacts",value:i,onChange:e=>l(e.target.value)})})]}),(0,s.jsxs)("div",{className:"flex-1 overflow-y-auto px-3 py-3",children:[a?(0,s.jsx)("p",{className:"px-3 py-4 text-sm text-white/45",children:"Loading contacts..."}):null,a||0!==m.length?null:(0,s.jsx)("p",{className:"px-3 py-4 text-sm leading-6 text-white/40",children:"No synced contacts yet. Connect your device and wait for WhatsApp sync to complete."}),(0,s.jsx)("div",{className:"space-y-2",children:m.map(e=>(0,s.jsxs)("button",{type:"button",className:"flex w-full items-center gap-3 rounded-[16px] bg-transparent px-3 py-3 text-left transition hover:bg-white/[0.05]",onClick:()=>r(e.id),children:[(0,s.jsx)(M,{src:e.avatarUrl,label:e.displayName}),(0,s.jsxs)("div",{className:"min-w-0 flex-1",children:[(0,s.jsxs)("div",{className:"flex items-center justify-between gap-3",children:[(0,s.jsx)("h3",{className:"truncate text-sm font-medium text-white",children:e.displayName}),e.hasChat?(0,s.jsx)("span",{className:"rounded-full bg-[#144d37] px-2 py-0.5 text-[10px] font-semibold uppercase tracking-[0.12em] text-white",children:"Chat"}):null]}),(0,s.jsx)("p",{className:"mt-1 truncate text-xs text-white/40",children:e.lastMessagePreview?e.lastMessagePreview:e.externalId.replace(/@.+$/,"")})]}),(0,s.jsx)("div",{className:"shrink-0 text-xs text-white/35",children:c===e.id?"...":"+"})]},e.id))})]})]})})}function q(e){let{open:t,onClose:a}=e,i=(0,o.C)(e=>e.token),[l,d]=(0,n.useState)(!1),[c,x]=(0,n.useState)(!1),[m,u]=(0,n.useState)("");if((0,n.useEffect)(()=>{if(!t)return;let e=!0;return(async()=>{d(!0);try{let t=await (0,r.n)("/api/agent/tools",{token:i});if(!e)return;u(t.content||"")}catch(e){alert(e.message||"Failed to load tools file")}finally{e&&d(!1)}})(),()=>{e=!1}},[t,i]),!t)return null;let h=async()=>{if(!i)return alert("Not authenticated");x(!0);try{await (0,r.n)("/api/agent/tools",{method:"PUT",token:i,body:{action:"replace",content:m}}),alert("TOOLS.md replaced"),a()}catch(e){alert(e.message||"Failed to save")}finally{x(!1)}},p=async()=>{if(!i)return alert("Not authenticated");x(!0);try{await (0,r.n)("/api/agent/tools",{method:"PUT",token:i,body:{action:"append",content:m}}),alert("Appended to TOOLS.md"),a()}catch(e){alert(e.message||"Failed to append")}finally{x(!1)}};return(0,s.jsx)("div",{className:"fixed inset-0 z-50 flex items-center justify-center bg-black/70 px-6 py-8",children:(0,s.jsxs)("div",{className:"flex h-[80vh] w-full max-w-[980px] flex-col overflow-hidden rounded-[20px] bg-[#161717] p-4",children:[(0,s.jsxs)("div",{className:"flex items-center justify-between px-2",children:[(0,s.jsx)("h3",{className:"text-lg font-semibold text-white",children:"Edit TOOLS.md"}),(0,s.jsx)("div",{className:"flex gap-2",children:(0,s.jsx)("button",{className:"rounded-full bg-white/5 px-3 py-1 text-sm text-white/80",onClick:a,children:"Close"})})]}),(0,s.jsx)("div",{className:"mt-3 flex-1 overflow-hidden",children:l?(0,s.jsx)("div",{className:"flex h-full items-center justify-center text-white/50",children:"Loading..."}):(0,s.jsx)("textarea",{className:"h-full w-full resize-none rounded-lg bg-[#0f1111] p-3 text-sm text-white outline-none",value:m,onChange:e=>u(e.target.value)})}),(0,s.jsxs)("div",{className:"mt-3 flex justify-end gap-2",children:[(0,s.jsx)("button",{className:"rounded-2xl bg-white/5 px-4 py-2 text-sm text-white/80",onClick:p,disabled:c,children:c?"Saving...":"Append"}),(0,s.jsx)("button",{className:"rounded-2xl bg-brand-500 px-4 py-2 text-sm font-semibold text-[#10251a]",onClick:h,disabled:c,children:c?"Saving...":"Replace"})]})]})})}let B=function(e){var t,a;let{open:i,onClose:l}=e,d=(0,o.C)(e=>e.token),c=(0,o.C)(e=>e.socket),[x,m]=(0,n.useState)(!1),[u,h]=(0,n.useState)([]),[p,f]=(0,n.useState)(null),[g,b]=(0,n.useState)(null),[w,y]=(0,n.useState)(!1),[v,j]=(0,n.useState)(!1);(0,n.useEffect)(()=>{if(!i)return;let e=!0;return(async()=>{m(!0);try{let t=await (0,r.n)("/api/terminal/history",{token:d});if(!e)return;h(t.items||[])}catch(e){}finally{e&&m(!1)}})(),()=>e=!1},[i,d]),(0,n.useEffect)(()=>{if(!c)return;let e=e=>{h(t=>t.find(t=>t.id===e.id)?t.map(t=>t.id===e.id?{...t,status:e.status}:t):[e,...t]),p&&e.id===p&&(async()=>{try{let t=await (0,r.n)("/api/terminal/".concat(e.id),{token:d});b(t.item||null)}catch(e){}})()};return c.on("terminal_result",e),c.on("terminal_request",e),()=>{try{c.off("terminal_result",e),c.off("terminal_request",e)}catch(e){}}},[c,d,p]);let N=async e=>{f(e),y(!0);try{let t=await (0,r.n)("/api/terminal/".concat(e),{token:d});b(t.item||null)}catch(e){b(null)}finally{y(!1)}},k=async()=>{try{let e=await (0,r.n)("/api/terminal/history",{token:d});h(e.items||[])}catch(e){}},C=async()=>{if(p){j(!0);try{await (0,r.n)("/api/terminal/".concat(p,"/approve"),{method:"POST",token:d});let e=await (0,r.n)("/api/terminal/".concat(p),{token:d});b(e.item||null),await k()}catch(e){}finally{j(!1)}}},S=async()=>{if(p&&window.confirm("Are you sure you want to deny this terminal request?")){j(!0);try{await (0,r.n)("/api/terminal/".concat(p,"/deny"),{method:"POST",token:d});let e=await (0,r.n)("/api/terminal/".concat(p),{token:d});b(e.item||null),await k()}catch(e){}finally{j(!1)}}},A=async()=>{if(p){j(!0);try{await (0,r.n)("/api/terminal/".concat(p,"/rerun"),{method:"POST",token:d});let e=await (0,r.n)("/api/terminal/".concat(p),{token:d});b(e.item||null),await k()}catch(e){}finally{j(!1)}}};return i?(0,s.jsx)("div",{className:"fixed inset-0 z-50 flex items-center justify-center bg-black/70 px-6 py-8",children:(0,s.jsxs)("div",{className:"flex h-[80vh] w-full max-w-[1100px] flex-col overflow-hidden rounded-[20px] bg-[#161717] p-4",children:[(0,s.jsxs)("div",{className:"flex items-center justify-between px-2",children:[(0,s.jsx)("h3",{className:"text-lg font-semibold text-white",children:"Terminal Monitor"}),(0,s.jsx)("div",{className:"flex gap-2",children:(0,s.jsx)("button",{className:"rounded-full bg-white/5 px-3 py-1 text-sm text-white/80",onClick:l,children:"Close"})})]}),(0,s.jsxs)("div",{className:"mt-3 flex h-full gap-4",children:[(0,s.jsx)("div",{className:"w-1/3 overflow-auto rounded-lg bg-[#0f1111] p-3",children:x?(0,s.jsx)("div",{className:"text-white/50",children:"Loading..."}):0===u.length?(0,s.jsx)("div",{className:"text-white/50",children:"No terminal records yet."}):u.map(e=>(0,s.jsxs)("div",{className:"mb-2 cursor-pointer rounded-md p-2 ".concat(p===e.id?"bg-white/5":"hover:bg-white/3"),onClick:()=>N(e.id),children:[(0,s.jsxs)("div",{className:"flex items-center justify-between",children:[(0,s.jsx)("div",{className:"text-sm text-white/90 truncate",children:e.command}),(0,s.jsx)("div",{className:"text-xs text-white/60",children:new Date(e.requestedAt).toLocaleString()})]}),(0,s.jsxs)("div",{className:"mt-1 text-xs text-white/50",children:["Status: ",e.status]})]},e.id))}),(0,s.jsx)("div",{className:"flex-1 overflow-auto rounded-lg bg-[#0f1111] p-4",children:w?(0,s.jsx)("div",{className:"text-white/50",children:"Loading details..."}):g?(0,s.jsxs)("div",{children:[(0,s.jsxs)("div",{className:"mb-3 flex items-center justify-between",children:[(0,s.jsxs)("div",{children:[(0,s.jsx)("div",{className:"text-sm font-medium text-white",children:g.command}),(0,s.jsxs)("div",{className:"text-xs text-white/60",children:["Requested: ",new Date(g.requestedAt).toLocaleString()]}),(0,s.jsxs)("div",{className:"text-xs text-white/60",children:["Executed:"," ",g.executedAt?new Date(g.executedAt).toLocaleString():"N/A"]}),(0,s.jsxs)("div",{className:"text-xs text-white/60",children:["Status: ",g.status]})]}),(0,s.jsxs)("div",{className:"flex items-center gap-2",children:[(0,s.jsx)("button",{className:"rounded-full bg-yellow-600 px-3 py-1 text-sm text-white",onClick:A,disabled:v,children:"Rerun"}),"pending"===g.status&&(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)("button",{className:"rounded-full bg-emerald-600 px-3 py-1 text-sm text-white",onClick:C,disabled:v,children:"Approve"}),(0,s.jsx)("button",{className:"rounded-full bg-red-600 px-3 py-1 text-sm text-white",onClick:S,disabled:v,children:"Deny"})]})]})]}),(0,s.jsxs)("div",{className:"mb-4",children:[(0,s.jsx)("p",{className:"mb-2 text-sm text-white/70",children:"Stdout"}),(0,s.jsx)("pre",{className:"max-h-48 overflow-auto rounded bg-black/60 p-3 text-xs text-white/70",children:(null==(t=g.result)?void 0:t.stdout)||"(no stdout)"})]}),(0,s.jsxs)("div",{children:[(0,s.jsx)("p",{className:"mb-2 text-sm text-white/70",children:"Stderr"}),(0,s.jsx)("pre",{className:"max-h-48 overflow-auto rounded bg-black/60 p-3 text-xs text-white/70",children:(null==(a=g.result)?void 0:a.stderr)||"(no stderr)"})]})]}):(0,s.jsx)("div",{className:"text-white/50",children:"Select a record to view details."})})]})]})}):null};function z(e){let{mediaUrl:t,originalName:a,createdAt:i,size:l="h-56 w-56"}=e,[r,o]=(0,n.useState)(120),[c,x]=(0,n.useState)(!1);return((0,n.useEffect)(()=>{let e=new Date(i).getTime(),t=()=>{let t=120-Math.floor((Date.now()-e)/1e3);t<=0?(o(0),x(!0)):(o(t),x(!1))};t();let a=setInterval(t,1e3);return()=>clearInterval(a)},[i]),c)?(0,s.jsxs)("div",{className:"flex ".concat(l," flex-col items-center justify-center rounded-2xl bg-[#2e2f2f] p-6 text-center shadow-lg mx-auto"),children:[(0,s.jsx)(d.vSZ,{className:"mb-4 text-3xl text-white/30"}),(0,s.jsx)("p",{className:"mb-3 text-xs font-medium text-white/50",children:"QR Expired"}),(0,s.jsx)("button",{onClick:()=>window.location.reload(),className:"rounded-full bg-brand-500 px-4 py-1.5 text-[11px] font-bold text-[#10251a] shadow-md hover:bg-brand-600 transition",children:"Refresh"})]}):(0,s.jsxs)("div",{className:"relative ".concat(l," overflow-hidden rounded-2xl bg-white p-4 shadow-lg mx-auto"),children:[(0,s.jsx)("img",{src:t,alt:a,className:"h-full w-full object-contain"}),(0,s.jsx)("div",{className:"absolute bottom-2 right-2 flex items-center gap-1.5 rounded-full bg-black/70 px-2 py-0.5 text-[10px] font-bold text-white backdrop-blur-sm",children:(0,s.jsxs)("span",{className:r<=10?"text-red-400 animate-pulse":"",children:[r,"s"]})})]})}function K(e){let{status:t}=e,a={ready:"bg-brand-500/15 text-brand-100 ring-1 ring-brand-400/20",connecting:"bg-amber-500/15 text-amber-100 ring-1 ring-amber-400/20",disconnected:"bg-white/8 text-white/60 ring-1 ring-white/10",error:"bg-red-500/15 text-red-100 ring-1 ring-red-400/20"};return(0,s.jsx)("span",{className:"rounded-full px-2.5 py-1 text-[11px] font-medium capitalize tracking-[0.08em] ".concat(a[t]||a.disconnected),children:t})}function H(e){return!!((null==e?void 0:e.status)==="ready"||(null==e?void 0:e.phoneNumber)||(null==e?void 0:e.lastSeenAt)||(null==e?void 0:e.lastConnectedAt))}function Q(e){let{label:t}=e;return(0,s.jsx)("div",{className:"flex h-11 w-11 shrink-0 items-center justify-center rounded-2xl bg-[#2e2f2f] text-sm font-semibold text-white",children:String(t||"?").split(" ").map(e=>e[0]).join("").slice(0,2).toUpperCase()})}function G(e){return e?new Intl.DateTimeFormat("en-US",{day:"2-digit",month:"short",year:"numeric",hour:"2-digit",minute:"2-digit"}).format(new Date(e)):"Never"}let Z='app.post("/wanie-webhook", express.json(), async (req, res) => {\n const key = req.get("x-wanie-webhook-key") || req.get("x-openwa-webhook-key");\n if (key !== process.env.WANIE_WEBHOOK_KEY) {\n return res.status(401).json({ ok: false });\n }\n\n const { chat, message } = req.body;\n const isTelegram = String(chat.contact?.externalId || "").startsWith("tg:");\n const hasMedia = Boolean(message.mediaFile);\n\n console.log("Incoming Wanie message", {\n chatId: chat.id,\n channel: isTelegram ? "telegram" : "whatsapp",\n from: message.sender,\n text: message.body,\n type: message.type,\n media: hasMedia\n ? {\n id: message.mediaFile.id,\n mimeType: message.mediaFile.mimeType,\n originalName: message.mediaFile.originalName,\n size: message.mediaFile.size,\n }\n : null,\n });\n\n // Reply later with:\n // POST /api/chats/{chat.id}/messages/send\n // { "body": "Reply from external app", "type": "text" }\n\n res.json({ ok: true });\n});',Y='{\n "chat": {\n "id": "chat_123",\n "title": "Customer Name",\n "sessionId": "session_123",\n "transportType": "whatsapp",\n "contact": {\n "externalId": "6281234567890@c.us",\n "displayName": "Customer Name"\n },\n "updatedAt": "2026-05-15T08:30:00.000Z"\n },\n "message": {\n "id": "msg_123",\n "chatId": "chat_123",\n "sessionId": "session_123",\n "sender": "6281234567890@c.us",\n "receiver": "6289876543210@c.us",\n "body": "Ini foto area yang mau dibersihkan",\n "type": "image",\n "direction": "inbound",\n "createdAt": "2026-05-15T08:30:00.000Z",\n "mediaFileId": "media_123",\n "mediaFile": {\n "id": "media_123",\n "fileName": "photo.jpg",\n "originalName": "photo.jpg",\n "mimeType": "image/jpeg",\n "size": 248120,\n "relativePath": "media/photo.jpg",\n "createdAt": "2026-05-15T08:30:00.000Z"\n },\n "statuses": []\n }\n}';function J(e){var t;let{open:a,sessions:l,activeSessionId:d,onClose:c,onSelect:x,onConnect:m,onDisconnect:u,onClearSession:h,onDeleteSession:p,connectLoading:f,qrLoading:g,syncingWorkspace:b,sessionName:w,sessionPhone:y,sessionTransport:v,metaPhoneNumberId:j,metaBusinessAccountId:N,metaAccessToken:k,metaVerifyToken:C,metaAppSecret:S,onSessionNameChange:A,onSessionPhoneChange:T,onSessionTransportChange:E,onMetaPhoneNumberIdChange:P,onMetaBusinessAccountIdChange:I,onMetaAccessTokenChange:R,onMetaVerifyTokenChange:O,onMetaAppSecretChange:_,onCreateSession:L,apiKeys:W,apiKeysLoading:F,apiKeyName:M,apiKeySecret:U,onApiKeyNameChange:J,onCreateApiKey:V,onRevokeApiKey:X,revokingKeyId:$,webhookUrl:ee,webhookEnabled:et,webhookMethod:ea,webhookHeaders:es,webhookBodyTemplate:en,webhookApiKey:ei,onWebhookUrlChange:el,onWebhookEnabledChange:er,onWebhookMethodChange:eo,onWebhookHeadersChange:ed,onWebhookBodyTemplateChange:ec,onWebhookApiKeyChange:ex,onSaveWebhook:em,onDeleteWebhook:eu,webhookLoading:eh}=e,[ep,ef]=(0,n.useState)(!1),[eg,eb]=(0,n.useState)(null),ew=(0,o.C)(e=>e.token),ey=(0,o.C)(e=>e.socket),ev=(0,o.C)(e=>e.setActiveChat),ej=(0,o.C)(e=>e.upsertChat),eN=(0,o.C)(e=>e.terminalAutoApproveAll),ek=(0,o.C)(e=>e.setTerminalAutoApproveAll),eC=(0,o.C)(e=>e.defaultAiProviderId),eS=(0,o.C)(e=>e.defaultAiModel),eA=(0,o.C)(e=>e.setDefaultAiProvider),eT=(0,o.C)(e=>e.setDefaultAiModel);(0,o.C)(e=>e.logout),(0,i.useRouter)();let[eE,eP]=(0,n.useState)([]),[eI,eR]=(0,n.useState)(!1),[eO,e_]=(0,n.useState)(""),[eD,eL]=(0,n.useState)(""),[eW,eF]=(0,n.useState)(""),[eM,eU]=(0,n.useState)(""),[eq,eB]=(0,n.useState)(""),[ez,eK]=(0,n.useState)(""),[eH,eQ]=(0,n.useState)(!1),[eG,eZ]=(0,n.useState)(!1),[eY,eJ]=(0,n.useState)({}),[eV,eX]=(0,n.useState)(!0),[e$,e0]=(0,n.useState)(!1),[e1,e2]=(0,n.useState)(""),[e5,e3]=(0,n.useState)(!0),[e4,e6]=(0,n.useState)([]),[e8,e7]=(0,n.useState)(!1),[e9,te]=(0,n.useState)(!1),[tt,ta]=(0,n.useState)(null),[ts,tn]=(0,n.useState)([]),[ti,tl]=(0,n.useState)(!1),[tr,to]=(0,n.useState)(null),[td,tc]=(0,n.useState)(!1),[tx,tm]=(0,n.useState)(""),[tu,th]=(0,n.useState)(!1),[tp,tf]=(0,n.useState)(""),[tg,tb]=(0,n.useState)([]),[tw,ty]=(0,n.useState)(!1),[tv,tj]=(0,n.useState)(null),[tN,tk]=(0,n.useState)(null),[tC,tS]=(0,n.useState)(""),[tA,tT]=(0,n.useState)(!1),[tE,tP]=(0,n.useState)(!1),[tI,tR]=(0,n.useState)(null),[tO,t_]=(0,n.useState)({}),[tD,tL]=(0,n.useState)({}),[tW,tF]=(0,n.useState)({}),[tM,tU]=(0,n.useState)(null),[tq,tB]=(0,n.useState)(!1),[tz,tK]=(0,n.useState)(!1),[tH,tQ]=(0,n.useState)(!1),[tG,tZ]=(0,n.useState)("devices"),tY=eE.find(e=>e.id===eC),tJ=eS&&eY[eC]?(null==(t=eY[eC].find(e=>e.id===eS))?void 0:t.name)||eS:eS||null,tV=l.find(e=>e.id===d),tX=window.location.origin,t$="Integrate this application with Wanie as a messaging gateway.\n\nWanie base URL:\n".concat(tX,"\n\nRead these docs first:\n- ").concat(tX,"/docs/readme#webhooks\n- ").concat(tX,"/docs/json\n\nUse X-API-Key for authentication.\nConfigure a Wanie webhook so this application receives incoming customer messages.\nVerify the x-wanie-webhook-key header on every webhook request.\nStore chat.id from the webhook payload.\nReply to the customer with POST ").concat(tX,"/api/chats/{chatId}/messages/send.\nFor media replies, either pass a public mediaUrl or upload to POST ").concat(tX,"/api/media and then send the returned mediaFileId.\nDo not use Wanie internal CRM automation for this integration; set Wanie CRM automation to Off.\nTreat WhatsApp and non-admin Telegram customer messages the same way: receive them from the webhook and reply through the Wanie API.");function t0(e){if(null==e||""===e)return"";let t=Number(e);return Number.isFinite(t)?t<0?"0":t>2?"2":String(Math.round(10*t)/10):""}async function t1(e,t){await navigator.clipboard.writeText(t),eb(e),setTimeout(()=>eb(null),1500)}async function t2(){await navigator.clipboard.writeText(t$),eb("gateway-agent"),setTimeout(()=>eb(null),1500)}async function t5(){if(ew){tl(!0);try{let e=await (0,r.n)("/api/webhook/deliveries?limit=10",{token:ew});tn(e.deliveries||[])}catch(e){tn([])}finally{tl(!1)}}}async function t3(){ew&&e6((await (0,r.n)("/api/telegram/bots",{token:ew})).bots||[])}async function t4(e){if(ew&&e&&window.confirm("Delete this Telegram bot token and stop the running bot?")){ta(e);try{await (0,r.n)("/api/telegram/bots/".concat(e),{method:"DELETE",token:ew}),await t3()}catch(e){alert(e.message||"Failed to delete Telegram bot")}finally{ta(null)}}}async function t6(e){if(ew&&e){to(e);try{await (0,r.n)("/api/webhook/deliveries/".concat(e,"/retry"),{method:"POST",token:ew}),await t5()}catch(e){alert(e.message||"Failed to retry webhook delivery")}finally{to(null)}}}async function t8(){if(ew){tc(!0);try{let e=await (0,r.n)("/api/webhook/test",{method:"POST",token:ew});await t5(),(null==e?void 0:e.ok)||alert((null==e?void 0:e.error)||"Webhook test failed")}catch(e){alert(e.message||"Webhook test failed")}finally{tc(!1)}}}async function t7(){if(ew&&tx.trim()){th(!0),tf("");try{let e=await (0,r.n)("/api/webhook/generate-config",{method:"POST",token:ew,body:{prompt:tx}}),t=(null==e?void 0:e.config)||{};el(t.url||""),eo(t.method||"POST"),ed(t.headers&&Object.keys(t.headers).length?JSON.stringify(t.headers,null,2):""),ec(t.bodyTemplate||""),tf(t.notes||"")}catch(e){alert(e.message||"Failed to generate webhook config")}finally{th(!1)}}}async function t9(){if(ew){ty(!0);try{let e=await (0,r.n)("/api/outbound-deliveries?limit=20",{token:ew});tb(e.deliveries||[])}catch(e){tb([])}finally{ty(!1)}}}async function ae(e){if(ew&&e){tj(e);try{await (0,r.n)("/api/outbound-deliveries/".concat(e,"/retry"),{method:"POST",token:ew}),await t9()}catch(e){alert(e.message||"Failed to retry outbound delivery")}finally{tj(null)}}}async function at(e){if(ew&&e){tk(e);try{await (0,r.n)("/api/outbound-deliveries/".concat(e,"/cancel"),{method:"POST",token:ew}),await t9()}catch(e){alert(e.message||"Failed to cancel outbound delivery")}finally{tk(null)}}}(0,n.useEffect)(()=>{if(!a)return;let e=!0;return(async()=>{if(ew){eR(!0);try{let t=await (0,r.n)("/api/ai-providers",{token:ew});if(!e)return;eP(t.providers||[])}catch(e){}finally{e&&eR(!1)}}})(),()=>{e=!1}},[a,ew]),(0,n.useEffect)(()=>{if(!a)return;let e=!0;return(async()=>{try{let t=await (0,r.n)("/api/auth/config",{token:ew});if(!e)return;eX(!1!==t.allowRegistration)}catch(t){if(!e)return;eX(!0)}})(),()=>{e=!1}},[a,ew]),(0,n.useEffect)(()=>{if(!a||!ew)return;let e=!0;return(async()=>{e7(!0);try{var t,a;let[s,n]=await Promise.all([(0,r.n)("/api/telegram/config",{token:ew}),(0,r.n)("/api/telegram/bots",{token:ew})]);if(!e)return;let i=(null==(t=s.config)?void 0:t.adminTelegramIds)||[];e2(i.join("\n")),e3((null==(a=s.config)?void 0:a.aiReplyEnabled)!==!1),e6(n.bots||[])}catch(t){if(!e)return;e2(""),e3(!0),e6([])}finally{e&&e7(!1)}})(),()=>{e=!1}},[a,ew]),(0,n.useEffect)(()=>{a&&"webhooks"===tG&&ew&&t5()},[a,tG,ew]),(0,n.useEffect)(()=>{a&&"deliveries"===tG&&ew&&t9()},[a,tG,ew]),(0,n.useEffect)(()=>{if(!ey||!a)return;let e=function(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=e.delivery;(null==t?void 0:t.id)&&tn(e=>(e.some(e=>e.id===t.id)?e.map(e=>e.id===t.id?{...e,...t}:e):[t,...e]).slice(0,10))},t=function(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=e.delivery;(null==t?void 0:t.id)&&tb(e=>(e.some(e=>e.id===t.id)?e.map(e=>e.id===t.id?{...e,...t}:e):[t,...e]).slice(0,20))};return ey.on("webhook_delivery_update",e),ey.on("outbound_delivery_update",t),()=>{ey.off("webhook_delivery_update",e),ey.off("outbound_delivery_update",t)}},[ey,a]);let aa=async e=>{if(e.preventDefault(),!ew)return;let t=String(e1||"").split(/[,;\s]+/).map(e=>e.trim()).filter(Boolean);te(!0);try{var a,s;let e=await (0,r.n)("/api/telegram/config",{method:"POST",token:ew,body:{adminTelegramIds:t,aiReplyEnabled:e5}});e2(((null==(a=e.config)?void 0:a.adminTelegramIds)||t).join("\n")),e3((null==(s=e.config)?void 0:s.aiReplyEnabled)!==!1),alert("Telegram settings saved.")}catch(e){alert(e.message||"Failed to save Telegram admin IDs")}finally{te(!1)}},as=async e=>{if(e.preventDefault(),ew){eZ(!0);try{let e={};eW&&eW.trim()&&(e.apiKey=eW.trim()),eM&&eM.trim()&&(e.host=eM.trim()),eq&&eq.trim()&&(e.model=eq.trim()),""!==ez&&(e.temperature=Number(ez));let t=await (0,r.n)("/api/ai-providers",{method:"POST",token:ew,body:{provider:eD,name:eO,config:e}});eP(e=>[t.provider,...e]),e_(""),eL(""),eF(""),eU(""),eB(""),eK(""),eQ(!1)}catch(e){alert(e.message||"Failed to create provider")}finally{eZ(!1)}}},an=async e=>{if(e.preventDefault(),ew){if(!tC)return void alert("New password is required.");tT(!0);try{await (0,r.n)("/api/settings/reset-password",{method:"POST",token:ew,body:{password:tC}}),alert("Password reset successfully."),tS("")}catch(e){alert(e.message||"Failed to reset password")}finally{tT(!1)}}},ai=async()=>{if(ew&&"YES"===window.prompt("Type YES to confirm resetting all Wanie data. This action is permanent.")){tP(!0);try{await (0,r.n)("/api/settings/reset-all",{method:"POST",token:ew,body:{confirm:"YES"}}),window.location.reload()}catch(e){alert(e.message||"Failed to reset all data")}finally{tP(!1)}}},al=async e=>{if(ew&&confirm("Delete this provider?"))try{await (0,r.n)("/api/ai-providers/".concat(e),{method:"DELETE",token:ew}),eP(t=>t.filter(t=>t.id!==e))}catch(e){alert(e.message||"Failed to delete provider")}},ar=async e=>{if(ew){tR(e);try{let t=await (0,r.n)("/api/ai-providers/".concat(e,"/models"),{token:ew});eJ(a=>({...a,[e]:t.models||[]}))}catch(e){alert(e.message||"Failed to fetch models")}finally{tR(null)}}},ao=async(e,t)=>{var a;if(!ew||!(null==e?void 0:e.id))return;let s=Object.prototype.hasOwnProperty.call(tD,e.id),n=t0(void 0!==t?t:s?tD[e.id]:null==(a=e.config)?void 0:a.temperature),i={...e.config&&"object"==typeof e.config?e.config:{}};""===n?delete i.temperature:i.temperature=Number(n),tU(e.id);try{let t=await (0,r.n)("/api/ai-providers/".concat(e.id),{method:"PUT",token:ew,body:{config:i}});eP(a=>a.map(a=>a.id===e.id?t.provider:a)),tL(t=>({...t,[e.id]:n}))}catch(e){alert(e.message||"Failed to save provider temperature")}finally{tU(null)}},ad=async(e,t,a)=>{var s,n;if(!ew||!(null==e?void 0:e.id)||!t)return;let i=Object.prototype.hasOwnProperty.call(tW,e.id),l=t0(void 0!==a?a:i?tW[e.id]:null==(n=e.config)||null==(s=n.modelTemperatures)?void 0:s[t]),o={...e.config&&"object"==typeof e.config?e.config:{}},d=o.modelTemperatures&&"object"==typeof o.modelTemperatures?{...o.modelTemperatures}:{};""===l?delete d[t]:d[t]=Number(l),Object.keys(d).length?o.modelTemperatures=d:delete o.modelTemperatures,tU(e.id);try{let t=await (0,r.n)("/api/ai-providers/".concat(e.id),{method:"PUT",token:ew,body:{config:o}});eP(a=>a.map(a=>a.id===e.id?t.provider:a)),tF(t=>({...t,[e.id]:l}))}catch(e){alert(e.message||"Failed to save model temperature")}finally{tU(null)}};return a?(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)("div",{className:"fixed inset-0 z-50 flex items-center justify-center bg-black/70 px-6 py-8 backdrop-blur-sm",children:(0,s.jsxs)("div",{className:"flex h-full max-h-[840px] w-full max-w-[1080px] flex-col overflow-hidden rounded-[32px] bg-[#161717] shadow-[0_40px_120px_rgba(0,0,0,0.5)]",children:[(0,s.jsxs)("div",{className:"flex items-center justify-between px-6 py-5",children:[(0,s.jsxs)("div",{className:"flex items-center gap-4",children:[(0,s.jsx)("div",{className:"flex h-12 w-12 items-center justify-center rounded-2xl bg-white p-2 shadow-[0_16px_40px_rgba(0,0,0,0.18)]",children:(0,s.jsx)(D.F,{variant:"square",alt:"Wanie",className:"h-full w-full rounded-xl"})}),(0,s.jsxs)("div",{children:[(0,s.jsx)("p",{className:"text-[11px] uppercase tracking-[0.26em] text-white/35",children:"Settings"}),(0,s.jsx)("h2",{className:"mt-2 text-xl font-semibold text-white",children:"Wanie Devices"}),(0,s.jsxs)("div",{className:"mt-1 text-sm text-white/60",children:[(0,s.jsx)("span",{className:"font-medium text-white/85",children:"AI:"})," ",tY?(0,s.jsxs)("span",{children:[tY.name," (",tY.provider,")",tJ?" — ".concat(tJ):""]}):(0,s.jsx)("span",{children:"None active"})]}),b?(0,s.jsx)("div",{className:"mt-3 rounded-2xl bg-[#22302a] px-4 py-3 text-sm text-emerald-200 ring-1 ring-emerald-400/15",children:"Syncing WhatsApp chats and contacts..."}):null]})]}),(0,s.jsxs)("div",{className:"flex gap-2",children:[(0,s.jsx)("button",{type:"button",className:"rounded-2xl bg-[#2e2f2f] px-3 py-1 text-sm text-white/70 transition hover:bg-[#3a3b3b] hover:text-white",onClick:async()=>{if(ew&&confirm("Start a new Assistant conversation?")){tQ(!0);try{let e=(await (0,r.n)("/api/assistant/sessions",{method:"POST",token:ew,body:{}})).chat;e&&e.id&&(ej(e),ev(e.id),c())}catch(e){alert(e.message||"Failed to create assistant session")}finally{tQ(!1)}}},disabled:tH,children:tH?"Creating...":"New Assistant"}),(0,s.jsx)("button",{type:"button",className:"rounded-2xl bg-[#2e2f2f] px-3 py-1 text-sm text-white/70 transition hover:bg-[#3a3b3b] hover:text-white",onClick:()=>tB(!0),children:"Edit Tools"}),(0,s.jsx)("button",{type:"button",className:"rounded-2xl bg-[#2e2f2f] px-3 py-1 text-sm text-white/70 transition hover:bg-[#3a3b3b] hover:text-white",onClick:()=>tK(!0),children:"Terminal Monitor"}),(0,s.jsx)("button",{type:"button",className:"rounded-full bg-[#2e2f2f] px-4 py-2 text-sm text-white/70 transition hover:bg-[#3a3b3b] hover:text-white",onClick:c,children:"Close"})]})]}),(0,s.jsxs)("div",{className:"grid min-h-0 flex-1 gap-0 md:grid-cols-[1.2fr_0.8fr]",children:[(0,s.jsx)("div",{className:"min-h-0 px-5 py-5",children:(0,s.jsx)("div",{className:"h-full overflow-y-auto pr-1",children:(0,s.jsx)("div",{className:"space-y-3",children:l.map(e=>{var t;return(0,s.jsxs)("button",{type:"button",className:"w-full rounded-[18px] px-4 py-4 text-left transition ".concat(e.id===d?"bg-[#2e2f2f]":"bg-transparent hover:bg-white/[0.04]"),onClick:()=>x(e.id),children:[(0,s.jsxs)("div",{className:"flex items-start gap-3",children:[(0,s.jsx)(Q,{label:e.name}),(0,s.jsxs)("div",{className:"min-w-0 flex-1",children:[(0,s.jsxs)("div",{className:"flex items-center justify-between gap-3",children:[(0,s.jsx)("h3",{className:"truncate font-medium text-white",children:e.name}),(0,s.jsx)(K,{status:e.status})]}),(0,s.jsx)("p",{className:"mt-1 text-sm text-white/45",children:e.phoneNumber||"Waiting for WhatsApp pairing"}),(0,s.jsxs)("p",{className:"mt-2 text-xs uppercase tracking-[0.16em] text-white/30",children:["Transport: ","mock"===(t=e.transportType)?"Mock":"whatsapp_cloud"===t?"WhatsApp Official API":"WhatsApp Web"]}),(0,s.jsxs)("div",{className:"mt-2 grid gap-1 text-xs text-white/35",children:[(0,s.jsxs)("p",{children:["Health:"," ",G(e.lastHealthCheckAt)]}),(0,s.jsxs)("p",{children:["Last seen: ",G(e.lastSeenAt)]}),e.reconnectAttempts?(0,s.jsxs)("p",{children:["Reconnect attempts:"," ",e.reconnectAttempts]}):null]}),e.lastError?(0,s.jsx)("p",{className:"mt-3 rounded-2xl bg-red-500/10 px-3 py-2 text-sm text-red-100",children:e.lastError}):null]})]}),(0,s.jsxs)("div",{className:"mt-4 flex gap-2 flex-wrap",children:[(0,s.jsx)("button",{type:"button",className:H(e)?"rounded-2xl bg-sky-500/15 px-4 py-2 text-sm font-semibold text-sky-100 ring-1 ring-sky-400/25 transition hover:bg-sky-500/25 disabled:opacity-60":"rounded-2xl bg-brand-500 px-4 py-2 text-sm font-semibold text-[#10251a] transition hover:bg-brand-600 disabled:opacity-60",disabled:!!f&&f===e.id||"whatsapp_cloud"===e.transportType,onClick:t=>{t.stopPropagation(),m(e.id)},children:"whatsapp_cloud"===e.transportType?"Configured":f===e.id?H(e)?"Reconnecting...":"Connecting...":H(e)?"Reconnect":"Connect"}),(0,s.jsx)("button",{type:"button",className:"rounded-2xl bg-[#2e2f2f] px-4 py-2 text-sm text-white/75 disabled:opacity-60",disabled:!!f&&f===e.id,onClick:t=>{t.stopPropagation(),u(e.id)},children:f===e.id?"Disconnecting...":"Disconnect"}),(0,s.jsx)("button",{type:"button",className:"rounded-2xl bg-yellow-700 px-4 py-2 text-sm text-white/90",onClick:t=>{t.stopPropagation(),h(e.id)},children:"Clear Session"}),(0,s.jsx)("button",{type:"button",className:"rounded-2xl bg-red-700 px-4 py-2 text-sm text-white/90",onClick:t=>{t.stopPropagation(),p(e.id)},children:"Delete Device"})]})]},e.id)})})})}),(0,s.jsxs)("div",{className:"min-h-0 overflow-y-auto px-6 py-5",children:[(0,s.jsxs)("div",{className:"mb-4 flex flex-wrap gap-2",children:[(0,s.jsx)("button",{type:"button",className:"rounded-2xl px-4 py-2 text-sm ".concat("devices"===tG?"bg-white/5 text-white":"bg-transparent text-white/60 hover:bg-white/[0.04]"),onClick:()=>tZ("devices"),children:"Devices"}),(0,s.jsx)("button",{type:"button",className:"rounded-2xl px-4 py-2 text-sm ".concat("api"===tG?"bg-white/5 text-white":"bg-transparent text-white/60 hover:bg-white/[0.04]"),onClick:()=>tZ("api"),children:"API Access"}),(0,s.jsx)("button",{type:"button",className:"rounded-2xl px-4 py-2 text-sm ".concat("webhooks"===tG?"bg-white/5 text-white":"bg-transparent text-white/60 hover:bg-white/[0.04]"),onClick:()=>tZ("webhooks"),children:"Webhooks"}),(0,s.jsx)("button",{type:"button",className:"rounded-2xl px-4 py-2 text-sm ".concat("deliveries"===tG?"bg-white/5 text-white":"bg-transparent text-white/60 hover:bg-white/[0.04]"),onClick:()=>tZ("deliveries"),children:"Deliveries"}),(0,s.jsx)("button",{type:"button",className:"rounded-2xl px-4 py-2 text-sm ".concat("telegram"===tG?"bg-white/5 text-white":"bg-transparent text-white/60 hover:bg-white/[0.04]"),onClick:()=>tZ("telegram"),children:"Telegram"}),(0,s.jsx)("button",{type:"button",className:"rounded-2xl px-4 py-2 text-sm ".concat("ai"===tG?"bg-white/5 text-white":"bg-transparent text-white/60 hover:bg-white/[0.04]"),onClick:()=>tZ("ai"),children:"AI Providers"}),(0,s.jsx)("button",{type:"button",className:"rounded-2xl px-4 py-2 text-sm ".concat("advanced"===tG?"bg-white/5 text-white":"bg-transparent text-white/60 hover:bg-white/[0.04]"),onClick:()=>tZ("advanced"),children:"Advanced"})]}),"devices"===tG&&(0,s.jsxs)(s.Fragment,{children:[(0,s.jsxs)("div",{className:"rounded-[28px] bg-[#161717] p-4",children:[(0,s.jsx)("p",{className:"text-[11px] uppercase tracking-[0.24em] text-white/35",children:"Pairing QR"}),!g||(null==tV?void 0:tV.qrCode)?(null==tV?void 0:tV.qrCode)?(0,s.jsx)("div",{className:"mt-4",children:(0,s.jsx)(z,{mediaUrl:tV.qrCode,originalName:"WhatsApp QR",createdAt:tV.updatedAt||new Date().toISOString(),size:"h-80 w-80"})}):(0,s.jsx)("div",{className:"mt-4 rounded-[24px] bg-[#2e2f2f] px-4 py-16 text-center text-sm leading-6 text-white/40",children:"QR code for pairing will appear here when session is connecting."}):(0,s.jsxs)("div",{className:"mt-4 rounded-[24px] bg-[#2e2f2f] px-4 py-16 text-center text-sm leading-6 text-white/40",children:[(0,s.jsx)("div",{className:"mx-auto mb-4 flex h-12 w-12 items-center justify-center rounded-full border border-white/10",children:(0,s.jsx)("div",{className:"h-5 w-5 animate-spin rounded-full border-2 border-white/20 border-t-white"})}),"Waiting for your QR code..."]})]}),(0,s.jsxs)("form",{className:"mt-5 space-y-3 rounded-[28px] bg-[#161717] p-4",onSubmit:L,children:[(0,s.jsxs)("div",{children:[(0,s.jsx)("p",{className:"mb-2 text-[11px] uppercase tracking-[0.24em] text-white/35",children:"Add device"}),(0,s.jsx)("input",{className:"w-full rounded-[22px] bg-[#2e2f2f] px-4 py-3 text-sm text-white outline-none placeholder:text-white/30",placeholder:"Session name, e.g. Sales Team",value:w,onChange:e=>A(e.target.value),required:!0})]}),(0,s.jsxs)("select",{className:"w-full rounded-[22px] bg-[#2e2f2f] px-4 py-3 text-sm text-white outline-none",value:v||"wwebjs",onChange:e=>E(e.target.value),children:[(0,s.jsx)("option",{value:"wwebjs",children:"WhatsApp Web QR"}),(0,s.jsx)("option",{value:"whatsapp_cloud",children:"WhatsApp Official API"})]}),(0,s.jsx)("input",{className:"w-full rounded-[22px] bg-[#2e2f2f] px-4 py-3 text-sm text-white outline-none placeholder:text-white/30",placeholder:"whatsapp_cloud"===v?"Display WhatsApp number, e.g. 62812...":"WhatsApp number (optional)",value:y,onChange:e=>T(e.target.value)}),"whatsapp_cloud"===v?(0,s.jsxs)("div",{className:"space-y-3 rounded-[22px] bg-[#2e2f2f] p-4",children:[(0,s.jsxs)("p",{className:"text-xs leading-5 text-white/45",children:["Configure this URL in Meta webhook settings:",(0,s.jsxs)("span",{className:"mt-1 block break-all font-mono text-white/70",children:[tX,"/api/whatsapp/meta/webhook"]})]}),(0,s.jsx)("input",{className:"w-full rounded-[18px] bg-[#111b21] px-4 py-3 text-sm text-white outline-none placeholder:text-white/30",placeholder:"Phone Number ID",value:j,onChange:e=>P(e.target.value),required:!0}),(0,s.jsx)("input",{className:"w-full rounded-[18px] bg-[#111b21] px-4 py-3 text-sm text-white outline-none placeholder:text-white/30",placeholder:"WABA ID / Business Account ID (optional)",value:N,onChange:e=>I(e.target.value)}),(0,s.jsx)("input",{className:"w-full rounded-[18px] bg-[#111b21] px-4 py-3 text-sm text-white outline-none placeholder:text-white/30",placeholder:"Permanent access token",value:k,onChange:e=>R(e.target.value),required:!0}),(0,s.jsx)("input",{className:"w-full rounded-[18px] bg-[#111b21] px-4 py-3 text-sm text-white outline-none placeholder:text-white/30",placeholder:"Webhook verify token",value:C,onChange:e=>O(e.target.value),required:!0}),(0,s.jsx)("input",{className:"w-full rounded-[18px] bg-[#111b21] px-4 py-3 text-sm text-white outline-none placeholder:text-white/30",placeholder:"App secret for signature validation (optional)",value:S,onChange:e=>_(e.target.value)})]}):null,(0,s.jsx)("button",{type:"submit",className:"w-full rounded-2xl bg-brand-500 px-4 py-3 text-sm font-semibold text-[#10251a]",children:"whatsapp_cloud"===v?"Add Official API Session":"Add WhatsApp Session"})]})]}),"api"===tG&&(0,s.jsxs)("div",{className:"rounded-[28px] bg-[#161717] p-4",children:[(0,s.jsx)("div",{className:"flex items-start justify-between gap-4",children:(0,s.jsxs)("div",{children:[(0,s.jsx)("p",{className:"text-[11px] uppercase tracking-[0.24em] text-white/35",children:"API Access"}),(0,s.jsx)("h3",{className:"mt-2 text-base font-semibold text-white",children:"Generate API key"}),(0,s.jsx)("p",{className:"mt-2 text-sm leading-6 text-white/45",children:"Use with external agents via `X-API-Key` header or `Authorization: Bearer <api-key>`."}),(0,s.jsxs)("p",{className:"mt-3 text-sm leading-6 text-white/45",children:["Webhooks: forward incoming messages to your endpoint. See the"," ",(0,s.jsx)("a",{href:"/docs/readme#webhooks",target:"_blank",rel:"noreferrer",className:"ml-1 font-medium text-brand-300 underline",children:"webhook documentation"})," ","for payload details and agent integration."]})]})}),(0,s.jsxs)("div",{className:"mt-4 rounded-[22px] bg-[#2e2f2f] p-4",children:[(0,s.jsxs)("div",{className:"flex flex-wrap items-center justify-between gap-2",children:[(0,s.jsxs)("div",{children:[(0,s.jsx)("h4",{className:"text-sm font-semibold text-white",children:"AI agent gateway prompt"}),(0,s.jsx)("p",{className:"mt-1 text-xs leading-5 text-white/45",children:"Copy this into an external AI agent so it can connect to Wanie as a WhatsApp and Telegram gateway."})]}),(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/5 px-3 py-1.5 text-xs font-medium text-white/75 transition hover:bg-white/10",onClick:t2,children:"gateway-agent"===eg?"Copied":"Copy prompt"})]}),(0,s.jsx)("pre",{className:"mt-3 max-h-56 overflow-auto rounded-[18px] bg-[#0f1010] p-4 text-xs leading-5 text-white/70",children:(0,s.jsx)("code",{children:t$})})]}),U?(0,s.jsxs)("div",{className:"mt-4 rounded-[22px] bg-[#2e2f2f] p-4",children:[(0,s.jsx)("p",{className:"text-[11px] uppercase tracking-[0.22em] text-brand-200/80",children:"Shown once"}),(0,s.jsx)("p",{className:"mt-2 break-all font-mono text-sm text-white",children:U}),(0,s.jsx)("button",{type:"button",className:"mt-3 rounded-full bg-brand-500 px-4 py-2 text-sm font-semibold text-[#10251a]",onClick:async()=>{await navigator.clipboard.writeText(U),ef(!0),setTimeout(()=>ef(!1),1500)},children:ep?"Copied":"Copy API key"})]}):null,(0,s.jsxs)("form",{className:"mt-4 flex gap-2",onSubmit:V,children:[(0,s.jsx)("input",{className:"w-full rounded-[22px] bg-[#2e2f2f] px-4 py-3 text-sm text-white outline-none placeholder:text-white/30",placeholder:"Key name, e.g. OpenClaw Agent",value:M,onChange:e=>J(e.target.value),required:!0}),(0,s.jsx)("button",{type:"submit",className:"shrink-0 rounded-[22px] bg-brand-500 px-4 py-3 text-sm font-semibold text-[#10251a]",disabled:F,children:F?"Generating...":"Generate"})]}),(0,s.jsxs)("div",{className:"mt-4 max-h-[260px] space-y-3 overflow-y-auto pr-1",children:[F?(0,s.jsx)("div",{className:"rounded-[22px] bg-[#2e2f2f] px-4 py-6 text-sm text-white/45",children:"Loading API keys..."}):null,F||W.length?null:(0,s.jsx)("div",{className:"rounded-[22px] bg-[#2e2f2f] px-4 py-6 text-sm leading-6 text-white/45",children:"No API keys yet. Create one for OpenAPI client, AI agents, or external integrations."}),W.map(e=>(0,s.jsxs)("div",{className:"rounded-[22px] bg-[#2e2f2f] px-4 py-4",children:[(0,s.jsxs)("div",{className:"flex items-start justify-between gap-3",children:[(0,s.jsxs)("div",{className:"min-w-0",children:[(0,s.jsx)("h4",{className:"truncate text-sm font-semibold text-white",children:e.name}),(0,s.jsx)("p",{className:"mt-1 font-mono text-xs text-white/55",children:e.maskedKey})]}),(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/5 px-3 py-1.5 text-xs font-medium text-red-200 transition hover:bg-red-500/15",onClick:()=>X(e.id),disabled:F||$===e.id,children:$===e.id?"Revoking...":"Revoke"})]}),(0,s.jsxs)("div",{className:"mt-3 grid gap-2 text-xs text-white/40",children:[(0,s.jsxs)("p",{children:["Created: ",G(e.createdAt)]}),(0,s.jsxs)("p",{children:["Last used: ",G(e.lastUsedAt)]})]})]},e.id))]})]}),"webhooks"===tG&&(0,s.jsxs)("div",{className:"rounded-[28px] bg-[#161717] p-4",children:[(0,s.jsx)("p",{className:"text-[11px] uppercase tracking-[0.24em] text-white/35",children:"Webhooks"}),(0,s.jsx)("h3",{className:"mt-2 text-base font-semibold text-white",children:"Incoming message webhook"}),(0,s.jsx)("p",{className:"mt-2 text-sm leading-6 text-white/45",children:"Forward incoming messages to an external application using a configurable method, headers, and body template. Use custom headers for Bearer tokens, API keys, or any gateway-specific authentication."}),(0,s.jsxs)("div",{className:"mt-4 rounded-[22px] bg-[#2e2f2f] p-4",children:[(0,s.jsxs)("div",{className:"flex flex-wrap items-center justify-between gap-2",children:[(0,s.jsxs)("div",{children:[(0,s.jsx)("h4",{className:"text-sm font-semibold text-white",children:"AI agent gateway prompt"}),(0,s.jsx)("p",{className:"mt-1 text-xs leading-5 text-white/45",children:"Paste this into the external app agent that will receive webhooks and send replies through Wanie."})]}),(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/5 px-3 py-1.5 text-xs font-medium text-white/75 transition hover:bg-white/10",onClick:t2,children:"gateway-agent"===eg?"Copied":"Copy prompt"})]}),(0,s.jsx)("pre",{className:"mt-3 max-h-56 overflow-auto rounded-[18px] bg-[#0f1010] p-4 text-xs leading-5 text-white/70",children:(0,s.jsx)("code",{children:t$})})]}),(0,s.jsxs)("form",{className:"mt-4 space-y-3",onSubmit:e=>{e.preventDefault(),em()},children:[(0,s.jsxs)("div",{className:"rounded-[22px] bg-[#2e2f2f] p-4",children:[(0,s.jsxs)("div",{className:"flex flex-wrap items-center justify-between gap-2",children:[(0,s.jsxs)("div",{children:[(0,s.jsx)("h4",{className:"text-sm font-semibold text-white",children:"Generate webhook config"}),(0,s.jsx)("p",{className:"mt-1 text-xs leading-5 text-white/45",children:"Paste an external app webhook example or API docs. The AI will fill the request fields below."})]}),(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/10 px-3 py-1.5 text-xs font-medium text-white/80 transition hover:bg-white/15 disabled:opacity-50",onClick:t7,disabled:tu||!tx.trim(),children:tu?"Generating...":"Generate"})]}),(0,s.jsx)("textarea",{className:"mt-3 min-h-32 w-full resize-y rounded-[18px] bg-[#0f1010] px-4 py-3 font-mono text-xs leading-5 text-white outline-none placeholder:text-white/30",placeholder:'Example:\nPOST https://crm.example.com/api/wanie/messages\nAuthorization: Bearer <token>\nBody: { "customerId": "{{chat.contact.externalId}}", "message": "{{message.body}}" }',value:tx,onChange:e=>tm(e.target.value)}),tp?(0,s.jsx)("p",{className:"mt-2 text-xs leading-5 text-brand-200",children:tp}):null]}),(0,s.jsxs)("label",{className:"flex items-center justify-between gap-3 rounded-[22px] bg-[#2e2f2f] px-4 py-3",children:[(0,s.jsxs)("span",{children:[(0,s.jsx)("span",{className:"block text-sm font-semibold text-white",children:"Webhook delivery"}),(0,s.jsx)("span",{className:"mt-1 block text-xs leading-5 text-white/45",children:"Turn off to keep the saved config but stop sending incoming WhatsApp and Telegram events to the external app."})]}),(0,s.jsx)("input",{type:"checkbox",className:"h-5 w-5 accent-brand-500",checked:!1!==et,onChange:e=>er(e.target.checked)})]}),(0,s.jsxs)("label",{className:"block",children:[(0,s.jsx)("span",{className:"mb-1 block text-xs font-medium text-white/45",children:"Request URL"}),(0,s.jsx)("input",{className:"w-full rounded-[22px] bg-[#2e2f2f] px-4 py-3 text-sm text-white outline-none placeholder:text-white/30",placeholder:"https://example.com/wanie-webhook",value:ee||"",onChange:e=>el(e.target.value)})]}),(0,s.jsxs)("div",{className:"grid gap-3 md:grid-cols-[160px_1fr]",children:[(0,s.jsxs)("label",{className:"block",children:[(0,s.jsx)("span",{className:"mb-1 block text-xs font-medium text-white/45",children:"Method"}),(0,s.jsxs)("select",{className:"w-full rounded-[22px] bg-[#2e2f2f] px-4 py-3 text-sm text-white outline-none",value:ea||"POST",onChange:e=>eo(e.target.value),children:[(0,s.jsx)("option",{value:"POST",children:"POST"}),(0,s.jsx)("option",{value:"PUT",children:"PUT"}),(0,s.jsx)("option",{value:"PATCH",children:"PATCH"}),(0,s.jsx)("option",{value:"DELETE",children:"DELETE"}),(0,s.jsx)("option",{value:"GET",children:"GET"})]})]}),(0,s.jsxs)("label",{className:"block",children:[(0,s.jsx)("span",{className:"mb-1 block text-xs font-medium text-white/45",children:"Legacy webhook key header"}),(0,s.jsx)("input",{className:"w-full rounded-[22px] bg-[#2e2f2f] px-4 py-3 text-sm text-white outline-none placeholder:text-white/30",placeholder:"Sent as x-wanie-webhook-key and legacy x-openwa-webhook-key unless custom headers override them",value:ei||"",onChange:e=>ex(e.target.value)})]})]}),(0,s.jsxs)("label",{className:"block",children:[(0,s.jsx)("span",{className:"mb-1 block text-xs font-medium text-white/45",children:"Headers JSON"}),(0,s.jsx)("textarea",{className:"min-h-32 w-full resize-y rounded-[22px] bg-[#2e2f2f] px-4 py-3 font-mono text-xs leading-5 text-white outline-none placeholder:text-white/30",placeholder:'{\n "Authorization": "Bearer YOUR_TOKEN",\n "x-api-key": "YOUR_API_KEY"\n}',value:es||"",onChange:e=>ed(e.target.value)})]}),(0,s.jsxs)("label",{className:"block",children:[(0,s.jsx)("span",{className:"mb-1 block text-xs font-medium text-white/45",children:"Body template"}),(0,s.jsx)("textarea",{className:"min-h-40 w-full resize-y rounded-[22px] bg-[#2e2f2f] px-4 py-3 font-mono text-xs leading-5 text-white outline-none placeholder:text-white/30",placeholder:"Use placeholders like {{payload}}, {{chat.id}}, {{message.body}}, {{message.mediaFile.url}}.",value:en||"",onChange:e=>ec(e.target.value)})]}),(0,s.jsxs)("div",{className:"flex gap-2",children:[(0,s.jsx)("button",{type:"submit",className:"rounded-2xl bg-brand-500 px-4 py-3 text-sm font-semibold text-[#10251a]",disabled:eh,children:eh?"Saving...":"Save webhook"}),(0,s.jsx)("button",{type:"button",className:"rounded-2xl bg-white/5 px-4 py-3 text-sm font-medium text-white/75 transition hover:bg-white/10",onClick:t8,disabled:eh||td,children:td?"Testing...":"Test webhook"}),(0,s.jsx)("button",{type:"button",className:"rounded-2xl bg-red-700 px-4 py-3 text-sm text-white/90",onClick:()=>eu(),disabled:eh,children:eh?"Removing...":"Remove webhook"})]})]}),(0,s.jsxs)("div",{className:"mt-5 grid gap-3",children:[(0,s.jsxs)("div",{children:[(0,s.jsxs)("div",{className:"flex flex-wrap items-center justify-between gap-2",children:[(0,s.jsx)("h4",{className:"text-sm font-semibold text-white",children:"Example receiver"}),(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/5 px-3 py-1.5 text-xs font-medium text-white/75 transition hover:bg-white/10",onClick:()=>t1("receiver",Z),children:"receiver"===eg?"Copied":"Copy receiver"})]}),(0,s.jsxs)("p",{className:"mt-1 text-xs leading-5 text-white/45",children:["Set your webhook URL to this route, then use the same API key value as"," ",(0,s.jsx)("span",{className:"font-mono",children:"WANIE_WEBHOOK_KEY"}),"."]}),(0,s.jsx)("pre",{className:"mt-3 max-h-72 overflow-auto rounded-[18px] bg-[#0f1010] p-4 text-xs leading-5 text-white/70",children:(0,s.jsx)("code",{children:Z})})]}),(0,s.jsxs)("div",{children:[(0,s.jsxs)("div",{className:"flex flex-wrap items-center justify-between gap-2",children:[(0,s.jsx)("h4",{className:"text-sm font-semibold text-white",children:"Example payload"}),(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/5 px-3 py-1.5 text-xs font-medium text-white/75 transition hover:bg-white/10",onClick:()=>t1("payload",Y),children:"payload"===eg?"Copied":"Copy payload"})]}),(0,s.jsxs)("p",{className:"mt-1 text-xs leading-5 text-white/45",children:["Wanie sends text and supported media messages as JSON with header"," ",(0,s.jsx)("span",{className:"font-mono",children:"x-wanie-webhook-key"}),". Media messages include"," ",(0,s.jsx)("span",{className:"font-mono",children:"message.mediaFile"}),"."]}),(0,s.jsx)("pre",{className:"mt-3 max-h-72 overflow-auto rounded-[18px] bg-[#0f1010] p-4 text-xs leading-5 text-white/70",children:(0,s.jsx)("code",{children:Y})})]}),(0,s.jsxs)("div",{children:[(0,s.jsxs)("div",{className:"flex flex-wrap items-center justify-between gap-2",children:[(0,s.jsx)("h4",{className:"text-sm font-semibold text-white",children:"Delivery logs"}),(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/5 px-3 py-1.5 text-xs font-medium text-white/75 transition hover:bg-white/10",onClick:t5,disabled:ti,children:ti?"Loading":"Refresh"})]}),(0,s.jsx)("p",{className:"mt-1 text-xs leading-5 text-white/45",children:"Recent webhook attempts are stored per user. Failed deliveries can be retried from here."}),(0,s.jsxs)("div",{className:"mt-3 space-y-2",children:[ts.map(e=>(0,s.jsxs)("div",{className:"rounded-[18px] bg-[#2e2f2f] p-3",children:[(0,s.jsxs)("div",{className:"flex flex-wrap items-center justify-between gap-2",children:[(0,s.jsxs)("div",{className:"min-w-0",children:[(0,s.jsxs)("p",{className:"truncate text-xs font-semibold text-white",children:[e.status," - ",e.attempts,"/",e.maxAttempts||3," attempts"]}),(0,s.jsx)("p",{className:"mt-1 truncate text-[11px] text-white/40",children:e.messageId||e.chatId})]}),"failed"===e.status?(0,s.jsx)("button",{type:"button",className:"rounded-full bg-brand-500 px-3 py-1.5 text-xs font-semibold text-[#10251a]",onClick:()=>t6(e.id),disabled:tr===e.id,children:tr===e.id?"Retrying":"Retry"}):null]}),e.error?(0,s.jsx)("p",{className:"mt-2 text-[11px] leading-5 text-red-200/80",children:e.error}):null]},e.id)),ts.length||ti?null:(0,s.jsx)("div",{className:"rounded-[18px] bg-[#2e2f2f] p-4 text-xs text-white/45",children:"No webhook delivery attempts yet."})]})]})]})]}),"deliveries"===tG&&(0,s.jsxs)("div",{className:"rounded-[28px] bg-[#161717] p-4",children:[(0,s.jsxs)("div",{className:"flex flex-wrap items-start justify-between gap-3",children:[(0,s.jsxs)("div",{children:[(0,s.jsx)("p",{className:"text-[11px] uppercase tracking-[0.24em] text-white/35",children:"Outbound Delivery"}),(0,s.jsx)("h3",{className:"mt-2 text-base font-semibold text-white",children:"WhatsApp and Telegram send queue"}),(0,s.jsx)("p",{className:"mt-2 text-sm leading-6 text-white/45",children:"Messages sent from the dashboard, API, CRM auto-reply, and assistant integrations are retried automatically. Failed jobs stop after the retry limit and can be retried manually here."})]}),(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/5 px-3 py-1.5 text-xs font-medium text-white/75 transition hover:bg-white/10",onClick:t9,disabled:tw,children:tw?"Loading":"Refresh"})]}),(0,s.jsxs)("div",{className:"mt-4 space-y-2",children:[tg.map(e=>{var t,a,n,i,l,r,o,d;let c=(null==(n=e.message)||null==(a=n.chat)||null==(t=a.contact)?void 0:t.displayName)||(null==(l=e.message)||null==(i=l.chat)?void 0:i.title)||(null==(r=e.message)?void 0:r.receiver)||e.messageId,x=(null==(o=e.message)?void 0:o.body)||(null==(d=e.message)?void 0:d.type)||"Outbound message";return(0,s.jsxs)("div",{className:"rounded-[18px] bg-[#2e2f2f] p-3",children:[(0,s.jsxs)("div",{className:"flex flex-wrap items-start justify-between gap-3",children:[(0,s.jsxs)("div",{className:"min-w-0",children:[(0,s.jsxs)("div",{className:"flex flex-wrap items-center gap-2",children:[(0,s.jsx)("span",{className:"rounded-full px-2.5 py-1 text-[11px] font-semibold capitalize ".concat("delivered"===e.status?"bg-brand-500/15 text-brand-100":"failed"===e.status?"bg-red-500/15 text-red-100":"canceled"===e.status?"bg-white/10 text-white/55":"bg-amber-500/15 text-amber-100"),children:e.status}),(0,s.jsxs)("span",{className:"text-[11px] text-white/40",children:[e.transport," - ",e.attempts,"/",e.maxAttempts," attempts"]})]}),(0,s.jsx)("p",{className:"mt-2 truncate text-sm font-semibold text-white",children:c}),(0,s.jsx)("p",{className:"mt-1 line-clamp-2 text-xs leading-5 text-white/45",children:x}),(0,s.jsxs)("p",{className:"mt-1 text-[11px] text-white/30",children:["Updated: ",G(e.updatedAt)]})]}),(0,s.jsxs)("div",{className:"flex shrink-0 flex-wrap items-center gap-2",children:[["queued","sending"].includes(e.status)?(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/5 px-3 py-1.5 text-xs font-medium text-white/75 transition hover:bg-white/10",onClick:()=>at(e.id),disabled:tN===e.id,children:tN===e.id?"Canceling":"Cancel"}):null,"failed"===e.status||"canceled"===e.status?(0,s.jsx)("button",{type:"button",className:"rounded-full bg-brand-500 px-3 py-1.5 text-xs font-semibold text-[#10251a]",onClick:()=>ae(e.id),disabled:tv===e.id,children:tv===e.id?"Retrying":"Retry"}):null]})]}),e.lastError?(0,s.jsx)("p",{className:"mt-2 text-[11px] leading-5 text-red-200/80",children:e.lastError}):null]},e.id)}),tg.length||tw?null:(0,s.jsx)("div",{className:"rounded-[18px] bg-[#2e2f2f] p-4 text-xs text-white/45",children:"No outbound delivery jobs yet."})]})]}),"telegram"===tG&&(0,s.jsxs)("div",{className:"rounded-[28px] bg-[#161717] p-4",children:[(0,s.jsx)("p",{className:"text-[11px] uppercase tracking-[0.24em] text-white/35",children:"Telegram Bot"}),(0,s.jsxs)("div",{className:"mt-4 rounded-[22px] bg-[#2e2f2f] p-4",children:[(0,s.jsxs)("div",{className:"flex flex-wrap items-center justify-between gap-2",children:[(0,s.jsxs)("div",{children:[(0,s.jsx)("h3",{className:"text-base font-semibold text-white",children:"Active bots"}),(0,s.jsx)("p",{className:"mt-1 text-xs leading-5 text-white/45",children:"Telegram bots configured for this Wanie account."})]}),(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/5 px-3 py-1.5 text-xs font-medium text-white/75 transition hover:bg-white/10",onClick:t3,disabled:e8,children:"Refresh"})]}),(0,s.jsxs)("div",{className:"mt-3 space-y-2",children:[e8?(0,s.jsx)("div",{className:"rounded-[18px] bg-[#161717] px-4 py-4 text-sm text-white/45",children:"Loading Telegram bots..."}):null,e8||e4.length?null:(0,s.jsx)("div",{className:"rounded-[18px] bg-[#161717] px-4 py-4 text-sm text-white/45",children:"No Telegram bot configured."}),e4.map(e=>(0,s.jsx)("div",{className:"rounded-[18px] bg-[#161717] p-4",children:(0,s.jsxs)("div",{className:"flex flex-wrap items-center justify-between gap-3",children:[(0,s.jsxs)("div",{className:"min-w-0",children:[(0,s.jsx)("p",{className:"truncate text-sm font-semibold text-white",children:e.name||"Telegram Bot"}),(0,s.jsxs)("p",{className:"mt-1 text-xs text-white/40",children:[e.running?"Running":"Stopped"," -"," ",e.configured?"Token ".concat(e.tokenPreview):"Runtime only"]})]}),(0,s.jsx)("button",{type:"button",className:"rounded-2xl bg-red-700 px-4 py-2 text-sm text-white/90 disabled:opacity-50",onClick:()=>t4(e.id),disabled:tt===e.id,children:tt===e.id?"Deleting...":"Delete Bot"})]})},e.id))]})]}),(0,s.jsx)("h3",{className:"mt-5 text-base font-semibold text-white",children:"Admin allowlist"}),(0,s.jsx)("p",{className:"mt-2 text-sm leading-6 text-white/45",children:"Telegram chat IDs in this list can use the bot as the Wanie assistant admin. Other Telegram users follow CRM mode; when CRM is off their messages are saved to the dashboard without assistant tool access."}),(0,s.jsxs)("form",{className:"mt-4 space-y-3",onSubmit:aa,children:[(0,s.jsxs)("label",{className:"flex items-center justify-between gap-3 rounded-[22px] bg-[#2e2f2f] px-4 py-3",children:[(0,s.jsxs)("span",{children:[(0,s.jsx)("span",{className:"block text-sm font-semibold text-white",children:"Telegram AI replies"}),(0,s.jsx)("span",{className:"mt-1 block text-xs leading-5 text-white/45",children:"Turn off to store Telegram messages without Wanie AI assistant or CRM auto-reply responses."})]}),(0,s.jsx)("input",{type:"checkbox",className:"h-5 w-5 accent-brand-500",checked:!1!==e5,onChange:e=>e3(e.target.checked),disabled:e8})]}),(0,s.jsx)("textarea",{className:"min-h-40 w-full resize-y rounded-[22px] bg-[#2e2f2f] px-4 py-3 font-mono text-sm text-white outline-none placeholder:text-white/30",placeholder:"123456789\n987654321",value:e1,onChange:e=>e2(e.target.value),disabled:e8}),(0,s.jsxs)("div",{className:"flex flex-wrap items-center gap-3",children:[(0,s.jsx)("button",{type:"submit",className:"rounded-2xl bg-brand-500 px-4 py-3 text-sm font-semibold text-[#10251a]",disabled:e8||e9,children:e9?"Saving...":e8?"Loading...":"Save Telegram settings"}),(0,s.jsx)("p",{className:"text-xs leading-5 text-white/40",children:"Use one ID per line, or separate IDs with comma, space, or semicolon."})]})]})]}),"ai"===tG&&(0,s.jsxs)("div",{className:"rounded-[28px] bg-[#161717] p-4",children:[(0,s.jsx)("p",{className:"text-[11px] uppercase tracking-[0.24em] text-white/35",children:"AI Providers"}),(0,s.jsx)("h3",{className:"mt-2 text-base font-semibold text-white",children:"Manage LLM providers"}),(0,s.jsx)("div",{className:"mt-2",children:(0,s.jsx)("button",{type:"button",className:"rounded-2xl bg-white/5 px-3 py-2 text-sm font-medium text-white/80",onClick:()=>tB(!0),children:"Edit Assistant Tools"})}),(0,s.jsx)("p",{className:"mt-2 text-sm leading-6 text-white/45",children:"Add provider configs for OpenAI, Anthropic, Ollama, OpenRouter, then fetch available models."}),(0,s.jsxs)("form",{className:"mt-4 space-y-3",onSubmit:as,children:[(0,s.jsxs)("div",{className:"grid grid-cols-3 gap-2",children:[(0,s.jsx)("input",{className:"col-span-2 w-full rounded-[22px] bg-[#2e2f2f] px-4 py-3 text-sm text-white outline-none placeholder:text-white/30",placeholder:"Provider name (e.g. My OpenAI)",autoComplete:"off",value:eO,onChange:e=>e_(e.target.value),required:!0}),(0,s.jsxs)("select",{className:"w-full rounded-[22px] bg-[#2e2f2f] px-4 py-3 text-sm text-white outline-none",value:eD,onChange:e=>eL(e.target.value),required:!0,children:[(0,s.jsx)("option",{value:"",children:"Select provider"}),(0,s.jsx)("option",{value:"openai",children:"OpenAI"}),(0,s.jsx)("option",{value:"anthropic",children:"Anthropic"}),(0,s.jsx)("option",{value:"ollama",children:"Ollama"}),(0,s.jsx)("option",{value:"openrouter",children:"OpenRouter"})]})]}),(0,s.jsxs)("div",{className:"grid gap-2",children:[(0,s.jsxs)("div",{className:"flex gap-2",children:[(0,s.jsx)("input",{className:"flex-1 w-full rounded-[22px] bg-[#2e2f2f] px-4 py-3 text-sm text-white outline-none placeholder:text-white/30",placeholder:"API key (sensitive, optional)",value:eW,autoComplete:"off",onChange:e=>eF(e.target.value),type:eH?"text":"password"}),(0,s.jsx)("button",{type:"button",className:"rounded-[22px] bg-white/5 px-4 py-3 text-sm text-white/70",onClick:()=>eQ(e=>!e),children:eH?"Hide":"Show"})]}),(0,s.jsx)("input",{className:"w-full rounded-[22px] bg-[#2e2f2f] px-4 py-3 text-sm text-white outline-none placeholder:text-white/30",placeholder:"Host (e.g. http://localhost:11434)",value:eM,onChange:e=>eU(e.target.value)}),(0,s.jsx)("input",{className:"w-full rounded-[22px] bg-[#2e2f2f] px-4 py-3 text-sm text-white outline-none placeholder:text-white/30",placeholder:"Default model (optional)",value:eq,onChange:e=>eB(e.target.value)}),(0,s.jsxs)("div",{className:"rounded-[16px] bg-[#232424] px-4 py-3",children:[(0,s.jsxs)("div",{className:"mb-2 flex items-center justify-between",children:[(0,s.jsx)("p",{className:"text-xs text-white/70",children:"Provider temperature"}),(0,s.jsxs)("div",{className:"flex items-center gap-2",children:[(0,s.jsx)("span",{className:"text-xs text-white/60",children:""===ez?"Auto":ez}),(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/5 px-2 py-1 text-[11px] text-white/70",onClick:()=>eK(""),children:"Auto"})]})]}),(0,s.jsx)("input",{type:"range",min:"0",max:"2",step:"0.1",value:""===ez?"1":ez,onChange:e=>eK(t0(e.target.value)),className:"w-full accent-brand-500"}),(0,s.jsx)("p",{className:"mt-2 text-[11px] text-white/45",children:"Set kosong untuk mengikuti default model/provider."})]}),(0,s.jsx)("p",{className:"text-xs text-white/45",children:function(e){if(!e)return"";switch(String(e).toLowerCase()){case"openai":return"OpenAI: provide an API key (platform.openai.com). Host is optional.";case"anthropic":return"Anthropic: provide an API key. Default model is typically `claude-2`.";case"ollama":return"Ollama: provide the local host (e.g. http://localhost:11434) and model name to use.";case"openrouter":return"OpenRouter: provide host or API key and optionally a default model.";default:return"Provide connection details (API key, host, default model)."}}(eD)})]}),(0,s.jsx)("div",{className:"flex gap-2",children:(0,s.jsx)("button",{type:"submit",className:"rounded-2xl bg-brand-500 px-4 py-3 text-sm font-semibold text-[#10251a] disabled:opacity-60",disabled:eG,"aria-busy":eG,children:eG?"Adding...":"Add provider"})})]}),(0,s.jsxs)("div",{className:"mt-4 max-h-[200px] space-y-3 overflow-y-auto pr-1",children:[eI?(0,s.jsx)("div",{className:"rounded-[22px] bg-[#2e2f2f] px-4 py-6 text-sm text-white/45",children:"Loading providers..."}):null,eI||eE.length?null:(0,s.jsx)("div",{className:"rounded-[22px] bg-[#2e2f2f] px-4 py-6 text-sm leading-6 text-white/45",children:"No providers configured."}),eE.map(e=>{var t,a,n,i,l,r;return(0,s.jsxs)("div",{className:"rounded-[22px] bg-[#2e2f2f] px-4 py-4",children:[(0,s.jsxs)("div",{className:"flex items-start justify-between gap-3",children:[(0,s.jsxs)("div",{className:"min-w-0",children:[(0,s.jsx)("h4",{className:"truncate text-sm font-semibold text-white",children:e.name}),(0,s.jsx)("p",{className:"mt-1 text-xs text-white/55",children:e.provider})]}),(0,s.jsxs)("div",{className:"flex gap-2",children:[(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/5 px-3 py-1.5 text-xs font-medium text-white/80",onClick:()=>ar(e.id),disabled:tI===e.id,children:tI===e.id?"Fetching...":"Fetch models"}),(0,s.jsx)("button",{type:"button",className:"rounded-full bg-red-700 px-3 py-1.5 text-xs font-medium text-white/80",onClick:()=>al(e.id),children:"Delete"})]})]}),(0,s.jsx)("div",{className:"mt-3 text-xs text-white/40",children:(0,s.jsxs)("p",{children:["Created: ",G(e.createdAt)]})}),(0,s.jsxs)("div",{className:"mt-3 rounded-[14px] bg-[#0f1111] px-3 py-3",children:[(0,s.jsxs)("div",{className:"mb-2 flex items-center justify-between",children:[(0,s.jsx)("p",{className:"text-xs text-white/60",children:"Provider temperature"}),(0,s.jsx)("span",{className:"text-xs text-white/60",children:t0(void 0!==tD[e.id]?tD[e.id]:null==(t=e.config)?void 0:t.temperature)||"Auto"})]}),(0,s.jsx)("input",{type:"range",min:"0",max:"2",step:"0.1",value:t0(void 0!==tD[e.id]?tD[e.id]:null==(a=e.config)?void 0:a.temperature)||"1",onChange:t=>tL(a=>({...a||{},[e.id]:t0(t.target.value)})),className:"w-full accent-brand-500"}),(0,s.jsxs)("div",{className:"mt-2 flex items-center gap-2",children:[(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/5 px-3 py-1 text-xs text-white/80",onClick:()=>ao(e),disabled:tM===e.id,children:tM===e.id?"Saving...":"Save provider temp"}),(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/5 px-3 py-1 text-xs text-white/70",onClick:async()=>{tL(t=>({...t||{},[e.id]:""})),await ao(e,"")},disabled:tM===e.id,children:"Auto"})]})]}),eY[e.id]&&eY[e.id].length?(0,s.jsxs)("div",{className:"mt-3 grid gap-2",children:[(0,s.jsx)("p",{className:"text-xs text-white/45",children:"Models:"}),(0,s.jsxs)("div",{className:"flex items-center justify-between gap-2 mt-2",children:[(0,s.jsx)("div",{className:"flex-1",children:(0,s.jsxs)("select",{className:"w-full rounded-[10px] bg-[#0f1111] px-3 py-2 text-sm text-white outline-none",value:eC===e.id&&eS?eS:"",onChange:async t=>{let a=t.target.value||null;try{await eA(e.id),await eT(a)}catch(e){}},children:[(0,s.jsx)("option",{value:"",children:"Select model (set as default)"}),eY[e.id].map(e=>(0,s.jsx)("option",{value:e.id,children:e.name||e.id},e.id))]})}),(0,s.jsx)("div",{children:eC===e.id?(0,s.jsx)("span",{className:"rounded-full bg-emerald-600 px-3 py-1 text-xs text-white",children:"Active"}):(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/5 px-3 py-1 text-xs text-white/80",onClick:async()=>{try{await eA(e.id)}catch(e){}},children:"Set active"})})]}),eC===e.id&&eS?(0,s.jsxs)("div",{className:"mt-2 rounded-[14px] bg-[#0f1111] px-3 py-3",children:[(0,s.jsxs)("div",{className:"mb-2 flex items-center justify-between",children:[(0,s.jsxs)("p",{className:"text-xs text-white/60",children:["Model temperature (",eS,")"]}),(0,s.jsx)("span",{className:"text-xs text-white/60",children:t0(void 0!==tW[e.id]?tW[e.id]:null==(i=e.config)||null==(n=i.modelTemperatures)?void 0:n[eS])||"Auto"})]}),(0,s.jsx)("input",{type:"range",min:"0",max:"2",step:"0.1",value:t0(void 0!==tW[e.id]?tW[e.id]:null==(r=e.config)||null==(l=r.modelTemperatures)?void 0:l[eS])||"1",onChange:t=>tF(a=>({...a||{},[e.id]:t0(t.target.value)})),className:"w-full accent-brand-500"}),(0,s.jsxs)("div",{className:"mt-2 flex items-center gap-2",children:[(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/5 px-3 py-1 text-xs text-white/80",onClick:()=>ad(e,eS),disabled:tM===e.id,children:tM===e.id?"Saving...":"Save model temp"}),(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/5 px-3 py-1 text-xs text-white/70",onClick:async()=>{tF(t=>({...t||{},[e.id]:""})),await ad(e,eS,"")},disabled:tM===e.id,children:"Auto"})]})]}):null]}):null,(0,s.jsxs)("div",{className:"mt-2",children:[(0,s.jsx)("p",{className:"text-xs text-white/45",children:"Manual model"}),(0,s.jsxs)("div",{className:"flex items-center gap-2 mt-2",children:[(0,s.jsx)("input",{className:"flex-1 w-full rounded-[10px] bg-[#0f1111] px-3 py-2 text-sm text-white outline-none",placeholder:"Enter model id (e.g. gpt-5-mini)",value:tO[e.id]||"",onChange:t=>t_(a=>({...a||{},[e.id]:t.target.value})),onKeyDown:async t=>{if("Enter"===t.key){t.preventDefault();let a=(tO[e.id]||t.target.value||"").trim().replace(/^\s+|\s+$/g,"")||null;try{await eA(e.id),await eT(a)}catch(e){}}},autoComplete:"off"}),(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/5 px-3 py-1 text-xs text-white/80",onClick:async()=>{let t=(tO[e.id]||"").trim().replace(/^\s+|\s+$/g,"")||null;try{await eA(e.id),await eT(t)}catch(e){}},children:"Set"})]})]})]},e.id)})]})]}),"advanced"===tG&&(0,s.jsxs)("div",{className:"rounded-[28px] bg-[#161717] p-4",children:[(0,s.jsx)("p",{className:"text-[11px] uppercase tracking-[0.24em] text-white/35",children:"Advanced"}),(0,s.jsx)("h3",{className:"mt-2 text-base font-semibold text-white",children:"Terminal auto-approve"}),(0,s.jsx)("p",{className:"mt-2 text-sm leading-6 text-white/45",children:'When enabled, terminal commands requested with approvalMode "auto" will be executed immediately without checking the host allowlist. Use with caution.'}),(0,s.jsxs)("div",{className:"mt-4 flex items-center justify-between",children:[(0,s.jsxs)("div",{children:[(0,s.jsx)("div",{className:"text-sm text-white",children:"Allow new registrations"}),(0,s.jsx)("div",{className:"text-xs text-white/45",children:"Toggle whether new users can sign up for this Wanie workspace."})]}),(0,s.jsx)("button",{type:"button",className:"rounded-full px-3 py-1 text-sm ".concat(eV?"bg-emerald-600 text-white":"bg-white/5 text-white/60"),onClick:async()=>{if(ew){e0(!0);try{let e=await (0,r.n)("/api/auth/config",{method:"POST",token:ew,body:{allowRegistration:!eV}});eX(!0===e.allowRegistration)}catch(e){alert(e.message||"Failed to update registration setting")}finally{e0(!1)}}},disabled:e$,children:e$?"Saving...":eV?"Enabled":"Disabled"})]}),(0,s.jsxs)("div",{className:"mt-4 flex items-center justify-between",children:[(0,s.jsxs)("div",{children:[(0,s.jsx)("div",{className:"text-sm text-white",children:"Auto-approve terminal commands"}),(0,s.jsx)("div",{className:"text-xs text-white/45",children:"Bypass WANIE_TERMINAL_ALLOWLIST and allow auto execution of any command."})]}),(0,s.jsx)("button",{type:"button",className:"rounded-full px-3 py-1 text-sm ".concat(eN?"bg-emerald-600 text-white":"bg-white/5 text-white/60"),onClick:async()=>{try{await ek(!eN)}catch(e){}},children:eN?"Enabled":"Disabled"})]}),(0,s.jsxs)("div",{className:"mt-8 rounded-[22px] bg-[#2e2f2f] p-4 ring-1 ring-white/10",children:[(0,s.jsxs)("div",{className:"flex items-center justify-between",children:[(0,s.jsxs)("div",{children:[(0,s.jsx)("p",{className:"text-sm font-semibold text-white",children:"Reset user password"}),(0,s.jsx)("p",{className:"mt-1 text-xs text-white/45",children:"Set a new password for an existing user account."})]}),(0,s.jsx)("span",{className:"rounded-full bg-white/5 px-3 py-1 text-xs text-white/70",children:"Admin only"})]}),(0,s.jsxs)("form",{className:"mt-4 space-y-3",onSubmit:an,children:[(0,s.jsx)("input",{className:"w-full rounded-[22px] bg-[#161717] px-4 py-3 text-sm text-white outline-none placeholder:text-white/30",placeholder:"New password",type:"password",value:tC,onChange:e=>tS(e.target.value),required:!0}),(0,s.jsx)("button",{type:"submit",className:"w-full rounded-2xl bg-brand-500 px-4 py-3 text-sm font-semibold text-[#10251a] disabled:opacity-60",disabled:tA,children:tA?"Resetting...":"Reset password"})]})]}),(0,s.jsxs)("div",{className:"mt-6 rounded-[22px] bg-[#2e2f2f] p-4 ring-1 ring-white/10",children:[(0,s.jsx)("p",{className:"text-sm font-semibold text-white",children:"Reset all Wanie data"}),(0,s.jsx)("p",{className:"mt-1 text-xs text-white/45",children:"Permanently deletes all stored app data. Requires confirmation."}),(0,s.jsx)("button",{type:"button",className:"mt-4 w-full rounded-2xl bg-red-600 px-4 py-3 text-sm font-semibold text-white disabled:opacity-60",onClick:ai,disabled:tE,children:tE?"Resetting...":"Reset all data"})]})]})]})]})]})}),(0,s.jsx)(q,{open:tq,onClose:()=>tB(!1)}),(0,s.jsx)(B,{open:tz,onClose:()=>tK(!1)})]}):null}var V=a(5842);function X(){let[e,t]=(0,n.useState)(null),[a,d]=(0,n.useState)(!1),c=(0,i.useRouter)(),{token:x,user:m,hydrateAuth:u,logout:h,setBootstrapData:p,setMessages:f,prependMessages:g,setActiveChat:b,upsertSession:w,upsertChat:y,addMessage:v,updateMessageStatus:j,updateMessageDelivery:N,updateMessage:k,upsertTerminalRecord:C,setSocket:S,socket:A,chats:T,sessions:E,activeChatId:P,activeSessionId:I,setActiveSession:R,messagesByChat:O,messageMetaByChat:D,typingByChat:L}=(0,o.C)(),[W,M]=(0,n.useState)(!0),[q,B]=(0,n.useState)(!1),[z,K]=(0,n.useState)(!1),[H,Q]=(0,n.useState)(!1),[G,Z]=(0,n.useState)(""),[Y,X]=(0,n.useState)(""),[$,ee]=(0,n.useState)(""),[et,ea]=(0,n.useState)("wwebjs"),[es,en]=(0,n.useState)(""),[ei,el]=(0,n.useState)(""),[er,eo]=(0,n.useState)(""),[ed,ec]=(0,n.useState)(""),[ex,em]=(0,n.useState)(""),[eu,eh]=(0,n.useState)(""),[ep,ef]=(0,n.useState)(""),[eg,eb]=(0,n.useState)(""),[ew,ey]=(0,n.useState)(!1),[ev,ej]=(0,n.useState)([]),[eN,ek]=(0,n.useState)([]),[eC,eS]=(0,n.useState)(!1),[eA,eT]=(0,n.useState)(""),[eE,eP]=(0,n.useState)(""),[eI,eR]=(0,n.useState)(null),[eO,e_]=(0,n.useState)(""),[eD,eL]=(0,n.useState)(!0),[eW,eF]=(0,n.useState)("POST"),[eM,eU]=(0,n.useState)(""),[eq,eB]=(0,n.useState)(""),[ez,eK]=(0,n.useState)(""),[eH,eQ]=(0,n.useState)(!1),[eG,eZ]=(0,n.useState)(null),[eY,eJ]=(0,n.useState)(!1),[eV,eX]=(0,n.useState)(!1),e$=(0,n.useRef)(null),e0=(0,n.useMemo)(()=>T.find(e=>e.id===P)||null,[P,T]),e1=O[P]||[],e2=D[P]||{hasMore:!1,nextBefore:null},e5=L[P];E.filter(e=>"ready"===e.status).length;let e3=(0,n.useCallback)(async()=>{if(x){B(!0);try{let e=await (0,r.n)("/api/contacts",{token:x});ej(e.contacts||[])}catch(e){Z(e.message)}finally{B(!1)}}},[x]),e4=(0,n.useCallback)(async()=>{if(x){eS(!0);try{let e=await (0,r.n)("/api/api-keys",{token:x});ek(e.apiKeys||[])}catch(e){Z(e.message)}finally{eS(!1)}}},[x]),e6=(0,n.useCallback)(async function(){let e=arguments.length>0&&void 0!==arguments[0]&&arguments[0];if(x){e&&M(!0);try{let e=await (0,r.n)("/api/bootstrap",{token:x});p(e)}catch(e){Z(e.message),401===e.status&&(h(),c.replace("/"))}finally{e&&M(!1)}}},[h,c,p,x]),e8=(0,n.useCallback)(async e=>{let t=Date.now()+25e3;for(;Date.now()<t&&(await new Promise(e=>setTimeout(e,1200)),x);)try{var a;let t=null==(a=(await (0,r.n)("/api/sessions",{token:x})).sessions)?void 0:a.find(t=>t.id===e);if(!t)continue;if(w(t),t.qrCode||"ready"===t.status||"error"===t.status||"disconnected"===t.status)return t}catch(e){}return null},[x,w]);(0,n.useEffect)(()=>{u()},[u]),(0,n.useEffect)(()=>{if(!x)return void c.replace("/");Promise.all([e6(!0),e3(),e4()]).finally(()=>{M(!1)})},[e4,e3,e6,c,x]),(0,n.useEffect)(()=>{eY&&async function(){if(x)try{let e=(await (0,r.n)("/api/webhook",{token:x})).webhook||{};e_(e.url||""),eL(!1!==e.enabled),eF(e.method||"POST"),eU(e.headers&&Object.keys(e.headers).length?JSON.stringify(e.headers,null,2):""),eB(e.bodyTemplate||""),eK(e.apiKey||"")}catch(e){}}()},[eY,x]),(0,n.useEffect)(()=>{var e;!I&&(null==(e=E[0])?void 0:e.id)&&R(E[0].id)},[I,E,R]),(0,n.useEffect)(()=>{if(!x)return;let e=(0,V.W)(x);S(e),e.on("new_message",e=>{v(e)}),e.on("message_status_update",e=>{j(e)}),e.on("outbound_delivery_update",e=>{let t=null==e?void 0:e.delivery;if((null==t?void 0:t.messageId)&&N(t),(null==t?void 0:t.status)==="failed"){var a,s,n,i,l,r;let e=(null==(n=t.message)||null==(s=n.chat)||null==(a=s.contact)?void 0:a.displayName)||(null==(l=t.message)||null==(i=l.chat)?void 0:i.title)||(null==(r=t.message)?void 0:r.receiver)||"message";Z("Delivery failed for ".concat(e,": ").concat(t.lastError))}}),e.on("contact_list_update",e=>{y(e),B(!0),e3()}),e.on("session_status_update",e=>{w(e),("ready"===e.status||"error"===e.status||"disconnected"===e.status)&&t(null),(e.qrCode||"ready"===e.status||"error"===e.status||"disconnected"===e.status)&&d(!1),"connecting"===e.status&&B(!0),"ready"===e.status&&(e6(),e3(),B(!1))}),e.on("workspace_sync_started",e=>{console.log("[Dashboard] Workspace sync started:",e),B(!0),K(!0),Q(!0)}),e.on("workspace_synced",e=>{console.log("[Dashboard] Workspace synced event received:",e),e6(),e3(),B(!1),K(!1),Q(!1),"failed"===e.status&&Z(e.error||"WhatsApp sync failed.");let t=o.C.getState();t.activeChatId&&(0,r.n)("/api/chats/".concat(t.activeChatId,"/messages"),{token:x}).then(e=>{f(t.activeChatId,e.messages,{hasMore:!!e.hasMore,nextBefore:e.nextBefore||null})}).catch(()=>{})}),e.on("typing_event",e=>{var t;let a=null==(t=o.C.getState().user)?void 0:t.id;e&&e.userId&&a&&e.userId===a||o.C.getState().setTyping(e)});let a=async e=>{if(!(null==e?void 0:e.id))return;let t=t=>{var a,s;let n=(null==t?void 0:t.chatId)||(null==e?void 0:e.chatId)||null;if(!n)return;let i=o.C.getState();if((i.messagesByChat[n]||[]).find(e=>e.externalMessageId==="terminal:".concat(t.id)||e.id==="terminal:".concat(t.id)))return;let l=i.chats.find(e=>e.id===n);i.addMessage({id:"terminal:".concat(t.id),chatId:n,sessionId:(null==l?void 0:l.sessionId)||null,mediaFileId:null,replyToId:null,externalMessageId:"terminal:".concat(t.id),sender:(null==l||null==(a=l.contact)?void 0:a.externalId)||"openwa:assistant",receiver:(null==(s=i.user)?void 0:s.id)?"user:".concat(i.user.id):"user",body:"pending"===t.status?"Terminal command pending approval: ".concat(t.command||"Terminal command"):"Terminal command finished: ".concat(t.command||"Terminal command"),type:"text",direction:"inbound",createdAt:t.requestedAt||new Date().toISOString(),updatedAt:t.requestedAt||new Date().toISOString(),mediaFile:null,replyTo:null,statuses:[{status:"delivered"}]})};try{let a=await (0,r.n)("/api/terminal/".concat(e.id),{token:x});(null==a?void 0:a.item)&&(C(a.item),t(a.item))}catch(a){(null==e?void 0:e.id)&&(C(e),t(e))}};return e.on("terminal_request",a),e.on("terminal_result",a),()=>{e.off("terminal_request",a),e.off("terminal_result",a),e.close(),S(null)}},[v,e3,e6,S,x,N,j,C,y,w]),(0,n.useEffect)(()=>{if(!P||O[P])return void K(!1);x&&(K(!0),(0,r.n)("/api/chats/".concat(P,"/messages"),{token:x}).then(e=>{f(P,e.messages,{hasMore:!!e.hasMore,nextBefore:e.nextBefore||null})}).catch(e=>{Z(e.message)}).finally(()=>{K(!1)}))},[P,O,f,x]);let e7=async e=>{e.preventDefault(),Z("");try{let e=await (0,r.n)("/api/sessions",{method:"POST",token:x,body:{name:Y,phoneNumber:$,transportType:et,phoneNumberId:es,businessAccountId:ei,accessToken:er,verifyToken:ed,appSecret:ex}});w(e.session),R(e.session.id),X(""),ee(""),ea("wwebjs"),en(""),el(""),eo(""),ec(""),em(""),eJ(!0),await e6()}catch(e){Z(e.message)}},e9=async(e,t)=>{Z("");try{if(!x)return;await (0,r.n)("/api/chats/".concat(e,"/").concat(t?"unpin":"pin"),{method:"POST",token:x}),await e6()}catch(e){Z(e.message)}},te=async e=>{Z("");try{var t;if(!x)return;let a=T.find(t=>t.id===e),s=(null==a||null==(t=a.contact)?void 0:t.externalId)||null,n=s&&("openwa:assistant"===s||String(s).startsWith("openwa:assistant")||String(s).endsWith(":assistant"));await (0,r.n)(n?"/api/assistant/sessions/".concat(e):"/api/chats/".concat(e),{method:"DELETE",token:x}),await e6(!0),P===e&&b(null)}catch(e){Z(e.message)}},tt=async e=>{Z(""),t(e),d(!0),R(e);try{let t=await (0,r.n)("/api/sessions/".concat(e,"/connect"),{method:"POST",token:x});w(t.session),await e8(e)||d(!1)}catch(e){Z(e.message),t(null),d(!1)}},ta=async e=>{Z(""),t(e);try{await (0,r.n)("/api/sessions/".concat(e,"/disconnect"),{method:"POST",token:x}),await e6(!0)}catch(e){Z(e.message)}finally{t(null)}},ts=async e=>{Z(""),t(e);try{await (0,r.n)("/api/sessions/".concat(e),{method:"DELETE",token:x}),await e6(!0),I===e&&R(null)}catch(e){Z(e.message)}finally{t(null)}},tn=async e=>{Z(""),t(e);try{let t=await (0,r.n)("/api/sessions/".concat(e,"/disconnect"),{method:"POST",token:x});w(t.session)}catch(e){Z(e.message)}finally{t(null)}},ti=async e=>{e.preventDefault(),Z(""),eS(!0);try{let e=await (0,r.n)("/api/api-keys",{method:"POST",token:x,body:{name:eA}});eP(e.secret),eT(""),ek(t=>[e.apiKey,...t]),eJ(!0)}catch(e){Z(e.message)}finally{eS(!1)}},tl=async()=>{eQ(!0),Z("");try{var e,t,a,s,n,i;let l=await (0,r.n)("/api/webhook",{method:"POST",token:x,body:{url:eO,apiKey:ez,enabled:eD,method:eW,headers:eM,bodyTemplate:eq}});e_((null==(e=l.webhook)?void 0:e.url)||""),eL((null==(t=l.webhook)?void 0:t.enabled)!==!1),eF((null==(a=l.webhook)?void 0:a.method)||"POST"),eU((null==(s=l.webhook)?void 0:s.headers)&&Object.keys(l.webhook.headers).length?JSON.stringify(l.webhook.headers,null,2):""),eB((null==(n=l.webhook)?void 0:n.bodyTemplate)||""),eK((null==(i=l.webhook)?void 0:i.apiKey)||"")}catch(e){Z(e.message)}finally{eQ(!1)}},tr=async()=>{eQ(!0),Z("");try{await (0,r.n)("/api/webhook",{method:"DELETE",token:x}),e_(""),eL(!0),eF("POST"),eU(""),eB(""),eK("")}catch(e){Z(e.message)}finally{eQ(!1)}},to=async e=>{Z(""),eR(e);try{await (0,r.n)("/api/api-keys/".concat(e),{method:"DELETE",token:x}),ek(t=>t.filter(t=>t.id!==e))}catch(e){Z(e.message)}finally{eR(null)}},td=async e=>{b(e),eb(""),null==A||A.emit("open_chat",{chatId:e})},tc=async e=>{eZ(e),Z("");try{let t=await (0,r.n)("/api/contacts/".concat(e,"/open"),{method:"POST",token:x});y(t.chat),await td(t.chat.id),ej(a=>a.map(a=>a.id===e?{...a,hasChat:!0,chatId:t.chat.id}:a)),eX(!1),setTimeout(()=>{var e;null==(e=e$.current)||e.focusComposer()},80)}catch(e){Z(e.message)}finally{eZ(null)}},tx=async e=>{let{body:t,replyToId:a}=e;if(!A)throw Error("Socket connection is not ready yet.");let s=await new Promise((e,s)=>{A.emit("send_message",{chatId:P,body:t,type:"text",replyToId:a},t=>{if(null==t?void 0:t.ok)return void e(t);s(Error((null==t?void 0:t.error)||"Failed to send message."))})});return(null==s?void 0:s.chat)&&s.chat.id&&(y(s.chat),b(s.chat.id)),s.message},tm=async e=>{let{file:t,caption:a}=e;if(!A)throw Error("Socket connection is not ready yet.");let s=new FormData;s.append("file",t);let n=await (0,r.n)("/api/media",{method:"POST",token:x,formData:s});await new Promise((e,t)=>{A.emit("send_media",{chatId:P,mediaFileId:n.mediaFile.id,body:a,type:n.type},a=>{if(null==a?void 0:a.ok)return void e(a.message);t(Error((null==a?void 0:a.error)||"Failed to send media."))})})},tu=async()=>{if(P&&e2.hasMore&&e2.nextBefore){ey(!0);try{let e=await (0,r.n)("/api/chats/".concat(P,"/messages?before=").concat(encodeURIComponent(e2.nextBefore),"&take=30"),{token:x});g(P,e.messages,{hasMore:!!e.hasMore,nextBefore:e.nextBefore||null})}catch(e){Z(e.message)}finally{ey(!1)}}},th=async e=>{try{let t=await (0,r.n)("/api/messages/".concat(e),{method:"DELETE",token:x});k(t.message),y(t.chat)}catch(e){Z(e.message)}},tp=async(e,t)=>{try{let a=await (0,r.n)("/api/messages/".concat(e,"/forward"),{method:"POST",token:x,body:{targetChatId:t}});v(a.message),y(a.chat)}catch(e){Z(e.message)}};return x?(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(l.A,{title:"Dashboard",description:"Dashboard Wanie untuk mengelola percakapan, kontak, device, dan session WhatsApp."}),H||q||z||W?(0,s.jsxs)("div",{className:"fixed left-4 bottom-4 z-50 flex items-center gap-3 rounded-[24px] bg-brand-600/95 px-4 py-3 text-sm font-semibold text-white shadow-[0_20px_60px_rgba(0,0,0,0.35)] ring-1 ring-brand-200/30",children:[(0,s.jsx)("span",{className:"flex h-4 w-4 items-center justify-center rounded-full bg-white/15",children:(0,s.jsx)("span",{className:"h-2 w-2 animate-spin rounded-full border-2 border-white border-t-transparent"})}),(0,s.jsx)("div",{className:"min-w-[220px] leading-5",children:H?"Syncing WhatsApp chats and contacts...":W?"Refreshing workspace...":q?"Syncing WhatsApp data...":"Fetching messages..."})]}):null,(0,s.jsx)("main",{className:"h-screen overflow-hidden bg-[#161717] text-white",children:(0,s.jsxs)("div",{className:"flex h-full w-full overflow-hidden bg-[#161717]",children:[(0,s.jsxs)("aside",{className:"flex flex-col",children:[(0,s.jsx)("button",{className:"mx-5 mt-5 mb-2 rounded-lg bg-brand-500 px-4 py-2 text-sm font-semibold text-[#10251a] hover:bg-brand-600 transition",onClick:()=>e6(!0),disabled:W,title:"Reload conversations",children:W?"Reloading...":"Reload Conversations"}),(0,s.jsx)(F,{chats:T,activeChatId:P,loading:W,onSelectChat:td,currentUser:m,query:eu,onQueryChange:eh,onTogglePin:e9,onDeleteChat:te})]}),(0,s.jsxs)("section",{className:"flex min-w-0 flex-1 flex-col",children:[G?(0,s.jsx)("div",{className:"mx-4 mt-4 rounded-2xl border border-red-400/25 bg-red-500/10 px-4 py-3 text-sm text-red-100",children:G}):null,(0,s.jsxs)("div",{className:"flex min-h-0 flex-1",children:[(0,s.jsx)(_,{ref:e$,chat:e0,messages:e1,chats:T,typingState:e5,loading:W,messagesLoading:z,loadingOlder:ew,hasMoreMessages:e2.hasMore,messageQuery:eg,onMessageQueryChange:eb,onLoadOlder:tu,onSendMessage:tx,onSendMedia:tm,onTyping:e=>{null==A||A.emit("typing",{chatId:P,isTyping:e})},onDeleteMessage:th,onForwardMessage:tp,onOpenContacts:()=>eX(!0),onOpenCrm:()=>c.push("/crm"),onOpenSettings:()=>eJ(!0),onLogout:()=>{h(),c.replace("/")}}),(0,s.jsx)(U,{contacts:ev,loading:q,open:eV,query:ep,onQueryChange:ef,onStartChat:tc,onClose:()=>eX(!1),startingContactId:eG})]})]})]})}),(0,s.jsx)(J,{open:eY,sessions:E,activeSessionId:I,onClose:()=>eJ(!1),onSelect:R,onConnect:tt,onDisconnect:tn,onClearSession:ta,onDeleteSession:ts,connectLoading:e,qrLoading:a,syncingWorkspace:H,sessionName:Y,sessionPhone:$,sessionTransport:et,metaPhoneNumberId:es,metaBusinessAccountId:ei,metaAccessToken:er,metaVerifyToken:ed,metaAppSecret:ex,onSessionNameChange:X,onSessionPhoneChange:ee,onSessionTransportChange:ea,onMetaPhoneNumberIdChange:en,onMetaBusinessAccountIdChange:el,onMetaAccessTokenChange:eo,onMetaVerifyTokenChange:ec,onMetaAppSecretChange:em,onCreateSession:e7,apiKeys:eN,apiKeysLoading:eC,apiKeyName:eA,apiKeySecret:eE,onApiKeyNameChange:eT,onCreateApiKey:ti,onRevokeApiKey:to,revokingKeyId:eI,webhookUrl:eO,webhookEnabled:eD,webhookMethod:eW,webhookHeaders:eM,webhookBodyTemplate:eq,webhookApiKey:ez,onWebhookUrlChange:e_,onWebhookEnabledChange:eL,onWebhookMethodChange:eF,onWebhookHeadersChange:eU,onWebhookBodyTemplateChange:eB,onWebhookApiKeyChange:eK,onSaveWebhook:tl,onDeleteWebhook:tr,webhookLoading:eH})]}):null}}},e=>{e.O(0,[202,774,112,345,853,636,593,792],()=>e(e.s=6998)),_N_E=e.O()}]);
|
|
1
|
+
(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[724],{6998:(e,t,a)=>{(window.__NEXT_P=window.__NEXT_P||[]).push(["/dashboard",function(){return a(9042)}])},9042:(e,t,a)=>{"use strict";a.r(t),a.d(t,{default:()=>X});var s=a(6514),n=a(5834),i=a(6225),l=a(2025),r=a(2309),o=a(644),d=a(848);function c(e){var t,a,i,l,c,x;let{open:m,chat:u,onClose:h}=e,p=(0,o.C)(e=>e.token),[f,g]=(0,n.useState)(""),[b,w]=(0,n.useState)(""),[y,v]=(0,n.useState)(""),[j,N]=(0,n.useState)(!1),[k,C]=(0,n.useState)(!1),[S,A]=(0,n.useState)(""),T=(0,n.useRef)(null);(0,n.useEffect)(()=>{var e,t,a;m&&(g((null==u||null==(e=u.contact)?void 0:e.displayName)||""),w((null==u||null==(t=u.contact)?void 0:t.avatarUrl)||""),v((null==u||null==(a=u.contact)?void 0:a.persona)||""),A(""))},[m,u]);let E=String((null==u||null==(t=u.contact)?void 0:t.externalId)||""),P=E.startsWith("openwa:assistant"),I="openwa:assistant"===E;async function R(e){var t,a;let s=null==(t=e.target.files)?void 0:t[0];if(s){if(!p)return alert("Authentication required.");A(s.name),C(!0);try{let e=new FormData;e.append("file",s);let t=await (0,r.n)("/api/media",{method:"POST",token:p,formData:e}),n="".concat((0,r.h)(),"/").concat(t.mediaFile.relativePath);if(P)if(I){let e=(await (0,r.n)("/api/assistant",{method:"PUT",token:p,body:{avatarUrl:n}})).assistant||{};w(e.avatarUrl||n);let t=(o.C.getState().chats||[]).map(t=>{var a;return(null==(a=t.contact)?void 0:a.externalId)==="openwa:assistant"?{...t,contact:{...t.contact,avatarUrl:e.avatarUrl||n}}:t});o.C.setState({chats:t})}else{let e=(await (0,r.n)("/api/contacts/".concat(u.contact.id),{method:"PUT",token:p,body:{avatarUrl:n}})).chat;if(e&&e.id){let t=(o.C.getState().chats||[]).map(t=>t.id===e.id?e:t);o.C.setState({chats:t}),w((null==(a=e.contact)?void 0:a.avatarUrl)||n)}else w(n)}else w(n)}catch(e){alert(e.message||"Failed to upload avatar")}finally{C(!1),T.current&&(T.current.value="")}}}async function O(e){if(e.preventDefault(),!P)return h();if(!p)return alert("Authentication required.");N(!0);try{let e={displayName:f,persona:y,avatarUrl:b};if(I){let t=(await (0,r.n)("/api/assistant",{method:"PUT",token:p,body:e})).assistant||{},a=(o.C.getState().chats||[]).map(e=>{var a;return(null==(a=e.contact)?void 0:a.externalId)==="openwa:assistant"?{...e,contact:{...e.contact,displayName:t.displayName,avatarUrl:t.avatarUrl,persona:t.persona}}:e});o.C.setState({chats:a}),h()}else{let t=(await (0,r.n)("/api/contacts/".concat(u.contact.id),{method:"PUT",token:p,body:e})).chat;if(t&&t.id){let e=(o.C.getState().chats||[]).map(e=>e.id===t.id?t:e);o.C.setState({chats:e})}h()}}catch(e){alert(e.message||"Failed to save assistant")}finally{N(!1)}}return m?(0,s.jsx)("div",{className:"fixed inset-0 z-50 flex items-center justify-center bg-black/70 px-4 py-6 backdrop-blur-sm",children:(0,s.jsxs)("div",{className:"flex max-h-[calc(100vh-3rem)] w-full max-w-md flex-col overflow-hidden rounded-[20px] border border-white/10 bg-[#161717] shadow-[0_28px_90px_rgba(0,0,0,0.45)]",children:[(0,s.jsxs)("div",{className:"flex shrink-0 items-center justify-between border-b border-white/10 bg-[#161717]/95 px-5 py-4 backdrop-blur-sm",children:[(0,s.jsx)("h3",{className:"text-lg font-semibold text-white",children:"Profile"}),(0,s.jsx)("button",{type:"button",className:"rounded-full bg-[#2e2f2f] px-3 py-1 text-sm text-white/70",onClick:h,children:"Close"})]}),(0,s.jsxs)("div",{className:"min-h-0 flex-1 overflow-y-auto px-5 py-4",children:[(0,s.jsxs)("div",{className:"flex items-center gap-3",children:[(0,s.jsx)("div",{className:"h-16 w-16 shrink-0 rounded-2xl overflow-hidden bg-[#2e2f2f]",children:b?(0,s.jsx)("img",{src:b,alt:f,className:"h-full w-full object-cover"}):(0,s.jsx)("div",{className:"flex h-full w-full items-center justify-center text-white",children:(f||"?").slice(0,2).toUpperCase()})}),(0,s.jsxs)("div",{className:"flex-1",children:[(0,s.jsx)("p",{className:"text-sm text-white/60",children:null==u||null==(a=u.contact)?void 0:a.externalId}),(0,s.jsx)("p",{className:"mt-1 font-medium text-white",children:null==u||null==(i=u.contact)?void 0:i.displayName})]})]}),(0,s.jsx)("div",{className:"mt-4",children:P?(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)("label",{className:"block text-xs text-white/50",children:"Display name"}),(0,s.jsx)("input",{className:"mt-1 w-full rounded-[10px] bg-[#2e2f2f] px-3 py-2 text-sm text-white outline-none",value:f,onChange:e=>g(e.target.value)}),(0,s.jsx)("label",{className:"mt-3 block text-xs text-white/50",children:"Avatar"}),(0,s.jsxs)("div",{className:"mt-2 rounded-[18px] border border-white/10 bg-[linear-gradient(135deg,rgba(255,255,255,0.06),rgba(255,255,255,0.02))] p-3",children:[(0,s.jsxs)("div",{className:"flex items-center gap-4",children:[(0,s.jsxs)("div",{className:"relative h-20 w-20 shrink-0 overflow-hidden rounded-[20px] border border-white/10 bg-[#2e2f2f] shadow-[0_12px_30px_rgba(0,0,0,0.25)]",children:[b?(0,s.jsx)("img",{src:b,alt:f,className:"h-full w-full object-cover"}):(0,s.jsx)("div",{className:"flex h-full w-full items-center justify-center text-white/70",children:(0,s.jsx)(d.TbK,{className:"h-8 w-8"})}),(0,s.jsx)("div",{className:"absolute inset-x-0 bottom-0 bg-black/55 px-2 py-1 text-center text-[10px] font-medium uppercase tracking-[0.18em] text-white/80",children:"Preview"})]}),(0,s.jsxs)("div",{className:"min-w-0 flex-1",children:[(0,s.jsx)("p",{className:"text-sm font-medium text-white",children:"Update avatar image"}),(0,s.jsx)("p",{className:"mt-1 text-xs leading-5 text-white/50",children:"Pilih gambar persegi agar hasil avatar lebih rapi. Format umum seperti JPG, PNG, dan WEBP didukung."}),(0,s.jsxs)("div",{className:"mt-3 flex flex-wrap items-center gap-2",children:[(0,s.jsxs)("button",{type:"button",className:"inline-flex items-center gap-2 rounded-full bg-white px-4 py-2 text-sm font-semibold text-[#111b21] transition hover:bg-white/90 disabled:cursor-not-allowed disabled:opacity-60",onClick:()=>{var e;return null==(e=T.current)?void 0:e.click()},disabled:k||j,children:[(0,s.jsx)(d.E10,{className:"h-4 w-4"}),k?"Uploading...":"Choose Image"]}),S?(0,s.jsxs)("div",{className:"inline-flex max-w-full items-center gap-2 rounded-full border border-emerald-400/20 bg-emerald-400/10 px-3 py-2 text-xs text-emerald-100",children:[(0,s.jsx)(d.mU9,{className:"h-4 w-4 shrink-0"}),(0,s.jsx)("span",{className:"truncate",children:S})]}):null]})]})]}),(0,s.jsx)("input",{ref:T,type:"file",accept:"image/*",onChange:R,className:"hidden"}),(0,s.jsx)("div",{className:"mt-3 rounded-2xl border border-dashed border-white/10 bg-black/20 px-3 py-3",children:(0,s.jsxs)("div",{className:"flex items-center justify-between gap-3",children:[(0,s.jsxs)("div",{className:"min-w-0",children:[(0,s.jsx)("p",{className:"truncate text-sm font-medium text-white/85",children:k?"Uploading avatar image":S||"Belum ada file baru yang dipilih"}),(0,s.jsx)("p",{className:"mt-1 text-xs text-white/45",children:k?"Avatar akan diperbarui otomatis setelah upload selesai.":b?"Avatar saat ini tetap dipakai sampai Anda memilih gambar baru.":"Klik tombol di atas untuk memilih gambar avatar."})]}),(0,s.jsx)("div",{className:"rounded-full bg-white/5 p-2 text-white/55",children:(0,s.jsx)(d.TbK,{className:"h-5 w-5"})})]})})]}),(0,s.jsx)("label",{className:"mt-3 block text-xs text-white/50",children:"Persona (system prompt)"}),(0,s.jsx)("textarea",{className:"mt-1 w-full rounded-[10px] bg-[#2e2f2f] px-3 py-2 text-sm text-white outline-none",rows:4,value:y,onChange:e=>v(e.target.value)})]}):(0,s.jsxs)("div",{className:"mt-3 space-y-2 text-sm text-white/50",children:[(0,s.jsxs)("p",{children:[(0,s.jsx)("strong",{children:"Name:"})," ",null==u||null==(l=u.contact)?void 0:l.displayName]}),(0,s.jsxs)("p",{children:[(0,s.jsx)("strong",{children:"External ID:"})," ",null==u||null==(c=u.contact)?void 0:c.externalId]}),(null==u||null==(x=u.contact)?void 0:x.lastMessageAt)?(0,s.jsxs)("p",{children:[(0,s.jsx)("strong",{children:"Last message:"})," ",new Date(u.contact.lastMessageAt).toLocaleString()]}):null]})})]}),(0,s.jsxs)("div",{className:"flex shrink-0 items-center justify-end gap-3 border-t border-white/10 bg-[#161717]/95 px-5 py-4 backdrop-blur-sm",children:[(0,s.jsx)("button",{type:"button",className:"rounded-2xl bg-white/5 px-4 py-2 text-sm font-semibold text-white/75 transition hover:bg-white/10",onClick:h,children:"Close"}),P?(0,s.jsx)("button",{type:"button",className:"rounded-2xl bg-brand-500 px-4 py-2 text-sm font-semibold text-[#10251a]",onClick:O,disabled:j,children:j?"Saving...":"Save"}):null]})]})}):null}var x=a(8944),m=a(5642),u=a(2493),h=a(4982);function p(e){let{inline:t,className:a,children:i,...l}=e,[r,o]=(0,n.useState)(!1),d=(function e(t){return"string"==typeof t||"number"==typeof t?String(t):Array.isArray(t)?t.map(e).join(""):t&&"object"==typeof t&&t.props&&t.props.children?e(t.props.children):""})(i).replace(/\n$/,""),c=String(a||"").replace(/^language-/,"").trim(),x=async()=>{if(d)try{var e;if("undefined"!=typeof navigator&&(null==(e=navigator.clipboard)?void 0:e.writeText))await navigator.clipboard.writeText(d);else{if("undefined"==typeof document)return;let e=document.createElement("textarea");e.value=d,e.setAttribute("readonly",""),e.style.position="absolute",e.style.left="-9999px",document.body.appendChild(e),e.select(),document.execCommand("copy"),document.body.removeChild(e)}o(!0),window.setTimeout(()=>o(!1),1600)}catch(e){o(!1)}};return t?(0,s.jsx)("code",{className:"rounded-md bg-black/30 px-1.5 py-0.5 font-mono text-[0.92em] text-emerald-100",...l,children:d}):(0,s.jsxs)("div",{className:"relative my-3 overflow-hidden rounded-2xl bg-black/35",children:[(0,s.jsxs)("div",{className:"flex items-center justify-between gap-3 border-b border-white/8 px-3 py-2",children:[(0,s.jsx)("span",{className:"min-w-0 truncate font-mono text-[11px] uppercase tracking-[0.18em] text-white/45",children:c||"code"}),(0,s.jsx)("button",{type:"button",className:"rounded-full border border-white/10 bg-white/5 px-2.5 py-1 text-[11px] font-medium text-white/75 transition hover:bg-white/10 hover:text-white",onClick:x,children:r?"Copied":"Copy"})]}),(0,s.jsx)("pre",{className:"overflow-x-auto p-3 text-xs leading-6 text-emerald-50",children:(0,s.jsx)("code",{className:a,...l,children:i})})]})}function f(e){let{content:t}=e;return(0,s.jsx)("div",{className:"message-markdown text-sm leading-6 text-white/88",children:(0,s.jsx)(m.oz,{rehypePlugins:[[u.A,{detect:!1,ignoreMissing:!0}]],remarkPlugins:[h.A],components:{code:p,a:e=>{let{href:t,children:a,...n}=e;return(0,s.jsx)("a",{href:t,target:"_blank",rel:"noreferrer",className:"text-emerald-200 underline decoration-emerald-300/40 underline-offset-4 transition hover:text-emerald-100",...n,children:a})},table:e=>{let{children:t,...a}=e;return(0,s.jsx)("div",{className:"overflow-x-auto",children:(0,s.jsx)("table",{className:"min-w-full border-collapse",...a,children:t})})}},children:String(t||"")})})}function g(e){return e?new Date(e).toLocaleString():"-"}function b(e){var t,a,i,l;let{terminalId:d,fallbackBody:c,onReplaceTerminalId:x}=e,m=(0,o.C)(e=>e.token),u=(0,o.C)(e=>e.terminalRecordsById[d]),h=(0,o.C)(e=>e.upsertTerminalRecord),[p,f]=(0,n.useState)(!1),[b,w]=(0,n.useState)(!1);(0,n.useEffect)(()=>{let e=!0;if(d&&m&&!u)return(async()=>{f(!0);try{let t=await (0,r.n)("/api/terminal/".concat(d),{token:m});e&&(null==t?void 0:t.item)&&h(t.item)}catch(e){}finally{e&&f(!1)}})(),()=>{e=!1}},[d,m,u,h]);let y=(null==u?void 0:u.status)||(p?"loading":"unknown"),v=(0,n.useMemo)(()=>"completed"===y?"bg-emerald-500/15 text-emerald-200 border-emerald-400/20":"failed"===y?"bg-red-500/15 text-red-200 border-red-400/20":"pending"===y?"bg-amber-500/15 text-amber-200 border-amber-400/20":"running"===y?"bg-sky-500/15 text-sky-200 border-sky-400/20":"denied"===y?"bg-zinc-500/20 text-zinc-200 border-white/10":"bg-white/10 text-white/75 border-white/10",[y]),j=async e=>{let t=e||d;if(!t||!m)return null;let a=await (0,r.n)("/api/terminal/".concat(t),{token:m});return(null==a?void 0:a.item)?(h(a.item),a.item):null},N=async()=>{if(d){w(!0);try{await (0,r.n)("/api/terminal/".concat(d,"/approve"),{method:"POST",token:m}),await j(d)}finally{w(!1)}}},k=async()=>{if(d){w(!0);try{await (0,r.n)("/api/terminal/".concat(d,"/deny"),{method:"POST",token:m}),await j(d)}finally{w(!1)}}},C=async()=>{if(d){w(!0);try{let e=await (0,r.n)("/api/terminal/".concat(d,"/rerun"),{method:"POST",token:m});(null==e?void 0:e.id)&&(await j(e.id),"function"==typeof x&&x(e.id))}finally{w(!1)}}};return(0,s.jsxs)("div",{className:"mb-2 rounded-2xl border border-white/10 bg-black/20 p-3",children:[(0,s.jsxs)("div",{className:"flex items-start justify-between gap-3",children:[(0,s.jsxs)("div",{className:"min-w-0 flex-1",children:[(0,s.jsx)("p",{className:"text-xs uppercase tracking-[0.2em] text-white/35",children:"Terminal"}),(0,s.jsx)("p",{className:"mt-1 break-words text-sm font-medium text-white/90",children:(null==u?void 0:u.command)||c||"Terminal command"})]}),(0,s.jsx)("span",{className:"rounded-full border px-2.5 py-1 text-[11px] font-medium capitalize ".concat(v),children:y})]}),(0,s.jsxs)("div",{className:"mt-3 grid gap-2 text-xs text-white/45",children:[(0,s.jsxs)("div",{children:["Requested: ",g(null==u?void 0:u.requestedAt)]}),(null==u?void 0:u.executedAt)?(0,s.jsxs)("div",{children:["Executed: ",g(u.executedAt)]}):null]}),(null==u||null==(t=u.result)?void 0:t.stdout)||(null==u||null==(a=u.result)?void 0:a.stderr)?(0,s.jsxs)("div",{className:"mt-3 space-y-2",children:[(null==u||null==(i=u.result)?void 0:i.stdout)?(0,s.jsx)("pre",{className:"max-h-28 overflow-auto rounded-xl bg-black/35 p-3 text-xs text-emerald-100/90",children:u.result.stdout}):null,(null==u||null==(l=u.result)?void 0:l.stderr)?(0,s.jsx)("pre",{className:"max-h-28 overflow-auto rounded-xl bg-black/35 p-3 text-xs text-red-100/90",children:u.result.stderr}):null]}):null,(0,s.jsxs)("div",{className:"mt-3 flex flex-wrap items-center justify-end gap-2",children:[(0,s.jsx)("button",{type:"button",className:"rounded-full bg-yellow-600 px-3 py-1.5 text-xs font-semibold text-white disabled:opacity-50",onClick:C,disabled:b||p,children:"Rerun"}),"pending"===y?(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)("button",{type:"button",className:"rounded-full bg-emerald-600 px-3 py-1.5 text-xs font-semibold text-white disabled:opacity-50",onClick:N,disabled:b||p,children:"Approve"}),(0,s.jsx)("button",{type:"button",className:"rounded-full bg-red-600 px-3 py-1.5 text-xs font-semibold text-white disabled:opacity-50",onClick:k,disabled:b||p,children:"Deny"})]}):null]})]})}function w(e){let{isOpen:t,onClose:a,message:i,onReply:l,onDelete:r,onForward:o,isOutbound:c,triggerRef:x}=e,m=(0,n.useRef)(null),[u,h]=(0,n.useState)({top:0,left:0,transformOrigin:"top-right"});return((0,n.useEffect)(()=>{function e(e){m.current&&!m.current.contains(e.target)&&(null==x?void 0:x.current)&&!x.current.contains(e.target)&&a()}if(t)return document.addEventListener("mousedown",e),()=>document.removeEventListener("mousedown",e)},[t,a,x]),(0,n.useEffect)(()=>{if(!t||!(null==x?void 0:x.current)||!(null==m?void 0:m.current))return;let e=()=>{let e=x.current,t=m.current,a=e.getBoundingClientRect(),s=t.getBoundingClientRect(),n=window.innerWidth,i=window.innerHeight,l=s.height||180,r=0,o="top-right",d=n-a.right,c=a.left;d>=232?(r=a.width+8,o="top-left"):c>=232?(r=-232,o="top-right"):(r=a.width+8,o="top-left");let u=i-a.bottom,p=a.top;h({top:u>=l+8?a.height+8:p>=l+8?-(l+8):a.height+8,left:r,transformOrigin:o})};return e(),window.addEventListener("resize",e),()=>window.removeEventListener("resize",e)},[t,x]),t)?(0,s.jsxs)("div",{ref:m,style:{position:"absolute",top:"".concat(u.top,"px"),left:"".concat(u.left,"px"),transformOrigin:u.transformOrigin},className:"z-50 w-56 rounded-2xl border border-white/10 bg-[#1a1b1b] shadow-[0_16px_32px_rgba(0,0,0,0.4)]",children:[(0,s.jsxs)("button",{type:"button",onClick:a,className:"flex w-full items-center gap-3 border-b border-white/10 px-4 py-3 text-sm font-medium text-white transition hover:bg-white/5",children:[(0,s.jsx)("span",{children:"←"}),(0,s.jsx)("span",{children:"Back"})]}),(0,s.jsxs)("div",{className:"space-y-0",children:[(0,s.jsxs)("button",{type:"button",onClick:()=>{l(),a()},className:"flex w-full items-center gap-3 px-4 py-3 text-sm text-white/80 transition hover:bg-white/5 hover:text-white",children:[(0,s.jsx)("span",{children:(0,s.jsx)(d.EdT,{className:"w-4 h-4"})}),(0,s.jsx)("span",{children:"Reply"})]}),c&&(0,s.jsxs)("button",{type:"button",onClick:()=>{r(),a()},className:"flex w-full items-center gap-3 px-4 py-3 text-sm text-white/80 transition hover:bg-red-500/10 hover:text-red-300",children:[(0,s.jsx)("span",{children:(0,s.jsx)(d.b6i,{className:"w-4 h-4"})}),(0,s.jsx)("span",{children:"Delete"})]}),(0,s.jsxs)("button",{type:"button",onClick:()=>{o(),a()},className:"flex w-full items-center gap-3 px-4 py-3 text-sm text-white/80 transition hover:bg-white/5 hover:text-white",children:[(0,s.jsx)("span",{children:(0,s.jsx)(d.RZ7,{className:"w-4 h-4"})}),(0,s.jsx)("span",{children:"Forward"})]})]})]}):null}function y(e){var t;let{media:a,onClose:i}=e,[l,r]=(0,n.useState)(1);if((0,n.useEffect)(()=>{let e=e=>{"Escape"===e.key&&i(),("+"===e.key||"="===e.key)&&(e.preventDefault(),r(e=>Math.min(e+.2,3))),"-"===e.key&&(e.preventDefault(),r(e=>Math.max(e-.2,.5)))};return window.addEventListener("keydown",e),()=>window.removeEventListener("keydown",e)},[i]),!a)return null;let o=a.mediaUrl||(a.relativePath?"".concat(null==(t=getApiBaseUrl)?void 0:t(),"/")+a.relativePath:""),c=a.mimeType||"",x=a.originalName||"media",m=void 0!==a.isImage?a.isImage:c.startsWith("image/")&&"image/webp"!==c,u=async()=>{try{let e=await fetch(o),t=await e.blob(),a=window.URL.createObjectURL(t),s=document.createElement("a");s.href=a,s.download=x,document.body.appendChild(s),s.click(),document.body.removeChild(s),window.URL.revokeObjectURL(a)}catch(e){console.error("Download failed:",e),alert("Download failed: "+e.message)}};return(0,s.jsx)("div",{className:"fixed inset-0 z-50 flex items-center justify-center bg-black/80 backdrop-blur-sm",onClick:i,children:(0,s.jsxs)("div",{className:"relative max-h-[90vh] max-w-[90vw] rounded-2xl bg-black shadow-2xl",onClick:e=>e.stopPropagation(),children:[(0,s.jsx)("button",{type:"button",onClick:i,className:"absolute right-6 top-6 z-10 rounded-full bg-black/60 w-10 h-10 flex items-center justify-center transition hover:bg-black/80 text-white",title:"Close (Esc)",children:(0,s.jsx)(d.m6K,{className:"w-5 h-5"})}),(0,s.jsxs)("button",{type:"button",onClick:u,className:"absolute bottom-6 right-6 z-10 rounded-lg bg-black/60 px-4 py-2 text-sm font-medium text-white transition hover:bg-black/80 flex items-center gap-2",title:"Download",children:[(0,s.jsx)(d.E5v,{className:"w-4 h-4"}),"Download"]}),(0,s.jsx)("div",{className:"flex items-center justify-center overflow-auto p-4",children:m?(0,s.jsx)("img",{src:o,alt:x,className:"max-h-[calc(90vh-80px)] max-w-[calc(90vw-32px)] rounded-xl object-contain",style:{transform:"scale(".concat(l,")"),transition:"transform 0.2s ease-in-out"}}):c.startsWith("video/")?(0,s.jsx)("video",{controls:!0,autoPlay:!0,className:"max-h-[calc(90vh-80px)] max-w-[calc(90vw-32px)] rounded-xl",children:(0,s.jsx)("source",{src:o,type:c})}):c.startsWith("audio/")?(0,s.jsx)("audio",{controls:!0,autoPlay:!0,className:"w-full max-w-[500px]",children:(0,s.jsx)("source",{src:o,type:c})}):null}),m&&(0,s.jsxs)("div",{className:"absolute bottom-6 left-6 flex items-center gap-2 rounded-lg bg-black/60 px-3 py-2",children:[(0,s.jsx)("button",{type:"button",onClick:()=>r(e=>Math.max(e-.2,.5)),className:"rounded p-1 transition hover:bg-black/80 text-white flex items-center justify-center",title:"Zoom out",children:(0,s.jsx)(d.ak5,{className:"w-4 h-4"})}),(0,s.jsxs)("span",{className:"min-w-[40px] text-center text-sm text-white",children:[Math.round(100*l),"%"]}),(0,s.jsx)("button",{type:"button",onClick:()=>r(e=>Math.min(e+.2,3)),className:"rounded p-1 transition hover:bg-black/80 text-white flex items-center justify-center",title:"Zoom in",children:(0,s.jsx)(d.jgn,{className:"w-4 h-4"})}),(0,s.jsx)("div",{className:"h-4 w-px bg-white/20"}),(0,s.jsx)("button",{type:"button",onClick:()=>r(1),className:"px-2 py-1 text-xs transition hover:bg-black/80 text-white",title:"Reset zoom",children:"Reset"})]})]})})}let v={smileys:{label:"\uD83D\uDE0A",emojis:["\uD83D\uDE00","\uD83D\uDE03","\uD83D\uDE04","\uD83D\uDE01","\uD83D\uDE06","\uD83D\uDE05","\uD83E\uDD23","\uD83D\uDE02","\uD83D\uDE42","\uD83D\uDE43","\uD83D\uDE09","\uD83D\uDE0A","\uD83D\uDE07","\uD83E\uDD70","\uD83D\uDE0D","\uD83E\uDD29","\uD83D\uDE18","\uD83D\uDE17","\uD83D\uDE1A","\uD83D\uDE19","\uD83E\uDD72","\uD83D\uDE0B","\uD83D\uDE1B","\uD83D\uDE1C","\uD83E\uDD2A","\uD83D\uDE0C","\uD83D\uDE14","\uD83D\uDE11","\uD83D\uDE10","\uD83D\uDE10","\uD83D\uDE11","\uD83D\uDE12","\uD83D\uDE41","☹️","\uD83D\uDE32","\uD83D\uDE1E","\uD83D\uDE16","\uD83D\uDE22","\uD83D\uDE2D","\uD83D\uDE24","\uD83D\uDE20","\uD83D\uDE21","\uD83E\uDD2C","\uD83D\uDE08","\uD83D\uDC7F","\uD83D\uDC80","☠️","\uD83D\uDCA9","\uD83E\uDD21","\uD83D\uDC79","\uD83D\uDC7A","\uD83D\uDC7B","\uD83D\uDC7D","\uD83D\uDC7E","\uD83E\uDD16"]},gestures:{label:"\uD83D\uDC4B",emojis:["\uD83D\uDC4B","\uD83E\uDD1A","\uD83D\uDD90️","✋","\uD83D\uDD96","\uD83D\uDC4C","\uD83E\uDD0C","\uD83E\uDD0F","✌️","\uD83E\uDD1E","\uD83E\uDEF0","\uD83E\uDD1F","\uD83E\uDD18","\uD83E\uDD19","\uD83D\uDC4D","\uD83D\uDC4E","✊","\uD83D\uDC4A","\uD83E\uDD1B","\uD83E\uDD1C","\uD83D\uDC4F","\uD83D\uDE4C","\uD83D\uDC50","\uD83E\uDD32","\uD83E\uDD1D","\uD83E\uDD1C","\uD83E\uDD1B","\uD83E\uDEF1","\uD83E\uDEF2","\uD83D\uDC85","\uD83D\uDC42","\uD83D\uDC43","\uD83E\uDDE0","\uD83E\uDEC0","\uD83E\uDEC1","\uD83E\uDDB7","\uD83E\uDDB4","\uD83D\uDC40","\uD83D\uDC41️","\uD83D\uDC45","\uD83D\uDC44"]},hearts:{label:"❤️",emojis:["❤️","\uD83E\uDDE1","\uD83D\uDC9B","\uD83D\uDC9A","\uD83D\uDC99","\uD83D\uDC9C","\uD83D\uDDA4","\uD83E\uDD0D","\uD83E\uDD0E","\uD83D\uDC94","\uD83D\uDC95","\uD83D\uDC9E","\uD83D\uDC93","\uD83D\uDC97","\uD83D\uDC96","\uD83D\uDC98","\uD83D\uDC9D","\uD83D\uDC9F","\uD83D\uDC8C","\uD83D\uDCA4","\uD83D\uDC8B"]},hand:{label:"\uD83D\uDC4C",emojis:["\uD83D\uDC4C","\uD83E\uDD0C","\uD83E\uDD0F","✌️","\uD83E\uDD1E","\uD83E\uDEF0","\uD83E\uDD1F","\uD83E\uDD18","\uD83E\uDD19","\uD83D\uDC4D","\uD83D\uDC4E","✊","\uD83D\uDC4A","\uD83E\uDD1B","\uD83E\uDD1C","\uD83D\uDC4F","\uD83D\uDE4C","\uD83D\uDC50","\uD83E\uDD32","\uD83E\uDD1D"]},animals:{label:"\uD83D\uDC36",emojis:["\uD83D\uDC36","\uD83D\uDC31","\uD83D\uDC2D","\uD83D\uDC39","\uD83D\uDC30","\uD83E\uDD8A","\uD83D\uDC3B","\uD83D\uDC3C","\uD83D\uDC28","\uD83D\uDC2F","\uD83E\uDD81","\uD83D\uDC2E","\uD83D\uDC37","\uD83D\uDC38","\uD83D\uDC35","\uD83D\uDE48","\uD83D\uDE49","\uD83D\uDE4A","\uD83D\uDC12","\uD83D\uDC14","\uD83D\uDC27","\uD83D\uDC26","\uD83D\uDC24","\uD83D\uDC23","\uD83D\uDC25","\uD83E\uDD86","\uD83E\uDD85","\uD83E\uDD89","\uD83E\uDD87","\uD83D\uDC3A","\uD83D\uDC17","\uD83D\uDC34","\uD83E\uDD84","\uD83D\uDC1D","\uD83E\uDEB1","\uD83D\uDC1B","\uD83E\uDD8B","\uD83D\uDC0C","\uD83D\uDC1E","\uD83D\uDC1C","\uD83E\uDEB0","\uD83E\uDD9F"]},food:{label:"\uD83C\uDF4E",emojis:["\uD83C\uDF4E","\uD83C\uDF4A","\uD83C\uDF4B","\uD83C\uDF4C","\uD83C\uDF49","\uD83C\uDF47","\uD83C\uDF53","\uD83E\uDED0","\uD83C\uDF48","\uD83C\uDF52","\uD83C\uDF51","\uD83E\uDD6D","\uD83C\uDF4D","\uD83E\uDD65","\uD83E\uDD51","\uD83C\uDF46","\uD83C\uDF45","\uD83C\uDF36️","\uD83C\uDF3D","\uD83E\uDD52","\uD83E\uDD6C","\uD83E\uDD66","\uD83E\uDDC4","\uD83E\uDDC5","\uD83C\uDF44","\uD83E\uDD5C","\uD83C\uDF30","\uD83C\uDF5E","\uD83E\uDD50","\uD83E\uDD56","\uD83E\uDD68","\uD83E\uDD6F","\uD83E\uDD5E","\uD83E\uDDC7","\uD83E\uDD5A","\uD83C\uDF73","\uD83E\uDDC8","\uD83E\uDD5E","\uD83E\uDD53","\uD83E\uDD54","\uD83C\uDF5F","\uD83C\uDF57","\uD83C\uDF56","\uD83C\uDF2D","\uD83C\uDF54","\uD83C\uDF5F","\uD83C\uDF55"]},travel:{label:"\uD83C\uDFA1",emojis:["✈️","\uD83D\uDEEB","\uD83D\uDEEC","\uD83D\uDEE9️","\uD83D\uDCBA","\uD83D\uDEF0️","\uD83D\uDE81","\uD83D\uDE82","\uD83D\uDE83","\uD83D\uDE84","\uD83D\uDE85","\uD83D\uDE86","\uD83D\uDE87","\uD83D\uDE88","\uD83D\uDE89","\uD83D\uDE8A","\uD83D\uDE9D","\uD83D\uDE9E","\uD83D\uDE8B","\uD83D\uDE8C","\uD83D\uDE8D","\uD83D\uDE8E","\uD83D\uDE90","\uD83D\uDE91","\uD83D\uDE92","\uD83D\uDE93","\uD83D\uDE94","\uD83D\uDE95","\uD83D\uDE96","\uD83D\uDE97","\uD83D\uDE98","\uD83D\uDE99","\uD83D\uDE9A","\uD83D\uDE9B","\uD83D\uDE9C","\uD83C\uDFCE️","\uD83C\uDFCD️","\uD83D\uDEF5","\uD83E\uDDAF","\uD83E\uDDBD","\uD83E\uDDBC","\uD83D\uDEFA","\uD83D\uDEB2","\uD83D\uDEF4","\uD83D\uDEF9","\uD83D\uDEFC","\uD83D\uDEDE","\uD83D\uDE8F","⛽"]},activities:{label:"⚽",emojis:["⚽","⚾","\uD83E\uDD4E","\uD83C\uDFBE","\uD83C\uDFC0","\uD83C\uDFD0","\uD83C\uDFC8","\uD83C\uDFC9","\uD83E\uDD4F","\uD83C\uDFB3","\uD83C\uDFD3","\uD83C\uDFF8","\uD83C\uDFD2","\uD83C\uDFD1","\uD83E\uDD4D","\uD83C\uDFCF","\uD83E\uDD45","⛳","⛸️","\uD83C\uDFA3","\uD83C\uDFBD","\uD83C\uDFBF","⛷️","\uD83C\uDFC2","\uD83E\uDE82","\uD83E\uDD3C","\uD83E\uDD38","⛹️","\uD83E\uDD3A","\uD83E\uDD3E","\uD83C\uDFCC️","\uD83C\uDFC7","\uD83E\uDDD8","\uD83C\uDFC4","\uD83C\uDFCA","\uD83E\uDD3D","\uD83D\uDEA3","\uD83E\uDDD7","\uD83D\uDEB4","\uD83D\uDEB5","\uD83E\uDD39","\uD83C\uDFAA"]}};function j(e){let{isOpen:t,onClose:a,onEmojiSelect:i,triggerRef:l}=e,r=(0,n.useRef)(null),o=(0,n.useRef)(null);return((0,n.useEffect)(()=>{function e(e){r.current&&!r.current.contains(e.target)&&(null==l?void 0:l.current)&&!l.current.contains(e.target)&&a()}if(t)return document.addEventListener("mousedown",e),()=>document.removeEventListener("mousedown",e)},[t,a,l]),t)?(0,s.jsxs)("div",{ref:r,className:"relative mb-2 w-80 rounded-2xl border border-white/10 bg-[#1a1b1b] shadow-[0_16px_32px_rgba(0,0,0,0.4)]",children:[(0,s.jsxs)("div",{className:"flex h-12 items-center border-b border-white/10 px-3",children:[(0,s.jsx)("h3",{className:"flex-1 text-sm font-semibold text-white",children:"Emoji"}),(0,s.jsx)("button",{type:"button",onClick:a,className:"flex h-6 w-6 items-center justify-center rounded-full text-white/60 transition hover:bg-white/10 hover:text-white",title:"Close emoji picker",children:(0,s.jsx)(d.m6K,{className:"w-4 h-4"})})]}),(0,s.jsx)("div",{ref:o,className:"max-h-64 overflow-y-auto p-3",children:(0,s.jsx)("div",{className:"space-y-3",children:Object.entries(v).map(e=>{let[t,n]=e;return(0,s.jsxs)("div",{children:[(0,s.jsx)("div",{className:"mb-2 text-xs font-semibold text-white/50",children:n.label}),(0,s.jsx)("div",{className:"grid grid-cols-8 gap-2",children:n.emojis.map((e,n)=>(0,s.jsx)("button",{type:"button",onClick:()=>{i(e),a()},className:"flex h-8 w-8 items-center justify-center rounded-lg text-lg transition hover:bg-white/10",title:e,children:e},"".concat(t,"-").concat(n)))})]},t)})})})]}):null}function N(){return(0,s.jsx)("div",{className:"flex justify-start",children:(0,s.jsxs)("div",{className:"max-w-[72%] space-y-2 rounded-[18px] bg-[#2e2f2f] px-4 py-3",children:[(0,s.jsx)("div",{className:"h-4 w-48 animate-pulse rounded bg-white/10"}),(0,s.jsx)("div",{className:"h-4 w-40 animate-pulse rounded bg-white/10"}),(0,s.jsxs)("div",{className:"mt-3 flex justify-between",children:[(0,s.jsx)("div",{className:"h-3 w-12 animate-pulse rounded bg-white/5"}),(0,s.jsx)("div",{className:"h-3 w-8 animate-pulse rounded bg-white/5"})]})]})})}function k(){return(0,s.jsx)("div",{className:"space-y-3",children:Array.from({length:5}).map((e,t)=>(0,s.jsx)(N,{},t))})}function C(){return(0,s.jsxs)("div",{className:"flex items-center gap-3 border-b border-white/5 px-3 py-3 transition hover:bg-white/5 cursor-pointer",children:[(0,s.jsx)("div",{className:"h-12 w-12 animate-pulse rounded-2xl bg-white/10"}),(0,s.jsxs)("div",{className:"flex-1 min-w-0",children:[(0,s.jsx)("div",{className:"h-4 w-32 animate-pulse rounded bg-white/10 mb-2"}),(0,s.jsx)("div",{className:"h-3 w-48 animate-pulse rounded bg-white/5"})]}),(0,s.jsx)("div",{className:"h-3 w-10 animate-pulse rounded bg-white/5"})]})}function S(){return(0,s.jsx)("div",{children:Array.from({length:8}).map((e,t)=>(0,s.jsx)(C,{},t))})}function A(){return(0,s.jsxs)("svg",{className:"h-5 w-5 animate-spin",xmlns:"http://www.w3.org/2000/svg",fill:"none",viewBox:"0 0 24 24",children:[(0,s.jsx)("circle",{className:"opacity-25",cx:"12",cy:"12",r:"10",stroke:"currentColor",strokeWidth:"4"}),(0,s.jsx)("path",{className:"opacity-75",fill:"currentColor",d:"M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"})]})}function T(e){return e?new Intl.DateTimeFormat("id-ID",{hour:"2-digit",minute:"2-digit"}).format(new Date(e)):""}function E(e){var t,a,s;let n=null==(t=e.outboundDelivery)?void 0:t.status;if("failed"===n)return"Failed";if("canceled"===n)return"Canceled";if("queued"===n||"sending"===n)return"Sending";let i=null==(s=e.statuses)||null==(a=s[e.statuses.length-1])?void 0:a.status;return i&&"outbound"===e.direction?"read"===i?"Read":"delivered"===i?"Delivered":"Sent":""}function P(e){var t;return e?e.body?e.body:(null==(t=e.mediaFile)?void 0:t.originalName)?e.mediaFile.originalName:"Attachment":""}function I(e){let{src:t,label:a}=e;return t?(0,s.jsx)("img",{src:t,alt:a,className:"h-11 w-11 rounded-2xl object-cover"}):(0,s.jsx)("div",{className:"flex h-11 w-11 items-center justify-center rounded-2xl bg-[#2e2f2f] text-sm font-semibold text-white",children:String(a||"?").slice(0,2).toUpperCase()})}function R(e){let{mediaUrl:t,originalName:a,createdAt:i}=e,[l,r]=(0,n.useState)(120),[o,c]=(0,n.useState)(!1);return((0,n.useEffect)(()=>{let e=new Date(i).getTime(),t=()=>{let t=120-Math.floor((Date.now()-e)/1e3);t<=0?(r(0),c(!0)):(r(t),c(!1))};t();let a=setInterval(t,1e3);return()=>clearInterval(a)},[i]),o)?(0,s.jsxs)("div",{className:"flex h-[450px] w-[450px] flex-col items-center justify-center rounded-2xl bg-[#2e2f2f] p-6 text-center shadow-lg border border-white/5",children:[(0,s.jsx)(d.joZ,{className:"mb-4 text-5xl text-white/20 animate-spin-slow"}),(0,s.jsx)("p",{className:"mb-4 font-medium text-white/60",children:"QR Code Expired"}),(0,s.jsx)("button",{onClick:()=>window.location.reload(),className:"rounded-full bg-emerald-500 px-6 py-2 text-sm font-semibold text-white shadow-md hover:bg-emerald-600 transition",children:"Refresh to Get New QR"})]}):(0,s.jsxs)("div",{className:"relative h-[450px] w-[450px] overflow-hidden rounded-2xl bg-white p-6 shadow-lg",children:[(0,s.jsx)("img",{src:t,alt:a,className:"h-full w-full object-contain"}),(0,s.jsx)("div",{className:"absolute bottom-4 right-4 flex items-center gap-2 rounded-full bg-black/70 px-3 py-1 text-xs font-bold text-white backdrop-blur-sm",children:(0,s.jsxs)("span",{className:l<=10?"text-red-400 animate-pulse":"",children:["Expires in ",l,"s"]})})]})}function O(e){return e&&e.startsWith("image/")&&"image/webp"!==e}let _=(0,n.forwardRef)(function(e,t){var a,i;let{chat:l,messages:m,chats:u,typingState:h,loading:p,messagesLoading:g,loadingOlder:v,hasMoreMessages:N,messageQuery:C,onMessageQueryChange:S,onLoadOlder:_,onSendMessage:D,onSendMedia:L,onTyping:W,onDeleteMessage:F,onForwardMessage:M,onOpenContacts:U,onOpenCrm:q,onOpenSettings:B,onLogout:z}=e,[K,H]=(0,n.useState)(""),[Q,G]=(0,n.useState)(!1),[Z,Y]=(0,n.useState)(!1),[J,V]=(0,n.useState)(null),[X,$]=(0,n.useState)(null),[ee,et]=(0,n.useState)(""),[ea,es]=(0,n.useState)(!!C),[en,ei]=(0,n.useState)(0),[el,er]=(0,n.useState)(null),[eo,ed]=(0,n.useState)(null),[ec,ex]=(0,n.useState)(null),[em,eu]=(0,n.useState)(!1),[eh,ep]=(0,n.useState)([]),[ef,eg]=(0,n.useState)(!1),[eb,ew]=(0,n.useState)({}),ey=(0,o.C)(e=>e.updateMessage),ev=(0,o.C)(e=>e.typingByChat),ej=h||((null==l?void 0:l.id)?ev[null==l?void 0:l.id]:null),eN=(0,n.useRef)(null),ek=(0,n.useRef)(null),eC=(0,n.useRef)(null),eS=(0,n.useRef)(null),eA=(0,n.useRef)(!1),eT=(0,n.useRef)(0),eE=(0,n.useRef)(null),eP=(0,n.useRef)(null),eI=(0,n.useRef)(null),eR=(0,n.useRef)(null),[eO,e_]=(0,n.useState)(!1),eD=(0,n.useRef)(null),eL=(0,n.useRef)(null),[eW,eF]=(0,n.useState)(!1),eM=(0,n.useRef)(null),eU=(0,n.useRef)(null),[eq,eB]=(0,n.useState)([]),[ez,eK]=(0,n.useState)(!1),[eH,eQ]=(0,n.useState)({}),[eG,eZ]=(0,n.useState)(""),[eY,eJ]=(0,n.useState)(""),[eV,eX]=(0,n.useState)(""),[e$,e0]=(0,n.useState)(!1);(0,n.useEffect)(()=>{let e=e=>{em&&eR.current&&!eR.current.contains(e.target)&&!eI.current.contains(e.target)&&eu(!1),eO&&eL.current&&!eL.current.contains(e.target)&&!eD.current.contains(e.target)&&e_(!1),eW&&eU.current&&!eU.current.contains(e.target)&&!eM.current.contains(e.target)&&eF(!1)};return document.addEventListener("mousedown",e),()=>{document.removeEventListener("mousedown",e)}},[em,eO,eW]);let e1=[...(null==l||null==(a=l.contact)?void 0:a.externalId)&&("openwa:assistant"===l.contact.externalId||String(l.contact.externalId).startsWith("openwa:assistant")||String(l.contact.externalId).endsWith(":assistant"))?[{label:"New Assistant Chat",message:"/new",icon:"✨"}]:[],{label:"Add WhatsApp Device",message:"Please help me add a new WhatsApp device",icon:"\uD83D\uDCF1"},{label:"Integrate Telegram",message:"How can I remote Wanie via Telegram?",icon:"\uD83E\uDD16"},{label:"Latest Messages",message:"Show me the latest messages from my WhatsApp chats",icon:"\uD83D\uDCAC"},{label:"Create Coding Project",message:"I want to create a new coding project in the workspace. Help me scaffold it.",icon:"\uD83D\uDE80"},{label:"Register New Tool",message:"I want to register a new external tool/API to your capabilities.",icon:"\uD83D\uDEE0️"},{label:"Setup LLM Provider",message:"Help me setup an LLM Provider (OpenAI/Anthropic/Ollama)",icon:"\uD83E\uDDE0"},{label:"Create API Key",message:"I want to create a new API Key",icon:"\uD83D\uDD11"},{label:"Check Workspace",message:"Check the contents of my workspace folder",icon:"\uD83D\uDCC1"},{label:"Reset Password",message:"/reset_password",icon:"\uD83D\uDD12"},{label:"Help / Capabilities",message:"What are your capabilities as an AI Assistant?",icon:"❓"}],e2=async e=>{e_(!1),H(""),G(!0);try{await D({body:e,replyToId:null}),W(!1)}catch(t){console.error("Failed to send shortcut message:",t),H(e)}finally{G(!1)}},e5=(0,n.useMemo)(()=>{let e=String(C||"").trim().toLowerCase();return e?m.map((t,a)=>{var s,n;return[t.body,t.sender,null==(s=t.replyTo)?void 0:s.body,null==(n=t.mediaFile)?void 0:n.originalName].filter(Boolean).some(t=>t.toLowerCase().includes(e))?a:-1}).filter(e=>-1!==e):[]},[C,m]);(0,n.useMemo)(()=>m,[C,m]);let e3=u.filter(e=>e.id!==(null==l?void 0:l.id)),e4=async()=>{if(eh.length>0)return void await e9();K.trim()&&await e8()},e6=async e=>{e.preventDefault(),await e4()},e8=async()=>{if(K.trim()){G(!0);try{await D({body:K.trim(),replyToId:(null==J?void 0:J.id)||null}),H(""),V(null),W(!1)}finally{G(!1)}}},e7=async e=>{let t=e.target.files;if(!t||0===t.length)return;let a=[];for(let e of t){let t=null,s=e.type||"";s.startsWith("image/")?t=URL.createObjectURL(e):s.startsWith("video/")&&(t=URL.createObjectURL(e)),a.push({file:e,name:e.name,size:e.size,type:s,preview:t})}ep(e=>[...e,...a]),e.target.value=""},e9=async()=>{if(0===eh.length)return e8();let e=[...eh];G(!0),Y(!0),H(""),V(null),ep([]),W(!1);try{for(let t=0;t<e.length;t++){let a=e[t],s=t===e.length-1?K.trim():"";await L({file:a.file,caption:s})}for(let t of e||[])(null==t?void 0:t.preview)&&URL.revokeObjectURL(t.preview)}catch(t){throw H(K),V(J),ep(e),t}finally{G(!1),Y(!1)}},te=(0,o.C)(e=>e.token),tt=(0,o.C)(e=>e.setActiveChat),ta=(0,o.C)(e=>e.upsertChat),ts=(0,o.C)(e=>e.defaultAiProviderId),tn=(0,o.C)(e=>e.defaultAiModel),ti=(0,o.C)(e=>e.setDefaultAiProvider),tl=(0,o.C)(e=>e.setDefaultAiModel),[tr,to]=(0,n.useState)(!1);(0,n.useEffect)(()=>{if(!eW)return;let e=!0;return eK(!0),eZ(ts||""),eJ(tn||""),eX(""),(async()=>{if(!te){e&&eK(!1);return}try{let t=await (0,r.n)("/api/ai-providers",{token:te});if(!e)return;eB(t.providers||[])}catch(e){}finally{e&&eK(!1)}})(),()=>{e=!1}},[eW,te,ts,tn]),(0,n.useEffect)(()=>{if(!eG||eH[eG]||!te)return;let e=!0;return(async()=>{try{let t=await (0,r.n)("/api/ai-providers/".concat(eG,"/models"),{token:te});if(!e)return;eQ(e=>({...e,[eG]:t.models||[]}))}catch(e){}})(),()=>{e=!1}},[eG,te,eH]);let td=async()=>{if(eG){e0(!0);try{await ti(eG);let e=eY||eV.trim()||null;await tl(e),eF(!1)}catch(e){}finally{e0(!1)}}},tc=e=>{eX(e)},tx=async(e,t)=>{if(!te)return alert("Not authenticated");ew(e=>({...e,[t]:!0}));try{let t=await fetch("".concat((0,r.h)(),"/api/sessions/").concat(e,"/confirm-scan"),{method:"POST",headers:{Authorization:"Bearer ".concat(te),"Content-Type":"application/json"}}),a=await t.json().catch(()=>({}));if(!t.ok)throw Error(a.error||"Request failed");alert("Scan confirmed; waiting for device to connect.")}catch(e){alert(e.message||"Failed to confirm scan")}finally{ew(e=>({...e,[t]:!1}))}},tm=async e=>{ee&&(await M(e,ee),$(null),et(""))},tu=e=>{var t;if(0===e5.length)return;let a=e5[e],s=document.querySelector('[data-message-id="'.concat(null==(t=m[a])?void 0:t.id,'"]'));s&&eC.current&&(eC.current.scrollTop=s.offsetTop-eC.current.offsetTop)};return((0,n.useEffect)(()=>{C&&e5.length>0&&(ei(0),tu(0))},[C]),(0,n.useImperativeHandle)(t,()=>({focusComposer(){var e;null==(e=eN.current)||e.focus()}}),[]),(0,n.useEffect)(()=>{let e=eN.current;e&&(e.style.height="0px",e.style.height="".concat(Math.min(e.scrollHeight,128),"px"))},[K]),(0,n.useEffect)(()=>{if(ea){var e;null==(e=ek.current)||e.focus()}},[ea]),(0,n.useEffect)(()=>{eA.current=!0,eT.current=0},[null==l?void 0:l.id]),(0,n.useEffect)(()=>{if(!(null==l?void 0:l.id))return;let e=function(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"smooth";eC.current&&eC.current.scrollTo({top:eC.current.scrollHeight,behavior:e})};if(eA.current){if(!m.length){e(),eA.current=!1;return}let t=setTimeout(()=>{requestAnimationFrame(()=>{requestAnimationFrame(()=>{requestAnimationFrame(()=>{e(),eA.current=!1})})})},300);return()=>clearTimeout(t)}let t=m.length,a=eT.current;if(eT.current=t,t>a&&!g){let t=setTimeout(()=>{e("smooth")},50);return()=>clearTimeout(t)}},[null==l?void 0:l.id,m.length,g]),p)?(0,s.jsx)("div",{className:"flex flex-1 items-center justify-center text-white/50",children:"Loading dashboard..."}):l?(0,s.jsxs)("section",{className:"flex min-w-0 flex-1 flex-col bg-[#161717] text-white",children:[(0,s.jsxs)("header",{className:"flex h-[78px] shrink-0 items-center justify-between gap-4 bg-[#161717] px-6 py-3",children:[(0,s.jsxs)("div",{className:"flex min-w-0 items-center gap-3 cursor-pointer",onClick:()=>eg(!0),children:[(0,s.jsx)(I,{src:l.contact.avatarUrl,label:l.contact.displayName}),(0,s.jsxs)("div",{className:"min-w-0",children:[(0,s.jsxs)("div",{className:"flex min-w-0 items-center gap-2",children:[(0,s.jsx)("h2",{className:"truncate font-semibold text-white",children:l.contact.displayName}),(0,s.jsx)(x.zd,{chat:l,compact:!0})]}),(0,s.jsx)("p",{className:"text-sm text-white/40",children:(null==ej?void 0:ej.isTyping)?"".concat(ej.name," is typing..."):"".concat((0,x.qh)(l)," chat synced locally")})]})]}),(0,s.jsxs)("div",{className:"flex items-center gap-2",children:[ea||C?(0,s.jsxs)("div",{className:"flex items-center gap-2 rounded-[22px] bg-[#2e2f2f] px-4 py-2",children:[(0,s.jsx)("input",{ref:ek,className:"w-[180px] border-none bg-transparent text-sm text-white outline-none placeholder:text-white/30",placeholder:"Search messages...",value:C,onChange:e=>S(e.target.value)}),e5.length>0&&(0,s.jsxs)("span",{className:"text-xs text-white/60",children:[en+1,"/",e5.length]}),e5.length>0&&(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)("button",{type:"button",title:"Previous result","aria-label":"Previous result",className:"text-sm leading-none text-white/55 transition hover:text-white",onClick:()=>{if(0===e5.length)return;let e=(en-1+e5.length)%e5.length;ei(e),tu(e)},children:"↑"}),(0,s.jsx)("button",{type:"button",title:"Next result","aria-label":"Next result",className:"text-sm leading-none text-white/55 transition hover:text-white",onClick:()=>{if(0===e5.length)return;let e=(en+1)%e5.length;ei(e),tu(e)},children:"↓"})]}),(0,s.jsx)("button",{type:"button",title:"Close search","aria-label":"Close search",className:"text-sm leading-none text-white/55 transition hover:text-white",onClick:()=>{S(""),es(!1)},children:(0,s.jsx)(d.m6K,{className:"w-4 h-4"})})]}):null,(0,s.jsx)("button",{type:"button",title:"Search","aria-label":"Search",className:"flex h-10 w-10 items-center justify-center rounded-full bg-[#2e2f2f] text-base leading-none text-white transition hover:bg-[#3a3b3b]",onClick:()=>es(!0),children:(0,s.jsx)(d.gZ7,{className:"w-5 h-5"})}),(0,s.jsx)("button",{type:"button",title:"New chat","aria-label":"New chat",className:"flex h-10 w-10 items-center justify-center rounded-full bg-[#2e2f2f] text-lg leading-none text-white transition hover:bg-[#3a3b3b]",onClick:U,children:(0,s.jsx)(d.jgn,{className:"w-5 h-5"})}),(0,s.jsxs)("button",{type:"button",title:"Open Dashboard CRM","aria-label":"Open Dashboard CRM",className:"relative flex h-10 items-center justify-center gap-2 overflow-hidden rounded-full bg-amber-400 px-3 text-sm font-semibold text-[#1d1600] shadow-[0_0_14px_rgba(251,191,36,0.28)] ring-1 ring-amber-100/70 transition hover:bg-amber-300 focus:outline-none focus:ring-2 focus:ring-amber-200",onClick:q,children:[(0,s.jsx)(d.Itr,{className:"relative h-5 w-5"}),(0,s.jsx)("span",{className:"relative whitespace-nowrap",children:"Dashboard CRM"})]}),(0,s.jsx)("button",{type:"button",title:"New Assistant","aria-label":"New Assistant",className:"flex h-10 w-10 items-center justify-center rounded-full bg-[#2e2f2f] text-sm leading-none text-white transition hover:bg-[#3a3b3b]",onClick:async()=>{if(!te)return alert("Not authenticated");if(confirm("Start a new Assistant conversation?")){to(!0);try{let e=await fetch("".concat((0,r.h)(),"/api/assistant/sessions"),{method:"POST",headers:{Authorization:"Bearer ".concat(te),"Content-Type":"application/json"},body:JSON.stringify({})}),t=await e.json().catch(()=>({}));if(!e.ok)throw Error(t.error||"Request failed");let a=t.chat;a&&a.id&&(ta(a),tt(a.id))}catch(e){alert(e.message||"Failed to create assistant session")}finally{to(!1)}}},disabled:tr,children:tr?"...":"AI"}),(0,s.jsx)("button",{type:"button",title:"Settings","aria-label":"Settings",className:"flex h-10 w-10 items-center justify-center rounded-full bg-[#2e2f2f] text-base leading-none text-white transition hover:bg-[#3a3b3b]",onClick:B,children:(0,s.jsx)(d.hgV,{className:"w-5 h-5"})}),(0,s.jsx)("button",{type:"button",title:"Logout","aria-label":"Logout",className:"flex h-10 w-10 items-center justify-center rounded-full bg-[#2e2f2f] text-base leading-none text-white transition hover:bg-[#3a3b3b]",onClick:z,children:(0,s.jsx)(d.EyH,{className:"w-5 h-5"})})]})]}),(0,s.jsx)(c,{open:ef,chat:l,onClose:()=>eg(!1)}),(0,s.jsxs)("div",{ref:eC,className:"flex-1 overflow-y-auto bg-[#161717] px-8 py-5",children:[(0,s.jsx)("div",{className:"mb-5 flex justify-center",children:(0,s.jsx)("button",{type:"button",className:"rounded-full bg-[#2e2f2f] px-4 py-2 text-xs font-medium text-white/60 transition hover:text-white disabled:opacity-40",onClick:_,disabled:!N||v,children:v?"Loading...":N?"Load older messages":"All messages loaded"})}),g?(0,s.jsx)(k,{}):null,(0,s.jsxs)("div",{className:"space-y-3",children:[(function(e){let t=[],a=null;for(let n=0;n<e.length;n++){var s;let i=e[n],l=String((null==(s=i.mediaFile)?void 0:s.mimeType)||""),r=i.mediaFile&&O(l);r&&null===a?a={type:"image-group",messages:[i],direction:i.direction,startTime:new Date(i.createdAt).getTime()}:r&&a&&"image-group"===a.type&&a.direction===i.direction&&new Date(i.createdAt).getTime()-a.startTime<=12e4?a.messages.push(i):(a&&(t.push(a),a=null),r||t.push({type:"single",message:i}))}return a&&t.push(a),t})(m).map((e,t)=>{if("image-group"===e.type){let a=e.messages[0],n="outbound"===a.direction,i=e.messages.map(e=>e.body).filter(Boolean).join("\n");return(0,s.jsx)("div",{className:"flex ".concat(n?"justify-end":"justify-start"),children:(0,s.jsxs)("div",{className:"max-w-[72%] rounded-[18px] px-4 py-3 shadow-[0_16px_32px_rgba(0,0,0,0.18)] transition-colors relative ".concat(n?"bg-[#144d37]":"bg-[#2e2f2f]"),children:[function(e,t){let a=e.messages.map(e=>e.mediaFile).filter(Boolean);if(0===a.length)return null;if(1===a.length){let n=a[0],i=e.messages[0],l="".concat((0,r.h)(),"/").concat(n.relativePath);return"whatsapp-qr.png"===String(n.originalName||"").toLowerCase()||String(n.fileName||"").toLowerCase().startsWith("qr-")?(0,s.jsx)("div",{className:"mb-2",children:(0,s.jsx)(R,{mediaUrl:l,originalName:n.originalName,createdAt:i.createdAt})}):(0,s.jsx)("img",{src:l,alt:n.originalName,className:"mb-2 h-24 w-24 cursor-pointer rounded-2xl object-cover",onClick:()=>t({mediaUrl:l,relativePath:n.relativePath,mimeType:n.mimeType,originalName:n.originalName,isImage:!0})})}return(0,s.jsx)("div",{className:"mb-2 grid grid-cols-2 gap-1",children:a.map((e,a)=>{let n="".concat((0,r.h)(),"/").concat(e.relativePath);return(0,s.jsx)("img",{src:n,alt:e.originalName,className:"h-32 w-32 cursor-pointer rounded-lg object-cover",onClick:()=>t({mediaUrl:n,relativePath:e.relativePath,mimeType:e.mimeType,originalName:e.originalName,isImage:!0})},a)})})}(e,e=>ex(e)),i?(0,s.jsx)("p",{className:"mt-3 whitespace-pre-wrap text-sm leading-6 text-white/88",children:i}):null,(0,s.jsxs)("div",{className:"mt-3 flex items-center justify-end gap-2 text-[11px] text-white/35",children:[(0,s.jsx)("span",{children:T(a.createdAt)}),n?(0,s.jsx)("span",{children:E(a)}):null]})]})},"group-".concat(t))}{var a,n;let t=e.message,i="outbound"===t.direction,o=m.indexOf(t),c=C&&e5.includes(o),x=c&&e5[en]===o,u=function(e){let t=String((null==e?void 0:e.externalMessageId)||"");return t.startsWith("terminal:")&&t.slice(9)||null}(t),h=i&&(null==(n=t.outboundDelivery)?void 0:n.status)==="failed";return(0,s.jsx)("div",{"data-message-id":t.id,className:"flex ".concat(i?"justify-end":"justify-start"),children:(0,s.jsxs)("div",{className:"max-w-[72%] rounded-[18px] px-4 py-3 shadow-[0_16px_32px_rgba(0,0,0,0.18)] transition-colors relative ".concat(x?"ring-2 ring-brand-500 "+(i?"bg-[#1a5f41]":"bg-[#3a4a4a]"):c?"ring-1 ring-brand-500/50 "+(i?"bg-[#144d37]":"bg-[#2e2f2f]"):h?"bg-red-950/80 ring-1 ring-red-400/30":i?"bg-[#144d37]":"bg-[#2e2f2f]"),onMouseEnter:()=>er(t.id),onMouseLeave:()=>er(null),children:[t.replyTo?(0,s.jsxs)("div",{className:"mb-2 rounded-2xl border-l-4 border-brand-500 bg-white/[0.04] px-3 py-2 text-xs text-white/55",children:[(0,s.jsx)("span",{className:"font-semibold text-white",children:"outbound"===t.replyTo.direction?"Anda":l.contact.displayName}),(0,s.jsx)("p",{className:"mt-1 truncate",children:P(t.replyTo)})]}):null,t.mediaFile&&function(e,t){if(!e.mediaFile)return null;let a="".concat((0,r.h)(),"/").concat(e.mediaFile.relativePath),n=String(e.mediaFile.mimeType||""),i="sticker"===e.type||"image/webp"===n,l="whatsapp-qr.png"===String(e.mediaFile.originalName||"").toLowerCase()||String(e.mediaFile.fileName||"").toLowerCase().startsWith("qr-");return i?(0,s.jsx)("a",{href:a,target:"_blank",rel:"noreferrer",className:"mb-2 inline-flex overflow-hidden rounded-2xl bg-transparent",children:(0,s.jsx)("img",{src:a,alt:e.mediaFile.originalName,className:"h-36 w-36 object-contain drop-shadow-sm"})}):O(n)?l?(0,s.jsx)("div",{className:"mb-2",children:(0,s.jsx)(R,{mediaUrl:a,originalName:e.mediaFile.originalName,createdAt:e.createdAt})}):(0,s.jsx)("img",{src:a,alt:e.mediaFile.originalName,className:"mb-2 max-h-[320px] w-full cursor-pointer rounded-2xl object-contain",onClick:()=>t&&t({mediaUrl:a,relativePath:e.mediaFile.relativePath,mimeType:e.mediaFile.mimeType,originalName:e.mediaFile.originalName,isImage:!0})}):n.startsWith("video/")?(0,s.jsx)("video",{controls:!0,className:"mb-2 max-h-[320px] w-full rounded-2xl bg-black",children:(0,s.jsx)("source",{src:a,type:n})}):n.startsWith("audio/")?(0,s.jsx)("audio",{controls:!0,className:"mb-2 w-full",children:(0,s.jsx)("source",{src:a,type:n})}):(0,s.jsx)("a",{href:a,target:"_blank",rel:"noreferrer",className:"mb-2 inline-flex rounded-xl bg-white/10 px-3 py-2 text-sm font-medium text-white underline-offset-2 hover:underline",children:e.mediaFile.originalName})}(t,e=>ex(e)),t.mediaFile&&t.sender&&String(t.sender).startsWith("session:")&&String(t.sender).endsWith(":assistant")&&("image"===t.type||String(t.mediaFile.mimeType||"").startsWith("image/"))&&(0,s.jsx)("div",{className:"mt-2",children:(0,s.jsx)("button",{type:"button",className:"rounded-2xl bg-brand-500 px-3 py-2 text-sm font-semibold text-[#10251a]",onClick:()=>{let e=String(t.sender).split(":")[1]||null;e&&tx(e,t.id)},disabled:!!eb[t.id],children:eb[t.id]?"Confirming...":"I scanned this QR"})}),u?(0,s.jsx)(b,{terminalId:u,fallbackBody:t.body,onReplaceTerminalId:e=>{ey({...t,externalMessageId:"terminal:".concat(e)})}}):null,!u&&t.body?(0,s.jsx)(f,{content:t.body}):null,(0,s.jsxs)("div",{className:"mt-3 flex items-center justify-end gap-3 text-[11px] text-white/35",children:[(0,s.jsx)("span",{children:T(t.createdAt)}),i?(0,s.jsx)("span",{className:h?"font-semibold text-red-200":"",children:E(t)}):null]}),h&&(null==(a=t.outboundDelivery)?void 0:a.lastError)?(0,s.jsx)("p",{className:"mt-2 text-[11px] leading-5 text-red-100/75",children:t.outboundDelivery.lastError}):null,el===t.id&&(0,s.jsxs)("div",{className:"absolute right-2 top-2 flex items-center gap-1",children:[(0,s.jsx)("button",{type:"button",ref:eP,className:"flex h-8 w-8 items-center justify-center rounded-full bg-[#2e2f2f] text-white/60 transition hover:bg-[#3a3b3b] hover:text-white",onClick:()=>ed(e=>e===t.id?null:t.id),title:"More options",children:(0,s.jsx)(d.a95,{className:"w-5 h-5"})}),(0,s.jsx)(w,{isOpen:eo===t.id,onClose:()=>ed(null),message:t,onReply:()=>V(t),onDelete:()=>F(t.id),onForward:()=>{$(e=>e===t.id?null:t.id),et("")},isOutbound:i,triggerRef:eP})]}),X===t.id?(0,s.jsxs)("div",{className:"mt-3 flex flex-wrap gap-2 rounded-2xl bg-white/[0.04] p-3",children:[(0,s.jsxs)("select",{className:"min-w-[200px] flex-1 rounded-xl border border-white/10 bg-[#0b141a] px-3 py-2 text-sm text-white outline-none",value:ee,onChange:e=>et(e.target.value),children:[(0,s.jsx)("option",{value:"",children:"Select target chat"}),e3.map(e=>(0,s.jsx)("option",{value:e.id,children:e.contact.displayName},e.id))]}),(0,s.jsx)("button",{type:"button",className:"rounded-xl bg-brand-500 px-4 py-2 text-sm font-semibold text-[#10251a]",onClick:()=>tm(t.id),children:"Send"})]}):null]})},t.id)}}),(null==ej?void 0:ej.isTyping)&&(0,s.jsx)("div",{className:"flex justify-start",children:(0,s.jsx)("div",{className:"max-w-[72%] rounded-[18px] bg-[#2e2f2f] px-4 py-3 shadow-[0_16px_32px_rgba(0,0,0,0.18)]",children:(0,s.jsxs)("div",{className:"flex items-center gap-2 text-sm text-white/60 italic",children:[(0,s.jsxs)("span",{children:[ej.name," is thinking..."]}),(0,s.jsxs)("div",{className:"flex gap-1",children:[(0,s.jsx)("span",{className:"h-1 w-1 animate-bounce rounded-full bg-white/40 delay-0"}),(0,s.jsx)("span",{className:"h-1 w-1 animate-bounce rounded-full bg-white/40 delay-150"}),(0,s.jsx)("span",{className:"h-1 w-1 animate-bounce rounded-full bg-white/40 delay-300"})]})]})})}),(0,s.jsx)("div",{ref:eS})]})]}),(0,s.jsxs)("form",{className:"shrink-0 bg-[#161717] px-6 py-3",onSubmit:e6,children:[J?(0,s.jsxs)("div",{className:"mb-3 flex items-start justify-between rounded-2xl bg-[#2e2f2f] px-4 py-3",children:[(0,s.jsxs)("div",{className:"min-w-0",children:[(0,s.jsx)("p",{className:"text-xs uppercase tracking-[0.22em] text-brand-100",children:"Reply"}),(0,s.jsx)("p",{className:"mt-1 truncate text-sm text-white/55",children:P(J)})]}),(0,s.jsx)("button",{type:"button",className:"text-sm text-white/45 hover:text-white",onClick:()=>V(null),children:(0,s.jsx)(d.m6K,{className:"w-4 h-4"})})]}):null,eh.length>0&&(0,s.jsx)("div",{className:"mb-3 flex flex-wrap gap-2 rounded-2xl bg-white/[0.04] p-3",children:eh.map((e,t)=>{var a;return(0,s.jsxs)("div",{className:"relative",children:[e.preview&&e.type.startsWith("image/")?(0,s.jsx)("img",{src:e.preview,alt:e.name,className:"h-16 w-16 rounded-lg object-cover"}):e.preview&&e.type.startsWith("video/")?(0,s.jsx)("video",{src:e.preview,className:"h-16 w-16 rounded-lg object-cover"}):(0,s.jsx)("div",{className:"flex h-16 w-16 items-center justify-center rounded-lg bg-[#2e2f2f] text-sm font-medium text-white/60",children:(null==(a=e.name.split(".").pop())?void 0:a.toUpperCase())||"FILE"}),(0,s.jsx)("button",{type:"button",onClick:()=>{ep(e=>{let a=[...e],s=a[t];return s.preview&&URL.revokeObjectURL(s.preview),a.splice(t,1),a})},className:"absolute -right-2 -top-2 flex h-6 w-6 items-center justify-center rounded-full bg-red-500 text-xs text-white transition hover:bg-red-600",children:(0,s.jsx)(d.m6K,{className:"w-4 h-4"})})]},t)})}),(0,s.jsxs)("div",{className:"flex items-center gap-2 relative",children:[(0,s.jsxs)("label",{className:"flex h-10 w-10 shrink-0 cursor-pointer items-center justify-center rounded-full bg-[#2e2f2f] text-[24px] leading-none text-white/60 transition hover:bg-[#3a3b3b] hover:text-white",children:[(0,s.jsx)(d.jgn,{className:"w-5 h-5"}),(0,s.jsx)("input",{ref:eE,type:"file",className:"hidden",onChange:e7,multiple:!0,accept:"image/*,video/*,audio/*,.pdf,.doc,.docx,.xls,.xlsx,.txt"})]}),(0,s.jsx)("button",{type:"button",ref:eI,className:"flex h-10 w-10 shrink-0 items-center justify-center rounded-full bg-[#2e2f2f] text-[20px] transition hover:bg-[#3a3b3b]",onClick:()=>eu(!em),title:"Emoji",children:(0,s.jsx)(d.aUC,{className:"w-5 h-5"})}),em&&(0,s.jsx)("div",{ref:eR,className:"absolute bottom-full left-0 z-50",children:(0,s.jsx)(j,{isOpen:em,onClose:()=>eu(!1),onEmojiSelect:e=>{let t=eN.current;if(!t)return;let a=t.selectionStart||0,s=t.selectionEnd||0;H(K.slice(0,a)+e+K.slice(s)),setTimeout(()=>{t.focus(),t.setSelectionRange(a+e.length,a+e.length)},0),eu(!1)},triggerRef:eI})}),(0,s.jsx)("button",{type:"button",ref:eD,className:"flex h-10 w-10 shrink-0 items-center justify-center rounded-full transition ".concat(eO?"bg-brand-500 text-[#10251a] ring-2 ring-emerald-400/60 shadow-[0_0_0_8px_rgba(16,37,26,0.12)]":"bg-[#2e2f2f] text-white/60 hover:bg-[#3a3b3b] hover:text-white ring-1 ring-emerald-400/30 animate-pulse"),onClick:()=>e_(!eO),title:"Shortcuts",children:(0,s.jsx)(d.vSZ,{className:"w-5 h-5"})}),eO&&(0,s.jsxs)("div",{ref:eL,className:"absolute bottom-full left-12 z-50 mb-2 w-64 overflow-hidden rounded-2xl bg-[#2e2f2f] p-1 shadow-2xl ring-1 ring-white/10",children:[(0,s.jsx)("div",{className:"px-3 py-2 text-[11px] font-bold uppercase tracking-wider text-white/30",children:"AI Shortcuts"}),(0,s.jsx)("div",{className:"max-h-64 overflow-y-auto",children:e1.map((e,t)=>(0,s.jsxs)("button",{type:"button",className:"flex w-full items-center gap-3 px-3 py-2.5 text-left text-sm text-white/90 transition hover:bg-white/5 first:rounded-t-xl last:rounded-b-xl",onClick:()=>e2(e.message),children:[(0,s.jsx)("span",{className:"text-lg",children:e.icon}),(0,s.jsx)("span",{className:"flex-1 font-medium",children:e.label})]},t))})]}),(0,s.jsx)("button",{type:"button",ref:eM,className:"flex h-10 w-10 shrink-0 items-center justify-center rounded-full transition ".concat(eW?"bg-emerald-500 text-white ring-2 ring-emerald-300/70 shadow-[0_0_0_8px_rgba(23,163,70,0.18)]":"bg-orange-500 text-white hover:bg-orange-600 ring-1 ring-orange-300/30"),onClick:()=>eF(!eW),title:"AI Provider settings",children:(0,s.jsx)(d.BtR,{className:"w-5 h-5"})}),eW&&(0,s.jsxs)("div",{ref:eU,className:"absolute bottom-full left-20 z-50 mb-2 w-72 overflow-hidden rounded-2xl bg-[#2e2f2f] p-3 shadow-2xl ring-1 ring-white/10",children:[(0,s.jsx)("div",{className:"px-2 pb-2 text-[11px] font-bold uppercase tracking-wider text-white/30",children:"AI Provider & Model"}),ez?(0,s.jsx)("div",{className:"rounded-2xl bg-[#161717] px-4 py-4 text-center text-sm text-white/50",children:"Loading providers..."}):0===eq.length?(0,s.jsx)("div",{className:"rounded-2xl bg-[#161717] px-4 py-4 text-sm text-white/60",children:"No AI providers configured. Open settings to add one."}):(0,s.jsxs)("div",{className:"space-y-3",children:[(0,s.jsxs)("div",{children:[(0,s.jsx)("label",{className:"mb-1 block text-xs uppercase tracking-[0.22em] text-white/40",children:"Provider"}),(0,s.jsxs)("select",{className:"w-full rounded-[14px] bg-[#161717] px-3 py-2 text-sm text-white outline-none",value:eG,onChange:e=>{eZ(e.target.value),eJ(""),eX("")},children:[(0,s.jsx)("option",{value:"",children:"Select provider"}),eq.map(e=>(0,s.jsxs)("option",{value:e.id,children:[e.name," (",e.provider,")"]},e.id))]})]}),eG?(0,s.jsxs)("div",{children:[(0,s.jsx)("label",{className:"mb-1 block text-xs uppercase tracking-[0.22em] text-white/40",children:"Model"}),(null==(i=eH[eG])?void 0:i.length)?(0,s.jsxs)(s.Fragment,{children:[(0,s.jsxs)("select",{className:"w-full rounded-[14px] bg-[#161717] px-3 py-2 text-sm text-white outline-none",value:eY,onChange:e=>{eJ(e.target.value),eX("")},children:[(0,s.jsx)("option",{value:"",children:"Select model"}),eH[eG].map(e=>(0,s.jsx)("option",{value:e.id,children:e.name||e.id},e.id)),(0,s.jsx)("option",{value:"manual-model",children:"Manual model"})]}),"manual-model"===eY?(0,s.jsx)("input",{className:"mt-2 w-full rounded-[14px] bg-[#161717] px-3 py-2 text-sm text-white outline-none",placeholder:"Enter model id",value:eV,onChange:e=>tc(e.target.value)}):null]}):(0,s.jsx)("input",{className:"w-full rounded-[14px] bg-[#161717] px-3 py-2 text-sm text-white outline-none",placeholder:"Enter model id",value:eV,onChange:e=>tc(e.target.value)})]}):null,(0,s.jsx)("button",{type:"button",className:"w-full rounded-[14px] bg-brand-500 px-4 py-2 text-sm font-semibold text-[#10251a] transition hover:bg-brand-600 disabled:opacity-60",onClick:td,disabled:!eG||e$,children:e$?"Saving...":"Set AI provider"})]})]}),(0,s.jsx)("div",{className:"flex flex-1 items-center rounded-[22px] bg-[#2e2f2f] px-4 py-2",children:(0,s.jsx)("textarea",{ref:eN,rows:1,className:"min-h-[20px] w-full resize-none overflow-y-auto border-none bg-transparent px-1 py-0.5 text-sm leading-5 text-white outline-none placeholder:text-white/30 disabled:opacity-60",placeholder:"Type a message",value:K,onChange:e=>{H(e.target.value),W(!!e.target.value)},onKeyDown:e=>{"Enter"===e.key&&!e.shiftKey&&(e.preventDefault(),Q||Z||e4())},disabled:Q||Z})}),(0,s.jsx)("button",{type:"submit",className:"flex h-10 w-10 items-center justify-center rounded-full bg-brand-500 text-sm font-semibold leading-none text-[#10251a] transition hover:bg-brand-600 disabled:cursor-not-allowed disabled:opacity-60",disabled:Q||Z,children:Q||Z?(0,s.jsx)(A,{}):(0,s.jsx)(d.KpL,{className:"w-5 h-5"})})]})]}),ec&&(0,s.jsx)(y,{media:ec,onClose:()=>ex(null)})]}):(0,s.jsx)("div",{className:"flex flex-1 items-center justify-center bg-[#161717] px-8 text-center text-white/50",children:(0,s.jsxs)("div",{children:[(0,s.jsx)("p",{className:"text-lg font-medium text-white",children:"No chat selected"}),(0,s.jsx)("p",{className:"mt-3 max-w-md text-sm leading-7 text-white/45",children:"Start a new conversation from the contact selector to begin chatting."}),(0,s.jsx)("button",{type:"button",className:"mt-5 rounded-full bg-brand-500 px-5 py-3 text-sm font-semibold text-[#10251a]",onClick:U,children:"New chat"})]})})});var D=a(1826);let L="1.2.5";function W(e){let{src:t,label:a}=e;return t?(0,s.jsx)("img",{src:t,alt:a,className:"h-12 w-12 shrink-0 rounded-2xl object-cover"}):(0,s.jsx)("div",{className:"flex h-12 w-12 shrink-0 items-center justify-center rounded-2xl bg-[#2e2f2f] text-sm font-semibold text-white",children:String(a||"?").split(" ").map(e=>e[0]).join("").slice(0,2).toUpperCase()})}function F(e){let{chats:t,activeChatId:a,onSelectChat:i,currentUser:l,loading:r,query:o,onQueryChange:c,onTogglePin:m,onDeleteChat:u}=e,h=String(o||"").trim().toLowerCase(),[p,f]=(0,n.useState)(null),g=t.find(e=>e.contact&&"openwa:assistant"===e.contact.externalId);(0,n.useEffect)(()=>{function e(e){if(p)try{let t=e.target.closest('[data-menu-button="'.concat(p,'"]')),a=e.target.closest('[data-menu-id="'.concat(p,'"]'));t||a||f(null)}catch(e){f(null)}}function t(e){"Escape"===e.key&&f(null)}return document.addEventListener("mousedown",e),document.addEventListener("touchstart",e),document.addEventListener("keydown",t),()=>{document.removeEventListener("mousedown",e),document.removeEventListener("touchstart",e),document.removeEventListener("keydown",t)}},[p]);let b=t.filter(e=>e.contact&&"openwa:assistant"!==e.contact.externalId).filter(e=>!h||[e.contact.displayName,e.contact.lastMessagePreview,e.title].filter(Boolean).some(e=>e.toLowerCase().includes(h)));return(0,s.jsxs)("aside",{className:"flex h-full w-[360px] shrink-0 flex-col bg-[#161717]",children:[(0,s.jsxs)("div",{className:"px-5 py-5",children:[(0,s.jsx)("div",{className:"flex items-center justify-center",children:(0,s.jsxs)("div",{className:"flex h-12 items-center gap-2",children:[(0,s.jsx)(D.F,{variant:"long",alt:"Wanie",className:"h-full"}),L?(0,s.jsxs)("span",{className:"rounded-full bg-white/5 px-2 py-1 text-[11px] font-semibold text-white/45",children:["v",L]}):null]})}),(0,s.jsx)("div",{className:"mt-4 rounded-[22px] bg-[#2e2f2f] px-3 py-2",children:(0,s.jsx)("input",{className:"w-full border-none bg-transparent text-sm text-white outline-none placeholder:text-white/30",placeholder:"Search conversation",value:o,onChange:e=>c(e.target.value)})})]}),(0,s.jsxs)("div",{className:"flex-1 overflow-y-auto px-3 py-3",children:[r?(0,s.jsx)(S,{}):null,r||0!==b.length?null:(0,s.jsx)("p",{className:"px-3 py-4 text-sm leading-6 text-white/40",children:"No synced conversations yet. Connect your device to load chats."}),(0,s.jsxs)("div",{className:"space-y-2",children:[g?(0,s.jsxs)("div",{className:"relative",children:[(0,s.jsxs)("button",{type:"button",className:"flex w-full items-start gap-3 rounded-[16px] px-4 py-3 pr-12 text-left transition "+(g.id===a?"bg-[#2e2f2f]":"bg-transparent hover:bg-white/[0.05]"),onClick:()=>i(g.id),children:[(0,s.jsx)(W,{src:g.contact.avatarUrl,label:g.contact.displayName}),(0,s.jsx)("div",{className:"min-w-0 flex-1",children:(0,s.jsxs)("div",{className:"grid grid-cols-[minmax(0,1fr)_auto] items-start gap-x-3",children:[(0,s.jsx)("h3",{className:"min-w-0 truncate font-medium text-white",children:g.contact.displayName}),(0,s.jsx)("div",{className:"w-[62px] shrink-0 text-right",children:(0,s.jsx)("span",{className:"block truncate text-[11px] text-white/35",children:g.contact.lastMessageAt||g.updatedAt?new Intl.DateTimeFormat("en-US",{hour:"2-digit",minute:"2-digit"}).format(new Date(g.contact.lastMessageAt||g.updatedAt)):""})}),(0,s.jsx)("p",{className:"mt-1 min-w-0 truncate text-sm text-white/42",children:g.contact.persona?g.contact.persona:g.contact.lastMessagePreview||"No messages yet"}),(0,s.jsx)("div",{className:"mt-1 w-[62px] shrink-0"})]})})]}),(0,s.jsxs)("div",{className:"absolute right-2 top-3 flex items-start gap-1",children:[(0,s.jsx)("button",{type:"button",onClick:e=>{e.stopPropagation(),f(p===g.id?null:g.id)},"data-menu-button":g.id,className:"text-white/40 hover:text-white/70","aria-label":"Open chat menu",children:(0,s.jsx)(d.a95,{className:"w-5 h-5"})}),p===g.id?(0,s.jsxs)("div",{"data-menu-id":g.id,className:"absolute right-0 z-50 mt-2 w-40 rounded-md bg-[#0b141a] border border-white/6 p-1 shadow-lg",children:[(0,s.jsx)("button",{type:"button",className:"w-full text-left px-3 py-2 text-sm text-white hover:bg-white/[0.03]",onClick:e=>{e.stopPropagation(),"function"==typeof m&&m(g.id,!!g.pinnedAt),f(null)},children:g.pinnedAt?"Unpin":"Pin"}),(0,s.jsx)("button",{type:"button",className:"w-full text-left px-3 py-2 text-sm text-red-400 hover:bg-white/[0.03]",onClick:e=>{if(e.stopPropagation(),!confirm("Delete this conversation?"))return void f(null);"function"==typeof u&&u(g.id),f(null)},children:"Delete"})]}):null]})]},g.id):null,b.map(e=>{var t;return(0,s.jsxs)("div",{className:"relative",children:[(0,s.jsxs)("button",{type:"button",className:"flex w-full items-start gap-3 rounded-[16px] px-4 py-3 pr-12 text-left transition "+(e.id===a?"bg-[#2e2f2f]":"bg-transparent hover:bg-white/[0.05]"),onClick:()=>i(e.id),children:[(0,s.jsx)(W,{src:e.contact.avatarUrl,label:e.contact.displayName}),(0,s.jsx)("div",{className:"min-w-0 flex-1",children:(0,s.jsxs)("div",{className:"grid grid-cols-[minmax(0,1fr)_auto] items-start gap-x-3",children:[(0,s.jsxs)("div",{className:"flex min-w-0 items-center gap-2",children:[(0,s.jsx)("h3",{className:"min-w-0 truncate font-medium text-white",children:e.contact.displayName}),(0,s.jsx)(x.zd,{chat:e,compact:!0})]}),(0,s.jsx)("div",{className:"flex w-[76px] shrink-0 items-center justify-end gap-2",children:(0,s.jsx)("span",{className:"block min-w-0 truncate text-[11px] text-white/35",children:(t=e.contact.lastMessageAt||e.updatedAt)?new Intl.DateTimeFormat("en-US",{hour:"2-digit",minute:"2-digit"}).format(new Date(t)):""})}),(0,s.jsx)("p",{className:"mt-1 min-w-0 truncate text-sm text-white/42",children:e.contact.lastMessagePreview||"No messages yet"}),(0,s.jsx)("div",{className:"mt-1 flex w-[76px] shrink-0 justify-end",children:e.contact.unreadCount?(0,s.jsx)("span",{className:"flex h-5 min-w-5 items-center justify-center rounded-full bg-brand-500 px-1.5 text-[11px] font-bold text-[#10251a]",children:e.contact.unreadCount}):null})]})})]}),(0,s.jsxs)("div",{className:"absolute right-2 top-3 flex items-start gap-1",children:[(0,s.jsx)("button",{type:"button",onClick:t=>{t.stopPropagation(),f(p===e.id?null:e.id)},"data-menu-button":e.id,className:"text-white/40 hover:text-white/70","aria-label":"Open chat menu",children:(0,s.jsx)(d.a95,{className:"w-5 h-5"})}),p===e.id?(0,s.jsxs)("div",{"data-menu-id":e.id,className:"absolute right-0 z-50 mt-2 w-40 rounded-md bg-[#0b141a] border border-white/6 p-1 shadow-lg",children:[(0,s.jsx)("button",{type:"button",className:"w-full text-left px-3 py-2 text-sm text-white hover:bg-white/[0.03]",onClick:t=>{t.stopPropagation(),"function"==typeof m&&m(e.id,!!e.pinnedAt),f(null)},children:e.pinnedAt?"Unpin":"Pin"}),(0,s.jsx)("button",{type:"button",className:"w-full text-left px-3 py-2 text-sm text-red-400 hover:bg-white/[0.03]",onClick:t=>{if(t.stopPropagation(),!confirm("Delete this conversation?"))return void f(null);"function"==typeof u&&u(e.id),f(null)},children:"Delete"})]}):null]})]},e.id)})]})]})]})}function M(e){let{src:t,label:a}=e;return t?(0,s.jsx)("img",{src:t,alt:a,className:"h-11 w-11 rounded-2xl object-cover"}):(0,s.jsx)("div",{className:"flex h-11 w-11 items-center justify-center rounded-2xl bg-[#2e2f2f] text-sm font-semibold text-white",children:String(a||"?").split(" ").map(e=>e[0]).join("").slice(0,2).toUpperCase()})}function U(e){let{contacts:t,loading:a,open:n,query:i,onQueryChange:l,onStartChat:r,onClose:o,startingContactId:c}=e,x=String(i||"").trim().toLowerCase(),m=t.filter(e=>!x||[e.displayName,e.externalId,e.lastMessagePreview].filter(Boolean).some(e=>e.toLowerCase().includes(x)));return(0,s.jsx)("aside",{className:"flex h-full shrink-0 overflow-hidden bg-[#161717] transition-[width,opacity] duration-300 ".concat(n?"w-[320px] opacity-100":"w-0 opacity-0"),children:(0,s.jsxs)("div",{className:"flex h-full w-[320px] min-w-[320px] flex-col transition-transform duration-300 ".concat(n?"translate-x-0":"translate-x-8"),children:[(0,s.jsxs)("div",{className:"px-5 py-5",children:[(0,s.jsxs)("div",{className:"flex items-start justify-between gap-3",children:[(0,s.jsxs)("div",{children:[(0,s.jsx)("p",{className:"text-[11px] uppercase tracking-[0.24em] text-white/35",children:"Contacts"}),(0,s.jsx)("h2",{className:"mt-2 text-lg font-semibold text-white",children:"Start new message"})]}),(0,s.jsx)("button",{type:"button",className:"flex h-8 w-8 items-center justify-center rounded-full bg-[#2e2f2f] text-xs leading-none text-white/60 transition hover:bg-[#3a3b3b] hover:text-white",onClick:o,title:"Close contacts","aria-label":"Close contacts",children:(0,s.jsx)(d.m6K,{className:"w-4 h-4"})})]}),(0,s.jsx)("div",{className:"mt-4 rounded-[22px] bg-[#2e2f2f] px-3 py-2",children:(0,s.jsx)("input",{className:"w-full border-none bg-transparent text-sm text-white outline-none placeholder:text-white/30",placeholder:"Search contacts",value:i,onChange:e=>l(e.target.value)})})]}),(0,s.jsxs)("div",{className:"flex-1 overflow-y-auto px-3 py-3",children:[a?(0,s.jsx)("p",{className:"px-3 py-4 text-sm text-white/45",children:"Loading contacts..."}):null,a||0!==m.length?null:(0,s.jsx)("p",{className:"px-3 py-4 text-sm leading-6 text-white/40",children:"No synced contacts yet. Connect your device and wait for WhatsApp sync to complete."}),(0,s.jsx)("div",{className:"space-y-2",children:m.map(e=>(0,s.jsxs)("button",{type:"button",className:"flex w-full items-center gap-3 rounded-[16px] bg-transparent px-3 py-3 text-left transition hover:bg-white/[0.05]",onClick:()=>r(e.id),children:[(0,s.jsx)(M,{src:e.avatarUrl,label:e.displayName}),(0,s.jsxs)("div",{className:"min-w-0 flex-1",children:[(0,s.jsxs)("div",{className:"flex items-center justify-between gap-3",children:[(0,s.jsx)("h3",{className:"truncate text-sm font-medium text-white",children:e.displayName}),e.hasChat?(0,s.jsx)("span",{className:"rounded-full bg-[#144d37] px-2 py-0.5 text-[10px] font-semibold uppercase tracking-[0.12em] text-white",children:"Chat"}):null]}),(0,s.jsx)("p",{className:"mt-1 truncate text-xs text-white/40",children:e.lastMessagePreview?e.lastMessagePreview:e.externalId.replace(/@.+$/,"")})]}),(0,s.jsx)("div",{className:"shrink-0 text-xs text-white/35",children:c===e.id?"...":"+"})]},e.id))})]})]})})}function q(e){let{open:t,onClose:a}=e,i=(0,o.C)(e=>e.token),[l,d]=(0,n.useState)(!1),[c,x]=(0,n.useState)(!1),[m,u]=(0,n.useState)("");if((0,n.useEffect)(()=>{if(!t)return;let e=!0;return(async()=>{d(!0);try{let t=await (0,r.n)("/api/agent/tools",{token:i});if(!e)return;u(t.content||"")}catch(e){alert(e.message||"Failed to load tools file")}finally{e&&d(!1)}})(),()=>{e=!1}},[t,i]),!t)return null;let h=async()=>{if(!i)return alert("Not authenticated");x(!0);try{await (0,r.n)("/api/agent/tools",{method:"PUT",token:i,body:{action:"replace",content:m}}),alert("TOOLS.md replaced"),a()}catch(e){alert(e.message||"Failed to save")}finally{x(!1)}},p=async()=>{if(!i)return alert("Not authenticated");x(!0);try{await (0,r.n)("/api/agent/tools",{method:"PUT",token:i,body:{action:"append",content:m}}),alert("Appended to TOOLS.md"),a()}catch(e){alert(e.message||"Failed to append")}finally{x(!1)}};return(0,s.jsx)("div",{className:"fixed inset-0 z-50 flex items-center justify-center bg-black/70 px-6 py-8",children:(0,s.jsxs)("div",{className:"flex h-[80vh] w-full max-w-[980px] flex-col overflow-hidden rounded-[20px] bg-[#161717] p-4",children:[(0,s.jsxs)("div",{className:"flex items-center justify-between px-2",children:[(0,s.jsx)("h3",{className:"text-lg font-semibold text-white",children:"Edit TOOLS.md"}),(0,s.jsx)("div",{className:"flex gap-2",children:(0,s.jsx)("button",{className:"rounded-full bg-white/5 px-3 py-1 text-sm text-white/80",onClick:a,children:"Close"})})]}),(0,s.jsx)("div",{className:"mt-3 flex-1 overflow-hidden",children:l?(0,s.jsx)("div",{className:"flex h-full items-center justify-center text-white/50",children:"Loading..."}):(0,s.jsx)("textarea",{className:"h-full w-full resize-none rounded-lg bg-[#0f1111] p-3 text-sm text-white outline-none",value:m,onChange:e=>u(e.target.value)})}),(0,s.jsxs)("div",{className:"mt-3 flex justify-end gap-2",children:[(0,s.jsx)("button",{className:"rounded-2xl bg-white/5 px-4 py-2 text-sm text-white/80",onClick:p,disabled:c,children:c?"Saving...":"Append"}),(0,s.jsx)("button",{className:"rounded-2xl bg-brand-500 px-4 py-2 text-sm font-semibold text-[#10251a]",onClick:h,disabled:c,children:c?"Saving...":"Replace"})]})]})})}let B=function(e){var t,a;let{open:i,onClose:l}=e,d=(0,o.C)(e=>e.token),c=(0,o.C)(e=>e.socket),[x,m]=(0,n.useState)(!1),[u,h]=(0,n.useState)([]),[p,f]=(0,n.useState)(null),[g,b]=(0,n.useState)(null),[w,y]=(0,n.useState)(!1),[v,j]=(0,n.useState)(!1);(0,n.useEffect)(()=>{if(!i)return;let e=!0;return(async()=>{m(!0);try{let t=await (0,r.n)("/api/terminal/history",{token:d});if(!e)return;h(t.items||[])}catch(e){}finally{e&&m(!1)}})(),()=>e=!1},[i,d]),(0,n.useEffect)(()=>{if(!c)return;let e=e=>{h(t=>t.find(t=>t.id===e.id)?t.map(t=>t.id===e.id?{...t,status:e.status}:t):[e,...t]),p&&e.id===p&&(async()=>{try{let t=await (0,r.n)("/api/terminal/".concat(e.id),{token:d});b(t.item||null)}catch(e){}})()};return c.on("terminal_result",e),c.on("terminal_request",e),()=>{try{c.off("terminal_result",e),c.off("terminal_request",e)}catch(e){}}},[c,d,p]);let N=async e=>{f(e),y(!0);try{let t=await (0,r.n)("/api/terminal/".concat(e),{token:d});b(t.item||null)}catch(e){b(null)}finally{y(!1)}},k=async()=>{try{let e=await (0,r.n)("/api/terminal/history",{token:d});h(e.items||[])}catch(e){}},C=async()=>{if(p){j(!0);try{await (0,r.n)("/api/terminal/".concat(p,"/approve"),{method:"POST",token:d});let e=await (0,r.n)("/api/terminal/".concat(p),{token:d});b(e.item||null),await k()}catch(e){}finally{j(!1)}}},S=async()=>{if(p&&window.confirm("Are you sure you want to deny this terminal request?")){j(!0);try{await (0,r.n)("/api/terminal/".concat(p,"/deny"),{method:"POST",token:d});let e=await (0,r.n)("/api/terminal/".concat(p),{token:d});b(e.item||null),await k()}catch(e){}finally{j(!1)}}},A=async()=>{if(p){j(!0);try{await (0,r.n)("/api/terminal/".concat(p,"/rerun"),{method:"POST",token:d});let e=await (0,r.n)("/api/terminal/".concat(p),{token:d});b(e.item||null),await k()}catch(e){}finally{j(!1)}}};return i?(0,s.jsx)("div",{className:"fixed inset-0 z-50 flex items-center justify-center bg-black/70 px-6 py-8",children:(0,s.jsxs)("div",{className:"flex h-[80vh] w-full max-w-[1100px] flex-col overflow-hidden rounded-[20px] bg-[#161717] p-4",children:[(0,s.jsxs)("div",{className:"flex items-center justify-between px-2",children:[(0,s.jsx)("h3",{className:"text-lg font-semibold text-white",children:"Terminal Monitor"}),(0,s.jsx)("div",{className:"flex gap-2",children:(0,s.jsx)("button",{className:"rounded-full bg-white/5 px-3 py-1 text-sm text-white/80",onClick:l,children:"Close"})})]}),(0,s.jsxs)("div",{className:"mt-3 flex h-full gap-4",children:[(0,s.jsx)("div",{className:"w-1/3 overflow-auto rounded-lg bg-[#0f1111] p-3",children:x?(0,s.jsx)("div",{className:"text-white/50",children:"Loading..."}):0===u.length?(0,s.jsx)("div",{className:"text-white/50",children:"No terminal records yet."}):u.map(e=>(0,s.jsxs)("div",{className:"mb-2 cursor-pointer rounded-md p-2 ".concat(p===e.id?"bg-white/5":"hover:bg-white/3"),onClick:()=>N(e.id),children:[(0,s.jsxs)("div",{className:"flex items-center justify-between",children:[(0,s.jsx)("div",{className:"text-sm text-white/90 truncate",children:e.command}),(0,s.jsx)("div",{className:"text-xs text-white/60",children:new Date(e.requestedAt).toLocaleString()})]}),(0,s.jsxs)("div",{className:"mt-1 text-xs text-white/50",children:["Status: ",e.status]})]},e.id))}),(0,s.jsx)("div",{className:"flex-1 overflow-auto rounded-lg bg-[#0f1111] p-4",children:w?(0,s.jsx)("div",{className:"text-white/50",children:"Loading details..."}):g?(0,s.jsxs)("div",{children:[(0,s.jsxs)("div",{className:"mb-3 flex items-center justify-between",children:[(0,s.jsxs)("div",{children:[(0,s.jsx)("div",{className:"text-sm font-medium text-white",children:g.command}),(0,s.jsxs)("div",{className:"text-xs text-white/60",children:["Requested: ",new Date(g.requestedAt).toLocaleString()]}),(0,s.jsxs)("div",{className:"text-xs text-white/60",children:["Executed:"," ",g.executedAt?new Date(g.executedAt).toLocaleString():"N/A"]}),(0,s.jsxs)("div",{className:"text-xs text-white/60",children:["Status: ",g.status]})]}),(0,s.jsxs)("div",{className:"flex items-center gap-2",children:[(0,s.jsx)("button",{className:"rounded-full bg-yellow-600 px-3 py-1 text-sm text-white",onClick:A,disabled:v,children:"Rerun"}),"pending"===g.status&&(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)("button",{className:"rounded-full bg-emerald-600 px-3 py-1 text-sm text-white",onClick:C,disabled:v,children:"Approve"}),(0,s.jsx)("button",{className:"rounded-full bg-red-600 px-3 py-1 text-sm text-white",onClick:S,disabled:v,children:"Deny"})]})]})]}),(0,s.jsxs)("div",{className:"mb-4",children:[(0,s.jsx)("p",{className:"mb-2 text-sm text-white/70",children:"Stdout"}),(0,s.jsx)("pre",{className:"max-h-48 overflow-auto rounded bg-black/60 p-3 text-xs text-white/70",children:(null==(t=g.result)?void 0:t.stdout)||"(no stdout)"})]}),(0,s.jsxs)("div",{children:[(0,s.jsx)("p",{className:"mb-2 text-sm text-white/70",children:"Stderr"}),(0,s.jsx)("pre",{className:"max-h-48 overflow-auto rounded bg-black/60 p-3 text-xs text-white/70",children:(null==(a=g.result)?void 0:a.stderr)||"(no stderr)"})]})]}):(0,s.jsx)("div",{className:"text-white/50",children:"Select a record to view details."})})]})]})}):null};function z(e){let{mediaUrl:t,originalName:a,createdAt:i,size:l="h-56 w-56"}=e,[r,o]=(0,n.useState)(120),[c,x]=(0,n.useState)(!1);return((0,n.useEffect)(()=>{let e=new Date(i).getTime(),t=()=>{let t=120-Math.floor((Date.now()-e)/1e3);t<=0?(o(0),x(!0)):(o(t),x(!1))};t();let a=setInterval(t,1e3);return()=>clearInterval(a)},[i]),c)?(0,s.jsxs)("div",{className:"flex ".concat(l," flex-col items-center justify-center rounded-2xl bg-[#2e2f2f] p-6 text-center shadow-lg mx-auto"),children:[(0,s.jsx)(d.vSZ,{className:"mb-4 text-3xl text-white/30"}),(0,s.jsx)("p",{className:"mb-3 text-xs font-medium text-white/50",children:"QR Expired"}),(0,s.jsx)("button",{onClick:()=>window.location.reload(),className:"rounded-full bg-brand-500 px-4 py-1.5 text-[11px] font-bold text-[#10251a] shadow-md hover:bg-brand-600 transition",children:"Refresh"})]}):(0,s.jsxs)("div",{className:"relative ".concat(l," overflow-hidden rounded-2xl bg-white p-4 shadow-lg mx-auto"),children:[(0,s.jsx)("img",{src:t,alt:a,className:"h-full w-full object-contain"}),(0,s.jsx)("div",{className:"absolute bottom-2 right-2 flex items-center gap-1.5 rounded-full bg-black/70 px-2 py-0.5 text-[10px] font-bold text-white backdrop-blur-sm",children:(0,s.jsxs)("span",{className:r<=10?"text-red-400 animate-pulse":"",children:[r,"s"]})})]})}function K(e){let{status:t}=e,a={ready:"bg-brand-500/15 text-brand-100 ring-1 ring-brand-400/20",connecting:"bg-amber-500/15 text-amber-100 ring-1 ring-amber-400/20",disconnected:"bg-white/8 text-white/60 ring-1 ring-white/10",error:"bg-red-500/15 text-red-100 ring-1 ring-red-400/20"};return(0,s.jsx)("span",{className:"rounded-full px-2.5 py-1 text-[11px] font-medium capitalize tracking-[0.08em] ".concat(a[t]||a.disconnected),children:t})}function H(e){return!!((null==e?void 0:e.status)==="ready"||(null==e?void 0:e.phoneNumber)||(null==e?void 0:e.lastSeenAt)||(null==e?void 0:e.lastConnectedAt))}function Q(e){let{label:t}=e;return(0,s.jsx)("div",{className:"flex h-11 w-11 shrink-0 items-center justify-center rounded-2xl bg-[#2e2f2f] text-sm font-semibold text-white",children:String(t||"?").split(" ").map(e=>e[0]).join("").slice(0,2).toUpperCase()})}function G(e){return e?new Intl.DateTimeFormat("en-US",{day:"2-digit",month:"short",year:"numeric",hour:"2-digit",minute:"2-digit"}).format(new Date(e)):"Never"}let Z='app.post("/wanie-webhook", express.json(), async (req, res) => {\n const key = req.get("x-wanie-webhook-key") || req.get("x-openwa-webhook-key");\n if (key !== process.env.WANIE_WEBHOOK_KEY) {\n return res.status(401).json({ ok: false });\n }\n\n const { chat, message } = req.body;\n const isTelegram = String(chat.contact?.externalId || "").startsWith("tg:");\n const hasMedia = Boolean(message.mediaFile);\n\n console.log("Incoming Wanie message", {\n chatId: chat.id,\n channel: isTelegram ? "telegram" : "whatsapp",\n from: message.sender,\n text: message.body,\n type: message.type,\n media: hasMedia\n ? {\n id: message.mediaFile.id,\n mimeType: message.mediaFile.mimeType,\n originalName: message.mediaFile.originalName,\n size: message.mediaFile.size,\n }\n : null,\n });\n\n // Reply later with:\n // POST /api/chats/{chat.id}/messages/send\n // { "body": "Reply from external app", "type": "text" }\n\n res.json({ ok: true });\n});',Y='{\n "chat": {\n "id": "chat_123",\n "title": "Customer Name",\n "sessionId": "session_123",\n "transportType": "whatsapp",\n "contact": {\n "externalId": "6281234567890@c.us",\n "displayName": "Customer Name"\n },\n "updatedAt": "2026-05-15T08:30:00.000Z"\n },\n "message": {\n "id": "msg_123",\n "chatId": "chat_123",\n "sessionId": "session_123",\n "sender": "6281234567890@c.us",\n "receiver": "6289876543210@c.us",\n "body": "Ini foto area yang mau dibersihkan",\n "type": "image",\n "direction": "inbound",\n "createdAt": "2026-05-15T08:30:00.000Z",\n "mediaFileId": "media_123",\n "mediaFile": {\n "id": "media_123",\n "fileName": "photo.jpg",\n "originalName": "photo.jpg",\n "mimeType": "image/jpeg",\n "size": 248120,\n "relativePath": "media/photo.jpg",\n "createdAt": "2026-05-15T08:30:00.000Z"\n },\n "statuses": []\n }\n}';function J(e){var t;let{open:a,sessions:l,activeSessionId:d,onClose:c,onSelect:x,onConnect:m,onDisconnect:u,onClearSession:h,onDeleteSession:p,connectLoading:f,qrLoading:g,syncingWorkspace:b,sessionName:w,sessionPhone:y,sessionTransport:v,metaPhoneNumberId:j,metaBusinessAccountId:N,metaAccessToken:k,metaVerifyToken:C,metaAppSecret:S,onSessionNameChange:A,onSessionPhoneChange:T,onSessionTransportChange:E,onMetaPhoneNumberIdChange:P,onMetaBusinessAccountIdChange:I,onMetaAccessTokenChange:R,onMetaVerifyTokenChange:O,onMetaAppSecretChange:_,onCreateSession:L,apiKeys:W,apiKeysLoading:F,apiKeyName:M,apiKeySecret:U,onApiKeyNameChange:J,onCreateApiKey:V,onRevokeApiKey:X,revokingKeyId:$,webhookUrl:ee,webhookEnabled:et,webhookMethod:ea,webhookHeaders:es,webhookBodyTemplate:en,webhookApiKey:ei,onWebhookUrlChange:el,onWebhookEnabledChange:er,onWebhookMethodChange:eo,onWebhookHeadersChange:ed,onWebhookBodyTemplateChange:ec,onWebhookApiKeyChange:ex,onSaveWebhook:em,onDeleteWebhook:eu,webhookLoading:eh}=e,[ep,ef]=(0,n.useState)(!1),[eg,eb]=(0,n.useState)(null),ew=(0,o.C)(e=>e.token),ey=(0,o.C)(e=>e.socket),ev=(0,o.C)(e=>e.setActiveChat),ej=(0,o.C)(e=>e.upsertChat),eN=(0,o.C)(e=>e.terminalAutoApproveAll),ek=(0,o.C)(e=>e.setTerminalAutoApproveAll),eC=(0,o.C)(e=>e.defaultAiProviderId),eS=(0,o.C)(e=>e.defaultAiModel),eA=(0,o.C)(e=>e.setDefaultAiProvider),eT=(0,o.C)(e=>e.setDefaultAiModel);(0,o.C)(e=>e.logout),(0,i.useRouter)();let[eE,eP]=(0,n.useState)([]),[eI,eR]=(0,n.useState)(!1),[eO,e_]=(0,n.useState)(""),[eD,eL]=(0,n.useState)(""),[eW,eF]=(0,n.useState)(""),[eM,eU]=(0,n.useState)(""),[eq,eB]=(0,n.useState)(""),[ez,eK]=(0,n.useState)(""),[eH,eQ]=(0,n.useState)(!1),[eG,eZ]=(0,n.useState)(!1),[eY,eJ]=(0,n.useState)({}),[eV,eX]=(0,n.useState)(!0),[e$,e0]=(0,n.useState)(!1),[e1,e2]=(0,n.useState)(""),[e5,e3]=(0,n.useState)(!0),[e4,e6]=(0,n.useState)([]),[e8,e7]=(0,n.useState)(!1),[e9,te]=(0,n.useState)(!1),[tt,ta]=(0,n.useState)(null),[ts,tn]=(0,n.useState)([]),[ti,tl]=(0,n.useState)(!1),[tr,to]=(0,n.useState)(null),[td,tc]=(0,n.useState)(!1),[tx,tm]=(0,n.useState)(""),[tu,th]=(0,n.useState)(!1),[tp,tf]=(0,n.useState)(""),[tg,tb]=(0,n.useState)([]),[tw,ty]=(0,n.useState)(!1),[tv,tj]=(0,n.useState)(null),[tN,tk]=(0,n.useState)(null),[tC,tS]=(0,n.useState)(""),[tA,tT]=(0,n.useState)(!1),[tE,tP]=(0,n.useState)(!1),[tI,tR]=(0,n.useState)(null),[tO,t_]=(0,n.useState)({}),[tD,tL]=(0,n.useState)({}),[tW,tF]=(0,n.useState)({}),[tM,tU]=(0,n.useState)(null),[tq,tB]=(0,n.useState)(!1),[tz,tK]=(0,n.useState)(!1),[tH,tQ]=(0,n.useState)(!1),[tG,tZ]=(0,n.useState)("devices"),tY=eE.find(e=>e.id===eC),tJ=eS&&eY[eC]?(null==(t=eY[eC].find(e=>e.id===eS))?void 0:t.name)||eS:eS||null,tV=l.find(e=>e.id===d),tX=window.location.origin,t$="Integrate this application with Wanie as a messaging gateway.\n\nWanie base URL:\n".concat(tX,"\n\nRead these docs first:\n- ").concat(tX,"/docs/readme#webhooks\n- ").concat(tX,"/docs/json\n\nUse X-API-Key for authentication.\nConfigure a Wanie webhook so this application receives incoming customer messages.\nVerify the x-wanie-webhook-key header on every webhook request.\nStore chat.id from the webhook payload.\nReply to the customer with POST ").concat(tX,"/api/chats/{chatId}/messages/send.\nFor media replies, either pass a public mediaUrl or upload to POST ").concat(tX,"/api/media and then send the returned mediaFileId.\nDo not use Wanie internal CRM automation for this integration; set Wanie CRM automation to Off.\nTreat WhatsApp and non-admin Telegram customer messages the same way: receive them from the webhook and reply through the Wanie API.");function t0(e){if(null==e||""===e)return"";let t=Number(e);return Number.isFinite(t)?t<0?"0":t>2?"2":String(Math.round(10*t)/10):""}async function t1(e,t){await navigator.clipboard.writeText(t),eb(e),setTimeout(()=>eb(null),1500)}async function t2(){await navigator.clipboard.writeText(t$),eb("gateway-agent"),setTimeout(()=>eb(null),1500)}async function t5(){if(ew){tl(!0);try{let e=await (0,r.n)("/api/webhook/deliveries?limit=10",{token:ew});tn(e.deliveries||[])}catch(e){tn([])}finally{tl(!1)}}}async function t3(){ew&&e6((await (0,r.n)("/api/telegram/bots",{token:ew})).bots||[])}async function t4(e){if(ew&&e&&window.confirm("Delete this Telegram bot token and stop the running bot?")){ta(e);try{await (0,r.n)("/api/telegram/bots/".concat(e),{method:"DELETE",token:ew}),await t3()}catch(e){alert(e.message||"Failed to delete Telegram bot")}finally{ta(null)}}}async function t6(e){if(ew&&e){to(e);try{await (0,r.n)("/api/webhook/deliveries/".concat(e,"/retry"),{method:"POST",token:ew}),await t5()}catch(e){alert(e.message||"Failed to retry webhook delivery")}finally{to(null)}}}async function t8(){if(ew){tc(!0);try{let e=await (0,r.n)("/api/webhook/test",{method:"POST",token:ew});await t5(),(null==e?void 0:e.ok)||alert((null==e?void 0:e.error)||"Webhook test failed")}catch(e){alert(e.message||"Webhook test failed")}finally{tc(!1)}}}async function t7(){if(ew&&tx.trim()){th(!0),tf("");try{let e=await (0,r.n)("/api/webhook/generate-config",{method:"POST",token:ew,body:{prompt:tx}}),t=(null==e?void 0:e.config)||{};el(t.url||""),eo(t.method||"POST"),ed(t.headers&&Object.keys(t.headers).length?JSON.stringify(t.headers,null,2):""),ec(t.bodyTemplate||""),tf(t.notes||"")}catch(e){alert(e.message||"Failed to generate webhook config")}finally{th(!1)}}}async function t9(){if(ew){ty(!0);try{let e=await (0,r.n)("/api/outbound-deliveries?limit=20",{token:ew});tb(e.deliveries||[])}catch(e){tb([])}finally{ty(!1)}}}async function ae(e){if(ew&&e){tj(e);try{await (0,r.n)("/api/outbound-deliveries/".concat(e,"/retry"),{method:"POST",token:ew}),await t9()}catch(e){alert(e.message||"Failed to retry outbound delivery")}finally{tj(null)}}}async function at(e){if(ew&&e){tk(e);try{await (0,r.n)("/api/outbound-deliveries/".concat(e,"/cancel"),{method:"POST",token:ew}),await t9()}catch(e){alert(e.message||"Failed to cancel outbound delivery")}finally{tk(null)}}}(0,n.useEffect)(()=>{if(!a)return;let e=!0;return(async()=>{if(ew){eR(!0);try{let t=await (0,r.n)("/api/ai-providers",{token:ew});if(!e)return;eP(t.providers||[])}catch(e){}finally{e&&eR(!1)}}})(),()=>{e=!1}},[a,ew]),(0,n.useEffect)(()=>{if(!a)return;let e=!0;return(async()=>{try{let t=await (0,r.n)("/api/auth/config",{token:ew});if(!e)return;eX(!1!==t.allowRegistration)}catch(t){if(!e)return;eX(!0)}})(),()=>{e=!1}},[a,ew]),(0,n.useEffect)(()=>{if(!a||!ew)return;let e=!0;return(async()=>{e7(!0);try{var t,a;let[s,n]=await Promise.all([(0,r.n)("/api/telegram/config",{token:ew}),(0,r.n)("/api/telegram/bots",{token:ew})]);if(!e)return;let i=(null==(t=s.config)?void 0:t.adminTelegramIds)||[];e2(i.join("\n")),e3((null==(a=s.config)?void 0:a.aiReplyEnabled)!==!1),e6(n.bots||[])}catch(t){if(!e)return;e2(""),e3(!0),e6([])}finally{e&&e7(!1)}})(),()=>{e=!1}},[a,ew]),(0,n.useEffect)(()=>{a&&"webhooks"===tG&&ew&&t5()},[a,tG,ew]),(0,n.useEffect)(()=>{a&&"deliveries"===tG&&ew&&t9()},[a,tG,ew]),(0,n.useEffect)(()=>{if(!ey||!a)return;let e=function(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=e.delivery;(null==t?void 0:t.id)&&tn(e=>(e.some(e=>e.id===t.id)?e.map(e=>e.id===t.id?{...e,...t}:e):[t,...e]).slice(0,10))},t=function(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=e.delivery;(null==t?void 0:t.id)&&tb(e=>(e.some(e=>e.id===t.id)?e.map(e=>e.id===t.id?{...e,...t}:e):[t,...e]).slice(0,20))};return ey.on("webhook_delivery_update",e),ey.on("outbound_delivery_update",t),()=>{ey.off("webhook_delivery_update",e),ey.off("outbound_delivery_update",t)}},[ey,a]);let aa=async e=>{if(e.preventDefault(),!ew)return;let t=String(e1||"").split(/[,;\s]+/).map(e=>e.trim()).filter(Boolean);te(!0);try{var a,s;let e=await (0,r.n)("/api/telegram/config",{method:"POST",token:ew,body:{adminTelegramIds:t,aiReplyEnabled:e5}});e2(((null==(a=e.config)?void 0:a.adminTelegramIds)||t).join("\n")),e3((null==(s=e.config)?void 0:s.aiReplyEnabled)!==!1),alert("Telegram settings saved.")}catch(e){alert(e.message||"Failed to save Telegram admin IDs")}finally{te(!1)}},as=async e=>{if(e.preventDefault(),ew){eZ(!0);try{let e={};eW&&eW.trim()&&(e.apiKey=eW.trim()),eM&&eM.trim()&&(e.host=eM.trim()),eq&&eq.trim()&&(e.model=eq.trim()),""!==ez&&(e.temperature=Number(ez));let t=await (0,r.n)("/api/ai-providers",{method:"POST",token:ew,body:{provider:eD,name:eO,config:e}});eP(e=>[t.provider,...e]),e_(""),eL(""),eF(""),eU(""),eB(""),eK(""),eQ(!1)}catch(e){alert(e.message||"Failed to create provider")}finally{eZ(!1)}}},an=async e=>{if(e.preventDefault(),ew){if(!tC)return void alert("New password is required.");tT(!0);try{await (0,r.n)("/api/settings/reset-password",{method:"POST",token:ew,body:{password:tC}}),alert("Password reset successfully."),tS("")}catch(e){alert(e.message||"Failed to reset password")}finally{tT(!1)}}},ai=async()=>{if(ew&&"YES"===window.prompt("Type YES to confirm resetting all Wanie data. This action is permanent.")){tP(!0);try{await (0,r.n)("/api/settings/reset-all",{method:"POST",token:ew,body:{confirm:"YES"}}),window.location.reload()}catch(e){alert(e.message||"Failed to reset all data")}finally{tP(!1)}}},al=async e=>{if(ew&&confirm("Delete this provider?"))try{await (0,r.n)("/api/ai-providers/".concat(e),{method:"DELETE",token:ew}),eP(t=>t.filter(t=>t.id!==e))}catch(e){alert(e.message||"Failed to delete provider")}},ar=async e=>{if(ew){tR(e);try{let t=await (0,r.n)("/api/ai-providers/".concat(e,"/models"),{token:ew});eJ(a=>({...a,[e]:t.models||[]}))}catch(e){alert(e.message||"Failed to fetch models")}finally{tR(null)}}},ao=async(e,t)=>{var a;if(!ew||!(null==e?void 0:e.id))return;let s=Object.prototype.hasOwnProperty.call(tD,e.id),n=t0(void 0!==t?t:s?tD[e.id]:null==(a=e.config)?void 0:a.temperature),i={...e.config&&"object"==typeof e.config?e.config:{}};""===n?delete i.temperature:i.temperature=Number(n),tU(e.id);try{let t=await (0,r.n)("/api/ai-providers/".concat(e.id),{method:"PUT",token:ew,body:{config:i}});eP(a=>a.map(a=>a.id===e.id?t.provider:a)),tL(t=>({...t,[e.id]:n}))}catch(e){alert(e.message||"Failed to save provider temperature")}finally{tU(null)}},ad=async(e,t,a)=>{var s,n;if(!ew||!(null==e?void 0:e.id)||!t)return;let i=Object.prototype.hasOwnProperty.call(tW,e.id),l=t0(void 0!==a?a:i?tW[e.id]:null==(n=e.config)||null==(s=n.modelTemperatures)?void 0:s[t]),o={...e.config&&"object"==typeof e.config?e.config:{}},d=o.modelTemperatures&&"object"==typeof o.modelTemperatures?{...o.modelTemperatures}:{};""===l?delete d[t]:d[t]=Number(l),Object.keys(d).length?o.modelTemperatures=d:delete o.modelTemperatures,tU(e.id);try{let t=await (0,r.n)("/api/ai-providers/".concat(e.id),{method:"PUT",token:ew,body:{config:o}});eP(a=>a.map(a=>a.id===e.id?t.provider:a)),tF(t=>({...t,[e.id]:l}))}catch(e){alert(e.message||"Failed to save model temperature")}finally{tU(null)}};return a?(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)("div",{className:"fixed inset-0 z-50 flex items-center justify-center bg-black/70 px-6 py-8 backdrop-blur-sm",children:(0,s.jsxs)("div",{className:"flex h-full max-h-[840px] w-full max-w-[1080px] flex-col overflow-hidden rounded-[32px] bg-[#161717] shadow-[0_40px_120px_rgba(0,0,0,0.5)]",children:[(0,s.jsxs)("div",{className:"flex items-center justify-between px-6 py-5",children:[(0,s.jsxs)("div",{className:"flex items-center gap-4",children:[(0,s.jsx)("div",{className:"flex h-12 w-12 items-center justify-center rounded-2xl bg-white p-2 shadow-[0_16px_40px_rgba(0,0,0,0.18)]",children:(0,s.jsx)(D.F,{variant:"square",alt:"Wanie",className:"h-full w-full rounded-xl"})}),(0,s.jsxs)("div",{children:[(0,s.jsx)("p",{className:"text-[11px] uppercase tracking-[0.26em] text-white/35",children:"Settings"}),(0,s.jsx)("h2",{className:"mt-2 text-xl font-semibold text-white",children:"Wanie Devices"}),(0,s.jsxs)("div",{className:"mt-1 text-sm text-white/60",children:[(0,s.jsx)("span",{className:"font-medium text-white/85",children:"AI:"})," ",tY?(0,s.jsxs)("span",{children:[tY.name," (",tY.provider,")",tJ?" — ".concat(tJ):""]}):(0,s.jsx)("span",{children:"None active"})]}),b?(0,s.jsx)("div",{className:"mt-3 rounded-2xl bg-[#22302a] px-4 py-3 text-sm text-emerald-200 ring-1 ring-emerald-400/15",children:"Syncing WhatsApp chats and contacts..."}):null]})]}),(0,s.jsxs)("div",{className:"flex gap-2",children:[(0,s.jsx)("button",{type:"button",className:"rounded-2xl bg-[#2e2f2f] px-3 py-1 text-sm text-white/70 transition hover:bg-[#3a3b3b] hover:text-white",onClick:async()=>{if(ew&&confirm("Start a new Assistant conversation?")){tQ(!0);try{let e=(await (0,r.n)("/api/assistant/sessions",{method:"POST",token:ew,body:{}})).chat;e&&e.id&&(ej(e),ev(e.id),c())}catch(e){alert(e.message||"Failed to create assistant session")}finally{tQ(!1)}}},disabled:tH,children:tH?"Creating...":"New Assistant"}),(0,s.jsx)("button",{type:"button",className:"rounded-2xl bg-[#2e2f2f] px-3 py-1 text-sm text-white/70 transition hover:bg-[#3a3b3b] hover:text-white",onClick:()=>tB(!0),children:"Edit Tools"}),(0,s.jsx)("button",{type:"button",className:"rounded-2xl bg-[#2e2f2f] px-3 py-1 text-sm text-white/70 transition hover:bg-[#3a3b3b] hover:text-white",onClick:()=>tK(!0),children:"Terminal Monitor"}),(0,s.jsx)("button",{type:"button",className:"rounded-full bg-[#2e2f2f] px-4 py-2 text-sm text-white/70 transition hover:bg-[#3a3b3b] hover:text-white",onClick:c,children:"Close"})]})]}),(0,s.jsxs)("div",{className:"grid min-h-0 flex-1 gap-0 md:grid-cols-[1.2fr_0.8fr]",children:[(0,s.jsx)("div",{className:"min-h-0 px-5 py-5",children:(0,s.jsx)("div",{className:"h-full overflow-y-auto pr-1",children:(0,s.jsx)("div",{className:"space-y-3",children:l.map(e=>{var t;return(0,s.jsxs)("button",{type:"button",className:"w-full rounded-[18px] px-4 py-4 text-left transition ".concat(e.id===d?"bg-[#2e2f2f]":"bg-transparent hover:bg-white/[0.04]"),onClick:()=>x(e.id),children:[(0,s.jsxs)("div",{className:"flex items-start gap-3",children:[(0,s.jsx)(Q,{label:e.name}),(0,s.jsxs)("div",{className:"min-w-0 flex-1",children:[(0,s.jsxs)("div",{className:"flex items-center justify-between gap-3",children:[(0,s.jsx)("h3",{className:"truncate font-medium text-white",children:e.name}),(0,s.jsx)(K,{status:e.status})]}),(0,s.jsx)("p",{className:"mt-1 text-sm text-white/45",children:e.phoneNumber||"Waiting for WhatsApp pairing"}),(0,s.jsxs)("p",{className:"mt-2 text-xs uppercase tracking-[0.16em] text-white/30",children:["Transport: ","mock"===(t=e.transportType)?"Mock":"whatsapp_cloud"===t?"WhatsApp Official API":"WhatsApp Web"]}),(0,s.jsxs)("div",{className:"mt-2 grid gap-1 text-xs text-white/35",children:[(0,s.jsxs)("p",{children:["Health:"," ",G(e.lastHealthCheckAt)]}),(0,s.jsxs)("p",{children:["Last seen: ",G(e.lastSeenAt)]}),e.reconnectAttempts?(0,s.jsxs)("p",{children:["Reconnect attempts:"," ",e.reconnectAttempts]}):null]}),e.lastError?(0,s.jsx)("p",{className:"mt-3 rounded-2xl bg-red-500/10 px-3 py-2 text-sm text-red-100",children:e.lastError}):null]})]}),(0,s.jsxs)("div",{className:"mt-4 flex gap-2 flex-wrap",children:[(0,s.jsx)("button",{type:"button",className:H(e)?"rounded-2xl bg-sky-500/15 px-4 py-2 text-sm font-semibold text-sky-100 ring-1 ring-sky-400/25 transition hover:bg-sky-500/25 disabled:opacity-60":"rounded-2xl bg-brand-500 px-4 py-2 text-sm font-semibold text-[#10251a] transition hover:bg-brand-600 disabled:opacity-60",disabled:!!f&&f===e.id||"whatsapp_cloud"===e.transportType,onClick:t=>{t.stopPropagation(),m(e.id)},children:"whatsapp_cloud"===e.transportType?"Configured":f===e.id?H(e)?"Reconnecting...":"Connecting...":H(e)?"Reconnect":"Connect"}),(0,s.jsx)("button",{type:"button",className:"rounded-2xl bg-[#2e2f2f] px-4 py-2 text-sm text-white/75 disabled:opacity-60",disabled:!!f&&f===e.id,onClick:t=>{t.stopPropagation(),u(e.id)},children:f===e.id?"Disconnecting...":"Disconnect"}),(0,s.jsx)("button",{type:"button",className:"rounded-2xl bg-yellow-700 px-4 py-2 text-sm text-white/90",onClick:t=>{t.stopPropagation(),h(e.id)},children:"Clear Session"}),(0,s.jsx)("button",{type:"button",className:"rounded-2xl bg-red-700 px-4 py-2 text-sm text-white/90",onClick:t=>{t.stopPropagation(),p(e.id)},children:"Delete Device"})]})]},e.id)})})})}),(0,s.jsxs)("div",{className:"min-h-0 overflow-y-auto px-6 py-5",children:[(0,s.jsxs)("div",{className:"mb-4 flex flex-wrap gap-2",children:[(0,s.jsx)("button",{type:"button",className:"rounded-2xl px-4 py-2 text-sm ".concat("devices"===tG?"bg-white/5 text-white":"bg-transparent text-white/60 hover:bg-white/[0.04]"),onClick:()=>tZ("devices"),children:"Devices"}),(0,s.jsx)("button",{type:"button",className:"rounded-2xl px-4 py-2 text-sm ".concat("api"===tG?"bg-white/5 text-white":"bg-transparent text-white/60 hover:bg-white/[0.04]"),onClick:()=>tZ("api"),children:"API Access"}),(0,s.jsx)("button",{type:"button",className:"rounded-2xl px-4 py-2 text-sm ".concat("webhooks"===tG?"bg-white/5 text-white":"bg-transparent text-white/60 hover:bg-white/[0.04]"),onClick:()=>tZ("webhooks"),children:"Webhooks"}),(0,s.jsx)("button",{type:"button",className:"rounded-2xl px-4 py-2 text-sm ".concat("deliveries"===tG?"bg-white/5 text-white":"bg-transparent text-white/60 hover:bg-white/[0.04]"),onClick:()=>tZ("deliveries"),children:"Deliveries"}),(0,s.jsx)("button",{type:"button",className:"rounded-2xl px-4 py-2 text-sm ".concat("telegram"===tG?"bg-white/5 text-white":"bg-transparent text-white/60 hover:bg-white/[0.04]"),onClick:()=>tZ("telegram"),children:"Telegram"}),(0,s.jsx)("button",{type:"button",className:"rounded-2xl px-4 py-2 text-sm ".concat("ai"===tG?"bg-white/5 text-white":"bg-transparent text-white/60 hover:bg-white/[0.04]"),onClick:()=>tZ("ai"),children:"AI Providers"}),(0,s.jsx)("button",{type:"button",className:"rounded-2xl px-4 py-2 text-sm ".concat("advanced"===tG?"bg-white/5 text-white":"bg-transparent text-white/60 hover:bg-white/[0.04]"),onClick:()=>tZ("advanced"),children:"Advanced"})]}),"devices"===tG&&(0,s.jsxs)(s.Fragment,{children:[(0,s.jsxs)("div",{className:"rounded-[28px] bg-[#161717] p-4",children:[(0,s.jsx)("p",{className:"text-[11px] uppercase tracking-[0.24em] text-white/35",children:"Pairing QR"}),!g||(null==tV?void 0:tV.qrCode)?(null==tV?void 0:tV.qrCode)?(0,s.jsx)("div",{className:"mt-4",children:(0,s.jsx)(z,{mediaUrl:tV.qrCode,originalName:"WhatsApp QR",createdAt:tV.updatedAt||new Date().toISOString(),size:"h-80 w-80"})}):(0,s.jsx)("div",{className:"mt-4 rounded-[24px] bg-[#2e2f2f] px-4 py-16 text-center text-sm leading-6 text-white/40",children:"QR code for pairing will appear here when session is connecting."}):(0,s.jsxs)("div",{className:"mt-4 rounded-[24px] bg-[#2e2f2f] px-4 py-16 text-center text-sm leading-6 text-white/40",children:[(0,s.jsx)("div",{className:"mx-auto mb-4 flex h-12 w-12 items-center justify-center rounded-full border border-white/10",children:(0,s.jsx)("div",{className:"h-5 w-5 animate-spin rounded-full border-2 border-white/20 border-t-white"})}),"Waiting for your QR code..."]})]}),(0,s.jsxs)("form",{className:"mt-5 space-y-3 rounded-[28px] bg-[#161717] p-4",onSubmit:L,children:[(0,s.jsxs)("div",{children:[(0,s.jsx)("p",{className:"mb-2 text-[11px] uppercase tracking-[0.24em] text-white/35",children:"Add device"}),(0,s.jsx)("input",{className:"w-full rounded-[22px] bg-[#2e2f2f] px-4 py-3 text-sm text-white outline-none placeholder:text-white/30",placeholder:"Session name, e.g. Sales Team",value:w,onChange:e=>A(e.target.value),required:!0})]}),(0,s.jsxs)("select",{className:"w-full rounded-[22px] bg-[#2e2f2f] px-4 py-3 text-sm text-white outline-none",value:v||"wwebjs",onChange:e=>E(e.target.value),children:[(0,s.jsx)("option",{value:"wwebjs",children:"WhatsApp Web QR"}),(0,s.jsx)("option",{value:"whatsapp_cloud",children:"WhatsApp Official API"})]}),(0,s.jsx)("input",{className:"w-full rounded-[22px] bg-[#2e2f2f] px-4 py-3 text-sm text-white outline-none placeholder:text-white/30",placeholder:"whatsapp_cloud"===v?"Display WhatsApp number, e.g. 62812...":"WhatsApp number (optional)",value:y,onChange:e=>T(e.target.value)}),"whatsapp_cloud"===v?(0,s.jsxs)("div",{className:"space-y-3 rounded-[22px] bg-[#2e2f2f] p-4",children:[(0,s.jsxs)("p",{className:"text-xs leading-5 text-white/45",children:["Configure this URL in Meta webhook settings:",(0,s.jsxs)("span",{className:"mt-1 block break-all font-mono text-white/70",children:[tX,"/api/whatsapp/meta/webhook"]})]}),(0,s.jsx)("input",{className:"w-full rounded-[18px] bg-[#111b21] px-4 py-3 text-sm text-white outline-none placeholder:text-white/30",placeholder:"Phone Number ID",value:j,onChange:e=>P(e.target.value),required:!0}),(0,s.jsx)("input",{className:"w-full rounded-[18px] bg-[#111b21] px-4 py-3 text-sm text-white outline-none placeholder:text-white/30",placeholder:"WABA ID / Business Account ID (optional)",value:N,onChange:e=>I(e.target.value)}),(0,s.jsx)("input",{className:"w-full rounded-[18px] bg-[#111b21] px-4 py-3 text-sm text-white outline-none placeholder:text-white/30",placeholder:"Permanent access token",value:k,onChange:e=>R(e.target.value),required:!0}),(0,s.jsx)("input",{className:"w-full rounded-[18px] bg-[#111b21] px-4 py-3 text-sm text-white outline-none placeholder:text-white/30",placeholder:"Webhook verify token",value:C,onChange:e=>O(e.target.value),required:!0}),(0,s.jsx)("input",{className:"w-full rounded-[18px] bg-[#111b21] px-4 py-3 text-sm text-white outline-none placeholder:text-white/30",placeholder:"App secret for signature validation (optional)",value:S,onChange:e=>_(e.target.value)})]}):null,(0,s.jsx)("button",{type:"submit",className:"w-full rounded-2xl bg-brand-500 px-4 py-3 text-sm font-semibold text-[#10251a]",children:"whatsapp_cloud"===v?"Add Official API Session":"Add WhatsApp Session"})]})]}),"api"===tG&&(0,s.jsxs)("div",{className:"rounded-[28px] bg-[#161717] p-4",children:[(0,s.jsx)("div",{className:"flex items-start justify-between gap-4",children:(0,s.jsxs)("div",{children:[(0,s.jsx)("p",{className:"text-[11px] uppercase tracking-[0.24em] text-white/35",children:"API Access"}),(0,s.jsx)("h3",{className:"mt-2 text-base font-semibold text-white",children:"Generate API key"}),(0,s.jsx)("p",{className:"mt-2 text-sm leading-6 text-white/45",children:"Use with external agents via `X-API-Key` header or `Authorization: Bearer <api-key>`."}),(0,s.jsxs)("p",{className:"mt-3 text-sm leading-6 text-white/45",children:["Webhooks: forward incoming messages to your endpoint. See the"," ",(0,s.jsx)("a",{href:"/docs/readme#webhooks",target:"_blank",rel:"noreferrer",className:"ml-1 font-medium text-brand-300 underline",children:"webhook documentation"})," ","for payload details and agent integration."]})]})}),(0,s.jsxs)("div",{className:"mt-4 rounded-[22px] bg-[#2e2f2f] p-4",children:[(0,s.jsxs)("div",{className:"flex flex-wrap items-center justify-between gap-2",children:[(0,s.jsxs)("div",{children:[(0,s.jsx)("h4",{className:"text-sm font-semibold text-white",children:"AI agent gateway prompt"}),(0,s.jsx)("p",{className:"mt-1 text-xs leading-5 text-white/45",children:"Copy this into an external AI agent so it can connect to Wanie as a WhatsApp and Telegram gateway."})]}),(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/5 px-3 py-1.5 text-xs font-medium text-white/75 transition hover:bg-white/10",onClick:t2,children:"gateway-agent"===eg?"Copied":"Copy prompt"})]}),(0,s.jsx)("pre",{className:"mt-3 max-h-56 overflow-auto rounded-[18px] bg-[#0f1010] p-4 text-xs leading-5 text-white/70",children:(0,s.jsx)("code",{children:t$})})]}),U?(0,s.jsxs)("div",{className:"mt-4 rounded-[22px] bg-[#2e2f2f] p-4",children:[(0,s.jsx)("p",{className:"text-[11px] uppercase tracking-[0.22em] text-brand-200/80",children:"Shown once"}),(0,s.jsx)("p",{className:"mt-2 break-all font-mono text-sm text-white",children:U}),(0,s.jsx)("button",{type:"button",className:"mt-3 rounded-full bg-brand-500 px-4 py-2 text-sm font-semibold text-[#10251a]",onClick:async()=>{await navigator.clipboard.writeText(U),ef(!0),setTimeout(()=>ef(!1),1500)},children:ep?"Copied":"Copy API key"})]}):null,(0,s.jsxs)("form",{className:"mt-4 flex gap-2",onSubmit:V,children:[(0,s.jsx)("input",{className:"w-full rounded-[22px] bg-[#2e2f2f] px-4 py-3 text-sm text-white outline-none placeholder:text-white/30",placeholder:"Key name, e.g. OpenClaw Agent",value:M,onChange:e=>J(e.target.value),required:!0}),(0,s.jsx)("button",{type:"submit",className:"shrink-0 rounded-[22px] bg-brand-500 px-4 py-3 text-sm font-semibold text-[#10251a]",disabled:F,children:F?"Generating...":"Generate"})]}),(0,s.jsxs)("div",{className:"mt-4 max-h-[260px] space-y-3 overflow-y-auto pr-1",children:[F?(0,s.jsx)("div",{className:"rounded-[22px] bg-[#2e2f2f] px-4 py-6 text-sm text-white/45",children:"Loading API keys..."}):null,F||W.length?null:(0,s.jsx)("div",{className:"rounded-[22px] bg-[#2e2f2f] px-4 py-6 text-sm leading-6 text-white/45",children:"No API keys yet. Create one for OpenAPI client, AI agents, or external integrations."}),W.map(e=>(0,s.jsxs)("div",{className:"rounded-[22px] bg-[#2e2f2f] px-4 py-4",children:[(0,s.jsxs)("div",{className:"flex items-start justify-between gap-3",children:[(0,s.jsxs)("div",{className:"min-w-0",children:[(0,s.jsx)("h4",{className:"truncate text-sm font-semibold text-white",children:e.name}),(0,s.jsx)("p",{className:"mt-1 font-mono text-xs text-white/55",children:e.maskedKey})]}),(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/5 px-3 py-1.5 text-xs font-medium text-red-200 transition hover:bg-red-500/15",onClick:()=>X(e.id),disabled:F||$===e.id,children:$===e.id?"Revoking...":"Revoke"})]}),(0,s.jsxs)("div",{className:"mt-3 grid gap-2 text-xs text-white/40",children:[(0,s.jsxs)("p",{children:["Created: ",G(e.createdAt)]}),(0,s.jsxs)("p",{children:["Last used: ",G(e.lastUsedAt)]})]})]},e.id))]})]}),"webhooks"===tG&&(0,s.jsxs)("div",{className:"rounded-[28px] bg-[#161717] p-4",children:[(0,s.jsx)("p",{className:"text-[11px] uppercase tracking-[0.24em] text-white/35",children:"Webhooks"}),(0,s.jsx)("h3",{className:"mt-2 text-base font-semibold text-white",children:"Incoming message webhook"}),(0,s.jsx)("p",{className:"mt-2 text-sm leading-6 text-white/45",children:"Forward incoming messages to an external application using a configurable method, headers, and body template. Use custom headers for Bearer tokens, API keys, or any gateway-specific authentication."}),(0,s.jsxs)("div",{className:"mt-4 rounded-[22px] bg-[#2e2f2f] p-4",children:[(0,s.jsxs)("div",{className:"flex flex-wrap items-center justify-between gap-2",children:[(0,s.jsxs)("div",{children:[(0,s.jsx)("h4",{className:"text-sm font-semibold text-white",children:"AI agent gateway prompt"}),(0,s.jsx)("p",{className:"mt-1 text-xs leading-5 text-white/45",children:"Paste this into the external app agent that will receive webhooks and send replies through Wanie."})]}),(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/5 px-3 py-1.5 text-xs font-medium text-white/75 transition hover:bg-white/10",onClick:t2,children:"gateway-agent"===eg?"Copied":"Copy prompt"})]}),(0,s.jsx)("pre",{className:"mt-3 max-h-56 overflow-auto rounded-[18px] bg-[#0f1010] p-4 text-xs leading-5 text-white/70",children:(0,s.jsx)("code",{children:t$})})]}),(0,s.jsxs)("form",{className:"mt-4 space-y-3",onSubmit:e=>{e.preventDefault(),em()},children:[(0,s.jsxs)("div",{className:"rounded-[22px] bg-[#2e2f2f] p-4",children:[(0,s.jsxs)("div",{className:"flex flex-wrap items-center justify-between gap-2",children:[(0,s.jsxs)("div",{children:[(0,s.jsx)("h4",{className:"text-sm font-semibold text-white",children:"Generate webhook config"}),(0,s.jsx)("p",{className:"mt-1 text-xs leading-5 text-white/45",children:"Paste an external app webhook example or API docs. The AI will fill the request fields below."})]}),(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/10 px-3 py-1.5 text-xs font-medium text-white/80 transition hover:bg-white/15 disabled:opacity-50",onClick:t7,disabled:tu||!tx.trim(),children:tu?"Generating...":"Generate"})]}),(0,s.jsx)("textarea",{className:"mt-3 min-h-32 w-full resize-y rounded-[18px] bg-[#0f1010] px-4 py-3 font-mono text-xs leading-5 text-white outline-none placeholder:text-white/30",placeholder:'Example:\nPOST https://crm.example.com/api/wanie/messages\nAuthorization: Bearer <token>\nBody: { "customerId": "{{chat.contact.externalId}}", "message": "{{message.body}}" }',value:tx,onChange:e=>tm(e.target.value)}),tp?(0,s.jsx)("p",{className:"mt-2 text-xs leading-5 text-brand-200",children:tp}):null]}),(0,s.jsxs)("label",{className:"flex items-center justify-between gap-3 rounded-[22px] bg-[#2e2f2f] px-4 py-3",children:[(0,s.jsxs)("span",{children:[(0,s.jsx)("span",{className:"block text-sm font-semibold text-white",children:"Webhook delivery"}),(0,s.jsx)("span",{className:"mt-1 block text-xs leading-5 text-white/45",children:"Turn off to keep the saved config but stop sending incoming WhatsApp and Telegram events to the external app."})]}),(0,s.jsx)("input",{type:"checkbox",className:"h-5 w-5 accent-brand-500",checked:!1!==et,onChange:e=>er(e.target.checked)})]}),(0,s.jsxs)("label",{className:"block",children:[(0,s.jsx)("span",{className:"mb-1 block text-xs font-medium text-white/45",children:"Request URL"}),(0,s.jsx)("input",{className:"w-full rounded-[22px] bg-[#2e2f2f] px-4 py-3 text-sm text-white outline-none placeholder:text-white/30",placeholder:"https://example.com/wanie-webhook",value:ee||"",onChange:e=>el(e.target.value)})]}),(0,s.jsxs)("div",{className:"grid gap-3 md:grid-cols-[160px_1fr]",children:[(0,s.jsxs)("label",{className:"block",children:[(0,s.jsx)("span",{className:"mb-1 block text-xs font-medium text-white/45",children:"Method"}),(0,s.jsxs)("select",{className:"w-full rounded-[22px] bg-[#2e2f2f] px-4 py-3 text-sm text-white outline-none",value:ea||"POST",onChange:e=>eo(e.target.value),children:[(0,s.jsx)("option",{value:"POST",children:"POST"}),(0,s.jsx)("option",{value:"PUT",children:"PUT"}),(0,s.jsx)("option",{value:"PATCH",children:"PATCH"}),(0,s.jsx)("option",{value:"DELETE",children:"DELETE"}),(0,s.jsx)("option",{value:"GET",children:"GET"})]})]}),(0,s.jsxs)("label",{className:"block",children:[(0,s.jsx)("span",{className:"mb-1 block text-xs font-medium text-white/45",children:"Legacy webhook key header"}),(0,s.jsx)("input",{className:"w-full rounded-[22px] bg-[#2e2f2f] px-4 py-3 text-sm text-white outline-none placeholder:text-white/30",placeholder:"Sent as x-wanie-webhook-key and legacy x-openwa-webhook-key unless custom headers override them",value:ei||"",onChange:e=>ex(e.target.value)})]})]}),(0,s.jsxs)("label",{className:"block",children:[(0,s.jsx)("span",{className:"mb-1 block text-xs font-medium text-white/45",children:"Headers JSON"}),(0,s.jsx)("textarea",{className:"min-h-32 w-full resize-y rounded-[22px] bg-[#2e2f2f] px-4 py-3 font-mono text-xs leading-5 text-white outline-none placeholder:text-white/30",placeholder:'{\n "Authorization": "Bearer YOUR_TOKEN",\n "x-api-key": "YOUR_API_KEY"\n}',value:es||"",onChange:e=>ed(e.target.value)})]}),(0,s.jsxs)("label",{className:"block",children:[(0,s.jsx)("span",{className:"mb-1 block text-xs font-medium text-white/45",children:"Body template"}),(0,s.jsx)("textarea",{className:"min-h-40 w-full resize-y rounded-[22px] bg-[#2e2f2f] px-4 py-3 font-mono text-xs leading-5 text-white outline-none placeholder:text-white/30",placeholder:"Use placeholders like {{payload}}, {{chat.id}}, {{message.body}}, {{message.mediaFile.url}}.",value:en||"",onChange:e=>ec(e.target.value)})]}),(0,s.jsxs)("div",{className:"flex gap-2",children:[(0,s.jsx)("button",{type:"submit",className:"rounded-2xl bg-brand-500 px-4 py-3 text-sm font-semibold text-[#10251a]",disabled:eh,children:eh?"Saving...":"Save webhook"}),(0,s.jsx)("button",{type:"button",className:"rounded-2xl bg-white/5 px-4 py-3 text-sm font-medium text-white/75 transition hover:bg-white/10",onClick:t8,disabled:eh||td,children:td?"Testing...":"Test webhook"}),(0,s.jsx)("button",{type:"button",className:"rounded-2xl bg-red-700 px-4 py-3 text-sm text-white/90",onClick:()=>eu(),disabled:eh,children:eh?"Removing...":"Remove webhook"})]})]}),(0,s.jsxs)("div",{className:"mt-5 grid gap-3",children:[(0,s.jsxs)("div",{children:[(0,s.jsxs)("div",{className:"flex flex-wrap items-center justify-between gap-2",children:[(0,s.jsx)("h4",{className:"text-sm font-semibold text-white",children:"Example receiver"}),(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/5 px-3 py-1.5 text-xs font-medium text-white/75 transition hover:bg-white/10",onClick:()=>t1("receiver",Z),children:"receiver"===eg?"Copied":"Copy receiver"})]}),(0,s.jsxs)("p",{className:"mt-1 text-xs leading-5 text-white/45",children:["Set your webhook URL to this route, then use the same API key value as"," ",(0,s.jsx)("span",{className:"font-mono",children:"WANIE_WEBHOOK_KEY"}),"."]}),(0,s.jsx)("pre",{className:"mt-3 max-h-72 overflow-auto rounded-[18px] bg-[#0f1010] p-4 text-xs leading-5 text-white/70",children:(0,s.jsx)("code",{children:Z})})]}),(0,s.jsxs)("div",{children:[(0,s.jsxs)("div",{className:"flex flex-wrap items-center justify-between gap-2",children:[(0,s.jsx)("h4",{className:"text-sm font-semibold text-white",children:"Example payload"}),(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/5 px-3 py-1.5 text-xs font-medium text-white/75 transition hover:bg-white/10",onClick:()=>t1("payload",Y),children:"payload"===eg?"Copied":"Copy payload"})]}),(0,s.jsxs)("p",{className:"mt-1 text-xs leading-5 text-white/45",children:["Wanie sends text and supported media messages as JSON with header"," ",(0,s.jsx)("span",{className:"font-mono",children:"x-wanie-webhook-key"}),". Media messages include"," ",(0,s.jsx)("span",{className:"font-mono",children:"message.mediaFile"}),"."]}),(0,s.jsx)("pre",{className:"mt-3 max-h-72 overflow-auto rounded-[18px] bg-[#0f1010] p-4 text-xs leading-5 text-white/70",children:(0,s.jsx)("code",{children:Y})})]}),(0,s.jsxs)("div",{children:[(0,s.jsxs)("div",{className:"flex flex-wrap items-center justify-between gap-2",children:[(0,s.jsx)("h4",{className:"text-sm font-semibold text-white",children:"Delivery logs"}),(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/5 px-3 py-1.5 text-xs font-medium text-white/75 transition hover:bg-white/10",onClick:t5,disabled:ti,children:ti?"Loading":"Refresh"})]}),(0,s.jsx)("p",{className:"mt-1 text-xs leading-5 text-white/45",children:"Recent webhook attempts are stored per user. Failed deliveries can be retried from here."}),(0,s.jsxs)("div",{className:"mt-3 space-y-2",children:[ts.map(e=>(0,s.jsxs)("div",{className:"rounded-[18px] bg-[#2e2f2f] p-3",children:[(0,s.jsxs)("div",{className:"flex flex-wrap items-center justify-between gap-2",children:[(0,s.jsxs)("div",{className:"min-w-0",children:[(0,s.jsxs)("p",{className:"truncate text-xs font-semibold text-white",children:[e.status," - ",e.attempts,"/",e.maxAttempts||3," attempts"]}),(0,s.jsx)("p",{className:"mt-1 truncate text-[11px] text-white/40",children:e.messageId||e.chatId})]}),"failed"===e.status?(0,s.jsx)("button",{type:"button",className:"rounded-full bg-brand-500 px-3 py-1.5 text-xs font-semibold text-[#10251a]",onClick:()=>t6(e.id),disabled:tr===e.id,children:tr===e.id?"Retrying":"Retry"}):null]}),e.error?(0,s.jsx)("p",{className:"mt-2 text-[11px] leading-5 text-red-200/80",children:e.error}):null]},e.id)),ts.length||ti?null:(0,s.jsx)("div",{className:"rounded-[18px] bg-[#2e2f2f] p-4 text-xs text-white/45",children:"No webhook delivery attempts yet."})]})]})]})]}),"deliveries"===tG&&(0,s.jsxs)("div",{className:"rounded-[28px] bg-[#161717] p-4",children:[(0,s.jsxs)("div",{className:"flex flex-wrap items-start justify-between gap-3",children:[(0,s.jsxs)("div",{children:[(0,s.jsx)("p",{className:"text-[11px] uppercase tracking-[0.24em] text-white/35",children:"Outbound Delivery"}),(0,s.jsx)("h3",{className:"mt-2 text-base font-semibold text-white",children:"WhatsApp and Telegram send queue"}),(0,s.jsx)("p",{className:"mt-2 text-sm leading-6 text-white/45",children:"Messages sent from the dashboard, API, CRM auto-reply, and assistant integrations are retried automatically. Failed jobs stop after the retry limit and can be retried manually here."})]}),(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/5 px-3 py-1.5 text-xs font-medium text-white/75 transition hover:bg-white/10",onClick:t9,disabled:tw,children:tw?"Loading":"Refresh"})]}),(0,s.jsxs)("div",{className:"mt-4 space-y-2",children:[tg.map(e=>{var t,a,n,i,l,r,o,d;let c=(null==(n=e.message)||null==(a=n.chat)||null==(t=a.contact)?void 0:t.displayName)||(null==(l=e.message)||null==(i=l.chat)?void 0:i.title)||(null==(r=e.message)?void 0:r.receiver)||e.messageId,x=(null==(o=e.message)?void 0:o.body)||(null==(d=e.message)?void 0:d.type)||"Outbound message";return(0,s.jsxs)("div",{className:"rounded-[18px] bg-[#2e2f2f] p-3",children:[(0,s.jsxs)("div",{className:"flex flex-wrap items-start justify-between gap-3",children:[(0,s.jsxs)("div",{className:"min-w-0",children:[(0,s.jsxs)("div",{className:"flex flex-wrap items-center gap-2",children:[(0,s.jsx)("span",{className:"rounded-full px-2.5 py-1 text-[11px] font-semibold capitalize ".concat("delivered"===e.status?"bg-brand-500/15 text-brand-100":"failed"===e.status?"bg-red-500/15 text-red-100":"canceled"===e.status?"bg-white/10 text-white/55":"bg-amber-500/15 text-amber-100"),children:e.status}),(0,s.jsxs)("span",{className:"text-[11px] text-white/40",children:[e.transport," - ",e.attempts,"/",e.maxAttempts," attempts"]})]}),(0,s.jsx)("p",{className:"mt-2 truncate text-sm font-semibold text-white",children:c}),(0,s.jsx)("p",{className:"mt-1 line-clamp-2 text-xs leading-5 text-white/45",children:x}),(0,s.jsxs)("p",{className:"mt-1 text-[11px] text-white/30",children:["Updated: ",G(e.updatedAt)]})]}),(0,s.jsxs)("div",{className:"flex shrink-0 flex-wrap items-center gap-2",children:[["queued","sending"].includes(e.status)?(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/5 px-3 py-1.5 text-xs font-medium text-white/75 transition hover:bg-white/10",onClick:()=>at(e.id),disabled:tN===e.id,children:tN===e.id?"Canceling":"Cancel"}):null,"failed"===e.status||"canceled"===e.status?(0,s.jsx)("button",{type:"button",className:"rounded-full bg-brand-500 px-3 py-1.5 text-xs font-semibold text-[#10251a]",onClick:()=>ae(e.id),disabled:tv===e.id,children:tv===e.id?"Retrying":"Retry"}):null]})]}),e.lastError?(0,s.jsx)("p",{className:"mt-2 text-[11px] leading-5 text-red-200/80",children:e.lastError}):null]},e.id)}),tg.length||tw?null:(0,s.jsx)("div",{className:"rounded-[18px] bg-[#2e2f2f] p-4 text-xs text-white/45",children:"No outbound delivery jobs yet."})]})]}),"telegram"===tG&&(0,s.jsxs)("div",{className:"rounded-[28px] bg-[#161717] p-4",children:[(0,s.jsx)("p",{className:"text-[11px] uppercase tracking-[0.24em] text-white/35",children:"Telegram Bot"}),(0,s.jsxs)("div",{className:"mt-4 rounded-[22px] bg-[#2e2f2f] p-4",children:[(0,s.jsxs)("div",{className:"flex flex-wrap items-center justify-between gap-2",children:[(0,s.jsxs)("div",{children:[(0,s.jsx)("h3",{className:"text-base font-semibold text-white",children:"Active bots"}),(0,s.jsx)("p",{className:"mt-1 text-xs leading-5 text-white/45",children:"Telegram bots configured for this Wanie account."})]}),(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/5 px-3 py-1.5 text-xs font-medium text-white/75 transition hover:bg-white/10",onClick:t3,disabled:e8,children:"Refresh"})]}),(0,s.jsxs)("div",{className:"mt-3 space-y-2",children:[e8?(0,s.jsx)("div",{className:"rounded-[18px] bg-[#161717] px-4 py-4 text-sm text-white/45",children:"Loading Telegram bots..."}):null,e8||e4.length?null:(0,s.jsx)("div",{className:"rounded-[18px] bg-[#161717] px-4 py-4 text-sm text-white/45",children:"No Telegram bot configured."}),e4.map(e=>(0,s.jsx)("div",{className:"rounded-[18px] bg-[#161717] p-4",children:(0,s.jsxs)("div",{className:"flex flex-wrap items-center justify-between gap-3",children:[(0,s.jsxs)("div",{className:"min-w-0",children:[(0,s.jsx)("p",{className:"truncate text-sm font-semibold text-white",children:e.name||"Telegram Bot"}),(0,s.jsxs)("p",{className:"mt-1 text-xs text-white/40",children:[e.running?"Running":"Stopped"," -"," ",e.configured?"Token ".concat(e.tokenPreview):"Runtime only"]})]}),(0,s.jsx)("button",{type:"button",className:"rounded-2xl bg-red-700 px-4 py-2 text-sm text-white/90 disabled:opacity-50",onClick:()=>t4(e.id),disabled:tt===e.id,children:tt===e.id?"Deleting...":"Delete Bot"})]})},e.id))]})]}),(0,s.jsx)("h3",{className:"mt-5 text-base font-semibold text-white",children:"Admin allowlist"}),(0,s.jsx)("p",{className:"mt-2 text-sm leading-6 text-white/45",children:"Telegram chat IDs in this list can use the bot as the Wanie assistant admin. Other Telegram users follow CRM mode; when CRM is off their messages are saved to the dashboard without assistant tool access."}),(0,s.jsxs)("form",{className:"mt-4 space-y-3",onSubmit:aa,children:[(0,s.jsxs)("label",{className:"flex items-center justify-between gap-3 rounded-[22px] bg-[#2e2f2f] px-4 py-3",children:[(0,s.jsxs)("span",{children:[(0,s.jsx)("span",{className:"block text-sm font-semibold text-white",children:"Telegram AI replies"}),(0,s.jsx)("span",{className:"mt-1 block text-xs leading-5 text-white/45",children:"Turn off to store Telegram messages without Wanie AI assistant or CRM auto-reply responses."})]}),(0,s.jsx)("input",{type:"checkbox",className:"h-5 w-5 accent-brand-500",checked:!1!==e5,onChange:e=>e3(e.target.checked),disabled:e8})]}),(0,s.jsx)("textarea",{className:"min-h-40 w-full resize-y rounded-[22px] bg-[#2e2f2f] px-4 py-3 font-mono text-sm text-white outline-none placeholder:text-white/30",placeholder:"123456789\n987654321",value:e1,onChange:e=>e2(e.target.value),disabled:e8}),(0,s.jsxs)("div",{className:"flex flex-wrap items-center gap-3",children:[(0,s.jsx)("button",{type:"submit",className:"rounded-2xl bg-brand-500 px-4 py-3 text-sm font-semibold text-[#10251a]",disabled:e8||e9,children:e9?"Saving...":e8?"Loading...":"Save Telegram settings"}),(0,s.jsx)("p",{className:"text-xs leading-5 text-white/40",children:"Use one ID per line, or separate IDs with comma, space, or semicolon."})]})]})]}),"ai"===tG&&(0,s.jsxs)("div",{className:"rounded-[28px] bg-[#161717] p-4",children:[(0,s.jsx)("p",{className:"text-[11px] uppercase tracking-[0.24em] text-white/35",children:"AI Providers"}),(0,s.jsx)("h3",{className:"mt-2 text-base font-semibold text-white",children:"Manage LLM providers"}),(0,s.jsx)("div",{className:"mt-2",children:(0,s.jsx)("button",{type:"button",className:"rounded-2xl bg-white/5 px-3 py-2 text-sm font-medium text-white/80",onClick:()=>tB(!0),children:"Edit Assistant Tools"})}),(0,s.jsx)("p",{className:"mt-2 text-sm leading-6 text-white/45",children:"Add provider configs for OpenAI, Anthropic, Ollama, OpenRouter, then fetch available models."}),(0,s.jsxs)("form",{className:"mt-4 space-y-3",onSubmit:as,children:[(0,s.jsxs)("div",{className:"grid grid-cols-3 gap-2",children:[(0,s.jsx)("input",{className:"col-span-2 w-full rounded-[22px] bg-[#2e2f2f] px-4 py-3 text-sm text-white outline-none placeholder:text-white/30",placeholder:"Provider name (e.g. My OpenAI)",autoComplete:"off",value:eO,onChange:e=>e_(e.target.value),required:!0}),(0,s.jsxs)("select",{className:"w-full rounded-[22px] bg-[#2e2f2f] px-4 py-3 text-sm text-white outline-none",value:eD,onChange:e=>eL(e.target.value),required:!0,children:[(0,s.jsx)("option",{value:"",children:"Select provider"}),(0,s.jsx)("option",{value:"openai",children:"OpenAI"}),(0,s.jsx)("option",{value:"anthropic",children:"Anthropic"}),(0,s.jsx)("option",{value:"ollama",children:"Ollama"}),(0,s.jsx)("option",{value:"openrouter",children:"OpenRouter"})]})]}),(0,s.jsxs)("div",{className:"grid gap-2",children:[(0,s.jsxs)("div",{className:"flex gap-2",children:[(0,s.jsx)("input",{className:"flex-1 w-full rounded-[22px] bg-[#2e2f2f] px-4 py-3 text-sm text-white outline-none placeholder:text-white/30",placeholder:"API key (sensitive, optional)",value:eW,autoComplete:"off",onChange:e=>eF(e.target.value),type:eH?"text":"password"}),(0,s.jsx)("button",{type:"button",className:"rounded-[22px] bg-white/5 px-4 py-3 text-sm text-white/70",onClick:()=>eQ(e=>!e),children:eH?"Hide":"Show"})]}),(0,s.jsx)("input",{className:"w-full rounded-[22px] bg-[#2e2f2f] px-4 py-3 text-sm text-white outline-none placeholder:text-white/30",placeholder:"Host (e.g. http://localhost:11434)",value:eM,onChange:e=>eU(e.target.value)}),(0,s.jsx)("input",{className:"w-full rounded-[22px] bg-[#2e2f2f] px-4 py-3 text-sm text-white outline-none placeholder:text-white/30",placeholder:"Default model (optional)",value:eq,onChange:e=>eB(e.target.value)}),(0,s.jsxs)("div",{className:"rounded-[16px] bg-[#232424] px-4 py-3",children:[(0,s.jsxs)("div",{className:"mb-2 flex items-center justify-between",children:[(0,s.jsx)("p",{className:"text-xs text-white/70",children:"Provider temperature"}),(0,s.jsxs)("div",{className:"flex items-center gap-2",children:[(0,s.jsx)("span",{className:"text-xs text-white/60",children:""===ez?"Auto":ez}),(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/5 px-2 py-1 text-[11px] text-white/70",onClick:()=>eK(""),children:"Auto"})]})]}),(0,s.jsx)("input",{type:"range",min:"0",max:"2",step:"0.1",value:""===ez?"1":ez,onChange:e=>eK(t0(e.target.value)),className:"w-full accent-brand-500"}),(0,s.jsx)("p",{className:"mt-2 text-[11px] text-white/45",children:"Set kosong untuk mengikuti default model/provider."})]}),(0,s.jsx)("p",{className:"text-xs text-white/45",children:function(e){if(!e)return"";switch(String(e).toLowerCase()){case"openai":return"OpenAI: provide an API key (platform.openai.com). Host is optional.";case"anthropic":return"Anthropic: provide an API key. Default model is typically `claude-2`.";case"ollama":return"Ollama: provide the local host (e.g. http://localhost:11434) and model name to use.";case"openrouter":return"OpenRouter: provide host or API key and optionally a default model.";default:return"Provide connection details (API key, host, default model)."}}(eD)})]}),(0,s.jsx)("div",{className:"flex gap-2",children:(0,s.jsx)("button",{type:"submit",className:"rounded-2xl bg-brand-500 px-4 py-3 text-sm font-semibold text-[#10251a] disabled:opacity-60",disabled:eG,"aria-busy":eG,children:eG?"Adding...":"Add provider"})})]}),(0,s.jsxs)("div",{className:"mt-4 max-h-[200px] space-y-3 overflow-y-auto pr-1",children:[eI?(0,s.jsx)("div",{className:"rounded-[22px] bg-[#2e2f2f] px-4 py-6 text-sm text-white/45",children:"Loading providers..."}):null,eI||eE.length?null:(0,s.jsx)("div",{className:"rounded-[22px] bg-[#2e2f2f] px-4 py-6 text-sm leading-6 text-white/45",children:"No providers configured."}),eE.map(e=>{var t,a,n,i,l,r;return(0,s.jsxs)("div",{className:"rounded-[22px] bg-[#2e2f2f] px-4 py-4",children:[(0,s.jsxs)("div",{className:"flex items-start justify-between gap-3",children:[(0,s.jsxs)("div",{className:"min-w-0",children:[(0,s.jsx)("h4",{className:"truncate text-sm font-semibold text-white",children:e.name}),(0,s.jsx)("p",{className:"mt-1 text-xs text-white/55",children:e.provider})]}),(0,s.jsxs)("div",{className:"flex gap-2",children:[(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/5 px-3 py-1.5 text-xs font-medium text-white/80",onClick:()=>ar(e.id),disabled:tI===e.id,children:tI===e.id?"Fetching...":"Fetch models"}),(0,s.jsx)("button",{type:"button",className:"rounded-full bg-red-700 px-3 py-1.5 text-xs font-medium text-white/80",onClick:()=>al(e.id),children:"Delete"})]})]}),(0,s.jsx)("div",{className:"mt-3 text-xs text-white/40",children:(0,s.jsxs)("p",{children:["Created: ",G(e.createdAt)]})}),(0,s.jsxs)("div",{className:"mt-3 rounded-[14px] bg-[#0f1111] px-3 py-3",children:[(0,s.jsxs)("div",{className:"mb-2 flex items-center justify-between",children:[(0,s.jsx)("p",{className:"text-xs text-white/60",children:"Provider temperature"}),(0,s.jsx)("span",{className:"text-xs text-white/60",children:t0(void 0!==tD[e.id]?tD[e.id]:null==(t=e.config)?void 0:t.temperature)||"Auto"})]}),(0,s.jsx)("input",{type:"range",min:"0",max:"2",step:"0.1",value:t0(void 0!==tD[e.id]?tD[e.id]:null==(a=e.config)?void 0:a.temperature)||"1",onChange:t=>tL(a=>({...a||{},[e.id]:t0(t.target.value)})),className:"w-full accent-brand-500"}),(0,s.jsxs)("div",{className:"mt-2 flex items-center gap-2",children:[(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/5 px-3 py-1 text-xs text-white/80",onClick:()=>ao(e),disabled:tM===e.id,children:tM===e.id?"Saving...":"Save provider temp"}),(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/5 px-3 py-1 text-xs text-white/70",onClick:async()=>{tL(t=>({...t||{},[e.id]:""})),await ao(e,"")},disabled:tM===e.id,children:"Auto"})]})]}),eY[e.id]&&eY[e.id].length?(0,s.jsxs)("div",{className:"mt-3 grid gap-2",children:[(0,s.jsx)("p",{className:"text-xs text-white/45",children:"Models:"}),(0,s.jsxs)("div",{className:"flex items-center justify-between gap-2 mt-2",children:[(0,s.jsx)("div",{className:"flex-1",children:(0,s.jsxs)("select",{className:"w-full rounded-[10px] bg-[#0f1111] px-3 py-2 text-sm text-white outline-none",value:eC===e.id&&eS?eS:"",onChange:async t=>{let a=t.target.value||null;try{await eA(e.id),await eT(a)}catch(e){}},children:[(0,s.jsx)("option",{value:"",children:"Select model (set as default)"}),eY[e.id].map(e=>(0,s.jsx)("option",{value:e.id,children:e.name||e.id},e.id))]})}),(0,s.jsx)("div",{children:eC===e.id?(0,s.jsx)("span",{className:"rounded-full bg-emerald-600 px-3 py-1 text-xs text-white",children:"Active"}):(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/5 px-3 py-1 text-xs text-white/80",onClick:async()=>{try{await eA(e.id)}catch(e){}},children:"Set active"})})]}),eC===e.id&&eS?(0,s.jsxs)("div",{className:"mt-2 rounded-[14px] bg-[#0f1111] px-3 py-3",children:[(0,s.jsxs)("div",{className:"mb-2 flex items-center justify-between",children:[(0,s.jsxs)("p",{className:"text-xs text-white/60",children:["Model temperature (",eS,")"]}),(0,s.jsx)("span",{className:"text-xs text-white/60",children:t0(void 0!==tW[e.id]?tW[e.id]:null==(i=e.config)||null==(n=i.modelTemperatures)?void 0:n[eS])||"Auto"})]}),(0,s.jsx)("input",{type:"range",min:"0",max:"2",step:"0.1",value:t0(void 0!==tW[e.id]?tW[e.id]:null==(r=e.config)||null==(l=r.modelTemperatures)?void 0:l[eS])||"1",onChange:t=>tF(a=>({...a||{},[e.id]:t0(t.target.value)})),className:"w-full accent-brand-500"}),(0,s.jsxs)("div",{className:"mt-2 flex items-center gap-2",children:[(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/5 px-3 py-1 text-xs text-white/80",onClick:()=>ad(e,eS),disabled:tM===e.id,children:tM===e.id?"Saving...":"Save model temp"}),(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/5 px-3 py-1 text-xs text-white/70",onClick:async()=>{tF(t=>({...t||{},[e.id]:""})),await ad(e,eS,"")},disabled:tM===e.id,children:"Auto"})]})]}):null]}):null,(0,s.jsxs)("div",{className:"mt-2",children:[(0,s.jsx)("p",{className:"text-xs text-white/45",children:"Manual model"}),(0,s.jsxs)("div",{className:"flex items-center gap-2 mt-2",children:[(0,s.jsx)("input",{className:"flex-1 w-full rounded-[10px] bg-[#0f1111] px-3 py-2 text-sm text-white outline-none",placeholder:"Enter model id (e.g. gpt-5-mini)",value:tO[e.id]||"",onChange:t=>t_(a=>({...a||{},[e.id]:t.target.value})),onKeyDown:async t=>{if("Enter"===t.key){t.preventDefault();let a=(tO[e.id]||t.target.value||"").trim().replace(/^\s+|\s+$/g,"")||null;try{await eA(e.id),await eT(a)}catch(e){}}},autoComplete:"off"}),(0,s.jsx)("button",{type:"button",className:"rounded-full bg-white/5 px-3 py-1 text-xs text-white/80",onClick:async()=>{let t=(tO[e.id]||"").trim().replace(/^\s+|\s+$/g,"")||null;try{await eA(e.id),await eT(t)}catch(e){}},children:"Set"})]})]})]},e.id)})]})]}),"advanced"===tG&&(0,s.jsxs)("div",{className:"rounded-[28px] bg-[#161717] p-4",children:[(0,s.jsx)("p",{className:"text-[11px] uppercase tracking-[0.24em] text-white/35",children:"Advanced"}),(0,s.jsx)("h3",{className:"mt-2 text-base font-semibold text-white",children:"Terminal auto-approve"}),(0,s.jsx)("p",{className:"mt-2 text-sm leading-6 text-white/45",children:'When enabled, terminal commands requested with approvalMode "auto" will be executed immediately without checking the host allowlist. Use with caution.'}),(0,s.jsxs)("div",{className:"mt-4 flex items-center justify-between",children:[(0,s.jsxs)("div",{children:[(0,s.jsx)("div",{className:"text-sm text-white",children:"Allow new registrations"}),(0,s.jsx)("div",{className:"text-xs text-white/45",children:"Toggle whether new users can sign up for this Wanie workspace."})]}),(0,s.jsx)("button",{type:"button",className:"rounded-full px-3 py-1 text-sm ".concat(eV?"bg-emerald-600 text-white":"bg-white/5 text-white/60"),onClick:async()=>{if(ew){e0(!0);try{let e=await (0,r.n)("/api/auth/config",{method:"POST",token:ew,body:{allowRegistration:!eV}});eX(!0===e.allowRegistration)}catch(e){alert(e.message||"Failed to update registration setting")}finally{e0(!1)}}},disabled:e$,children:e$?"Saving...":eV?"Enabled":"Disabled"})]}),(0,s.jsxs)("div",{className:"mt-4 flex items-center justify-between",children:[(0,s.jsxs)("div",{children:[(0,s.jsx)("div",{className:"text-sm text-white",children:"Auto-approve terminal commands"}),(0,s.jsx)("div",{className:"text-xs text-white/45",children:"Bypass WANIE_TERMINAL_ALLOWLIST and allow auto execution of any command."})]}),(0,s.jsx)("button",{type:"button",className:"rounded-full px-3 py-1 text-sm ".concat(eN?"bg-emerald-600 text-white":"bg-white/5 text-white/60"),onClick:async()=>{try{await ek(!eN)}catch(e){}},children:eN?"Enabled":"Disabled"})]}),(0,s.jsxs)("div",{className:"mt-8 rounded-[22px] bg-[#2e2f2f] p-4 ring-1 ring-white/10",children:[(0,s.jsxs)("div",{className:"flex items-center justify-between",children:[(0,s.jsxs)("div",{children:[(0,s.jsx)("p",{className:"text-sm font-semibold text-white",children:"Reset user password"}),(0,s.jsx)("p",{className:"mt-1 text-xs text-white/45",children:"Set a new password for an existing user account."})]}),(0,s.jsx)("span",{className:"rounded-full bg-white/5 px-3 py-1 text-xs text-white/70",children:"Admin only"})]}),(0,s.jsxs)("form",{className:"mt-4 space-y-3",onSubmit:an,children:[(0,s.jsx)("input",{className:"w-full rounded-[22px] bg-[#161717] px-4 py-3 text-sm text-white outline-none placeholder:text-white/30",placeholder:"New password",type:"password",value:tC,onChange:e=>tS(e.target.value),required:!0}),(0,s.jsx)("button",{type:"submit",className:"w-full rounded-2xl bg-brand-500 px-4 py-3 text-sm font-semibold text-[#10251a] disabled:opacity-60",disabled:tA,children:tA?"Resetting...":"Reset password"})]})]}),(0,s.jsxs)("div",{className:"mt-6 rounded-[22px] bg-[#2e2f2f] p-4 ring-1 ring-white/10",children:[(0,s.jsx)("p",{className:"text-sm font-semibold text-white",children:"Reset all Wanie data"}),(0,s.jsx)("p",{className:"mt-1 text-xs text-white/45",children:"Permanently deletes all stored app data. Requires confirmation."}),(0,s.jsx)("button",{type:"button",className:"mt-4 w-full rounded-2xl bg-red-600 px-4 py-3 text-sm font-semibold text-white disabled:opacity-60",onClick:ai,disabled:tE,children:tE?"Resetting...":"Reset all data"})]})]})]})]})]})}),(0,s.jsx)(q,{open:tq,onClose:()=>tB(!1)}),(0,s.jsx)(B,{open:tz,onClose:()=>tK(!1)})]}):null}var V=a(5842);function X(){let[e,t]=(0,n.useState)(null),[a,d]=(0,n.useState)(!1),c=(0,i.useRouter)(),{token:x,user:m,hydrateAuth:u,logout:h,setBootstrapData:p,setMessages:f,prependMessages:g,setActiveChat:b,upsertSession:w,upsertChat:y,addMessage:v,updateMessageStatus:j,updateMessageDelivery:N,updateMessage:k,upsertTerminalRecord:C,setSocket:S,socket:A,chats:T,sessions:E,activeChatId:P,activeSessionId:I,setActiveSession:R,messagesByChat:O,messageMetaByChat:D,typingByChat:L}=(0,o.C)(),[W,M]=(0,n.useState)(!0),[q,B]=(0,n.useState)(!1),[z,K]=(0,n.useState)(!1),[H,Q]=(0,n.useState)(!1),[G,Z]=(0,n.useState)(""),[Y,X]=(0,n.useState)(""),[$,ee]=(0,n.useState)(""),[et,ea]=(0,n.useState)("wwebjs"),[es,en]=(0,n.useState)(""),[ei,el]=(0,n.useState)(""),[er,eo]=(0,n.useState)(""),[ed,ec]=(0,n.useState)(""),[ex,em]=(0,n.useState)(""),[eu,eh]=(0,n.useState)(""),[ep,ef]=(0,n.useState)(""),[eg,eb]=(0,n.useState)(""),[ew,ey]=(0,n.useState)(!1),[ev,ej]=(0,n.useState)([]),[eN,ek]=(0,n.useState)([]),[eC,eS]=(0,n.useState)(!1),[eA,eT]=(0,n.useState)(""),[eE,eP]=(0,n.useState)(""),[eI,eR]=(0,n.useState)(null),[eO,e_]=(0,n.useState)(""),[eD,eL]=(0,n.useState)(!0),[eW,eF]=(0,n.useState)("POST"),[eM,eU]=(0,n.useState)(""),[eq,eB]=(0,n.useState)(""),[ez,eK]=(0,n.useState)(""),[eH,eQ]=(0,n.useState)(!1),[eG,eZ]=(0,n.useState)(null),[eY,eJ]=(0,n.useState)(!1),[eV,eX]=(0,n.useState)(!1),e$=(0,n.useRef)(null),e0=(0,n.useMemo)(()=>T.find(e=>e.id===P)||null,[P,T]),e1=O[P]||[],e2=D[P]||{hasMore:!1,nextBefore:null},e5=L[P];E.filter(e=>"ready"===e.status).length;let e3=(0,n.useCallback)(async()=>{if(x){B(!0);try{let e=await (0,r.n)("/api/contacts",{token:x});ej(e.contacts||[])}catch(e){Z(e.message)}finally{B(!1)}}},[x]),e4=(0,n.useCallback)(async()=>{if(x){eS(!0);try{let e=await (0,r.n)("/api/api-keys",{token:x});ek(e.apiKeys||[])}catch(e){Z(e.message)}finally{eS(!1)}}},[x]),e6=(0,n.useCallback)(async function(){let e=arguments.length>0&&void 0!==arguments[0]&&arguments[0];if(x){e&&M(!0);try{let e=await (0,r.n)("/api/bootstrap",{token:x});p(e)}catch(e){Z(e.message),401===e.status&&(h(),c.replace("/"))}finally{e&&M(!1)}}},[h,c,p,x]),e8=(0,n.useCallback)(async e=>{let t=Date.now()+25e3;for(;Date.now()<t&&(await new Promise(e=>setTimeout(e,1200)),x);)try{var a;let t=null==(a=(await (0,r.n)("/api/sessions",{token:x})).sessions)?void 0:a.find(t=>t.id===e);if(!t)continue;if(w(t),t.qrCode||"ready"===t.status||"error"===t.status||"disconnected"===t.status)return t}catch(e){}return null},[x,w]);(0,n.useEffect)(()=>{u()},[u]),(0,n.useEffect)(()=>{if(!x)return void c.replace("/");Promise.all([e6(!0),e3(),e4()]).finally(()=>{M(!1)})},[e4,e3,e6,c,x]),(0,n.useEffect)(()=>{eY&&async function(){if(x)try{let e=(await (0,r.n)("/api/webhook",{token:x})).webhook||{};e_(e.url||""),eL(!1!==e.enabled),eF(e.method||"POST"),eU(e.headers&&Object.keys(e.headers).length?JSON.stringify(e.headers,null,2):""),eB(e.bodyTemplate||""),eK(e.apiKey||"")}catch(e){}}()},[eY,x]),(0,n.useEffect)(()=>{var e;!I&&(null==(e=E[0])?void 0:e.id)&&R(E[0].id)},[I,E,R]),(0,n.useEffect)(()=>{if(!x)return;let e=(0,V.W)(x);S(e),e.on("new_message",e=>{v(e)}),e.on("message_status_update",e=>{j(e)}),e.on("outbound_delivery_update",e=>{let t=null==e?void 0:e.delivery;if((null==t?void 0:t.messageId)&&N(t),(null==t?void 0:t.status)==="failed"){var a,s,n,i,l,r;let e=(null==(n=t.message)||null==(s=n.chat)||null==(a=s.contact)?void 0:a.displayName)||(null==(l=t.message)||null==(i=l.chat)?void 0:i.title)||(null==(r=t.message)?void 0:r.receiver)||"message";Z("Delivery failed for ".concat(e,": ").concat(t.lastError))}}),e.on("contact_list_update",e=>{y(e),B(!0),e3()}),e.on("session_status_update",e=>{w(e),("ready"===e.status||"error"===e.status||"disconnected"===e.status)&&t(null),(e.qrCode||"ready"===e.status||"error"===e.status||"disconnected"===e.status)&&d(!1),"connecting"===e.status&&B(!0),"ready"===e.status&&(e6(),e3(),B(!1))}),e.on("workspace_sync_started",e=>{console.log("[Dashboard] Workspace sync started:",e),B(!0),K(!0),Q(!0)}),e.on("workspace_synced",e=>{console.log("[Dashboard] Workspace synced event received:",e),e6(),e3(),B(!1),K(!1),Q(!1),"failed"===e.status&&Z(e.error||"WhatsApp sync failed.");let t=o.C.getState();t.activeChatId&&(0,r.n)("/api/chats/".concat(t.activeChatId,"/messages"),{token:x}).then(e=>{f(t.activeChatId,e.messages,{hasMore:!!e.hasMore,nextBefore:e.nextBefore||null})}).catch(()=>{})}),e.on("typing_event",e=>{var t;let a=null==(t=o.C.getState().user)?void 0:t.id;e&&e.userId&&a&&e.userId===a||o.C.getState().setTyping(e)});let a=async e=>{if(!(null==e?void 0:e.id))return;let t=t=>{var a,s;let n=(null==t?void 0:t.chatId)||(null==e?void 0:e.chatId)||null;if(!n)return;let i=o.C.getState();if((i.messagesByChat[n]||[]).find(e=>e.externalMessageId==="terminal:".concat(t.id)||e.id==="terminal:".concat(t.id)))return;let l=i.chats.find(e=>e.id===n);i.addMessage({id:"terminal:".concat(t.id),chatId:n,sessionId:(null==l?void 0:l.sessionId)||null,mediaFileId:null,replyToId:null,externalMessageId:"terminal:".concat(t.id),sender:(null==l||null==(a=l.contact)?void 0:a.externalId)||"openwa:assistant",receiver:(null==(s=i.user)?void 0:s.id)?"user:".concat(i.user.id):"user",body:"pending"===t.status?"Terminal command pending approval: ".concat(t.command||"Terminal command"):"Terminal command finished: ".concat(t.command||"Terminal command"),type:"text",direction:"inbound",createdAt:t.requestedAt||new Date().toISOString(),updatedAt:t.requestedAt||new Date().toISOString(),mediaFile:null,replyTo:null,statuses:[{status:"delivered"}]})};try{let a=await (0,r.n)("/api/terminal/".concat(e.id),{token:x});(null==a?void 0:a.item)&&(C(a.item),t(a.item))}catch(a){(null==e?void 0:e.id)&&(C(e),t(e))}};return e.on("terminal_request",a),e.on("terminal_result",a),()=>{e.off("terminal_request",a),e.off("terminal_result",a),e.close(),S(null)}},[v,e3,e6,S,x,N,j,C,y,w]),(0,n.useEffect)(()=>{if(!P||O[P])return void K(!1);x&&(K(!0),(0,r.n)("/api/chats/".concat(P,"/messages"),{token:x}).then(e=>{f(P,e.messages,{hasMore:!!e.hasMore,nextBefore:e.nextBefore||null})}).catch(e=>{Z(e.message)}).finally(()=>{K(!1)}))},[P,O,f,x]);let e7=async e=>{e.preventDefault(),Z("");try{let e=await (0,r.n)("/api/sessions",{method:"POST",token:x,body:{name:Y,phoneNumber:$,transportType:et,phoneNumberId:es,businessAccountId:ei,accessToken:er,verifyToken:ed,appSecret:ex}});w(e.session),R(e.session.id),X(""),ee(""),ea("wwebjs"),en(""),el(""),eo(""),ec(""),em(""),eJ(!0),await e6()}catch(e){Z(e.message)}},e9=async(e,t)=>{Z("");try{if(!x)return;await (0,r.n)("/api/chats/".concat(e,"/").concat(t?"unpin":"pin"),{method:"POST",token:x}),await e6()}catch(e){Z(e.message)}},te=async e=>{Z("");try{var t;if(!x)return;let a=T.find(t=>t.id===e),s=(null==a||null==(t=a.contact)?void 0:t.externalId)||null,n=s&&("openwa:assistant"===s||String(s).startsWith("openwa:assistant")||String(s).endsWith(":assistant"));await (0,r.n)(n?"/api/assistant/sessions/".concat(e):"/api/chats/".concat(e),{method:"DELETE",token:x}),await e6(!0),P===e&&b(null)}catch(e){Z(e.message)}},tt=async e=>{Z(""),t(e),d(!0),R(e);try{let t=await (0,r.n)("/api/sessions/".concat(e,"/connect"),{method:"POST",token:x});w(t.session),await e8(e)||d(!1)}catch(e){Z(e.message),t(null),d(!1)}},ta=async e=>{Z(""),t(e);try{await (0,r.n)("/api/sessions/".concat(e,"/disconnect"),{method:"POST",token:x}),await e6(!0)}catch(e){Z(e.message)}finally{t(null)}},ts=async e=>{Z(""),t(e);try{await (0,r.n)("/api/sessions/".concat(e),{method:"DELETE",token:x}),await e6(!0),I===e&&R(null)}catch(e){Z(e.message)}finally{t(null)}},tn=async e=>{Z(""),t(e);try{let t=await (0,r.n)("/api/sessions/".concat(e,"/disconnect"),{method:"POST",token:x});w(t.session)}catch(e){Z(e.message)}finally{t(null)}},ti=async e=>{e.preventDefault(),Z(""),eS(!0);try{let e=await (0,r.n)("/api/api-keys",{method:"POST",token:x,body:{name:eA}});eP(e.secret),eT(""),ek(t=>[e.apiKey,...t]),eJ(!0)}catch(e){Z(e.message)}finally{eS(!1)}},tl=async()=>{eQ(!0),Z("");try{var e,t,a,s,n,i;let l=await (0,r.n)("/api/webhook",{method:"POST",token:x,body:{url:eO,apiKey:ez,enabled:eD,method:eW,headers:eM,bodyTemplate:eq}});e_((null==(e=l.webhook)?void 0:e.url)||""),eL((null==(t=l.webhook)?void 0:t.enabled)!==!1),eF((null==(a=l.webhook)?void 0:a.method)||"POST"),eU((null==(s=l.webhook)?void 0:s.headers)&&Object.keys(l.webhook.headers).length?JSON.stringify(l.webhook.headers,null,2):""),eB((null==(n=l.webhook)?void 0:n.bodyTemplate)||""),eK((null==(i=l.webhook)?void 0:i.apiKey)||"")}catch(e){Z(e.message)}finally{eQ(!1)}},tr=async()=>{eQ(!0),Z("");try{await (0,r.n)("/api/webhook",{method:"DELETE",token:x}),e_(""),eL(!0),eF("POST"),eU(""),eB(""),eK("")}catch(e){Z(e.message)}finally{eQ(!1)}},to=async e=>{Z(""),eR(e);try{await (0,r.n)("/api/api-keys/".concat(e),{method:"DELETE",token:x}),ek(t=>t.filter(t=>t.id!==e))}catch(e){Z(e.message)}finally{eR(null)}},td=async e=>{b(e),eb(""),null==A||A.emit("open_chat",{chatId:e})},tc=async e=>{eZ(e),Z("");try{let t=await (0,r.n)("/api/contacts/".concat(e,"/open"),{method:"POST",token:x});y(t.chat),await td(t.chat.id),ej(a=>a.map(a=>a.id===e?{...a,hasChat:!0,chatId:t.chat.id}:a)),eX(!1),setTimeout(()=>{var e;null==(e=e$.current)||e.focusComposer()},80)}catch(e){Z(e.message)}finally{eZ(null)}},tx=async e=>{let{body:t,replyToId:a}=e;if(!A)throw Error("Socket connection is not ready yet.");let s=await new Promise((e,s)=>{A.emit("send_message",{chatId:P,body:t,type:"text",replyToId:a},t=>{if(null==t?void 0:t.ok)return void e(t);s(Error((null==t?void 0:t.error)||"Failed to send message."))})});return(null==s?void 0:s.chat)&&s.chat.id&&(y(s.chat),b(s.chat.id)),s.message},tm=async e=>{let{file:t,caption:a}=e;if(!A)throw Error("Socket connection is not ready yet.");let s=new FormData;s.append("file",t);let n=await (0,r.n)("/api/media",{method:"POST",token:x,formData:s});await new Promise((e,t)=>{A.emit("send_media",{chatId:P,mediaFileId:n.mediaFile.id,body:a,type:n.type},a=>{if(null==a?void 0:a.ok)return void e(a.message);t(Error((null==a?void 0:a.error)||"Failed to send media."))})})},tu=async()=>{if(P&&e2.hasMore&&e2.nextBefore){ey(!0);try{let e=await (0,r.n)("/api/chats/".concat(P,"/messages?before=").concat(encodeURIComponent(e2.nextBefore),"&take=30"),{token:x});g(P,e.messages,{hasMore:!!e.hasMore,nextBefore:e.nextBefore||null})}catch(e){Z(e.message)}finally{ey(!1)}}},th=async e=>{try{let t=await (0,r.n)("/api/messages/".concat(e),{method:"DELETE",token:x});k(t.message),y(t.chat)}catch(e){Z(e.message)}},tp=async(e,t)=>{try{let a=await (0,r.n)("/api/messages/".concat(e,"/forward"),{method:"POST",token:x,body:{targetChatId:t}});v(a.message),y(a.chat)}catch(e){Z(e.message)}};return x?(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(l.A,{title:"Dashboard",description:"Dashboard Wanie untuk mengelola percakapan, kontak, device, dan session WhatsApp."}),H||q||z||W?(0,s.jsxs)("div",{className:"fixed left-4 bottom-4 z-50 flex items-center gap-3 rounded-[24px] bg-brand-600/95 px-4 py-3 text-sm font-semibold text-white shadow-[0_20px_60px_rgba(0,0,0,0.35)] ring-1 ring-brand-200/30",children:[(0,s.jsx)("span",{className:"flex h-4 w-4 items-center justify-center rounded-full bg-white/15",children:(0,s.jsx)("span",{className:"h-2 w-2 animate-spin rounded-full border-2 border-white border-t-transparent"})}),(0,s.jsx)("div",{className:"min-w-[220px] leading-5",children:H?"Syncing WhatsApp chats and contacts...":W?"Refreshing workspace...":q?"Syncing WhatsApp data...":"Fetching messages..."})]}):null,(0,s.jsx)("main",{className:"h-screen overflow-hidden bg-[#161717] text-white",children:(0,s.jsxs)("div",{className:"flex h-full w-full overflow-hidden bg-[#161717]",children:[(0,s.jsxs)("aside",{className:"flex flex-col",children:[(0,s.jsx)("button",{className:"mx-5 mt-5 mb-2 rounded-lg bg-brand-500 px-4 py-2 text-sm font-semibold text-[#10251a] hover:bg-brand-600 transition",onClick:()=>e6(!0),disabled:W,title:"Reload conversations",children:W?"Reloading...":"Reload Conversations"}),(0,s.jsx)(F,{chats:T,activeChatId:P,loading:W,onSelectChat:td,currentUser:m,query:eu,onQueryChange:eh,onTogglePin:e9,onDeleteChat:te})]}),(0,s.jsxs)("section",{className:"flex min-w-0 flex-1 flex-col",children:[G?(0,s.jsx)("div",{className:"mx-4 mt-4 rounded-2xl border border-red-400/25 bg-red-500/10 px-4 py-3 text-sm text-red-100",children:G}):null,(0,s.jsxs)("div",{className:"flex min-h-0 flex-1",children:[(0,s.jsx)(_,{ref:e$,chat:e0,messages:e1,chats:T,typingState:e5,loading:W,messagesLoading:z,loadingOlder:ew,hasMoreMessages:e2.hasMore,messageQuery:eg,onMessageQueryChange:eb,onLoadOlder:tu,onSendMessage:tx,onSendMedia:tm,onTyping:e=>{null==A||A.emit("typing",{chatId:P,isTyping:e})},onDeleteMessage:th,onForwardMessage:tp,onOpenContacts:()=>eX(!0),onOpenCrm:()=>c.push("/crm"),onOpenSettings:()=>eJ(!0),onLogout:()=>{h(),c.replace("/")}}),(0,s.jsx)(U,{contacts:ev,loading:q,open:eV,query:ep,onQueryChange:ef,onStartChat:tc,onClose:()=>eX(!1),startingContactId:eG})]})]})]})}),(0,s.jsx)(J,{open:eY,sessions:E,activeSessionId:I,onClose:()=>eJ(!1),onSelect:R,onConnect:tt,onDisconnect:tn,onClearSession:ta,onDeleteSession:ts,connectLoading:e,qrLoading:a,syncingWorkspace:H,sessionName:Y,sessionPhone:$,sessionTransport:et,metaPhoneNumberId:es,metaBusinessAccountId:ei,metaAccessToken:er,metaVerifyToken:ed,metaAppSecret:ex,onSessionNameChange:X,onSessionPhoneChange:ee,onSessionTransportChange:ea,onMetaPhoneNumberIdChange:en,onMetaBusinessAccountIdChange:el,onMetaAccessTokenChange:eo,onMetaVerifyTokenChange:ec,onMetaAppSecretChange:em,onCreateSession:e7,apiKeys:eN,apiKeysLoading:eC,apiKeyName:eA,apiKeySecret:eE,onApiKeyNameChange:eT,onCreateApiKey:ti,onRevokeApiKey:to,revokingKeyId:eI,webhookUrl:eO,webhookEnabled:eD,webhookMethod:eW,webhookHeaders:eM,webhookBodyTemplate:eq,webhookApiKey:ez,onWebhookUrlChange:e_,onWebhookEnabledChange:eL,onWebhookMethodChange:eF,onWebhookHeadersChange:eU,onWebhookBodyTemplateChange:eB,onWebhookApiKeyChange:eK,onSaveWebhook:tl,onDeleteWebhook:tr,webhookLoading:eH})]}):null}}},e=>{e.O(0,[202,774,112,345,853,636,593,792],()=>e(e.s=6998)),_N_E=e.O()}]);
|