import{u as p,j as n,a as g}from"./query-CBHSEszN.js";import{M as l,a as o}from"./index-BXO8FbHk.js";import{a as x,P as h,E as d}from"./PageError-B8nAUtH9.js";import"./react-B_PbFb63.js";async function u(e){try{const i=e?`?deviceId=${encodeURIComponent(e)}`:"";return await x(`/my/messages/conversations${i}`)}catch(i){throw i instanceof Error?i:new Error("fetchConversations failed")}}async function m(e,i=100,t){try{const a=new URLSearchParams({limit:String(i)});return await x(`/my/messages/conversations/${encodeURIComponent(e)}/messages?${a}`)}catch(a){throw a instanceof Error?a:new Error("fetchThread failed")}}function b(e){return p({queryKey:["conversations",e],queryFn:()=>u(e),refetchInterval:3e4})}function y(e){return p({queryKey:["thread",e],queryFn:()=>m(e),enabled:e!==null,refetchInterval:1e4})}const w=o.ul`
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  flex: 1;
`,j=o.li`
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 12px 16px;
  cursor: pointer;
  border-bottom: 1px solid #1e1e2a;
  background: ${({$selected:e})=>e?"rgba(201,168,76,0.12)":"transparent"};
  transition: background 0.1s;

  &:hover {
    background: ${({$selected:e})=>e?"rgba(201,168,76,0.14)":"#13131a"};
  }
`,v=o.span`
  font-size: 13px;
  font-weight: 600;
  color: ${({$selected:e})=>e?"#c9a84c":"#e0e0f0"};
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
`,$=o.span`
  font-size: 11px;
  color: #8a8a9a;
`,k=o.span`
  display: inline-block;
  font-size: 10px;
  padding: 1px 6px;
  background: rgba(138,138,154,0.15);
  border-radius: 10px;
  color: #8a8a9a;
  margin-left: 6px;
`;function C(e){if(!e)return"";const i=new Date(e),a=Date.now()-i.getTime();return a<6e4?"just now":a<36e5?`${Math.floor(a/6e4)}m ago`:a<864e5?`${Math.floor(a/36e5)}h ago`:i.toLocaleDateString()}function M({conversations:e,selectedId:i,onSelect:t}){return n.jsx(w,{role:"listbox","aria-label":"Conversations",children:e.map(a=>n.jsxs(j,{$selected:a.id===i,onClick:()=>t(a.id),role:"option","aria-selected":a.id===i,tabIndex:0,onKeyDown:s=>{(s.key==="Enter"||s.key===" ")&&(s.preventDefault(),t(a.id))},children:[n.jsxs(v,{$selected:a.id===i,children:[n.jsx(l,{size:11,style:{marginRight:5,opacity:.5,verticalAlign:"middle"}}),a.displayName||a.participants[0]||"Unknown",a.isGroup&&n.jsx(k,{children:"group"})]}),n.jsxs($,{children:[a.messageCount," msg",a.messageCount!==1?"s":""," ",a.lastMessageAt&&`· ${C(a.lastMessageAt)}`]})]},a.id))})}const z=o.div`
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
`,S=o.div`
  padding: 12px 16px;
  border-bottom: 1px solid #2a2a38;
  font-size: 14px;
  font-weight: 600;
  color: #e0e0f0;
  background: #0a0a0f;
`,T=o.ol`
  list-style: none;
  margin: 0;
  padding: 16px;
  overflow-y: auto;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
`,E=o.li`
  max-width: 72%;
  align-self: ${({$fromMe:e})=>e?"flex-end":"flex-start"};
  background: ${({$fromMe:e})=>e?"rgba(201,168,76,0.18)":"#1c1c26"};
  border: 1px solid ${({$fromMe:e})=>e?"rgba(201,168,76,0.35)":"#2a2a38"};
  border-radius: 12px;
  padding: 8px 12px;
`,L=o.p`
  margin: 0;
  font-size: 13px;
  line-height: 1.5;
  color: #e0e0f0;
  white-space: pre-wrap;
  word-break: break-word;
`,c=o.span`
  display: block;
  font-size: 10px;
  color: #8a8a9a;
  margin-top: 4px;
`,R=o.span`
  display: inline-block;
  font-size: 10px;
  color: #c9a84c;
  margin-top: 3px;
`,F=o.div`
  padding: 24px;
  color: #8a8a9a;
  font-size: 13px;
  text-align: center;
`;function B(e){return new Date(e).toLocaleTimeString([],{hour:"2-digit",minute:"2-digit"})}function q({conversationId:e,displayName:i}){const{data:t,isLoading:a,error:s}=y(e);return n.jsxs(z,{children:[n.jsx(S,{"aria-label":`Thread: ${i}`,children:i}),a&&n.jsx(F,{"aria-live":"polite",children:"Loading messages…"}),s&&n.jsx(h,{message:"Failed to load messages"}),!a&&!s&&(t==null?void 0:t.length)===0&&n.jsx(d,{icon:n.jsx(l,{size:32}),heading:"No messages",sub:"This thread is empty."}),!a&&!s&&t&&t.length>0&&n.jsx(T,{"aria-label":"Message thread","aria-live":"polite",children:t.map(r=>n.jsxs(E,{$fromMe:r.isFromMe,"aria-label":`${r.isFromMe?"Sent":"Received"}: ${r.body}`,children:[!r.isFromMe&&n.jsx(c,{children:r.fromHandle}),n.jsx(L,{children:r.body||n.jsx("em",{style:{opacity:.5},children:"no text"})}),r.hasAttachments&&n.jsxs(R,{children:[r.attachmentCount," attachment",r.attachmentCount!==1?"s":""]}),n.jsx(c,{children:B(r.sentAt)})]},r.id))})]})}const A=o.div`
  display: flex;
  flex-direction: column;
  height: 100%;
`,D=o.h1`
  margin: 0;
  padding: 14px 20px;
  font-size: 18px;
  font-weight: 700;
  color: #e0e0f0;
  border-bottom: 1px solid #2a2a38;
  background: #0a0a0f;
`,N=o.div`
  display: flex;
  flex: 1;
  overflow: hidden;
`,P=o.aside`
  width: 260px;
  flex-shrink: 0;
  border-right: 1px solid #2a2a38;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: #0d0d14;

  @media (max-width: 640px) {
    width: 100%;
  }
`,I=o.div`
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: #0d0d14;
`,U=o.span`
  font-size: 11px;
  color: #8a8a9a;
  padding: 8px 16px 4px;
`,G=o.div`
  padding: 20px;
  font-size: 12px;
  color: #8a8a9a;
  text-align: center;
`;function W(){const[e,i]=g.useState(null),{data:t,isLoading:a,error:s}=b(),r=(t==null?void 0:t.find(f=>f.id===e))??null;return n.jsxs(A,{children:[n.jsx(D,{children:"Messages"}),n.jsxs(N,{children:[n.jsxs(P,{"aria-label":"Conversation list",children:[a&&n.jsx(G,{"aria-live":"polite",children:"Loading…"}),s&&n.jsx(h,{message:"Failed to load conversations"}),!a&&!s&&(t==null?void 0:t.length)===0&&n.jsx(d,{icon:n.jsx(l,{size:28}),heading:"No conversations",sub:"Messages will appear here once sync runs."}),!a&&!s&&t&&t.length>0&&n.jsxs(n.Fragment,{children:[n.jsxs(U,{children:[t.length," conversation",t.length!==1?"s":""]}),n.jsx(M,{conversations:t,selectedId:e,onSelect:i})]})]}),n.jsx(I,{children:r?n.jsx(q,{conversationId:r.id,displayName:(r.displayName||r.participants[0])??"Thread"}):n.jsx(d,{icon:n.jsx(l,{size:36}),heading:"Select a conversation",sub:"Choose a thread from the left panel."})})]})]})}export{W as MessagesTab};
