import { jsx as a, jsxs as _ } from "react/jsx-runtime";
import P, { createContext as k, useState as C, useRef as T, useLayoutEffect as I, useContext as H } from "react";
import { Textarea as R } from "../atoms/textarea.js";
import { TooltipProvider as $, Tooltip as D, TooltipTrigger as j, TooltipContent as K } from "../atoms/tooltip.js";
import { cn as x } from "../../../lib/utils.js";
const v = k({
  isLoading: !1,
  value: "",
  setValue: () => {
  },
  maxHeight: 240,
  onSubmit: void 0,
  disabled: !1,
  textareaRef: P.createRef()
});
function y() {
  return H(v);
}
function M({
  className: r,
  isLoading: o = !1,
  maxHeight: n = 240,
  value: s,
  onValueChange: i,
  onSubmit: u,
  children: e,
  disabled: p = !1,
  onClick: d,
  ...c
}) {
  const [f, m] = C(s || ""), h = T(null), g = (l) => {
    m(l), i?.(l);
  }, t = (l) => {
    const b = l.target, w = b ? l.currentTarget.contains(b) : !1;
    !p && w && h.current?.focus(), d?.(l);
  };
  return /* @__PURE__ */ a($, { children: /* @__PURE__ */ a(
    v.Provider,
    {
      value: {
        isLoading: o,
        value: s ?? f,
        setValue: i ?? g,
        maxHeight: n,
        onSubmit: u,
        disabled: p,
        textareaRef: h
      },
      children: /* @__PURE__ */ a(
        "div",
        {
          className: x(
            "cursor-text rounded-lg border border-input bg-background p-2 shadow-xs transition-[box-shadow,border-color] focus-within:border focus-within:border-muted-foreground focus-within:shadow-[0_0_4px_color-mix(in_oklch,var(--ring)_4%,transparent)] dark:focus-within:shadow-[0_0_4px_color-mix(in_oklch,var(--ring)_8%,transparent)]",
            r
          ),
          "data-slot": "prompt-input",
          onClick: t,
          ...c,
          children: e
        }
      )
    }
  ) });
}
function W({
  className: r,
  onKeyDown: o,
  disableAutosize: n = !1,
  ...s
}) {
  const { value: i, setValue: u, maxHeight: e, onSubmit: p, disabled: d, textareaRef: c } = y(), f = (t) => {
    !t || n || (t.style.height = "auto", typeof e == "number" ? t.style.height = `${Math.min(t.scrollHeight, e)}px` : t.style.height = `min(${t.scrollHeight}px, ${e})`);
  }, m = (t) => {
    c.current = t, f(t);
  };
  I(() => {
    if (!c.current || n)
      return;
    const t = c.current;
    t.style.height = "auto", typeof e == "number" ? t.style.height = `${Math.min(t.scrollHeight, e)}px` : t.style.height = `min(${t.scrollHeight}px, ${e})`;
  }, [i, e, n]);
  const h = (t) => {
    f(t.target), u(t.target.value);
  }, g = (t) => {
    t.key === "Enter" && !t.shiftKey && (t.preventDefault(), d || p?.()), o?.(t);
  };
  return /* @__PURE__ */ a(
    R,
    {
      className: x(
        "min-h-[56px] w-full resize-none border-none bg-transparent text-foreground shadow-none outline-none focus-visible:ring-0 focus-visible:ring-offset-0 dark:bg-transparent",
        r
      ),
      "data-slot": "prompt-input-textarea",
      onChange: h,
      onKeyDown: g,
      ref: m,
      rows: 1,
      value: i,
      ...s
    }
  );
}
function q({
  children: r,
  className: o,
  ...n
}) {
  return /* @__PURE__ */ a(
    "div",
    {
      className: x("flex items-center gap-2", o),
      "data-slot": "prompt-input-actions",
      ...n,
      children: r
    }
  );
}
function A({
  tooltip: r,
  children: o,
  className: n,
  side: s = "top",
  ...i
}) {
  const { disabled: u } = y();
  return /* @__PURE__ */ _(D, { ...i, children: [
    /* @__PURE__ */ a(
      j,
      {
        disabled: u,
        onClick: (e) => e.stopPropagation(),
        onPointerDown: (e) => {
          e.pointerType !== "touch" && e.preventDefault();
        },
        render: o
      }
    ),
    /* @__PURE__ */ a(K, { className: n, side: s, children: r })
  ] });
}
export {
  M as PromptInput,
  A as PromptInputAction,
  q as PromptInputActions,
  W as PromptInputTextarea
};
