"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const react = require("../_vendor/@legendapp/state/react.cjs");
const React = require("react");
const tv = require("./tv.cjs");
const index = require("../i18n/index.cjs");
const useModelSelection = require("./prompt-input/hooks/use-model-selection.cjs");
const modelUtils = require("../utils/model-utils.cjs");
const useDragAndDrop = require("./prompt-input/components/prompt-drag-zone/hooks/use-drag-and-drop.cjs");
const promptDragZone = require("./prompt-input/components/prompt-drag-zone/prompt-drag-zone.cjs");
const feedbackDialog = require("./dialog/feedback-dialog.cjs");
const attachmentPreviewDialog = require("./dialog/attachment-preview-dialog.cjs");
const coreStore = require("../stores/core-store.cjs");
const ratingStore = require("../stores/rating-store.cjs");
const messageStore = require("../stores/message-store.cjs");
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 = React.createContext(null);
function usePromptKitDragAndDrop() {
  const ctx = React.useContext(DragContext);
  if (!ctx) return null;
  return ctx;
}
const PromptKit = react.observer(function PromptKit2(props) {
  const {
    enableDragAndDrop = true,
    initialChat,
    className,
    children,
    enableModalSelection,
    language,
    acceptedFileTypes = DEFAULT_ACCEPT,
    maxFileSize = DEFAULT_MAX_SIZE,
    maxFiles = DEFAULT_MAX_FILES,
    maxWidth,
    viewportHeight,
    onAttachmentsChange,
    onFileUploadError,
    ...rest
  } = props;
  const feedbackDialog$1 = react.use$(ratingStore.ratingStore$.feedbackDialog);
  const { currentModel } = useModelSelection.useModelSelection(enableModalSelection);
  const isCurrentModelMultimodal = modelUtils.isMultimodalModel(currentModel);
  react.useMount(() => {
    if (initialChat) {
      coreStore.coreActions.setCurrentChat(initialChat);
      messageStore.messageActions.resetMessages();
    }
  });
  if (language) index.setLanguage(language);
  const {
    isDragging,
    attachments,
    handlers,
    removeAttachment,
    clearAttachments,
    addAttachments,
    handleFileInput,
    updateAttachmentProgress,
    setAttachmentError,
    updateAttachmentUrl,
    updateAttachmentExtraData,
    setAttachmentUploading,
    getFileById,
    createAbortController,
    cleanupAbortController,
    getAbortController,
    processFiles
  } = useDragAndDrop.useDragAndDrop({
    accept: acceptedFileTypes,
    maxFiles,
    maxSize: maxFileSize,
    onError: onFileUploadError,
    onFilesAdded: (files) => {
      if (!isCurrentModelMultimodal) return;
      onAttachmentsChange?.(attachments.concat(files));
    }
  });
  const containerRef = React.useRef(null);
  const fileInputRef = React.useRef(null);
  const allowDropRef = React.useRef(true);
  const wrappedHandlers = React.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 = React.useMemo(
    () => ({
      isDragging,
      attachments,
      handlers: wrappedHandlers,
      removeAttachment,
      clearAttachments,
      addAttachments,
      handleFileInput,
      updateAttachmentProgress,
      setAttachmentError,
      updateAttachmentUrl,
      updateAttachmentExtraData,
      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,
      updateAttachmentExtraData,
      setAttachmentUploading,
      getFileById,
      createAbortController,
      cleanupAbortController,
      getAbortController,
      processFiles
    ]
  );
  const tv$1 = tv.PromptKitTv();
  return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
    /* @__PURE__ */ jsxRuntime.jsx(
      "div",
      {
        ref: containerRef,
        className: tv$1.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,
          "--viewport-height": viewportHeight ? `${viewportHeight}px` : void 0
        },
        ...rest,
        children: enableDragAndDrop ? /* @__PURE__ */ jsxRuntime.jsxs(DragContext.Provider, { value: contextValue, children: [
          /* @__PURE__ */ jsxRuntime.jsx(
            promptDragZone.PromptDragZone,
            {
              isDragging,
              unsupported: !isCurrentModelMultimodal,
              maxFileSize,
              maxFiles
            }
          ),
          /* @__PURE__ */ jsxRuntime.jsx(
            "input",
            {
              ref: fileInputRef,
              type: "file",
              multiple: true,
              accept: acceptedFileTypes?.join(","),
              onChange: handleFileInput,
              className: "hidden"
            }
          ),
          children
        ] }) : children
      }
    ),
    feedbackDialog$1.isOpen && feedbackDialog$1.rating && /* @__PURE__ */ jsxRuntime.jsx(
      feedbackDialog.FeedbackDialog,
      {
        onSubmit: ({ rating, reason, additionalFeedback }) => {
          const targetMessageId = feedbackDialog$1.targetMessageId || "";
          if (!targetMessageId) return;
          ratingStore.ratingActions.submitFeedback({
            messageId: targetMessageId,
            rating,
            reason,
            additionalFeedback
          });
        }
      }
    ),
    /* @__PURE__ */ jsxRuntime.jsx(attachmentPreviewDialog.AttachmentPreviewDialog, {})
  ] });
});
exports.PromptKit = PromptKit;
exports.usePromptKitDragAndDrop = usePromptKitDragAndDrop;
//# sourceMappingURL=prompt-kit.cjs.map
