var X=Object.defineProperty;var Z=(k)=>k;function $(k,z){this[k]=Z.bind(null,z)}var q=(k,z)=>{for(var F in z)X(k,F,{get:z[F],enumerable:!0,configurable:!0,set:$.bind(z,F)})};var A=(k,z)=>()=>(k&&(z=k(k=0)),z);import{gql as O}from"@apollo/client";import{useMutation as h,useQuery as y}from"@apollo/client/react";import{useTemplateRuntime as D}from"@contractspec/lib.example-shared-ui";import{useEffect as L,useRef as G}from"react";import{jsx as C,jsxs as N}from"react/jsx-runtime";var U=O`
  query Messages($conversationId: ID!) {
    messages(conversationId: $conversationId, limit: 50) {
      id
      content
      senderId
      senderName
      createdAt
      status
    }
  }
`,Y=O`
  mutation SetMessagesRead($conversationId: ID!, $userId: String!) {
    setMessagesRead(conversationId: $conversationId, userId: $userId)
  }
`;function p({conversationId:k,viewerId:z="local-user"}){let F=G(null),H=Boolean(k),{data:P,loading:V}=y(U,{variables:{conversationId:k},skip:!H,fetchPolicy:"cache-and-network"}),[J]=h(Y),{projectId:W}=D(),K=P?.messages??[];if(L(()=>{if(!H||!k)return;J({variables:{conversationId:k,userId:z}}).catch(()=>{})},[k,H,J,z]),L(()=>{let B=F.current;if(!B)return;B.scrollTop=B.scrollHeight},[K]),!k)return C("div",{className:"flex flex-1 items-center justify-center rounded-2xl border border-border border-dashed",children:C("p",{className:"text-muted-foreground text-sm",children:"Select a conversation"})});return C("div",{className:"flex flex-1 flex-col rounded-2xl border border-border bg-card",children:C("div",{ref:F,className:"flex-1 space-y-3 overflow-y-auto p-4",children:V?C("p",{className:"text-center text-muted-foreground text-sm",children:"Loading…"}):K.slice().reverse().map((B)=>N("div",{className:`flex flex-col ${B.senderId===z?"items-end":"items-start"}`,children:[C("div",{className:`max-w-xs rounded-2xl px-3 py-2 text-sm ${B.senderId===z?"bg-violet-500 text-white":"bg-muted text-foreground"}`,children:C("p",{children:B.content})}),N("span",{className:"mt-1 text-muted-foreground text-xs",children:[B.senderName||B.senderId," ·"," ",new Date(B.createdAt).toLocaleTimeString([],{hour:"2-digit",minute:"2-digit"})]})]},B.id))})})}export{p as MessageThread,U as MESSAGES_QUERY};
