"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require$$0 = require("react/jsx-runtime");
const designSystem = require("@choiceform/design-system");
const react = require("../../_vendor/@legendapp/state/react.cjs");
const React = require("react");
const index = require("../../_vendor/usehooks-ts/dist-index.cjs");
const index$1 = require("../../i18n/index.cjs");
const coreStore = require("../../stores/core-store.cjs");
const promptKit = require("../prompt-kit.cjs");
const tv = require("./tv.cjs");
const useFileUploadHandler = require("./hooks/use-file-upload-handler.cjs");
const useInputState = require("./hooks/use-input-state.cjs");
const useMessageSending = require("./hooks/use-message-sending.cjs");
const useModelSelection = require("./hooks/use-model-selection.cjs");
const useVoiceInput = require("./hooks/use-voice-input.cjs");
const messageUtils = require("./utils/message-utils.cjs");
const promptInputContext = require("./context/prompt-input-context.cjs");
const promptAttachmentPreview = require("./components/prompt-attachment-preview/prompt-attachment-preview.cjs");
const promptTextArea = require("./components/prompt-text-area/prompt-text-area.cjs");
const promptCharacterCount = require("./components/prompt-character-count/prompt-character-count.cjs");
const promptContextMenu = require("./components/prompt-context-menu/prompt-context-menu.cjs");
const promptVoiceInput = require("./components/prompt-voice-input/prompt-voice-input.cjs");
const promptModelMenu = require("./components/prompt-model-menu/prompt-model-menu.cjs");
const promptSendButton = require("./components/prompt-send-button/prompt-send-button.cjs");
const promptStateBar = require("./components/prompt-state-bar/prompt-state-bar.cjs");
const promptMessageQueue = require("./components/prompt-message-queue/prompt-message-queue.cjs");
const promptPlanTodoList = require("./components/prompt-plan-todo-list/prompt-plan-todo-list.cjs");
const promptResearchSwitch = require("./components/prompt-research-switch/prompt-research-switch.cjs");
const usePlanUpdates = require("./components/prompt-plan-todo-list/hooks/use-plan-updates.cjs");
const modelUtils = require("../../utils/model-utils.cjs");
const PromptInputBase = react.observer(function PromptInput2(props) {
  const {
    beforeOnSendMessage,
    className,
    value,
    mentionTriggers,
    mentionPrefix,
    placeholder,
    minHeight = 52,
    maxHeight = 256,
    size = "large",
    variant = "default",
    onChange,
    onAttachmentsChange,
    enableModalSelection,
    onSendMessage,
    onCreateNewChat,
    onFileUploadError,
    onFileUpload,
    onMentionSelect,
    children
  } = props;
  const inputContainerRef = React.useRef(null);
  const dragCtx = promptKit.usePromptKitDragAndDrop();
  const removeAttachment = React.useMemo(
    () => dragCtx?.removeAttachment || (() => {
    }),
    [dragCtx?.removeAttachment]
  );
  const clearAttachments = React.useMemo(
    () => dragCtx?.clearAttachments || (() => {
    }),
    [dragCtx?.clearAttachments]
  );
  const addAttachments = React.useMemo(
    () => dragCtx?.addAttachments || (() => {
    }),
    [dragCtx?.addAttachments]
  );
  const updateAttachmentProgress = React.useMemo(
    () => dragCtx?.updateAttachmentProgress || (() => {
    }),
    [dragCtx?.updateAttachmentProgress]
  );
  const setAttachmentError = React.useMemo(
    () => dragCtx?.setAttachmentError || (() => {
    }),
    [dragCtx?.setAttachmentError]
  );
  const updateAttachmentUrl = React.useMemo(
    () => dragCtx?.updateAttachmentUrl || (() => {
    }),
    [dragCtx?.updateAttachmentUrl]
  );
  const setAttachmentUploading = React.useMemo(
    () => dragCtx?.setAttachmentUploading || (() => {
    }),
    [dragCtx?.setAttachmentUploading]
  );
  const getFileById = React.useMemo(
    () => dragCtx?.getFileById || (() => void 0),
    [dragCtx?.getFileById]
  );
  const createAbortController = React.useMemo(
    () => dragCtx?.createAbortController || (() => new AbortController()),
    [dragCtx?.createAbortController]
  );
  const cleanupAbortController = React.useMemo(
    () => dragCtx?.cleanupAbortController || (() => {
    }),
    [dragCtx?.cleanupAbortController]
  );
  const { handleFilesUpload } = useFileUploadHandler.useFileUploadHandler({
    onFileUpload,
    onFileUploadError,
    updateAttachmentProgress,
    setAttachmentError,
    updateAttachmentUrl,
    setAttachmentUploading,
    getFileById,
    createAbortController,
    cleanupAbortController
  });
  const {
    inputValue,
    contextInputRef,
    setInputValue,
    clearInput,
    focusInput,
    handleKeyDown,
    handleInputChange,
    handleCompositionStart,
    handleCompositionEnd
  } = useInputState.useInputState({
    value,
    onChange,
    onSendMessage: () => {
      const currentAttachments = dragCtx?.attachments || [];
      handleSendMessage(
        inputValue.text,
        currentAttachments,
        clearInput,
        focusInput,
        getCurrentModelId()
      );
      if (dragCtx?.clearAttachments) {
        dragCtx.clearAttachments();
      }
    }
  });
  const { getCurrentModelId, currentModel } = useModelSelection.useModelSelection(enableModalSelection);
  const { currentPlan } = usePlanUpdates.usePlanUpdates();
  const {
    isVoiceSupported,
    isVoiceListening,
    handleVoiceMouseDown,
    handleVoiceMouseUp,
    handleVoiceClick,
    useControlKeyVoiceInput
  } = useVoiceInput.useVoiceInput({
    inputValue,
    language: "zh-CN",
    onChange,
    onInputChange: handleInputChange,
    onSetInputValue: setInputValue
  });
  const [isInputFocused, setIsInputFocused] = React.useState(false);
  useControlKeyVoiceInput(isInputFocused);
  const isCurrentModelMultimodal = modelUtils.isMultimodalModel(currentModel);
  const attachmentEnabled = isCurrentModelMultimodal && Boolean(dragCtx);
  const {
    streamingStatus,
    handleSendMessage,
    handleStopStreaming,
    getSendButtonState,
    hasPendingMessages,
    sendJumpQueueMessage
  } = useMessageSending.useMessageSending({
    onSendMessage,
    onCreateNewChat,
    beforeOnSendMessage,
    onFileUploadError
  });
  const hasUnuploadedAttachments = messageUtils.hasUnuploadedFiles(dragCtx?.attachments || []);
  React.useEffect(() => {
    onAttachmentsChange?.(dragCtx?.attachments || []);
  }, [dragCtx?.attachments, onAttachmentsChange]);
  const processedAttachmentIdsRef = React.useRef(/* @__PURE__ */ new Set());
  React.useEffect(() => {
    const attachments = dragCtx?.attachments || [];
    if (!attachments.length) return;
    if (!attachmentEnabled) return;
    const newPending = attachments.filter(
      (a) => a.uploadStatus === "pending" && !processedAttachmentIdsRef.current.has(a.id)
    );
    if (newPending.length > 0) {
      newPending.forEach((a) => processedAttachmentIdsRef.current.add(a.id));
      handleFilesUpload(newPending);
    }
  }, [dragCtx?.attachments, attachmentEnabled, handleFilesUpload, clearAttachments]);
  const handleFileButtonClick = index.useEventCallback(() => {
    if (!attachmentEnabled) {
      return;
    }
    if (dragCtx?.openFilePicker) {
      dragCtx.openFilePicker();
    }
  });
  const sendButtonState = getSendButtonState(inputValue.text, dragCtx?.attachments?.length || 0);
  const send = index$1.t("prompt-input", "send");
  const enterMessageOrAttach = index$1.t("prompt-input", "enter-message-or-attach");
  const waitingForUpload = index$1.t("prompt-input", "waiting-for-upload");
  const sendButtonTooltip = React.useMemo(() => {
    return messageUtils.getSendButtonTooltip(
      hasUnuploadedAttachments,
      streamingStatus.isStreaming,
      sendButtonState.hasContent,
      {
        send,
        enterMessageOrAttach,
        waitingForUpload
      }
    );
  }, [
    hasUnuploadedAttachments,
    streamingStatus.isStreaming,
    sendButtonState.hasContent,
    send,
    enterMessageOrAttach,
    waitingForUpload
  ]);
  const styles = React.useMemo(() => messageUtils.generateStyles(maxHeight), [maxHeight]);
  const [modelMenuOpen, setModelMenuOpen] = React.useState(false);
  const isDeepThinkingEnabled = react.use$(coreStore.coreStore$.isDeepThinkingEnabled);
  const isStreaming = Boolean(streamingStatus.isStreaming && streamingStatus.currentMessageId);
  const handleStopAndRollback = index.useEventCallback(() => {
    const { combinedContent, combinedAttachments } = handleStopStreaming();
    const currentText = inputValue.text.trim();
    const queueText = combinedContent.trim();
    let finalText = "";
    if (currentText && queueText) {
      finalText = currentText + "\n" + queueText;
    } else {
      finalText = currentText || queueText;
    }
    if (finalText) {
      setTimeout(() => {
        const newValue = {
          text: finalText,
          mentions: []
          // TODO: 未来可以考虑保留原有的mentions
        };
        if (onChange) {
          onChange(newValue);
        }
        setInputValue(newValue);
        if (contextInputRef.current) {
          handleInputChange(newValue);
        }
        if (combinedAttachments.length > 0) {
          addAttachments(combinedAttachments);
        }
        focusInput();
      }, 0);
    } else {
      if (combinedAttachments.length > 0) {
        addAttachments(combinedAttachments);
      }
      focusInput();
    }
  });
  const handleModelMenuKeyDown = index.useEventCallback((event) => {
    if (event.key === "/" && event.metaKey) {
      setModelMenuOpen(true);
      event.preventDefault();
    }
    if (isStreaming && event.key === "Escape") {
      event.preventDefault();
      event.stopPropagation();
      handleStopAndRollback();
    }
  });
  const handleSendMessageClick = index.useEventCallback(() => {
    if (isStreaming) {
      handleStopAndRollback();
    } else {
      const currentAttachments = dragCtx?.attachments || [];
      handleSendMessage(
        inputValue.text,
        currentAttachments,
        clearInput,
        focusInput,
        getCurrentModelId()
      );
      if (dragCtx?.clearAttachments) {
        dragCtx.clearAttachments();
      }
    }
  });
  const toggleDeepThinking = index.useEventCallback(() => {
    coreStore.coreActions.toggleDeepThinking();
  });
  const tv$1 = tv.PromptInputTv({
    hasAttachments: (dragCtx?.attachments?.length || 0) > 0
  });
  const contextValue = React.useMemo(
    () => ({
      // Props
      className,
      enableModalSelection,
      maxHeight,
      mentionTriggers,
      mentionPrefix,
      minHeight,
      placeholder,
      size,
      variant,
      // State and refs
      inputValue,
      contextInputRef,
      inputContainerRef,
      isInputFocused,
      modelMenuOpen,
      isStreaming,
      currentPlan,
      isDeepThinkingEnabled,
      // Computed values
      attachmentEnabled,
      hasUnuploadedAttachments,
      isCurrentModelMultimodal,
      sendButtonState,
      sendButtonTooltip,
      streamingStatus,
      hasPendingMessages,
      // Voice input
      isVoiceSupported,
      isVoiceListening,
      // Drag and drop
      dragCtx,
      removeAttachment,
      // Methods
      setInputValue,
      setIsInputFocused,
      setModelMenuOpen,
      clearInput,
      focusInput,
      handleKeyDown,
      handleInputChange,
      handleCompositionStart,
      handleCompositionEnd,
      handleFileButtonClick,
      handleSendMessageClick,
      handleStopAndRollback,
      handleModelMenuKeyDown,
      handleVoiceMouseDown,
      handleVoiceMouseUp,
      handleVoiceClick,
      getCurrentModelId,
      sendJumpQueueMessage,
      toggleDeepThinking,
      // From props
      onMentionSelect,
      onAttachmentsChange,
      onChange,
      onCreateNewChat,
      onFileUpload,
      onFileUploadError,
      onSendMessage,
      // Styles
      styles
    }),
    [
      className,
      enableModalSelection,
      maxHeight,
      mentionTriggers,
      mentionPrefix,
      minHeight,
      placeholder,
      size,
      variant,
      inputValue,
      contextInputRef,
      isInputFocused,
      modelMenuOpen,
      isStreaming,
      currentPlan,
      attachmentEnabled,
      hasUnuploadedAttachments,
      isCurrentModelMultimodal,
      sendButtonState,
      sendButtonTooltip,
      streamingStatus,
      hasPendingMessages,
      isVoiceSupported,
      isVoiceListening,
      dragCtx,
      removeAttachment,
      setInputValue,
      clearInput,
      focusInput,
      handleKeyDown,
      handleInputChange,
      handleCompositionStart,
      handleCompositionEnd,
      handleFileButtonClick,
      handleSendMessageClick,
      handleStopAndRollback,
      handleModelMenuKeyDown,
      handleVoiceMouseDown,
      handleVoiceMouseUp,
      handleVoiceClick,
      getCurrentModelId,
      sendJumpQueueMessage,
      isDeepThinkingEnabled,
      toggleDeepThinking,
      onMentionSelect,
      onAttachmentsChange,
      onChange,
      onCreateNewChat,
      onFileUpload,
      onFileUploadError,
      onSendMessage,
      styles
    ]
  );
  return /* @__PURE__ */ require$$0.jsx(promptInputContext.PromptInputContext.Provider, { value: contextValue, children: /* @__PURE__ */ require$$0.jsx(
    "div",
    {
      className: designSystem.tcx(tv$1.root(), className),
      style: styles,
      ref: inputContainerRef,
      onKeyDown: handleModelMenuKeyDown,
      children
    }
  ) });
});
const PromptInput = PromptInputBase;
PromptInput.AttachmentPreview = promptAttachmentPreview.PromptAttachmentPreview;
PromptInput.TextArea = promptTextArea.PromptTextArea;
PromptInput.CharacterCount = promptCharacterCount.PromptCharacterCount;
PromptInput.AttachmentMenu = promptContextMenu.PromptContextMenu;
PromptInput.VoiceInput = promptVoiceInput.PromptVoiceInput;
PromptInput.ModelMenu = promptModelMenu.PromptModelMenu;
PromptInput.SendButton = promptSendButton.PromptSendButton;
PromptInput.StateBar = promptStateBar.PromptStateBar;
PromptInput.MessageQueue = promptMessageQueue.PromptMessageQueue;
PromptInput.PlanTodoList = promptPlanTodoList.PromptPlanTodoList;
PromptInput.ResearchSwitch = promptResearchSwitch.PromptResearchSwitch;
exports.PromptInput = PromptInput;
//# sourceMappingURL=prompt-input.cjs.map
