import{e as se,r as l,j as e,cw as G,R as De,c as A,ay as Ae,aH as $e,az as Ke,$ as U,I as P,eP as Fe,B as b,cc as we,aB as ie,V as Oe,f as te,a7 as Ce,a4 as W,a5 as Be,eQ as Se,eR as ze,bF as _e,eS as qe,eT as He,eU as Ve,eV as Ue,eW as We,eX as Ge,eY as Ye,eZ as Ie,aa as Xe,W as Qe,eK as Ze,cy as ce}from"./index-C4inJ1rU.js";import{C as q,P as de,a as me,B as Je}from"./PageLayout-CdGXajgM.js";import{R as es,C as ue}from"./Grid-rJkrpDAP.js";import{c as ss,m as ts}from"./node-types-chTBCezS.js";import{R as as}from"./repeat-CyMKCb2G.js";import{L as ns,a as rs,b as ls}from"./listbox-C-kqERsa.js";import{P as os}from"./plus-BvSKm1y6.js";import{F as E,L as D,D as is}from"./fieldset-DUGQoGas.js";import{M as Z}from"./map-pin-DlTTkXK_.js";import{T as Re}from"./trash-2-1fU70LRT.js";import{S as J,K as Y}from"./KeycapButton-DSV3CIRm.js";import{C as xe}from"./ConfirmModal-Dr1cpMpw.js";import{S as cs,C as ds}from"./ChatBubble-CKqMOZK4.js";import{T as pe}from"./terminal-DBN8OPLd.js";import{e as ms,g as us,a as xs}from"./chat-utils-C6kvX8nY.js";import{W as ee}from"./wifi-CrCnex2M.js";import{M as ps}from"./message-square-text-oWvumC9a.js";import{K as hs}from"./key-round-Dx44ZbzS.js";import"./element-movement-Dl45gerR.js";import"./floating-4TW7J4UF.js";import"./use-text-value-BO4BQQts.js";import"./frozen-BB9d2dXD.js";import"./keycap-sfx-ByZp-njj.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 fs=[["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"}]],Me=se("message-circle",fs);/**
 * @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 gs=[["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"}]],js=se("message-square-plus",gs);/**
 * @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 ys=[["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"}]],vs=se("user-x",ys);function Ns(s,t){return l.useMemo(()=>{const a=new Map;for(const[n,c]of Object.entries(s)){const m=c.name||c.node_name||null,{type:d}=ss(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,o=c._advertNodeType!=null?ts(c._advertNodeType):"companion";r.set(m,{name:d,type:o})}return function(c){const m=c.toLowerCase(),d=a.get(m);if(d!=null&&d.name)return d;const o=r.get(m);return o!=null&&o.name?o:d||o||{name:null,type:"companion"}}},[s,t])}function he({icon:s,title:t,description:i,onClick:a,accent:r}){return e.jsxs("button",{onClick:a,className:A("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:A("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:i})]})}function bs({onCreateRoom:s}){return e.jsxs("div",{className:"flex flex-col items-center justify-center py-16 px-4",children:[e.jsx(G,{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(he,{icon:e.jsx(as,{className:"w-6 h-6"}),title:"Repeater + Room Server",description:"Keep forwarding packets and add group messaging.",onClick:()=>s("hybrid"),accent:"primary"}),e.jsx(he,{icon:e.jsx(De,{className:"w-6 h-6"}),title:"Dedicated Room Server",description:"Stop repeating. Focus entirely on room server duties.",onClick:()=>s("dedicated"),accent:"secondary"})]})]})}function ks({rooms:s,selected:t,onSelect:i,onAdd:a}){const r=a?e.jsx("button",{onClick:a,className:A("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(os,{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:()=>i(n.room_name),className:A("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(ns,{value:t??"",onChange:i,children:s.map(n=>e.jsx(rs,{value:n.room_name,children:e.jsx(ls,{children:n.room_name})},n.room_name))}),r]})}function ws(s){return s.settings??{}}function Cs(s){if(!s)return"";const t=s.trim().toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-|-$/g,"");return t?`${t}-room`:""}function fe({open:s,onClose:t,mode:i,identity:a,repeaterConfig:r,onCreate:n,onUpdate:c,onDelete:m,onSendAdvert:d}){var ae,ne,re,le;const o=i==="edit"&&a,x=`${s}-${(a==null?void 0:a.name)??""}-${i}`,[p,h]=l.useState(""),[g,j]=l.useState(""),[y,$]=l.useState(!1),[S,_]=l.useState(""),[k,R]=l.useState(""),[N,M]=l.useState(""),[w,T]=l.useState(""),[C,K]=l.useState(!1),[F,O]=l.useState(!1),[H,B]=l.useState(!1),[z,L]=l.useState(null),[X,f]=l.useState(x);if(x!==X)if(f(x),L(null),K(!1),O(!1),B(!1),o){const u=ws(a);h(u.node_name??a.name),j(a.identity_key??""),$(!1),_(u.admin_password??""),R(u.guest_password??""),M(((ae=u.latitude)==null?void 0:ae.toString())??""),T(((ne=u.longitude)==null?void 0:ne.toString())??"")}else{const u=r;h(""),j(""),$(!1),_(""),R(""),M(((re=u==null?void 0:u.repeater)==null?void 0:re.latitude)!=null?u.repeater.latitude.toString():""),T(((le=u==null?void 0:u.repeater)==null?void 0:le.longitude)!=null?u.repeater.longitude.toString():"")}const v=l.useCallback(()=>{const u={};return p&&(u.node_name=p),S&&(u.admin_password=S),k&&(u.guest_password=k),N!==""&&(u.latitude=parseFloat(N)),w!==""&&(u.longitude=parseFloat(w)),u},[p,S,k,N,w]),Le=l.useCallback(async()=>{if(!p.trim()){L("Room name is required");return}K(!0),L(null);const u=o?a.name:Cs(p);let Q;if(o)Q=await c({name:a.name,settings:v()});else{const oe={name:u,type:"room_server",settings:v()};g.trim()&&(oe.identity_key=g.trim()),Q=await n(oe)}K(!1),Q?t():L("Failed to save. Check that the name is unique and passwords differ.")},[p,o,a,v,n,c,t]),Pe=l.useCallback(async()=>{if(!o)return;O(!0);const u=await m(a.name);O(!1),u&&t()},[o,a,m,t]),Ee=l.useCallback(async()=>{o&&(B(!0),await d(a.name),B(!1))},[o,a,d]);return e.jsxs(Ae,{open:s,onClose:t,size:"md",children:[e.jsx($e,{icon:o?void 0:e.jsx(js,{className:"w-5 h-5"}),title:o?"Edit Room Server":"Add Room Server",onClose:t}),e.jsx(Ke,{children:e.jsxs("div",{className:"space-y-5",children:[o&&e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(U,{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(E,{children:[e.jsx(D,{title:"What people see on the mesh",children:"Room Name"}),e.jsx(P,{value:p,onChange:u=>h(u.target.value),placeholder:"Room Name"})]}),!o&&e.jsxs(E,{children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(D,{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:()=>$(u=>!u),className:"type-data-xs text-fg-muted underline hover:text-fg-secondary",children:y?"Hide":"Show"})]}),y&&e.jsxs(e.Fragment,{children:[e.jsx(P,{value:g,onChange:u=>j(u.target.value),placeholder:"Leave empty to auto-generate"}),e.jsx(is,{children:"Leave empty to automatically generate a secure key"})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs(E,{children:[e.jsxs(D,{children:[e.jsx(Z,{className:"inline w-3.5 h-3.5 -mt-0.5 mr-1 text-fg-muted"}),"Latitude"]}),e.jsx(P,{type:"number",step:"any",value:N,onChange:u=>M(u.target.value),placeholder:"0"})]}),e.jsxs(E,{children:[e.jsxs(D,{children:[e.jsx(Z,{className:"inline w-3.5 h-3.5 -mt-0.5 mr-1 text-fg-muted"}),"Longitude"]}),e.jsx(P,{type:"number",step:"any",value:w,onChange:u=>T(u.target.value),placeholder:"0"})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs(E,{children:[e.jsxs(D,{title:"Full access to room server",children:["Admin Password ",e.jsx("span",{className:"text-fg-muted font-normal",children:"(Optional)"})]}),e.jsx(P,{type:"password",value:S,onChange:u=>_(u.target.value),placeholder:"None Set"})]}),e.jsxs(E,{children:[e.jsxs(D,{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(P,{type:"password",value:k,onChange:u=>R(u.target.value),placeholder:"None Set"})]})]}),z&&e.jsx("p",{className:"type-data-xs text-sys-red",children:z})]})}),e.jsxs(Fe,{children:[o&&e.jsxs("div",{className:"flex items-center gap-2 mr-auto",children:[e.jsxs(b,{color:"danger",plain:!0,onClick:Pe,disabled:F,children:[e.jsx(Re,{"data-slot":"icon"}),F?"Deleting…":"Delete"]}),e.jsxs(b,{color:"primary",plain:!0,onClick:Ee,disabled:H,children:[e.jsx(we,{"data-slot":"icon"}),H?"Sending…":"Send Advert"]})]}),e.jsx(b,{plain:!0,onClick:t,children:"Cancel"}),e.jsx(b,{color:"primary",onClick:Le,disabled:C,children:C?"Saving…":o?"Save Changes":"Create"})]})]})}const Te=l.createContext(null),ge="room-tui";function je({children:s}){const[t,i]=l.useState(()=>localStorage.getItem(ge)!=="0"),a=l.useCallback(()=>{i(r=>{const n=!r;return localStorage.setItem(ge,n?"1":"0"),n})},[]);return e.jsx(Te.Provider,{value:{terminalMode:t,toggleTerminalMode:a},children:s})}function I(){const s=l.useContext(Te);if(!s)throw new Error("useTerminalMode must be used within RoomTerminalProvider");return s}const ye=s=>s.author_pubkey==="server"||s.author_pubkey==="system";function ve({messages:s,onDelete:t,terminalMode:i,serverName:a}){const r=l.useRef(null),[n,c]=l.useState(!1),m=l.useMemo(()=>[...s].sort((x,p)=>x.post_timestamp-p.post_timestamp),[s]);l.useEffect(()=>{!n&&r.current&&(r.current.scrollTop=0)},[m.length,n]);const d=l.useCallback(()=>{r.current&&c(r.current.scrollTop<-100)},[]),o=l.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${i?" font-mono":""}`,children:i?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(G,{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"})]})}):i?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(x=>{const p=ye(x),h=new Date(x.post_timestamp*1e3).toLocaleTimeString(void 0,{hour:"2-digit",minute:"2-digit",second:"2-digit",hour12:!1}),g=p?"SYS":x.author_name||x.author_prefix,j=!p&&a&&g===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:y=>y.currentTarget.style.background="var(--tui-hover-bg)",onMouseLeave:y=>y.currentTarget.style.background="",children:[e.jsxs("span",{className:"select-none shrink-0",style:{color:"var(--tui-muted)"},children:["[",h,"]"]}),e.jsxs("span",{className:"mx-1.5 shrink-0",style:{color:j?"var(--tui-accent-local)":"var(--tui-accent)"},children:["<",g,">"]}),e.jsx("span",{style:{color:p?"var(--tui-system)":j?"var(--tui-body)":"var(--tui-accent)"},className:p?"italic":"",children:x.message_text}),e.jsx("button",{onClick:()=>t(x.id),className:"opacity-0 group-hover:opacity-100 ml-auto pl-2 shrink-0 transition-colors",style:{color:"var(--tui-muted)"},onMouseEnter:y=>y.currentTarget.style.color="var(--tui-danger)",onMouseLeave:y=>y.currentTarget.style.color="var(--tui-muted)",title:"Delete",children:"×"})]},x.id)})})}),n&&e.jsxs("button",{onClick:o,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(ie,{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:"p-4",children:m.map(x=>ye(x)?e.jsx(cs,{text:x.message_text},x.id):e.jsx(ds,{senderName:x.author_name||x.author_prefix,text:x.message_text,timestamp:x.post_timestamp,bubbleAccessory:e.jsx("button",{onClick:()=>t(x.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(Re,{className:"w-3.5 h-3.5"})})},x.id))})}),n&&e.jsxs("button",{onClick:o,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(ie,{className:"w-3.5 h-3.5"}),e.jsx("span",{className:"type-data-xs",children:"New messages"})]})]})}function Ne({roomName:s,onSend:t,disabled:i,terminalMode:a}){const[r,n]=l.useState(""),[c,m]=l.useState(!1),d=l.useRef(null),o=l.useCallback(async()=>{var g;const p=r.trim();if(!p||c)return;m(!0);const h=await t(p);m(!1),h&&(n(""),(g=d.current)==null||g.focus())},[r,c,t]),x=l.useCallback(p=>{p.key==="Enter"&&!p.shiftKey&&(p.preventDefault(),o())},[o]);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:p=>n(p.target.value),onKeyDown:x,placeholder:"type message...",disabled:i||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:o,disabled:!r.trim()||c||i,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:p=>n(p.target.value),onKeyDown:x,placeholder:`Message ${s}…`,disabled:i||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:o,disabled:!r.trim()||c||i,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(we,{className:"w-4.5 h-4.5"})})]})}function Ss({selectedName:s,selectedIdentity:t,messages:i,onSend:a,onDelete:r,onClear:n}){var h;const{terminalMode:c,toggleTerminalMode:m}=I(),[d,o]=l.useState(!1),x=((h=t==null?void 0:t.settings)==null?void 0:h.node_name)??s??void 0,p=l.useCallback(()=>{o(!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(J,{text:"chat",minChars:7,size:24})}),e.jsx("div",{className:"card-terminal-ridge flex-1"}),e.jsxs("div",{className:"header-well",children:[e.jsx(Y,{icon:e.jsx(pe,{className:"!w-5.5 !h-5.5 translate-y-px"}),onClick:m,title:"Exit terminal mode"}),i.length>0&&e.jsx(Y,{icon:e.jsx("span",{className:"text-[13px] font-bold font-mono leading-none translate-y-px",children:"DEL"}),onClick:()=>o(!0),title:"Clear all messages",variant:"red",iconActiveColor:Oe.red})]})]}),e.jsx(q,{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(ve,{messages:i,onDelete:r,terminalMode:!0,serverName:x}),e.jsx("div",{className:"card-terminal-divider"}),s&&e.jsx(Ne,{roomName:s,onSend:a,disabled:!(t!=null&&t.registered),terminalMode:!0})]})})]}),e.jsx(xe,{isOpen:d,title:"Clear All Messages",message:`This will permanently delete all ${i.length} message${i.length===1?"":"s"} from this room. This cannot be undone.`,confirmLabel:"Clear Messages",variant:"danger",onConfirm:p,onCancel:()=>o(!1)})]}):e.jsxs(e.Fragment,{children:[e.jsxs(q,{noPadding:!0,className:"flex flex-col h-full overflow-hidden",children:[e.jsx(te,{listHeader:!0,icon:e.jsx(Me,{className:"icon-sm"}),title:"Messages",actions:e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(b,{plain:!0,onClick:m,title:"Terminal mode",children:e.jsx(pe,{"data-slot":"icon",className:"!w-5 !h-5 translate-y-px"})}),i.length>0&&e.jsx(b,{plain:!0,onClick:()=>o(!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(ve,{messages:i,onDelete:r,terminalMode:!1,serverName:x}),s&&e.jsx(Ne,{roomName:s,onSend:a,disabled:!(t!=null&&t.registered),terminalMode:!1})]}),e.jsx(xe,{isOpen:d,title:"Clear All Messages",message:`This will permanently delete all ${i.length} message${i.length===1?"":"s"} from this room. This cannot be undone.`,confirmLabel:"Clear Messages",variant:"danger",onConfirm:p,onCancel:()=>o(!1)})]})}function _s({pubkey:s,className:t}){const[i,a]=l.useState(!1),r=l.useCallback(async()=>{await Ce(s),a(!0),setTimeout(()=>a(!1),1500)},[s]);return e.jsxs("button",{onClick:r,title:i?"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)}),i&&e.jsx(W,{className:"w-2.5 h-2.5 text-sys-green flex-shrink-0"})]})}const Rs=l.memo(function({client:t,resolvedName:i,onRemove:a}){const r=t.name||i,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?ms(r):{emoji:null,cleanName:t.pubkey_prefix},o=r?us(d):void 0,x=r?xs(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:A("w-8 h-8 rounded-full flex items-center justify-center",o?"clay-avatar":"bg-sys-blue/12"),style:o?{"--avatar-color":o}:void 0,children:m?e.jsx("span",{className:"text-base leading-none",children:m}):e.jsx("span",{className:A("leading-none",o?"text-white text-[11px] font-bold tracking-tight":"text-sys-blue font-mono text-[11px]"),children:x})}),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(_s,{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(vs,{className:"w-3.5 h-3.5"})})]})});function be({clients:s,onRemove:t,messages:i,terminalMode:a}){const r=l.useMemo(()=>{const n=new Map;if(!i)return n;for(let c=i.length-1;c>=0;c--){const m=i[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},[i]);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:o=>o.currentTarget.style.background="var(--tui-hover-bg)",onMouseLeave:o=>o.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:o=>o.currentTarget.style.color="var(--tui-danger)",onMouseLeave:o=>o.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(Rs,{client:n,resolvedName:r.get(n.pubkey),onRemove:t},n.pubkey))})}function Ms({name:s,onSend:t}){const{terminalMode:i}=I(),[a,r]=l.useState("idle"),n=l.useRef(),c=l.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 l.useEffect(()=>()=>clearTimeout(n.current),[]),i?e.jsxs("button",{className:"btn-terminal",onClick:c,disabled:a==="sending",children:[a==="idle"&&"[ send_advert ]",a==="sending"&&"[ broadcasting... ]",a==="sent"&&"[ sent ✓ ]"]}):e.jsxs(b,{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 V(s){return s.settings??{}}function ke({identity:s,onSendAdvert:t}){var c,m,d,o,x,p;const{terminalMode:i}=I(),[a,r]=l.useState(!1),n=l.useCallback(async()=>{s!=null&&s.identity_key&&(await Ce(s.identity_key),r(!0),setTimeout(()=>r(!1),1500))},[s==null?void 0:s.identity_key]);return i?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]"})]})]}),(()=>{var g,j;const h=V(s);return h.latitude!=null||h.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:`${h.latitude??"—"}, ${h.longitude??"—"}`,children:[((g=h.latitude)==null?void 0:g.toFixed(4))??"—",", ",((j=h.longitude)==null?void 0:j.toFixed(4))??"—"]})]}):null})(),(()=>{const h=V(s);return 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:h.admin_password?"var(--tui-accent)":"var(--tui-muted)"},children:h.admin_password?"[✓ admin]":"[— admin]"}),e.jsx("span",{style:{color:h.guest_password?"var(--tui-accent)":"var(--tui-muted)"},children:h.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(te,{listHeader:!0,icon:e.jsx(ps,{className:"icon-sm"}),title:"Room Info"}),s?e.jsxs("div",{className:"space-y-2 px-4 py-3",children:[((o=s.settings)==null?void 0:o.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||((x=s.runtime)==null?void 0:x.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||((p=s.runtime)==null?void 0:p.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(hs,{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(U,{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(Be,{className:"w-3 h-3"}),"Copy"]})})})]})]}),(()=>{var g,j;const h=V(s);return h.latitude!=null||h.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(Z,{className:"w-3 h-3"}),"Location"]}),e.jsxs("span",{className:"type-data-sm font-mono text-fg-primary",title:`${h.latitude??"—"}, ${h.longitude??"—"}`,children:[((g=h.latitude)==null?void 0:g.toFixed(4))??"—",", ",((j=h.longitude)==null?void 0:j.toFixed(4))??"—"]})]}):null})(),(()=>{const h=V(s);return 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(U,{color:h.admin_password?"green":"zinc",children:[h.admin_password&&e.jsx(W,{className:"w-3 h-3"}),"Admin"]}),e.jsxs(U,{color:h.guest_password?"green":"zinc",children:[h.guest_password&&e.jsx(W,{className:"w-3 h-3"}),"Guest"]})]})]})})(),e.jsx("div",{className:"pt-3",children:e.jsx(Ms,{name:s.name,onSend:t})})]}):e.jsx("p",{className:"type-data-xs text-fg-muted text-center py-3",children:"No identity configured"})]})}function Ts({selectedIdentity:s,clients:t,messages:i,onSendAdvert:a}){const{terminalMode:r}=I(),n=l.useCallback(async c=>{Se.setState(m=>({clients:m.clients.filter(d=>d.pubkey!==c)})),await ze({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(Y,{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(J,{text:`OnLInE ${String(t.length).padStart(3,"0")}`,size:24})})}),e.jsx(q,{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(be,{clients:t,onRemove:n,messages:i,terminalMode:!0})})}),e.jsxs("div",{className:"card-terminal-header flex-shrink-0",children:[e.jsx("span",{className:"seven-seg-panel",children:e.jsx(J,{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(Y,{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(q,{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(ke,{identity:s,onSendAdvert:a})})})]})]}):e.jsxs(q,{compact:!0,className:"flex-1 min-h-0 flex flex-col overflow-hidden",children:[e.jsx(te,{listHeader:!0,icon:e.jsx(_e,{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(be,{clients:t,onRemove:n,messages:i,terminalMode:!1})}),e.jsx(ke,{identity:s,onSendAdvert:a})]})}function et(){const s=qe(),t=He(),i=Ve(),a=Ue(),r=We(),n=Ge(),c=Ye(),m=Ie(),d=Xe(),o=Qe(),x=l.useMemo(()=>(d==null?void 0:d.neighbors)??{},[d==null?void 0:d.neighbors]),p=Ns(x,o),h=l.useMemo(()=>n.map(f=>{if(f.name)return f;const v=p(f.pubkey);return v.name?{...f,name:v.name}:f}),[n,p]),g=l.useMemo(()=>r.map(f=>{if(f.author_name||f.author_pubkey==="server"||f.author_pubkey==="system")return f;const v=p(f.author_pubkey);return v.name?{...f,author_name:v.name}:f}),[r,p]),{selectRoom:j,postMessage:y,deleteMessage:$,clearMessages:S,createRoom:_,updateRoom:k,deleteRoom:R,sendAdvert:N,markAsRead:M,startActivePolling:w}=Se.getState(),[T,C]=l.useState(!1),[K,F]=l.useState("create"),[O,H]=l.useState("hybrid");l.useEffect(()=>{M()},[t,M]),l.useEffect(()=>w(),[w]);const B=l.useCallback(async f=>t?y({room_name:t,message:f,author_pubkey:"server"}):!1,[t,y]),z=l.useCallback((f="hybrid")=>{H(f),F("create"),C(!0)},[]),L=l.useCallback(()=>{F("edit"),C(!0)},[]),X=e.jsxs("div",{className:"flex items-center gap-2 sm:gap-4 flex-shrink-0",children:[i&&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:`${i.total_messages} message${i.total_messages!==1?"s":""} in this room`,children:[e.jsx(Me,{className:"w-3.5 h-3.5 text-fg-secondary"}),e.jsx("span",{children:i.total_messages})]}),e.jsxs("span",{className:"data-box flex items-center gap-1.5 cursor-default",title:`${i.total_clients} client${i.total_clients!==1?"s":""} connected`,children:[e.jsx(_e,{className:"w-3.5 h-3.5 text-fg-secondary"}),e.jsx("span",{children:i.total_clients})]})]}),e.jsx(ks,{rooms:s,selected:t,onSelect:j,onAdd:()=>z("hybrid")}),s.length>0&&e.jsx(b,{plain:!0,onClick:L,title:"Manage room server",children:e.jsx(Ze,{"data-slot":"icon"})})]});return s.length===0&&c.length===0&&!m?e.jsx(je,{children:e.jsxs(de,{children:[e.jsx(me,{title:"Room Server",icon:e.jsx(G,{})}),e.jsx(bs,{onCreateRoom:f=>z(f)}),e.jsx(fe,{open:T,onClose:()=>C(!1),mode:"create",repeaterConfig:d==null?void 0:d.config,onCreate:async f=>{const v=await _(f);return v&&O==="dedicated"&&(await ce.getState().setMode("monitor"),ce.getState().clearModeMutation()),v},onUpdate:k,onDelete:R,onSendAdvert:N})]})}):e.jsx(je,{children:e.jsxs(de,{children:[e.jsx(me,{title:"Room Server",icon:e.jsx(G,{}),controls:X}),e.jsx(Je,{children:e.jsxs(es,{template:"hero-tall",className:"!h-[calc(100vh-11rem)] !min-h-[400px]",children:[e.jsx(ue,{span:12,lg:8,className:"h-full",children:e.jsx(Ss,{selectedName:t,selectedIdentity:a,messages:g,onSend:B,onDelete:$,onClear:S})}),e.jsx(ue,{span:12,lg:4,className:"h-full",children:e.jsx(Ts,{selectedIdentity:a,clients:h,messages:g,onSendAdvert:N})})]})}),e.jsx(fe,{open:T,onClose:()=>C(!1),mode:K,identity:a,repeaterConfig:d==null?void 0:d.config,onCreate:_,onUpdate:k,onDelete:R,onSendAdvert:N})]})})}export{et as default};
