function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
/*
 * Copyright 2026 Palantir Technologies, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { Input } from "@base-ui/react/input";
import classNames from "classnames";
import * as React from "react";
import { ActionButton } from "../../base-components/action-button/ActionButton.js";
import styles from "../AipAgentChat.module.css.js";
/**
 * Textarea + send/stop button. Enter sends; Shift+Enter inserts a newline.
 * The button toggles to "Stop" while a stream is in flight (when an
 * `onStop` callback is provided).
 */
export function AipAgentChatComposer({
  isInFlight,
  onSendMessage,
  onStop,
  placeholder,
  className,
  footerLeft
}) {
  const [draft, setDraft] = React.useState("");
  const canSend = !isInFlight && draft.trim().length > 0;
  const handleSend = React.useCallback(() => {
    const trimmed = draft.trim();
    if (trimmed.length === 0) {
      return;
    }
    // Absorb sync throws and unhandled rejections; chat-level errors are
    // surfaced via `onError` on the upstream useChat hook.
    try {
      const result = onSendMessage(trimmed);
      if (result instanceof Promise) {
        result.catch(noop);
      }
    } catch {
      // swallow sync throws
    }
    setDraft("");
  }, [draft, onSendMessage]);
  const handleKeyDown = React.useCallback(event => {
    if (event.key === "Enter" && !event.shiftKey) {
      event.preventDefault();
      if (!isInFlight) {
        handleSend();
      }
    }
  }, [handleSend, isInFlight]);

  // Attach the keydown handler in the `render` prop so it can be typed against
  // the actual rendered <textarea> element rather than base-ui's HTMLInputElement.
  const renderTextarea = React.useCallback(props => /*#__PURE__*/React.createElement("textarea", _extends({}, props, {
    onKeyDown: handleKeyDown,
    rows: 3
  })), [handleKeyDown]);
  return /*#__PURE__*/React.createElement("div", {
    className: classNames(styles.composer, className)
  }, /*#__PURE__*/React.createElement("div", {
    className: styles.inputWrapper
  }, /*#__PURE__*/React.createElement(Input, {
    "aria-label": "Message input",
    className: styles.textarea,
    onValueChange: setDraft,
    placeholder: placeholder,
    value: draft,
    render: renderTextarea
  }), /*#__PURE__*/React.createElement("div", {
    className: styles.inputActions
  }, isInFlight && onStop != null ? /*#__PURE__*/React.createElement(ActionButton, {
    onClick: onStop,
    type: "button"
  }, "Stop") : /*#__PURE__*/React.createElement(ActionButton, {
    disabled: !canSend,
    onClick: handleSend,
    type: "button",
    variant: "primary"
  }, "Send"))), footerLeft != null && /*#__PURE__*/React.createElement("div", {
    className: styles.composerFooterLeft
  }, footerLeft));
}
function noop() {}
//# sourceMappingURL=AipAgentChatComposer.js.map