/**
 * An internal context to separate the messages state (which is constantly changing) from the rest of VN SDK context
 */

import { Message } from "@vn-sdk/runtime-client-gql";
import React from "react";
import { SuggestionItem } from "../utils/suggestions";

export interface AiMessagesContextParams {
  messages: Message[];
  setMessages: React.Dispatch<React.SetStateAction<Message[]>>; // suggestions state
  suggestions: SuggestionItem[];
  setSuggestions: React.Dispatch<React.SetStateAction<SuggestionItem[]>>;
}

const emptyAiContext: AiMessagesContextParams = {
  messages: [],
  setMessages: () => [],
  // suggestions state
  suggestions: [],
  setSuggestions: () => [],
};

export const AiMessagesContext =
  React.createContext<AiMessagesContextParams>(emptyAiContext);

export function useAiMessagesContext(): AiMessagesContextParams {
  const context = React.useContext(AiMessagesContext);
  if (context === emptyAiContext) {
    throw new Error(
      "A messages consuming component was not wrapped with `<AiMessages> {...} </AiMessages>`",
    );
  }
  return context;
}
