import { o as K } from "./rolldown-runtime.js";
import { i as Q, t as U } from "./vendor/react.js";
import { t as X } from "./minimize-2.js";
import { t as u } from "./utils.js";
import { t as Y } from "./create-safe-context.js";
import { t as B } from "./AIInput.js";
import { t as _ } from "./AIMessage.js";
import { n as z, r as Z, t as H } from "./AIScrollButton.js";
var a = /* @__PURE__ */ K(Q(), 1), e = U(), [T, $] = Y("AIContainer");
function E({ children: o, className: t }) {
  return /* @__PURE__ */ (0, e.jsx)("div", {
    "data-slot": "ai-container-header",
    className: u("w-full max-w-[850px] px-4 pt-4", t),
    children: o
  });
}
E.displayName = "AIContainer.Header";
function R({ children: o, renderMessage: t, messages: d, emptyContent: n, showScrollButton: m = !0, showTopFade: s = !0, className: f }) {
  const { containerRef: x, anchorRef: p, isAtBottom: h, scrollToBottom: i, messages: k } = $(), r = d ?? k, b = r.length > 0, g = a.Children.toArray(o).length > 0, v = s ? {
    maskImage: "linear-gradient(to bottom, transparent 0%, black 10%, black 100%)",
    WebkitMaskImage: "linear-gradient(to bottom, transparent 0%, black 10%, black 100%)"
  } : void 0;
  return /* @__PURE__ */ (0, e.jsx)("div", {
    className: u("flex-1 flex flex-col gap-2 items-center w-full max-w-[850px] py-2 overflow-hidden", f),
    children: /* @__PURE__ */ (0, e.jsxs)("div", {
      className: "relative flex-1 w-full overflow-hidden",
      style: v,
      children: [/* @__PURE__ */ (0, e.jsx)("div", {
        ref: x,
        className: "absolute inset-0 overflow-y-auto px-4 pb-20 pt-4 scroll-smooth",
        tabIndex: 0,
        role: "log",
        "aria-label": "Chat messages",
        children: g || b ? /* @__PURE__ */ (0, e.jsxs)("div", {
          className: "flex flex-col gap-8 justify-end min-h-full",
          children: [g ? o : r.map((C, j) => t ? t(C, j) : /* @__PURE__ */ (0, e.jsx)(_, { ...C }, C.id)), /* @__PURE__ */ (0, e.jsx)(H, { ref: p })]
        }) : n
      }), m && (b || g) && /* @__PURE__ */ (0, e.jsx)(z, {
        visible: !h,
        onClick: () => i("smooth"),
        position: "bottom-center",
        variant: "ghost",
        size: "sm",
        className: "absolute z-10"
      })]
    })
  });
}
R.displayName = "AIContainer.Messages";
function q({ children: o, className: t }) {
  return /* @__PURE__ */ (0, e.jsx)("div", {
    "data-slot": "ai-container-footer",
    className: u("w-full max-w-[850px] px-4", t),
    children: o
  });
}
q.displayName = "AIContainer.Footer";
function L({ before: o, after: t, children: d, inputProps: n, className: m }) {
  const s = a.useContext(T), f = a.useMemo(() => {
    if (!n || !s) return n;
    const h = n.onChange;
    return {
      ...n,
      onChange: (i) => {
        s.setInputHasText(i.trim().length > 0), h?.(i);
      }
    };
  }, [n, s]), x = () => s?.setInputIsFocused(!0), p = () => s?.setInputIsFocused(!1);
  return /* @__PURE__ */ (0, e.jsxs)("div", {
    className: u("w-full max-w-[850px] px-4 pb-4", m),
    onFocusCapture: x,
    onBlurCapture: p,
    children: [
      o,
      d ?? /* @__PURE__ */ (0, e.jsx)(B, { ...f }),
      t
    ]
  });
}
L.displayName = "AIContainer.Input";
function O({ children: o, messages: t = [], inputProps: d, showMinimize: n = !1, onMinimize: m, emptyContent: s, showScrollButton: f = !0, scrollThreshold: x = 100, autoScrollOnNewMessage: p = !0, showTopFade: h = !0, className: i, messageAreaClassName: k }) {
  const r = t.length > 0, b = a.useRef(t.length), g = a.Children.toArray(o).length > 0, [v, C] = a.useState(!1), [j, V] = a.useState(!1), { containerRef: y, anchorRef: M, isAtBottom: F, scrollToBottom: I, triggerAutoScroll: S } = Z({
    threshold: x,
    autoScroll: p
  });
  a.useEffect(() => {
    t.length > b.current && S(), b.current = t.length;
  }, [t.length, S]), a.useEffect(() => {
    r && I("instant");
  }, [r, I]);
  const W = a.useMemo(() => ({
    containerRef: y,
    anchorRef: M,
    isAtBottom: F,
    scrollToBottom: I,
    messages: t,
    hasMessages: r,
    inputHasText: v,
    inputIsFocused: j,
    setInputHasText: C,
    setInputIsFocused: V
  }), [
    y,
    M,
    F,
    I,
    t,
    r,
    v,
    j
  ]), D = () => {
    let l = null, A = null, w = null, N = null;
    return a.Children.forEach(o, (c) => {
      if (a.isValidElement(c))
        switch (c.type?.displayName) {
          case "AIContainer.Header":
            l = c;
            break;
          case "AIContainer.Messages":
            A = c;
            break;
          case "AIContainer.Footer":
            w = c;
            break;
          case "AIContainer.Input":
            N = c;
            break;
        }
    }), {
      header: l,
      messagesComponent: A,
      footer: w,
      input: N
    };
  }, G = () => /* @__PURE__ */ (0, e.jsxs)(e.Fragment, { children: [/* @__PURE__ */ (0, e.jsx)("div", {
    className: u("flex-1 flex flex-col gap-2 items-center w-full max-w-[850px] py-2 overflow-hidden", k),
    children: /* @__PURE__ */ (0, e.jsxs)("div", {
      className: "relative flex-1 w-full overflow-hidden",
      style: h ? {
        maskImage: "linear-gradient(to bottom, transparent 0%, black 10%, black 100%)",
        WebkitMaskImage: "linear-gradient(to bottom, transparent 0%, black 10%, black 100%)"
      } : void 0,
      children: [/* @__PURE__ */ (0, e.jsx)("div", {
        ref: y,
        className: "absolute inset-0 overflow-y-auto px-4 pb-20 pt-4 scroll-smooth",
        tabIndex: 0,
        role: "log",
        "aria-label": "Chat messages",
        children: r ? /* @__PURE__ */ (0, e.jsxs)("div", {
          className: "flex flex-col gap-8 justify-end min-h-full",
          children: [t.map((l) => /* @__PURE__ */ (0, e.jsx)(_, { ...l }, l.id)), /* @__PURE__ */ (0, e.jsx)(H, { ref: M })]
        }) : s
      }), f && r && /* @__PURE__ */ (0, e.jsx)(z, {
        visible: !F,
        onClick: () => I("smooth"),
        position: "bottom-center",
        variant: "ghost",
        size: "sm",
        className: "absolute z-10"
      })]
    })
  }), /* @__PURE__ */ (0, e.jsx)("div", {
    className: "w-full max-w-[850px] px-4 pb-4",
    children: /* @__PURE__ */ (0, e.jsx)(B, { ...d })
  })] }), J = () => {
    const { header: l, messagesComponent: A, footer: w, input: N } = D();
    return /* @__PURE__ */ (0, e.jsxs)(e.Fragment, { children: [
      l,
      A,
      w,
      N
    ] });
  };
  return /* @__PURE__ */ (0, e.jsx)(T.Provider, {
    value: W,
    children: /* @__PURE__ */ (0, e.jsxs)("div", {
      "data-slot": "ai-container",
      className: u("relative flex flex-col items-center w-full h-full bg-card/60 rounded-3xl overflow-hidden", i),
      children: [g ? J() : G(), n && /* @__PURE__ */ (0, e.jsx)("button", {
        type: "button",
        onClick: m,
        "aria-label": "Minimize chat",
        className: "absolute bottom-6 end-6 p-1 rounded bg-transparent hover:bg-accent focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring transition-colors duration-150 ease-out",
        children: /* @__PURE__ */ (0, e.jsx)(X, { className: "size-4 text-foreground" })
      })]
    })
  });
}
O.displayName = "AIContainer";
var le = Object.assign(O, {
  Header: E,
  Messages: R,
  Footer: q,
  Input: L
});
export {
  T as n,
  le as t
};

//# sourceMappingURL=AIContainer.js.map