import {
  Window
} from "./chunk-C3GSYRC3.mjs";
import {
  Header
} from "./chunk-V6DDX4LH.mjs";
import {
  Button
} from "./chunk-UFN2VWSR.mjs";
import {
  CopilotChat
} from "./chunk-NJY6RHHQ.mjs";
import {
  Input
} from "./chunk-DTRPPNSA.mjs";
import {
  Messages
} from "./chunk-NGA4T3VT.mjs";
import {
  UserMessage
} from "./chunk-VVL6JFCJ.mjs";
import {
  AssistantMessage
} from "./chunk-X477GDGZ.mjs";
import {
  ChatContextProvider,
  useChatContext
} from "./chunk-IEMQ2SQW.mjs";
import {
  __objRest,
  __spreadProps,
  __spreadValues
} from "./chunk-MRXNTQOX.mjs";

// src/components/chat/Modal.tsx
import React, { useMemo, useCallback, useEffect, useRef } from "react";
import { useCopilotContext } from "@copilotkit/react-core";
import {
  CopilotKitError,
  CopilotKitErrorCode,
  Severity,
  ErrorVisibility,
  styledConsole
} from "@copilotkit/shared";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
var CopilotModalInner = (_a) => {
  var _b = _a, {
    observabilityHooks,
    onSetOpen,
    clickOutsideToClose,
    hitEscapeToClose,
    shortcut,
    className,
    children,
    Window: Window2,
    Button: Button2,
    Header: Header2
  } = _b, chatProps = __objRest(_b, [
    "observabilityHooks",
    "onSetOpen",
    "clickOutsideToClose",
    "hitEscapeToClose",
    "shortcut",
    "className",
    "children",
    "Window",
    "Button",
    "Header"
  ]);
  const { copilotApiConfig, setBannerError } = useCopilotContext();
  const { publicApiKey } = copilotApiConfig;
  const triggerObservabilityHook = useCallback(
    (hookName, ...args) => {
      if (publicApiKey && (observabilityHooks == null ? void 0 : observabilityHooks[hookName])) {
        observabilityHooks[hookName](...args);
      }
      if ((observabilityHooks == null ? void 0 : observabilityHooks[hookName]) && !publicApiKey) {
        setBannerError(
          new CopilotKitError({
            message: "observabilityHooks requires a publicApiKey to function.",
            code: CopilotKitErrorCode.MISSING_PUBLIC_API_KEY_ERROR,
            severity: Severity.CRITICAL,
            visibility: ErrorVisibility.BANNER
          })
        );
        styledConsole.publicApiKeyRequired("observabilityHooks");
      }
    },
    [publicApiKey, observabilityHooks, setBannerError]
  );
  const { open } = useChatContext();
  const prevOpen = useRef(open);
  useEffect(() => {
    if (prevOpen.current !== open) {
      onSetOpen == null ? void 0 : onSetOpen(open);
      if (open) {
        triggerObservabilityHook("onChatExpanded");
      } else {
        triggerObservabilityHook("onChatMinimized");
      }
      prevOpen.current = open;
    }
  }, [open, onSetOpen, triggerObservabilityHook]);
  const memoizedHeader = useMemo(() => /* @__PURE__ */ jsx(Header2, {}), [Header2]);
  const memoizedChildren = useMemo(() => children, [children]);
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    memoizedChildren,
    /* @__PURE__ */ jsxs("div", { className, children: [
      /* @__PURE__ */ jsx(Button2, {}),
      /* @__PURE__ */ jsxs(
        Window2,
        {
          clickOutsideToClose,
          shortcut,
          hitEscapeToClose,
          children: [
            memoizedHeader,
            /* @__PURE__ */ jsx(CopilotChat, __spreadProps(__spreadValues({}, chatProps), { observabilityHooks }))
          ]
        }
      )
    ] })
  ] });
};
var CopilotModal = (_a) => {
  var _b = _a, {
    instructions,
    defaultOpen = false,
    clickOutsideToClose = true,
    hitEscapeToClose = true,
    onSetOpen,
    onSubmitMessage,
    onStopGeneration,
    onReloadMessages,
    shortcut = "/",
    icons,
    labels,
    makeSystemMessage,
    onInProgress,
    Window: Window2 = Window,
    Button: Button2 = Button,
    Header: Header2 = Header,
    Messages: Messages2 = Messages,
    Input: Input2 = Input,
    AssistantMessage: AssistantMessage2 = AssistantMessage,
    UserMessage: UserMessage2 = UserMessage,
    onThumbsUp,
    onThumbsDown,
    onCopy,
    onRegenerate,
    markdownTagRenderers,
    className,
    children,
    observabilityHooks
  } = _b, props = __objRest(_b, [
    "instructions",
    "defaultOpen",
    "clickOutsideToClose",
    "hitEscapeToClose",
    "onSetOpen",
    "onSubmitMessage",
    "onStopGeneration",
    "onReloadMessages",
    "shortcut",
    "icons",
    "labels",
    "makeSystemMessage",
    "onInProgress",
    "Window",
    "Button",
    "Header",
    "Messages",
    "Input",
    "AssistantMessage",
    "UserMessage",
    "onThumbsUp",
    "onThumbsDown",
    "onCopy",
    "onRegenerate",
    "markdownTagRenderers",
    "className",
    "children",
    "observabilityHooks"
  ]);
  const [openState, setOpenState] = React.useState(defaultOpen);
  return /* @__PURE__ */ jsx(ChatContextProvider, { icons, labels, open: openState, setOpen: setOpenState, children: /* @__PURE__ */ jsx(
    CopilotModalInner,
    __spreadProps(__spreadValues({
      observabilityHooks,
      onSetOpen,
      clickOutsideToClose: clickOutsideToClose != null ? clickOutsideToClose : true,
      hitEscapeToClose: hitEscapeToClose != null ? hitEscapeToClose : true,
      shortcut: shortcut != null ? shortcut : "/",
      className,
      Window: Window2,
      Button: Button2,
      Header: Header2,
      instructions,
      onSubmitMessage,
      onStopGeneration,
      onReloadMessages,
      makeSystemMessage,
      onInProgress,
      Messages: Messages2,
      Input: Input2,
      AssistantMessage: AssistantMessage2,
      UserMessage: UserMessage2,
      onThumbsUp,
      onThumbsDown,
      onCopy,
      onRegenerate,
      markdownTagRenderers
    }, props), {
      children
    })
  ) });
};

export {
  CopilotModal
};
//# sourceMappingURL=chunk-GKIR7J5D.mjs.map