import { jsxs, Fragment, jsx } from "react/jsx-runtime";
import { observer, use$ as useSelector, useMount } from "../_vendor/@legendapp/state/react.js";
import { createContext, useRef, useMemo, useContext } from "react";
import { PromptKitTv } from "./tv.js";
import { setLanguage } from "../i18n/index.js";
import { useModelSelection } from "./prompt-input/hooks/use-model-selection.js";
import { isMultimodalModel } from "../utils/model-utils.js";
import { useDragAndDrop } from "./prompt-input/components/prompt-drag-zone/hooks/use-drag-and-drop.js";
import { PromptDragZone } from "./prompt-input/components/prompt-drag-zone/prompt-drag-zone.js";
import { FeedbackDialog } from "./dialog/feedback-dialog.js";
import { AttachmentPreviewDialog } from "./dialog/attachment-preview-dialog.js";
import { coreActions } from "../stores/core-store.js";
import { ratingStore$, ratingActions } from "../stores/rating-store.js";
import { messageActions } from "../stores/message-store.js";
const DEFAULT_ACCEPT = [
  "image/jpeg",
  "image/png",
  "image/gif",
  "image/webp",
  "application/pdf",
  "text/plain",
  "text/markdown",
  "application/json"
];
const DEFAULT_MAX_SIZE = 10 * 1024 * 1024;
const DEFAULT_MAX_FILES = 10;
const DragContext = createContext(null);
function usePromptKitDragAndDrop() {
  const ctx = useContext(DragContext);
  if (!ctx) return null;
  return ctx;
}
const PromptKit = observer(function PromptKit2(props) {
  const {
    enableDragAndDrop = true,
    initialChat,
    className,
    children,
    enableModalSelection,
    language,
    acceptedFileTypes = DEFAULT_ACCEPT,
    maxFileSize = DEFAULT_MAX_SIZE,
    maxFiles = DEFAULT_MAX_FILES,
    maxWidth,
    onAttachmentsChange,
    onFileUploadError,
    ...rest
  } = props;
  const feedbackDialog = useSelector(ratingStore$.feedbackDialog);
  const { currentModel } = useModelSelection(enableModalSelection);
  const isCurrentModelMultimodal = isMultimodalModel(currentModel);
  useMount(() => {
    if (initialChat) {
      coreActions.setCurrentChat(initialChat);
      messageActions.resetMessages();
    }
  });
  if (language) setLanguage(language);
  const {
    isDragging,
    attachments,
    handlers,
    removeAttachment,
    clearAttachments,
    addAttachments,
    handleFileInput,
    updateAttachmentProgress,
    setAttachmentError,
    updateAttachmentUrl,
    setAttachmentUploading,
    getFileById,
    createAbortController,
    cleanupAbortController,
    getAbortController,
    processFiles
  } = useDragAndDrop({
    accept: acceptedFileTypes,
    maxFiles,
    maxSize: maxFileSize,
    onError: onFileUploadError,
    onFilesAdded: (files) => {
      if (!isCurrentModelMultimodal) return;
      onAttachmentsChange?.(attachments.concat(files));
    }
  });
  const containerRef = useRef(null);
  const fileInputRef = useRef(null);
  const allowDropRef = useRef(true);
  const wrappedHandlers = useMemo(
    () => ({
      onDragEnter: (e) => {
        if (!allowDropRef.current) return;
        handlers.onDragEnter(e);
      },
      onDragLeave: (e) => {
        if (!allowDropRef.current) return;
        handlers.onDragLeave(e);
      },
      onDragOver: (e) => {
        if (!allowDropRef.current) return;
        handlers.onDragOver(e);
      },
      onDrop: (e) => {
        if (!allowDropRef.current) return;
        handlers.onDrop(e);
        if (!isCurrentModelMultimodal) {
          clearAttachments();
          onFileUploadError?.("Current model does not support file uploads");
        }
      },
      onPaste: (e) => {
        if (!allowDropRef.current) return;
        if (!isCurrentModelMultimodal) return;
        handlers.onPaste(e);
      }
    }),
    [handlers, isCurrentModelMultimodal, clearAttachments, onFileUploadError]
  );
  const contextValue = useMemo(
    () => ({
      isDragging,
      attachments,
      handlers: wrappedHandlers,
      removeAttachment,
      clearAttachments,
      addAttachments,
      handleFileInput,
      updateAttachmentProgress,
      setAttachmentError,
      updateAttachmentUrl,
      setAttachmentUploading,
      getFileById,
      createAbortController,
      cleanupAbortController,
      getAbortController,
      processFiles,
      openFilePicker: () => {
        fileInputRef.current?.click();
      },
      setDropEnabled: (enabled) => {
        allowDropRef.current = enabled;
      }
    }),
    [
      isDragging,
      attachments,
      wrappedHandlers,
      removeAttachment,
      clearAttachments,
      addAttachments,
      handleFileInput,
      updateAttachmentProgress,
      setAttachmentError,
      updateAttachmentUrl,
      setAttachmentUploading,
      getFileById,
      createAbortController,
      cleanupAbortController,
      getAbortController,
      processFiles
    ]
  );
  const tv = PromptKitTv();
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsx(
      "div",
      {
        ref: containerRef,
        className: tv.root({ className }),
        onDragEnter: enableDragAndDrop ? wrappedHandlers.onDragEnter : void 0,
        onDragOver: enableDragAndDrop ? wrappedHandlers.onDragOver : void 0,
        onDragLeave: enableDragAndDrop ? wrappedHandlers.onDragLeave : void 0,
        onDrop: enableDragAndDrop ? wrappedHandlers.onDrop : void 0,
        onPaste: enableDragAndDrop ? wrappedHandlers.onPaste : void 0,
        style: {
          "--max-width": maxWidth ? `${maxWidth}px` : void 0
        },
        ...rest,
        children: enableDragAndDrop ? /* @__PURE__ */ jsxs(DragContext.Provider, { value: contextValue, children: [
          /* @__PURE__ */ jsx(
            PromptDragZone,
            {
              isDragging,
              unsupported: !isCurrentModelMultimodal,
              maxFileSize,
              maxFiles
            }
          ),
          /* @__PURE__ */ jsx(
            "input",
            {
              ref: fileInputRef,
              type: "file",
              multiple: true,
              accept: acceptedFileTypes?.join(","),
              onChange: handleFileInput,
              className: "hidden"
            }
          ),
          children
        ] }) : children
      }
    ),
    feedbackDialog.isOpen && feedbackDialog.rating && /* @__PURE__ */ jsx(
      FeedbackDialog,
      {
        onSubmit: ({ rating, reason, additionalFeedback }) => {
          const targetMessageId = feedbackDialog.targetMessageId || "";
          if (!targetMessageId) return;
          ratingActions.submitFeedback({
            messageId: targetMessageId,
            rating,
            reason,
            additionalFeedback
          });
        }
      }
    ),
    /* @__PURE__ */ jsx(AttachmentPreviewDialog, {})
  ] });
});
export {
  PromptKit,
  usePromptKitDragAndDrop
};
//# sourceMappingURL=prompt-kit.js.map
