{"version":3,"file":"chat-context.cjs","sources":["../../../components/chat/chat-context.tsx"],"sourcesContent":["'use client';\n\nimport { createContext, useContext } from 'react';\n\nexport interface ChatMessagesState {\n  /** Whether the reader is at the live edge (scrolled to the bottom). */\n  atBottom: boolean;\n  /**\n   * Ids of the registered messages currently intersecting the viewport, in\n   * document order. Only messages given a `messageId` are tracked.\n   */\n  visibleMessageIds: string[];\n}\n\nexport interface ChatMessagesActions {\n  /** Scrolls the conversation to the live edge. */\n  scrollToBottom: (behavior?: ScrollBehavior) => void;\n  /** Scrolls the message registered with the given `messageId` into view. */\n  scrollToMessage: (\n    id: string,\n    options?: { behavior?: ScrollBehavior }\n  ) => void;\n}\n\nexport interface ChatMessageRegistration {\n  id?: string;\n  scrollAnchor?: boolean;\n}\n\nexport interface ChatMessagesRegistry {\n  register: (\n    element: HTMLElement,\n    registration: ChatMessageRegistration\n  ) => () => void;\n}\n\nexport const ChatMessagesStateContext = createContext<ChatMessagesState | null>(\n  null\n);\nexport const ChatMessagesActionsContext =\n  createContext<ChatMessagesActions | null>(null);\nexport const ChatMessagesRegistryContext =\n  createContext<ChatMessagesRegistry | null>(null);\n\nexport function useChatMessagesState(part: string): ChatMessagesState {\n  const context = useContext(ChatMessagesStateContext);\n  if (!context) {\n    throw new Error(`${part} must be used within <Chat.Messages>`);\n  }\n  return context;\n}\n\nexport function useChatMessagesActions(part: string): ChatMessagesActions {\n  const context = useContext(ChatMessagesActionsContext);\n  if (!context) {\n    throw new Error(`${part} must be used within <Chat.Messages>`);\n  }\n  return context;\n}\n\nexport interface UseChatMessagesReturn\n  extends ChatMessagesState,\n    ChatMessagesActions {}\n\n/**\n * Scroll state and commands for the enclosing `Chat.Messages`: `atBottom`,\n * `visibleMessageIds`, `scrollToBottom` and `scrollToMessage`.\n */\nexport function useChatMessages(): UseChatMessagesReturn {\n  const state = useChatMessagesState('useChatMessages');\n  const actions = useChatMessagesActions('useChatMessages');\n  return { ...state, ...actions };\n}\n"],"names":[],"mappings":";;;;;;;;AA4CM;AACJ;;AAEE;;AAEF;AACF;AAEM;AACJ;;AAEE;;AAEF;AACF;AAMA;;;AAGG;;AAED;AACA;AACA;AACF;;;;;;;"}