import{d as U,j as e,db as Fe,c as L,aD as Oe,d$ as Be,r as o,bY as Y,R as qe,ak as ze,dI as He,al as Ge,x as V,I as q,e0 as Ue,B as F,bH as Me,am as ue,d6 as Ve,N as se,F as Re,D as W,E as We,e1 as Te,e2 as Ye,bf as Le,e3 as Ie,e4 as Xe,e5 as Ze,e6 as Je,e7 as Qe,e8 as es,e9 as ss,ea as ts,K as as,G as ns,dW as rs,b_ as xe}from"./index-CeMilrX4.js";import{C as G,P as pe,a as he,B as ls}from"./PageLayout-Cw_MXmcE.js";import{R as os,C as ge}from"./Grid-BuDgMX1I.js";import{c as is,m as cs}from"./node-types-C09ZlzBQ.js";import{L as ds,a as ms,b as us}from"./listbox-CGIGCOya.js";import{P as xs}from"./plus-D-z0bUE_.js";import{M as J}from"./map-pin-D6_BkIVb.js";import{T as Ee}from"./trash-2-Bu7BoH4P.js";import{S as Q,K as I}from"./KeycapButton-BFRxj00q.js";import{C as fe}from"./ConfirmModal-BF449Kzb.js";import{S as ps,C as hs,M as gs}from"./ChatBubble-D4uyE1Zh.js";import{e as fs,g as js,a as vs}from"./chat-utils-BJwCtjDV.js";import{W as ee}from"./wifi-DjxkOZ88.js";import{K as ys}from"./key-round-Dkqq4aT0.js";import"./element-movement-zLH7y51p.js";import"./keycap-sfx-ByZp-njj.js";import"./triangle-alert-DiFold9L.js";/**
 * @license lucide-react v0.559.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const Ns=[["path",{d:"M2.992 16.342a2 2 0 0 1 .094 1.167l-1.065 3.29a1 1 0 0 0 1.236 1.168l3.413-.998a2 2 0 0 1 1.099.092 10 10 0 1 0-4.777-4.719",key:"1sd12s"}]],De=U("message-circle",Ns);/**
 * @license lucide-react v0.559.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const bs=[["path",{d:"M22 17a2 2 0 0 1-2 2H6.828a2 2 0 0 0-1.414.586l-2.202 2.202A.71.71 0 0 1 2 21.286V5a2 2 0 0 1 2-2h16a2 2 0 0 1 2 2z",key:"18887p"}],["path",{d:"M12 8v6",key:"1ib9pf"}],["path",{d:"M9 11h6",key:"1fldmi"}]],ks=U("message-square-plus",bs);/**
 * @license lucide-react v0.559.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const ws=[["path",{d:"m17 2 4 4-4 4",key:"nntrym"}],["path",{d:"M3 11v-1a4 4 0 0 1 4-4h14",key:"84bu3i"}],["path",{d:"m7 22-4-4 4-4",key:"1wqhfi"}],["path",{d:"M21 13v1a4 4 0 0 1-4 4H3",key:"1rx37r"}]],Cs=U("repeat",ws);/**
 * @license lucide-react v0.559.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const Ss=[["path",{d:"M12 19h8",key:"baeox8"}],["path",{d:"m4 17 6-6-6-6",key:"1yngyt"}]],je=U("terminal",Ss);/**
 * @license lucide-react v0.559.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const _s=[["path",{d:"M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2",key:"1yyitq"}],["circle",{cx:"9",cy:"7",r:"4",key:"nufk8"}],["line",{x1:"17",x2:"22",y1:"8",y2:"13",key:"3nzzx3"}],["line",{x1:"22",x2:"17",y1:"8",y2:"13",key:"1swrse"}]],Ms=U("user-x",_s);function z({disabled:s,className:t,children:l}){return e.jsx(Fe,{disabled:s,className:L("flex flex-col gap-2",t),children:l})}function H({required:s,className:t,children:l,...a}){return e.jsxs(Oe,{className:L("text-sm font-medium text-fg-primary select-none","data-[disabled]:opacity-50",a.title&&"cursor-help",t),...a,children:[l,s&&e.jsx("span",{className:"text-sys-red ml-0.5","aria-hidden":"true",children:"*"})]})}function Rs({className:s,children:t,...l}){return e.jsx(Be,{className:L("text-sm text-fg-muted","data-[disabled]:opacity-50",s),...l,children:t})}function Ts(s,t){return o.useMemo(()=>{const a=new Map;for(const[n,c]of Object.entries(s)){const m=c.name||c.node_name||null,{type:d}=is(c);a.set(n.toLowerCase(),{name:m,type:d})}const r=new Map;for(let n=t.length-1;n>=0;n--){const c=t[n];if(!c._advertSender)continue;const m=c._advertSender.toLowerCase();if(r.has(m))continue;const d=c._advertName??null,i=c._advertNodeType!=null?cs(c._advertNodeType):"companion";r.set(m,{name:d,type:i})}return function(c){const m=c.toLowerCase(),d=a.get(m);if(d!=null&&d.name)return d;const i=r.get(m);return i!=null&&i.name?i:d||i||{name:null,type:"companion"}}},[s,t])}function ve({icon:s,title:t,description:l,onClick:a,accent:r}){return e.jsxs("button",{onClick:a,className:L("flex flex-col items-center gap-3 p-6 rounded-2xl text-center","ring-1 ring-edge-subtle hover:ring-2 transition-all duration-150","w-full max-w-[260px]",r==="primary"?"hover:ring-sys-blue hover:bg-sys-blue/5":"hover:ring-sys-indigo hover:bg-sys-indigo/5"),children:[e.jsx("div",{className:L("p-3 rounded-xl",r==="primary"?"bg-sys-blue/10 text-sys-blue":"bg-sys-indigo/10 text-sys-indigo"),children:s}),e.jsx("p",{className:"type-body-sm font-medium text-fg-primary",children:t}),e.jsx("p",{className:"type-data-xs text-fg-muted leading-relaxed",children:l})]})}function Ls({onCreateRoom:s}){return e.jsxs("div",{className:"flex flex-col items-center justify-center py-16 px-4",children:[e.jsx(Y,{className:"w-12 h-12 text-fg-muted opacity-30 mb-4"}),e.jsx("p",{className:"type-subheading text-fg-secondary mb-1",children:"Add a Room Server"}),e.jsx("p",{className:"type-body-sm text-fg-muted mb-8",children:"Choose how this device should operate."}),e.jsxs("div",{className:"flex flex-col sm:flex-row items-center gap-4",children:[e.jsx(ve,{icon:e.jsx(Cs,{className:"w-6 h-6"}),title:"Repeater + Room Server",description:"Keep forwarding packets and add group messaging.",onClick:()=>s("hybrid"),accent:"primary"}),e.jsx(ve,{icon:e.jsx(qe,{className:"w-6 h-6"}),title:"Dedicated Room Server",description:"Stop repeating. Focus entirely on room server duties.",onClick:()=>s("dedicated"),accent:"secondary"})]})]})}function Es({rooms:s,selected:t,onSelect:l,onAdd:a}){const r=a?e.jsx("button",{onClick:a,className:L("flex items-center justify-center px-2 py-1 rounded-md","text-fg-muted hover:text-fg-primary hover:bg-surface/60","transition-all duration-150"),title:"Add room server",children:e.jsx(xs,{className:"w-4 h-4"})}):null;return s.length<=1?r:s.length<=3?e.jsxs("div",{className:"flex items-center gap-0.5 p-0.5 rounded-lg bg-subtle-fill",children:[s.map(n=>e.jsx("button",{onClick:()=>l(n.room_name),className:L("px-3 py-1 rounded-md text-sm font-medium transition-all duration-150",n.room_name===t?"bg-surface text-fg-primary shadow-sm":"text-fg-muted hover:text-fg-secondary"),children:n.room_name},n.room_name)),r]}):e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(ds,{value:t??"",onChange:l,children:s.map(n=>e.jsx(ms,{value:n.room_name,children:e.jsx(us,{children:n.room_name})},n.room_name))}),r]})}function Ds(s){if(!s)return"";const t=s.trim().toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-|-$/g,"");return t?`${t}-room`:""}function ye({open:s,onClose:t,mode:l,identity:a,repeaterConfig:r,onCreate:n,onUpdate:c,onDelete:m,onSendAdvert:d}){var te,ae,ne,re,le,oe,ie,ce,de;const i=l==="edit"&&a,u=`${s}-${(a==null?void 0:a.name)??""}-${l}`,[x,f]=o.useState(""),[j,y]=o.useState(""),[g,E]=o.useState(!1),[C,S]=o.useState(""),[b,_]=o.useState(""),[N,M]=o.useState(""),[k,R]=o.useState(""),[w,D]=o.useState(!1),[P,A]=o.useState(!1),[O,$]=o.useState(!1),[K,T]=o.useState(null),[B,h]=o.useState(u);if(u!==B)if(h(u),T(null),D(!1),A(!1),$(!1),i)f(((te=a.settings)==null?void 0:te.node_name)??a.name),y(a.identity_key??""),E(!1),S(((ae=a.settings)==null?void 0:ae.admin_password)??""),_(((ne=a.settings)==null?void 0:ne.guest_password)??""),M(((le=(re=a.settings)==null?void 0:re.latitude)==null?void 0:le.toString())??""),R(((ie=(oe=a.settings)==null?void 0:oe.longitude)==null?void 0:ie.toString())??"");else{const p=r;f(""),y(""),E(!1),S(""),_(""),M(((ce=p==null?void 0:p.repeater)==null?void 0:ce.latitude)!=null?p.repeater.latitude.toString():""),R(((de=p==null?void 0:p.repeater)==null?void 0:de.longitude)!=null?p.repeater.longitude.toString():"")}const v=o.useCallback(()=>{const p={};return x&&(p.node_name=x),C&&(p.admin_password=C),b&&(p.guest_password=b),N!==""&&(p.latitude=parseFloat(N)),k!==""&&(p.longitude=parseFloat(k)),p},[x,C,b,N,k]),Ae=o.useCallback(async()=>{if(!x.trim()){T("Room name is required");return}D(!0),T(null);const p=i?a.name:Ds(x);let Z;if(i)Z=await c({name:a.name,settings:v()});else{const me={name:p,type:"room_server",settings:v()};j.trim()&&(me.identity_key=j.trim()),Z=await n(me)}D(!1),Z?t():T("Failed to save. Check that the name is unique and passwords differ.")},[x,i,a,v,n,c,t]),$e=o.useCallback(async()=>{if(!i)return;A(!0);const p=await m(a.name);A(!1),p&&t()},[i,a,m,t]),Ke=o.useCallback(async()=>{i&&($(!0),await d(a.name),$(!1))},[i,a,d]);return e.jsxs(ze,{open:s,onClose:t,size:"md",children:[e.jsx(He,{icon:i?void 0:e.jsx(ks,{className:"w-5 h-5"}),title:i?"Edit Room Server":"Add Room Server",onClose:t}),e.jsx(Ge,{children:e.jsxs("div",{className:"space-y-5",children:[i&&e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(V,{color:a.registered?"green":"red",children:a.registered?"Active":"Inactive"}),a.identity_key&&e.jsxs("span",{className:"type-data-xs text-fg-muted truncate",title:a.identity_key,children:["Key: ",a.identity_key.slice(0,16),"…"]})]}),e.jsxs(z,{children:[e.jsx(H,{title:"What people see on the mesh",children:"Room Name"}),e.jsx(q,{value:x,onChange:p=>f(p.target.value),placeholder:"Room Name"})]}),!i&&e.jsxs(z,{children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(H,{title:"The unique hex public ID",children:["Identity Key ",e.jsx("span",{className:"text-fg-muted font-normal",children:"(Optional)"})]}),e.jsx("button",{type:"button",onClick:()=>E(p=>!p),className:"type-data-xs text-fg-muted underline hover:text-fg-secondary",children:g?"Hide":"Show"})]}),g&&e.jsxs(e.Fragment,{children:[e.jsx(q,{value:j,onChange:p=>y(p.target.value),placeholder:"Leave empty to auto-generate"}),e.jsx(Rs,{children:"Leave empty to automatically generate a secure key"})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs(z,{children:[e.jsxs(H,{children:[e.jsx(J,{className:"inline w-3.5 h-3.5 -mt-0.5 mr-1 text-fg-muted"}),"Latitude"]}),e.jsx(q,{type:"number",step:"any",value:N,onChange:p=>M(p.target.value),placeholder:"0"})]}),e.jsxs(z,{children:[e.jsxs(H,{children:[e.jsx(J,{className:"inline w-3.5 h-3.5 -mt-0.5 mr-1 text-fg-muted"}),"Longitude"]}),e.jsx(q,{type:"number",step:"any",value:k,onChange:p=>R(p.target.value),placeholder:"0"})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs(z,{children:[e.jsxs(H,{title:"Full access to room server",children:["Admin Password ",e.jsx("span",{className:"text-fg-muted font-normal",children:"(Optional)"})]}),e.jsx(q,{type:"password",value:C,onChange:p=>S(p.target.value),placeholder:"None Set"})]}),e.jsxs(z,{children:[e.jsxs(H,{title:"Can post and read, but not administer the room",children:["Guest Password ",e.jsx("span",{className:"text-fg-muted font-normal",children:"(Optional)"})]}),e.jsx(q,{type:"password",value:b,onChange:p=>_(p.target.value),placeholder:"None Set"})]})]}),K&&e.jsx("p",{className:"type-data-xs text-sys-red",children:K})]})}),e.jsxs(Ue,{children:[i&&e.jsxs("div",{className:"flex items-center gap-2 mr-auto",children:[e.jsxs(F,{color:"danger",plain:!0,onClick:$e,disabled:P,children:[e.jsx(Ee,{"data-slot":"icon"}),P?"Deleting…":"Delete"]}),e.jsxs(F,{color:"primary",plain:!0,onClick:Ke,disabled:O,children:[e.jsx(Me,{"data-slot":"icon"}),O?"Sending…":"Send Advert"]})]}),e.jsx(F,{plain:!0,onClick:t,children:"Cancel"}),e.jsx(F,{color:"primary",onClick:Ae,disabled:w,children:w?"Saving…":i?"Save Changes":"Create"})]})]})}const Pe=o.createContext(null),Ne="room-tui";function be({children:s}){const[t,l]=o.useState(()=>localStorage.getItem(Ne)!=="0"),a=o.useCallback(()=>{l(r=>{const n=!r;return localStorage.setItem(Ne,n?"1":"0"),n})},[]);return e.jsx(Pe.Provider,{value:{terminalMode:t,toggleTerminalMode:a},children:s})}function X(){const s=o.useContext(Pe);if(!s)throw new Error("useTerminalMode must be used within RoomTerminalProvider");return s}const ke=s=>s.author_pubkey==="server"||s.author_pubkey==="system";function we({messages:s,onDelete:t,terminalMode:l,serverName:a}){const r=o.useRef(null),[n,c]=o.useState(!1),m=o.useMemo(()=>[...s].sort((u,x)=>u.post_timestamp-x.post_timestamp),[s]);o.useEffect(()=>{!n&&r.current&&(r.current.scrollTop=0)},[m.length,n]);const d=o.useCallback(()=>{r.current&&c(r.current.scrollTop<-100)},[]),i=o.useCallback(()=>{r.current&&r.current.scrollTo({top:0,behavior:"smooth"}),c(!1)},[]);return m.length===0?e.jsx("div",{className:`flex-1 flex flex-col items-center justify-center py-12 min-h-0${l?" font-mono":""}`,children:l?e.jsx("p",{className:"text-sm font-mono",style:{color:"var(--tui-accent)",opacity:.4},children:"> no messages in buffer_"}):e.jsxs(e.Fragment,{children:[e.jsx(Y,{className:"w-10 h-10 text-fg-muted opacity-20 mb-2"}),e.jsx("p",{className:"type-data-sm text-fg-muted",children:"No messages yet"})]})}):l?e.jsxs("div",{className:"relative flex-1 min-h-0",children:[e.jsx("div",{ref:r,onScroll:d,className:"h-full overflow-y-auto scroll-smooth p-4 font-mono text-sm leading-relaxed flex flex-col-reverse",children:e.jsx("div",{children:m.map(u=>{const x=ke(u),f=new Date(u.post_timestamp*1e3).toLocaleTimeString(void 0,{hour:"2-digit",minute:"2-digit",second:"2-digit",hour12:!1}),j=x?"SYS":u.author_name||u.author_prefix,y=!x&&a&&j===a;return e.jsxs("div",{className:"group flex items-start gap-0 -mx-4 px-4 py-px",style:{"--tw-hover-bg":"var(--tui-hover-bg)"},onMouseEnter:g=>g.currentTarget.style.background="var(--tui-hover-bg)",onMouseLeave:g=>g.currentTarget.style.background="",children:[e.jsxs("span",{className:"select-none shrink-0",style:{color:"var(--tui-muted)"},children:["[",f,"]"]}),e.jsxs("span",{className:"mx-1.5 shrink-0",style:{color:y?"var(--tui-accent-local)":"var(--tui-accent)"},children:["<",j,">"]}),e.jsx("span",{style:{color:x?"var(--tui-system)":y?"var(--tui-body)":"var(--tui-accent)"},className:x?"italic":"",children:u.message_text}),e.jsx("button",{onClick:()=>t(u.id),className:"opacity-0 group-hover:opacity-100 ml-auto pl-2 shrink-0 transition-colors",style:{color:"var(--tui-muted)"},onMouseEnter:g=>g.currentTarget.style.color="var(--tui-danger)",onMouseLeave:g=>g.currentTarget.style.color="var(--tui-muted)",title:"Delete",children:"×"})]},u.id)})})}),n&&e.jsxs("button",{onClick:i,className:"absolute bottom-3 left-1/2 -translate-x-1/2 flex items-center gap-1.5 px-3 py-1 font-mono text-xs transition-colors duration-150",style:{background:"var(--tui-hover-bg)",border:"1px solid var(--tui-muted)",color:"var(--tui-accent)"},children:[e.jsx(ue,{className:"w-3 h-3"}),"↓ scroll"]})]}):e.jsxs("div",{className:"relative flex-1 min-h-0",children:[e.jsx("div",{ref:r,onScroll:d,className:"h-full overflow-y-auto scroll-smooth flex flex-col-reverse",children:e.jsx("div",{className:"space-y-3 p-4",children:m.map(u=>ke(u)?e.jsx(ps,{text:u.message_text},u.id):e.jsx(hs,{senderName:u.author_name||u.author_prefix,text:u.message_text,timestamp:u.post_timestamp,bubbleAccessory:e.jsx("button",{onClick:()=>t(u.id),className:"opacity-0 group-hover:opacity-100 transition-opacity duration-150 p-1 text-fg-muted hover:text-sys-red flex-shrink-0",title:"Delete message",children:e.jsx(Ee,{className:"w-3.5 h-3.5"})})},u.id))})}),n&&e.jsxs("button",{onClick:i,className:"absolute bottom-3 left-1/2 -translate-x-1/2 flex items-center gap-1.5 px-3 py-1.5 rounded-full bg-surface/90 backdrop-blur-sm ring-1 ring-edge-subtle shadow-md text-fg-secondary hover:text-fg-primary transition-colors duration-150",children:[e.jsx(ue,{className:"w-3.5 h-3.5"}),e.jsx("span",{className:"type-data-xs",children:"New messages"})]})]})}function Ce({roomName:s,onSend:t,disabled:l,terminalMode:a}){const[r,n]=o.useState(""),[c,m]=o.useState(!1),d=o.useRef(null),i=o.useCallback(async()=>{var j;const x=r.trim();if(!x||c)return;m(!0);const f=await t(x);m(!1),f&&(n(""),(j=d.current)==null||j.focus())},[r,c,t]),u=o.useCallback(x=>{x.key==="Enter"&&!x.shiftKey&&(x.preventDefault(),i())},[i]);return a?e.jsxs("div",{className:"flex items-center gap-0 px-4 py-2 type-data-sm",children:[e.jsx("input",{ref:d,type:"text",value:r,onChange:x=>n(x.target.value),onKeyDown:u,placeholder:"type message...",disabled:l||c,className:"flex-1 bg-transparent outline-none ring-0 shadow-none border-none focus:outline-none focus:ring-0 font-mono tui-input",style:{color:"var(--tui-body)"}}),e.jsx("button",{onClick:i,disabled:!r.trim()||c||l,className:"transition-colors duration-100 px-1 font-mono text-xs disabled:opacity-30",style:{color:"var(--tui-accent)"},title:"Send",children:"[↵]"})]}):e.jsxs("div",{className:"flex items-center gap-2 p-3 border-t border-edge-subtle",children:[e.jsx("input",{ref:d,type:"text",value:r,onChange:x=>n(x.target.value),onKeyDown:u,placeholder:`Message ${s}…`,disabled:l||c,className:"flex-1 surface-input radius-inner px-3 py-2 type-body-sm text-fg-primary placeholder:text-fg-muted outline-none ring-focus"}),e.jsx("button",{onClick:i,disabled:!r.trim()||c||l,className:"p-2 rounded-lg text-sys-blue hover:bg-sys-blue/10 disabled:opacity-30 disabled:hover:bg-transparent transition-colors duration-150",title:"Send message",children:e.jsx(Me,{className:"w-4.5 h-4.5"})})]})}function Ps({selectedName:s,selectedIdentity:t,messages:l,onSend:a,onDelete:r,onClear:n}){var f;const{terminalMode:c,toggleTerminalMode:m}=X(),[d,i]=o.useState(!1),u=((f=t==null?void 0:t.settings)==null?void 0:f.node_name)??s??void 0,x=o.useCallback(()=>{i(!1),n()},[n]);return c?e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"flex flex-col h-full gap-1.5",children:[e.jsxs("div",{className:"card-terminal-header flex-shrink-0",children:[e.jsx("span",{className:"seven-seg-panel",children:e.jsx(Q,{text:"chat",minChars:7,size:24})}),e.jsx("div",{className:"card-terminal-ridge flex-1"}),e.jsxs("div",{className:"header-well",children:[e.jsx(I,{icon:e.jsx(je,{className:"!w-5.5 !h-5.5 translate-y-px"}),onClick:m,title:"Exit terminal mode"}),l.length>0&&e.jsx(I,{icon:e.jsx("span",{className:"text-[13px] font-bold font-mono leading-none translate-y-px",children:"DEL"}),onClick:()=>i(!0),title:"Clear all messages",variant:"red",iconActiveColor:Ve.red})]})]}),e.jsx(G,{noPadding:!0,className:"flex flex-col flex-1 min-h-0 overflow-hidden card-terminal",children:e.jsxs("div",{className:"card-terminal-well flex-1 min-h-0 flex flex-col overflow-hidden",children:[e.jsx(we,{messages:l,onDelete:r,terminalMode:!0,serverName:u}),e.jsx("div",{className:"card-terminal-divider"}),s&&e.jsx(Ce,{roomName:s,onSend:a,disabled:!(t!=null&&t.registered),terminalMode:!0})]})})]}),e.jsx(fe,{isOpen:d,title:"Clear All Messages",message:`This will permanently delete all ${l.length} message${l.length===1?"":"s"} from this room. This cannot be undone.`,confirmLabel:"Clear Messages",variant:"danger",onConfirm:x,onCancel:()=>i(!1)})]}):e.jsxs(e.Fragment,{children:[e.jsxs(G,{noPadding:!0,className:"flex flex-col h-full overflow-hidden",children:[e.jsx(se,{listHeader:!0,icon:e.jsx(De,{className:"icon-sm"}),title:"Messages",actions:e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(F,{plain:!0,onClick:m,title:"Terminal mode",children:e.jsx(je,{"data-slot":"icon",className:"!w-5 !h-5 translate-y-px"})}),l.length>0&&e.jsx(F,{plain:!0,onClick:()=>i(!0),title:"Clear all messages",children:e.jsx("span",{"data-slot":"icon",className:"text-[12px] font-bold font-mono leading-none translate-y-px text-sys-red",children:"DEL"})})]})}),e.jsx(we,{messages:l,onDelete:r,terminalMode:!1,serverName:u}),s&&e.jsx(Ce,{roomName:s,onSend:a,disabled:!(t!=null&&t.registered),terminalMode:!1})]}),e.jsx(fe,{isOpen:d,title:"Clear All Messages",message:`This will permanently delete all ${l.length} message${l.length===1?"":"s"} from this room. This cannot be undone.`,confirmLabel:"Clear Messages",variant:"danger",onConfirm:x,onCancel:()=>i(!1)})]})}function As({pubkey:s,className:t}){const[l,a]=o.useState(!1),r=o.useCallback(async()=>{await Re(s),a(!0),setTimeout(()=>a(!1),1500)},[s]);return e.jsxs("button",{onClick:r,title:l?"Copied!":"Copy public key",className:`inline-flex items-center gap-1 type-data-xs leading-tight text-fg-muted hover:text-fg-secondary transition-colors duration-150 cursor-copy ${t??""}`,children:[e.jsx("span",{className:"truncate min-w-0",children:s.slice(0,8)}),l&&e.jsx(W,{className:"w-2.5 h-2.5 text-sys-green flex-shrink-0"})]})}const $s=o.memo(function({client:t,resolvedName:l,onRemove:a}){const r=t.name||l,n=t.unsynced_count===0,c=t.last_activity?new Date(t.last_activity*1e3).toLocaleString(void 0,{month:"short",day:"numeric",hour:"2-digit",minute:"2-digit"}):"Never",{emoji:m,cleanName:d}=r?fs(r):{emoji:null,cleanName:t.pubkey_prefix},i=r?js(d):void 0,u=r?vs(r):t.pubkey_prefix.slice(0,2).toUpperCase();return e.jsxs("div",{className:"group flex items-center gap-3 px-3 py-2.5 radius-inner row-hover transition-base",children:[e.jsxs("div",{className:"relative flex-shrink-0",children:[e.jsx("div",{className:L("w-8 h-8 rounded-full flex items-center justify-center",!i&&"bg-sys-blue/12"),style:i?{backgroundColor:i}:void 0,children:m?e.jsx("span",{className:"text-base leading-none",children:m}):e.jsx("span",{className:L("leading-none",i?"text-white text-[11px] font-bold tracking-tight":"text-sys-blue font-mono text-[11px]"),children:u})}),e.jsx("span",{className:`absolute -bottom-0.5 -right-0.5 w-2.5 h-2.5 rounded-sm ring-2 ring-surface ${n?"bg-sys-green":"bg-sys-indigo"}`})]}),e.jsxs("div",{className:"flex-1 min-w-0",children:[r?e.jsx("p",{className:"text-[13px] font-medium leading-tight text-fg-primary truncate",children:r}):e.jsx("p",{className:"font-mono text-[13px] leading-tight text-fg-primary truncate",children:t.pubkey_prefix}),e.jsxs("div",{className:"flex items-center gap-1.5 mt-0.5",children:[r&&e.jsx(As,{pubkey:t.pubkey}),r&&e.jsx("span",{className:"text-xs text-fg-muted/40",children:"·"}),e.jsxs("span",{className:"text-xs leading-tight text-fg-muted truncate",children:[!n&&e.jsxs("span",{className:"text-sys-indigo",children:[t.unsynced_count," unsynced · "]}),c]})]})]}),e.jsx("button",{onClick:()=>a(t.pubkey),className:"opacity-0 group-hover:opacity-100 transition-opacity duration-150 p-1.5 -mr-1 rounded-md text-fg-muted hover:text-sys-red hover:bg-sys-red/8",title:"Remove client",children:e.jsx(Ms,{className:"w-3.5 h-3.5"})})]})});function Se({clients:s,onRemove:t,messages:l,terminalMode:a}){const r=o.useMemo(()=>{const n=new Map;if(!l)return n;for(let c=l.length-1;c>=0;c--){const m=l[c];m.author_name&&m.author_pubkey&&m.author_pubkey!=="server"&&m.author_pubkey!=="system"&&n.set(m.author_pubkey,m.author_name)}return n},[l]);return s.length===0?a?e.jsx("p",{className:"type-data-sm text-left px-4 py-4",style:{color:"var(--tui-accent)"},children:"< NO CLIENTS CONNECTED >"}):e.jsx("p",{className:"type-data-xs text-fg-muted text-center py-4",children:"No clients connected"}):a?e.jsx("div",{className:"type-data-sm pt-2",children:s.map(n=>{const c=n.name||r.get(n.pubkey),m=n.unsynced_count===0,d=n.pubkey_prefix;return e.jsxs("div",{className:"group flex items-center gap-0 -mx-0 px-4 py-0.5",onMouseEnter:i=>i.currentTarget.style.background="var(--tui-hover-bg)",onMouseLeave:i=>i.currentTarget.style.background="",children:[e.jsx("span",{style:{color:m?"var(--tui-accent)":"var(--tui-warn)"},children:"■"}),e.jsx("span",{className:"ml-2 shrink-0",style:{color:"var(--tui-accent)"},children:c||d}),c&&e.jsxs("span",{className:"ml-1.5 shrink-0",style:{color:"var(--tui-accent)"},children:["[",d,"]"]}),!m&&e.jsxs("span",{className:"ml-1.5",style:{color:"var(--tui-warn)"},children:[n.unsynced_count,"unsync"]}),e.jsx("button",{onClick:()=>t(n.pubkey),className:"opacity-0 group-hover:opacity-100 ml-auto pl-2 shrink-0 transition-colors",style:{color:"var(--tui-muted)"},onMouseEnter:i=>i.currentTarget.style.color="var(--tui-danger)",onMouseLeave:i=>i.currentTarget.style.color="var(--tui-muted)",title:"Remove",children:"×"})]},n.pubkey)})}):e.jsx("div",{className:"space-y-0.5 pt-1",children:s.map(n=>e.jsx($s,{client:n,resolvedName:r.get(n.pubkey),onRemove:t},n.pubkey))})}function Ks({name:s,onSend:t}){const{terminalMode:l}=X(),[a,r]=o.useState("idle"),n=o.useRef(),c=o.useCallback(async()=>{if(a==="sending")return;r("sending");const m=await t(s);r(m?"sent":"idle"),m&&(n.current=setTimeout(()=>r("idle"),2e3))},[s,t,a]);return o.useEffect(()=>()=>clearTimeout(n.current),[]),l?e.jsxs("button",{className:"btn-terminal",onClick:c,disabled:a==="sending",children:[a==="idle"&&"[ send_advert ]",a==="sending"&&"[ broadcasting... ]",a==="sent"&&"[ sent ✓ ]"]}):e.jsxs(F,{color:a==="sent"?"success":"primary",className:"w-full",onClick:c,disabled:a==="sending",children:[e.jsx(ee,{"data-slot":"icon"}),a==="idle"&&"Send Advert",a==="sending"&&"Broadcasting…",a==="sent"&&"Advert Sent"]})}function _e({identity:s,onSendAdvert:t}){var c,m,d,i,u,x,f,j,y,g,E,C,S,b,_,N,M,k,R,w,D,P,A,O,$,K,T,B,h,v;const{terminalMode:l}=X(),[a,r]=o.useState(!1),n=o.useCallback(async()=>{s!=null&&s.identity_key&&(await Re(s.identity_key),r(!0),setTimeout(()=>r(!1),1500))},[s==null?void 0:s.identity_key]);return l?s?e.jsxs("div",{className:"space-y-1",children:[((c=s.settings)==null?void 0:c.node_name)&&e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("span",{style:{color:"var(--tui-muted)"},children:"node"}),e.jsx("span",{style:{color:"var(--tui-accent)"},children:s.settings.node_name})]}),(s.address||((m=s.runtime)==null?void 0:m.address))&&e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("span",{style:{color:"var(--tui-muted)"},children:"addr"}),e.jsx("span",{style:{color:"var(--tui-accent)"},children:s.address||((d=s.runtime)==null?void 0:d.address)})]}),s.identity_key&&e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("span",{style:{color:"var(--tui-muted)"},children:"key"}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs("span",{className:"truncate max-w-[140px]",style:{color:"var(--tui-accent)"},title:s.identity_key,children:[s.identity_key.slice(0,16),"…"]}),e.jsx("button",{type:"button",onClick:n,className:"text-xs",style:{color:"var(--tui-muted)"},children:a?"[copied ✓]":"[copy]"})]})]}),(((i=s.settings)==null?void 0:i.latitude)!=null||((u=s.settings)==null?void 0:u.longitude)!=null)&&e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("span",{style:{color:"var(--tui-muted)"},children:"loc"}),e.jsxs("span",{style:{color:"var(--tui-accent)"},title:`${((x=s.settings)==null?void 0:x.latitude)??"—"}, ${((f=s.settings)==null?void 0:f.longitude)??"—"}`,children:[((y=(j=s.settings)==null?void 0:j.latitude)==null?void 0:y.toFixed(4))??"—",", ",((E=(g=s.settings)==null?void 0:g.longitude)==null?void 0:E.toFixed(4))??"—"]})]}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("span",{style:{color:"var(--tui-muted)"},children:"pass"}),e.jsxs("span",{className:"flex items-center gap-1.5",children:[e.jsx("span",{style:{color:(C=s.settings)!=null&&C.admin_password?"var(--tui-accent)":"var(--tui-muted)"},children:(S=s.settings)!=null&&S.admin_password?"[✓ admin]":"[— admin]"}),e.jsx("span",{style:{color:(b=s.settings)!=null&&b.guest_password?"var(--tui-accent)":"var(--tui-muted)"},children:(_=s.settings)!=null&&_.guest_password?"[✓ guest]":"[— guest]"})]})]})]}):e.jsx("span",{style:{color:"var(--tui-muted)"},children:"no identity configured"}):e.jsxs("div",{className:"flex-shrink-0 border-t border-edge-subtle",children:[e.jsx(se,{listHeader:!0,icon:e.jsx(gs,{className:"icon-sm"}),title:"Room Info"}),s?e.jsxs("div",{className:"space-y-2 px-4 py-3",children:[((N=s.settings)==null?void 0:N.node_name)&&e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("span",{className:"type-data-xs text-fg-muted",children:"Node"}),e.jsx("span",{className:"type-data-sm text-fg-primary",children:s.settings.node_name})]}),(s.address||((M=s.runtime)==null?void 0:M.address))&&e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("span",{className:"type-data-xs text-fg-muted",children:"Address"}),e.jsx("span",{className:"type-data-sm font-mono text-fg-primary",children:s.address||((k=s.runtime)==null?void 0:k.address)})]}),s.identity_key&&e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("span",{className:"type-data-xs text-fg-muted flex items-center gap-1",title:"The unique hex public ID",children:[e.jsx(ys,{className:"w-3 h-3"}),"Key"]}),e.jsxs("div",{className:"flex items-center gap-1.5",children:[e.jsxs("span",{className:"type-data-sm font-mono text-fg-primary truncate max-w-[140px]",title:s.identity_key,children:[s.identity_key.slice(0,16),"…"]}),e.jsx("button",{type:"button",onClick:n,className:"inline-flex items-center gap-0.5",children:e.jsx(V,{color:a?"green":"zinc",children:a?e.jsxs(e.Fragment,{children:[e.jsx(W,{className:"w-3 h-3"}),"Copied"]}):e.jsxs(e.Fragment,{children:[e.jsx(We,{className:"w-3 h-3"}),"Copy"]})})})]})]}),(((R=s.settings)==null?void 0:R.latitude)!=null||((w=s.settings)==null?void 0:w.longitude)!=null)&&e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("span",{className:"type-data-xs text-fg-muted flex items-center gap-1",children:[e.jsx(J,{className:"w-3 h-3"}),"Location"]}),e.jsxs("span",{className:"type-data-sm font-mono text-fg-primary",title:`${((D=s.settings)==null?void 0:D.latitude)??"—"}, ${((P=s.settings)==null?void 0:P.longitude)??"—"}`,children:[((O=(A=s.settings)==null?void 0:A.latitude)==null?void 0:O.toFixed(4))??"—",", ",((K=($=s.settings)==null?void 0:$.longitude)==null?void 0:K.toFixed(4))??"—"]})]}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("span",{className:"type-data-xs text-fg-muted",children:"Passwords"}),e.jsxs("div",{className:"flex items-center gap-1.5",children:[e.jsxs(V,{color:(T=s.settings)!=null&&T.admin_password?"green":"zinc",children:[((B=s.settings)==null?void 0:B.admin_password)&&e.jsx(W,{className:"w-3 h-3"}),"Admin"]}),e.jsxs(V,{color:(h=s.settings)!=null&&h.guest_password?"green":"zinc",children:[((v=s.settings)==null?void 0:v.guest_password)&&e.jsx(W,{className:"w-3 h-3"}),"Guest"]})]})]}),e.jsx("div",{className:"pt-3",children:e.jsx(Ks,{name:s.name,onSend:t})})]}):e.jsx("p",{className:"type-data-xs text-fg-muted text-center py-3",children:"No identity configured"})]})}function Fs({selectedIdentity:s,clients:t,messages:l,onSendAdvert:a}){const{terminalMode:r}=X(),n=o.useCallback(async c=>{Te.setState(m=>({clients:m.clients.filter(d=>d.pubkey!==c)})),await Ye({public_key:c})},[]);return r?e.jsxs("div",{className:"flex flex-col h-full gap-1.5",children:[s&&e.jsx("div",{className:"hidden lg:block",children:e.jsx("div",{className:"card-terminal-header flex-shrink-0",children:e.jsx("div",{className:"header-well flex-1",children:e.jsx(I,{icon:e.jsx(ee,{}),onPress:()=>a(s.name),title:"Send room server advert",indicators:[{label:"ADVERTISE",trackPress:!0},{label:"ACTIVE",active:s.registered}]})})})}),e.jsxs("div",{className:"flex flex-col flex-1 min-h-0 gap-1.5",children:[e.jsx("div",{className:"card-terminal-header flex-shrink-0",children:e.jsx("span",{className:"seven-seg-panel",children:e.jsx(Q,{text:`OnLInE ${String(t.length).padStart(3,"0")}`,size:24})})}),e.jsx(G,{noPadding:!0,className:"!h-auto flex flex-col flex-1 min-h-0 overflow-hidden card-terminal",children:e.jsx("div",{className:"card-terminal-well flex-1 min-h-0 overflow-y-auto",children:e.jsx(Se,{clients:t,onRemove:n,messages:l,terminalMode:!0})})}),e.jsxs("div",{className:"card-terminal-header flex-shrink-0",children:[e.jsx("span",{className:"seven-seg-panel",children:e.jsx(Q,{text:`StAtUS ${s!=null&&s.registered?"On":"OFF"}`,size:24})}),s&&e.jsx("div",{className:"lg:hidden flex-1 flex min-w-0",children:e.jsx("div",{className:"header-well flex-1 !flex-shrink",children:e.jsx(I,{icon:e.jsx(ee,{}),onPress:()=>a(s.name),title:"Send room server advert",className:"flex-row-reverse",indicators:[{label:"ACTIVE",active:s.registered},{label:"ADVERTISE",trackPress:!0}]})})})]}),e.jsx(G,{noPadding:!0,className:"!h-auto flex-shrink-0 overflow-hidden card-terminal",children:e.jsx("div",{className:"card-terminal-well px-4 py-3 type-data-sm",children:e.jsx(_e,{identity:s,onSendAdvert:a})})})]})]}):e.jsxs(G,{compact:!0,className:"flex-1 min-h-0 flex flex-col overflow-hidden",children:[e.jsx(se,{listHeader:!0,icon:e.jsx(Le,{className:"icon-sm"}),title:"Clients",actions:t.length>0?e.jsx("span",{className:"type-data-xs text-fg-muted",children:t.length}):void 0}),e.jsx("div",{className:"flex-1 min-h-0 overflow-y-auto",children:e.jsx(Se,{clients:t,onRemove:n,messages:l,terminalMode:!1})}),e.jsx(_e,{identity:s,onSendAdvert:a})]})}function tt(){const s=Ie(),t=Xe(),l=Ze(),a=Je(),r=Qe(),n=es(),c=ss(),m=ts(),d=as(),i=ns(),u=o.useMemo(()=>(d==null?void 0:d.neighbors)??{},[d==null?void 0:d.neighbors]),x=Ts(u,i),f=o.useMemo(()=>n.map(h=>{if(h.name)return h;const v=x(h.pubkey);return v.name?{...h,name:v.name}:h}),[n,x]),j=o.useMemo(()=>r.map(h=>{if(h.author_name||h.author_pubkey==="server"||h.author_pubkey==="system")return h;const v=x(h.author_pubkey);return v.name?{...h,author_name:v.name}:h}),[r,x]),{selectRoom:y,postMessage:g,deleteMessage:E,clearMessages:C,createRoom:S,updateRoom:b,deleteRoom:_,sendAdvert:N,markAsRead:M,startActivePolling:k}=Te.getState(),[R,w]=o.useState(!1),[D,P]=o.useState("create"),[A,O]=o.useState("hybrid");o.useEffect(()=>{M()},[t,M]),o.useEffect(()=>k(),[k]);const $=o.useCallback(async h=>t?g({room_name:t,message:h,author_pubkey:"server"}):!1,[t,g]),K=o.useCallback((h="hybrid")=>{O(h),P("create"),w(!0)},[]),T=o.useCallback(()=>{P("edit"),w(!0)},[]),B=e.jsxs("div",{className:"flex items-center gap-2 sm:gap-4 flex-shrink-0",children:[l&&e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs("span",{className:"data-box flex items-center gap-1.5 cursor-default",title:`${l.total_messages} message${l.total_messages!==1?"s":""} in this room`,children:[e.jsx(De,{className:"w-3.5 h-3.5 text-fg-secondary"}),e.jsx("span",{children:l.total_messages})]}),e.jsxs("span",{className:"data-box flex items-center gap-1.5 cursor-default",title:`${l.total_clients} client${l.total_clients!==1?"s":""} connected`,children:[e.jsx(Le,{className:"w-3.5 h-3.5 text-fg-secondary"}),e.jsx("span",{children:l.total_clients})]})]}),e.jsx(Es,{rooms:s,selected:t,onSelect:y,onAdd:()=>K("hybrid")}),s.length>0&&e.jsx(F,{plain:!0,onClick:T,title:"Manage room server",children:e.jsx(rs,{"data-slot":"icon"})})]});return s.length===0&&c.length===0&&!m?e.jsx(be,{children:e.jsxs(pe,{children:[e.jsx(he,{title:"Room Server",icon:e.jsx(Y,{})}),e.jsx(Ls,{onCreateRoom:h=>K(h)}),e.jsx(ye,{open:R,onClose:()=>w(!1),mode:"create",repeaterConfig:d==null?void 0:d.config,onCreate:async h=>{const v=await S(h);return v&&A==="dedicated"&&(await xe.getState().setMode("monitor"),xe.getState().clearModeMutation()),v},onUpdate:b,onDelete:_,onSendAdvert:N})]})}):e.jsx(be,{children:e.jsxs(pe,{children:[e.jsx(he,{title:"Room Server",icon:e.jsx(Y,{}),controls:B}),e.jsx(ls,{children:e.jsxs(os,{template:"hero-tall",className:"!h-[calc(100vh-11rem)] !min-h-[400px]",children:[e.jsx(ge,{span:12,lg:8,className:"h-full",children:e.jsx(Ps,{selectedName:t,selectedIdentity:a,messages:j,onSend:$,onDelete:E,onClear:C})}),e.jsx(ge,{span:12,lg:4,className:"h-full",children:e.jsx(Fs,{selectedIdentity:a,clients:f,messages:j,onSendAdvert:N})})]})}),e.jsx(ye,{open:R,onClose:()=>w(!1),mode:D,identity:a,repeaterConfig:d==null?void 0:d.config,onCreate:S,onUpdate:b,onDelete:_,onSendAdvert:N})]})})}export{tt as default};
