var Q=Object.defineProperty;var _=(z)=>z;function b(z,B){this[z]=_.bind(null,B)}var o=(z,B)=>{for(var H in B)Q(z,H,{get:B[H],enumerable:!0,configurable:!0,set:b.bind(B,H)})};var t=(z,B)=>()=>(z&&(B=z(z=0)),B);import{gql as h}from"@apollo/client";import{useQuery as T}from"@apollo/client/react";import{useTemplateRuntime as C}from"@contractspec/lib.example-shared-ui";import{MessageCircle as w,Users as R}from"lucide-react";import{useMemo as E}from"react";import{jsx as Z,jsxs as W}from"react/jsx-runtime";var M=h`
  query Conversations($projectId: ID!) {
    conversations(projectId: $projectId) {
      id
      name
      isGroup
      updatedAt
      participants {
        id
        userId
        displayName
      }
    }
  }
`;function f({selectedId:z,onSelect:B}){let{projectId:H}=C(),{data:P,loading:K}=T(M,{variables:{projectId:H},fetchPolicy:"cache-and-network"}),X=E(()=>P?.conversations??[],[P]);return W("aside",{className:"h-full overflow-y-auto rounded-2xl border border-border bg-card",children:[W("header",{className:"px -4 flex items-center justify-between border-border border-b px-4 py-3",children:[W("div",{children:[Z("p",{className:"font-semibold text-sm",children:"Conversations"}),Z("p",{className:"text-muted-foreground text-xs",children:K?"Loading…":`${X.length} active`})]}),Z(R,{className:"h-4 w-4 text-muted-foreground"})]}),W("div",{className:"divide-y divide-border/70",children:[X.map((J)=>{let k=z===J.id;return W("button",{type:"button",className:`flex w-full flex-col items-start gap-1 px-4 py-3 text-left transition ${k?"bg-violet-500/10 text-foreground":"hover:bg-muted/40"}`,onClick:()=>B?.(J.id),children:[W("div",{className:"flex items-center gap-2",children:[Z(w,{className:"h-4 w-4 text-muted-foreground"}),Z("span",{className:"font-medium text-sm",children:J.name||"Untitled conversation"})]}),Z("p",{className:"text-muted-foreground text-xs",children:J.participants.map((V)=>V.displayName||V.userId).join(" · ")})]},J.id)}),!K&&X.length===0?Z("p",{className:"p-4 text-center text-muted-foreground text-xs",children:"No conversations yet."}):null]})]})}import{gql as q}from"@apollo/client";import{useMutation as S,useQuery as p}from"@apollo/client/react";import{useTemplateRuntime as u}from"@contractspec/lib.example-shared-ui";import{useEffect as G,useRef as m}from"react";import{jsx as $,jsxs as Y}from"react/jsx-runtime";var y=q`
  query Messages($conversationId: ID!) {
    messages(conversationId: $conversationId, limit: 50) {
      id
      content
      senderId
      senderName
      createdAt
      status
    }
  }
`,x=q`
  mutation SetMessagesRead($conversationId: ID!, $userId: String!) {
    setMessagesRead(conversationId: $conversationId, userId: $userId)
  }
`;function D({conversationId:z,viewerId:B="local-user"}){let H=m(null),P=Boolean(z),{data:K,loading:X}=p(y,{variables:{conversationId:z},skip:!P,fetchPolicy:"cache-and-network"}),[J]=S(x),{projectId:k}=u(),V=K?.messages??[];if(G(()=>{if(!P||!z)return;J({variables:{conversationId:z,userId:B}}).catch(()=>{})},[z,P,J,B]),G(()=>{let F=H.current;if(!F)return;F.scrollTop=F.scrollHeight},[V]),!z)return $("div",{className:"flex flex-1 items-center justify-center rounded-2xl border border-border border-dashed",children:$("p",{className:"text-muted-foreground text-sm",children:"Select a conversation"})});return $("div",{className:"flex flex-1 flex-col rounded-2xl border border-border bg-card",children:$("div",{ref:H,className:"flex-1 space-y-3 overflow-y-auto p-4",children:X?$("p",{className:"text-center text-muted-foreground text-sm",children:"Loading…"}):V.slice().reverse().map((F)=>Y("div",{className:`flex flex-col ${F.senderId===B?"items-end":"items-start"}`,children:[$("div",{className:`max-w-xs rounded-2xl px-3 py-2 text-sm ${F.senderId===B?"bg-violet-500 text-white":"bg-muted text-foreground"}`,children:$("p",{children:F.content})}),Y("span",{className:"mt-1 text-muted-foreground text-xs",children:[F.senderName||F.senderId," ·"," ",new Date(F.createdAt).toLocaleTimeString([],{hour:"2-digit",minute:"2-digit"})]})]},F.id))})})}import{gql as l}from"@apollo/client";import{useMutation as g}from"@apollo/client/react";import{useTemplateRuntime as j}from"@contractspec/lib.example-shared-ui";import{Send as I}from"lucide-react";import{useState as v}from"react";import{jsx as U,jsxs as A}from"react/jsx-runtime";var d=l`
  mutation SendMessage($input: SendMessageInput!) {
    sendMessage(input: $input) {
      id
    }
  }
`;function L({conversationId:z,viewerId:B="local-user",viewerName:H="You"}){let{projectId:P}=j(),[K,X]=v(""),[J,{loading:k}]=g(d);return A("form",{onSubmit:async(F)=>{if(F.preventDefault(),!z||!K.trim())return;await J({variables:{input:{projectId:P,conversationId:z,senderId:B,senderName:H,content:K.trim()}},refetchQueries:[{query:y,variables:{conversationId:z}}]}),X("")},className:"mt-3 flex items-center gap-2 rounded-2xl border border-border bg-card px-4 py-3",children:[U("input",{type:"text",className:"flex-1 bg-transparent text-foreground text-sm outline-none",placeholder:z?"Type a message…":"Select a conversation to start",disabled:!z||k,value:K,onChange:(F)=>X(F.target.value)}),A("button",{type:"submit",className:"btn-primary inline-flex items-center gap-1 text-sm",disabled:!z||k||!K.trim(),children:[U(I,{className:"h-4 w-4"}),"Send"]})]})}import{registerTemplateComponents as c}from"@contractspec/lib.example-shared-ui";import{useState as r}from"react";import{jsx as O,jsxs as N}from"react/jsx-runtime";function i(){let[z,B]=r();return N("div",{className:"grid gap-4 md:grid-cols-[280px,1fr]",children:[O(f,{selectedId:z,onSelect:B}),N("div",{className:"flex flex-col gap-3",children:[O(D,{conversationId:z}),O(L,{conversationId:z})]})]})}c("messaging-app",{list:i,detail:D,form:L});export{i as MessagingWorkspace,D as MessageThread,L as MessageComposer,y as MESSAGES_QUERY,f as ConversationList};
