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

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

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