import {
  Textarea_default
} from "./chunk-QIOJXTIQ.mjs";
import {
  PoweredByTag
} from "./chunk-CGEAG65D.mjs";
import {
  useChatContext
} from "./chunk-IEMQ2SQW.mjs";
import {
  usePushToTalk
} from "./chunk-O72ZB5V3.mjs";

// src/components/chat/Input.tsx
import { useMemo, useRef, useState } from "react";
import { useCopilotContext } from "@copilotkit/react-core";
import { jsx, jsxs } from "react/jsx-runtime";
var MAX_NEWLINES = 6;
var Input = ({
  inProgress,
  onSend,
  isVisible = false,
  onStop,
  onUpload,
  hideStopButton = false
}) => {
  var _a, _b;
  const context = useChatContext();
  const copilotContext = useCopilotContext();
  const showPoweredBy = !((_a = copilotContext.copilotApiConfig) == null ? void 0 : _a.publicApiKey);
  const pushToTalkConfigured = copilotContext.copilotApiConfig.textToSpeechUrl !== void 0 && copilotContext.copilotApiConfig.transcribeAudioUrl !== void 0;
  const textareaRef = useRef(null);
  const [isComposing, setIsComposing] = useState(false);
  const handleDivClick = (event) => {
    var _a2;
    const target = event.target;
    if (target.closest("button"))
      return;
    if (target.tagName === "TEXTAREA")
      return;
    (_a2 = textareaRef.current) == null ? void 0 : _a2.focus();
  };
  const [text, setText] = useState("");
  const send = () => {
    var _a2;
    if (inProgress)
      return;
    onSend(text);
    setText("");
    (_a2 = textareaRef.current) == null ? void 0 : _a2.focus();
  };
  const { pushToTalkState, setPushToTalkState } = usePushToTalk({
    sendFunction: onSend,
    inProgress
  });
  const isInProgress = inProgress || pushToTalkState === "transcribing";
  const buttonIcon = isInProgress && !hideStopButton ? context.icons.stopIcon : context.icons.sendIcon;
  const showPushToTalk = pushToTalkConfigured && (pushToTalkState === "idle" || pushToTalkState === "recording") && !inProgress;
  const canSend = useMemo(() => {
    var _a2;
    const interruptEvent = (_a2 = copilotContext.langGraphInterruptAction) == null ? void 0 : _a2.event;
    const interruptInProgress = (interruptEvent == null ? void 0 : interruptEvent.name) === "LangGraphInterruptEvent" && !(interruptEvent == null ? void 0 : interruptEvent.response);
    return !isInProgress && text.trim().length > 0 && pushToTalkState === "idle" && !interruptInProgress;
  }, [(_b = copilotContext.langGraphInterruptAction) == null ? void 0 : _b.event, isInProgress, text, pushToTalkState]);
  const canStop = useMemo(() => {
    return isInProgress && !hideStopButton;
  }, [isInProgress, hideStopButton]);
  const sendDisabled = !canSend && !canStop;
  return /* @__PURE__ */ jsxs("div", { className: `copilotKitInputContainer ${showPoweredBy ? "poweredByContainer" : ""}`, children: [
    /* @__PURE__ */ jsxs("div", { className: "copilotKitInput", onClick: handleDivClick, children: [
      /* @__PURE__ */ jsx(
        Textarea_default,
        {
          ref: textareaRef,
          placeholder: context.labels.placeholder,
          autoFocus: false,
          maxRows: MAX_NEWLINES,
          value: text,
          onChange: (event) => setText(event.target.value),
          onCompositionStart: () => setIsComposing(true),
          onCompositionEnd: () => setIsComposing(false),
          onKeyDown: (event) => {
            if (event.key === "Enter" && !event.shiftKey && !isComposing) {
              event.preventDefault();
              if (canSend) {
                send();
              }
            }
          }
        }
      ),
      /* @__PURE__ */ jsxs("div", { className: "copilotKitInputControls", children: [
        onUpload && /* @__PURE__ */ jsx("button", { onClick: onUpload, className: "copilotKitInputControlButton", children: context.icons.uploadIcon }),
        /* @__PURE__ */ jsx("div", { style: { flexGrow: 1 } }),
        showPushToTalk && /* @__PURE__ */ jsx(
          "button",
          {
            onClick: () => setPushToTalkState(pushToTalkState === "idle" ? "recording" : "transcribing"),
            className: pushToTalkState === "recording" ? "copilotKitInputControlButton copilotKitPushToTalkRecording" : "copilotKitInputControlButton",
            children: context.icons.pushToTalkIcon
          }
        ),
        /* @__PURE__ */ jsx(
          "button",
          {
            disabled: sendDisabled,
            onClick: isInProgress && !hideStopButton ? onStop : send,
            "data-copilotkit-in-progress": inProgress,
            "data-test-id": inProgress ? "copilot-chat-request-in-progress" : "copilot-chat-ready",
            className: "copilotKitInputControlButton",
            children: buttonIcon
          }
        )
      ] })
    ] }),
    /* @__PURE__ */ jsx(PoweredByTag, { showPoweredBy })
  ] });
};

export {
  Input
};
//# sourceMappingURL=chunk-HRVH7TIP.mjs.map