var D=Object.defineProperty;var U=(k)=>k;function Y(k,z){this[k]=U.bind(null,z)}var p=(k,z)=>{for(var F in z)D(k,F,{get:z[F],enumerable:!0,configurable:!0,set:Y.bind(z,F)})};var R=(k,z)=>()=>(k&&(z=k(k=0)),z);import{gql as $}from"@apollo/client";import{useMutation as h,useQuery as q}from"@apollo/client/react";import{useTemplateRuntime as G}from"@contractspec/lib.example-shared-ui";import{useEffect as X,useRef as N}from"react";import{jsx as H,jsxs as Z}from"react/jsx-runtime";var W=$`
  query Messages($conversationId: ID!) {
    messages(conversationId: $conversationId, limit: 50) {
      id
      content
      senderId
      senderName
      createdAt
      status
    }
  }
`,Q=$`
  mutation SetMessagesRead($conversationId: ID!, $userId: String!) {
    setMessagesRead(conversationId: $conversationId, userId: $userId)
  }
`;function u({conversationId:k,viewerId:z="local-user"}){let F=N(null),K=Boolean(k),{data:J,loading:L}=q(W,{variables:{conversationId:k},skip:!K,fetchPolicy:"cache-and-network"}),[O]=h(Q),{projectId:P}=G(),V=J?.messages??[];if(X(()=>{if(!K||!k)return;O({variables:{conversationId:k,userId:z}}).catch(()=>{})},[k,K,O,z]),X(()=>{let B=F.current;if(!B)return;B.scrollTop=B.scrollHeight},[V]),!k)return H("div",{className:"flex flex-1 items-center justify-center rounded-2xl border border-border border-dashed",children:H("p",{className:"text-muted-foreground text-sm",children:"Select a conversation"})});return H("div",{className:"flex flex-1 flex-col rounded-2xl border border-border bg-card",children:H("div",{ref:F,className:"flex-1 space-y-3 overflow-y-auto p-4",children:L?H("p",{className:"text-center text-muted-foreground text-sm",children:"Loading…"}):V.slice().reverse().map((B)=>Z("div",{className:`flex flex-col ${B.senderId===z?"items-end":"items-start"}`,children:[H("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:H("p",{children:B.content})}),Z("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))})})}import{gql as A}from"@apollo/client";import{useMutation as T}from"@apollo/client/react";import{useTemplateRuntime as _}from"@contractspec/lib.example-shared-ui";import{Send as b}from"lucide-react";import{useState as f}from"react";import{jsx as y,jsxs as C}from"react/jsx-runtime";var M=A`
  mutation SendMessage($input: SendMessageInput!) {
    sendMessage(input: $input) {
      id
    }
  }
`;function r({conversationId:k,viewerId:z="local-user",viewerName:F="You"}){let{projectId:K}=_(),[J,L]=f(""),[O,{loading:P}]=T(M);return C("form",{onSubmit:async(B)=>{if(B.preventDefault(),!k||!J.trim())return;await O({variables:{input:{projectId:K,conversationId:k,senderId:z,senderName:F,content:J.trim()}},refetchQueries:[{query:W,variables:{conversationId:k}}]}),L("")},className:"mt-3 flex items-center gap-2 rounded-2xl border border-border bg-card px-4 py-3",children:[y("input",{type:"text",className:"flex-1 bg-transparent text-foreground text-sm outline-none",placeholder:k?"Type a message…":"Select a conversation to start",disabled:!k||P,value:J,onChange:(B)=>L(B.target.value)}),C("button",{type:"submit",className:"btn-primary inline-flex items-center gap-1 text-sm",disabled:!k||P||!J.trim(),children:[y(b,{className:"h-4 w-4"}),"Send"]})]})}export{r as MessageComposer};
