1import{X as z,J as n,C as e,e as w,T as J,a as O,N as l,O as A}from"./index-DaujsIbz.js";import{A as X}from"./AdminLayout-BS9AcuwQ.js";import{I as G}from"./input-Ci_0_zq6.js";import{S as H}from"./search-CMeXAMKz.js";import{M as B}from"./message-square-ShnYRzRe.js";import{S as Q}from"./send-aOs8ouCU.js";import"./sheet-CT8N_qGz.js";import"./index-B-DiPpGG.js";import"./index-Ce8oBWiU.js";import"./Logo-D06ITUvA.js";import"./wallet-DHvgKr_8.js";import"./book-open-yIElqopX.js";import"./pen-line-xceTTcOP.js";import"./settings-CEq7uVHb.js";function ce(){const{user:N}=z(),[h,y]=n.useState([]),[F,_]=n.useState(!0),[v,I]=n.useState(""),[r,V]=n.useState(null),[m,S]=n.useState([]),[$,M]=n.useState(!1),[u,b]=n.useState(""),[E,T]=n.useState(!1),C=n.useRef(null),g=async(s=!1)=>{s||_(!0);try{const{data:t,error:a}=await l.from("user_roles").select("user_id").eq("role","partner");if(a)throw a;if(!t||t.length===0){y([]);return}const x=t.map(i=>i.user_id),{data:D,error:d}=await l.from("profiles").select("user_id, full_name, email").in("user_id",x);if(d)throw d;const{data:K,error:q}=await l.from("chat_messages").select("*").order("created_at",{ascending:!0});if(q)throw q;const R=(D??[]).map(i=>{const o=(K??[]).filter(f=>f.user_id===i.user_id),j=o.filter(f=>f.sender_id===i.user_id&&!f.read).length,c=o.length>0?o[o.length-1]:null;return{user_id:i.user_id,full_name:i.full_name,email:i.email,unreadCount:j,lastMessage:c?c.message:null,lastMessageTime:c?c.created_at:null}});R.sort((i,o)=>
1{if(i.unreadCount!==o.unreadCount)return o.unreadCount-i.unreadCount;const j=i.lastMessageTime?new Date(i.lastMessageTime).getTime():0;return(o.lastMessageTime?new Date(o.lastMessageTime).getTime():0)-j}),y(R)}catch(t){console.error(t),A.error("Erro ao carregar parceiros: "+t.message)}finally{_(!1)}},p=async(s,t=!1)=>{t||M(!0);try{const{data:a,error:x}=await l.from("chat_messages").select("*").eq("user_id",s).order("created_at",{ascending:!0});if(x)throw x;S(a??[]),(a??[]).filter(d=>d.sender_id===s&&!d.read).length>0&&(await l.from("chat_messages").update({read:!0}).eq("user_id",s).eq("sender_id",s),g(!0))}catch(a){console.error(a)}finally{M(!1)}};n.useEffect(()=>{g();const s=()=>{document.visibilityState==="visible"&&g(!0)};return document.addEventListener("visibilitychange",s),()=>{document.removeEventListener("visibilitychange",s)}},[]),n.useEffect(()=>{if(r){p(r);const s=()=>{document.visibilityState==="visible"&&p(r,!0)};return document.addEventListener("visibilitychange",s),()=>{document.removeEventListener("visibilitychange",s)}}else S([])},[r]),n.useEffect(()=>{C.current?.scrollIntoView({behavior:"smooth"})},[m]);const L=async s=>{if(s.preventDefault(),!u.trim()||!r||!N)return;T(!0);const t=u.trim();b("");try{const{error:a}=await l.from("chat_messages").insert({user_id:r,sender_id:N.id,message:t,read:!1});if(a)throw a;await l.from("notifications").insert({user_id:r,title:"Nova mensagem da administração",message:"Você recebeu uma nova mensagem no chat privado.",type:"mensagem"}),p(r,!0),g(!0)}catch(a){A.error("Erro ao enviar mensagem: "+a.message),b(t)}finally{T(!1)}},P=h.find(s=>s.user_id===r),k=h.filter(s=>(s.full_name??"").toLowerCase().includes(v.toLowerCase())||(s.email??"").toLowerCase().includes(v.toLowerCase()));return e.jsx(X,{title:"Mensagens",children:e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-3 gap-6 h-[calc(100vh-180px)] min-h-[500px]",children:[e.jsxs("div",{className:"bg-card border border-[var(--gold-soft)]/30 rounded-sm flex flex-col h-full overflow-hidden",children:[e.jsx("div",{className:"p-4 border-b border-border/50 bg-[var(--beige)]/10",children:e.jsxs("div",{className:"relative",children:[e.jsx(H,{className:"absolute left-3 top-2.5 h-4 w-4 text-muted-foreground"}),e.jsx(G,{placeholder:"Buscar parceiro...",value:v,onChange:s=>I(s.target.value),className:"pl-9 bg-background"})]})}),e.jsx("div",{className:"flex-1 overflow-y-auto divide-y divide-border/40",children:F&&h.length===0?e.jsx("div",{className:"flex justify-center py-8",children:e.jsx(w,{className:"h-6 w-6 animate-spin text-[var(--gold)]"})}):k.length===0?e.jsx("p",{className:"p-4 text-xs text-muted-foreground italic text-center",children:"Nenhum parceiro encontrado."}):k.map(s=>{const t=s.user_id===r;return e.jsxs("button",{onClick:()=>V(s.user_id),className:`w-full text-left p-4 transition-colors flex justify-between items-start gap-2 ${t?"bg-[var(--beige)]/30":"hover:bg-[var(--beige)]/10 bg-card"}`,children:[e.jsxs("div",{className:"space-y-1 min-w-0 flex-1",children:[e.jsx("div",{className:"font-semibold text-sm truncate text-[var(--ink)]",children:s.full_name||"Parceiro sem nome"}),e.jsx("div",{className:"text-xs text-muted-foreground truncate",children:s.email}),s.lastMessage&&e.jsx("div",{className:"text-xs text-muted-foreground/80 truncate mt-1",children:s.lastMessage})]}),e.jsxs("div",{className:"flex flex-col items-end gap-1.5 shrink-0",children:[s.lastMessageTime&&e.jsx("span",{className:"text-[10px] text-muted-foreground",children:new Date(s.lastMessageTime).toLocaleTimeString("pt-BR",{hour:"2-digit",minute:"2-digit"})}),s.unreadCount>0&&e.jsx("span",{className:"bg-[var(--gold)] text-white text-[10px] font-bold px-2 py-0.5 rounded-full",children:s.unreadCount})]})]},s.user_id)})})]}),e.jsx("div",{className:"md:col-span-2 bg-card border border-[var(--gold-soft)]/30 rounded-sm flex flex-col h-full overflow-hidden",children:r?e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"p-4 border-b border-border/50 bg-[var(--beige)]/10 flex items-center justify-between",children:e.jsxs("div",{children:[e.jsx("h3",{className:"font-serif-display text-lg text-[var(--ink)]",children:P?.full_name||"Parceiro"}),e.jsx("p",{className:"text-xs text-muted-foreground",children:P?.email})]})}),e.jsxs("div",{className:"flex-1 overflow-y-auto p-4 space-y-4 bg-[var(--beige)]/5",children:[$&&m.length===0?e.jsx("div",{className:"flex justify-center py-8",children:e.jsx(w,{className:"h-6 w-6 animate-spin text-[var(--gold)]"})}):m.length===0?e.jsxs("div",{className:"text-center py-12 text-muted-foreground flex flex-col items-center",children:[e.jsx(B,{className:"h-10 w-10 text-[var(--gold-soft)] mb-2"}),e.jsx("p",{className:"text-sm font-medium",children:"Inicie a conversa!"}),e.jsx("p",{className:"text-xs",children:"Envie uma mensagem abaixo."})]}):m.map(s=>{const t=s.sender_id!==r;return e.jsx("div",{className:`flex ${t?"justify-end":"justify-start"}`,children:e.jsxs("div",{className:`max-w-[80%] rounded-sm p-3 shadow-sm text-sm leading-relaxed ${t?"bg-[var(--gold)] text-white":"bg-white border border-border text-[var(--ink)]"}`,children:[e.jsx("div",{className:"whitespace-pre-line break-all",children:s.message}),e.jsxs("div",{className:`text-[9px] mt-1.5 text-right ${t?"text-white/80":"text-muted-foreground"}`,children:[new Date(s.created_at).toLocaleTimeString("pt-BR",{hour:"2-digit",minute:"2-digit"}),t&&e.jsx("span",{className:"ml-1 font-bold",children:s.read?" · Lido":" · Entregue"})]})]})},s.id)}),e.jsx("div",{ref:C})]}),e.jsxs("form",{onSubmit:L,className:"p-4 border-t border-border bg-background flex gap-2 items-end",children:[e.jsx(J,{value:u,onChange:s=>b(s.target.value),placeholder:"Escreva sua resposta...",rows:2,className:"resize-none text-sm bg-background border-border/80 focus-visible:ring-[var(--gold)] min-h-[50px] flex-1",onKeyDown:s=>{s.key==="Enter"&&!s.shiftKey&&(s.preventDefault(),L(s))}}),e.jsx(O,{type:"submit",disabled:E||!u.trim(),className:"bg-[var(--gold)] hover:bg-[var(--gold-old)] text-white h-12 w-12 p-0 rounded-sm shrink-0",children:E?e.
1jsx(w,{className:"h-4 w-4 animate-spin"}):e.jsx(Q,{className:"h-4 w-4"})})]})]}):e.jsxs("div",{className:"flex-1 flex flex-col items-center justify-center p-8 text-center text-muted-foreground",children:[e.jsx(B,{className:"h-16 w-16 text-[var(--gold-soft)]/50 mb-3"}),e.jsx("h3",{className:"font-serif-display text-xl text-[var(--gold-old)] mb-1",children:"Central de Mensagens"}),e.jsx("p",{className:"text-sm max-w-sm",children:"Selecione um parceiro no menu lateral para iniciar ou continuar uma conversa privada."})]})})]})})}export{ce as component};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.