import { o as j } from "./rolldown-runtime.js";
import { i as k, t as w } from "./vendor/react.js";
import { t as I } from "./arrow-down.js";
import { t as y } from "./proxy.js";
import { t as _ } from "./AnimatePresence.js";
import { t as N } from "./Button.js";
import { t as H } from "./dist.js";
import { t as x } from "./utils.js";
var e = /* @__PURE__ */ j(k(), 1), f = w(), T = H("fixed flex items-center justify-center shadow-lg transition-all", {
  variants: {
    variant: {
      default: "bg-background hover:bg-muted border border-border",
      primary: "bg-primary/10 hover:bg-primary/90 text-primary-foreground",
      ghost: "bg-card/80 hover:bg-background supports-backdrop-filter:backdrop-blur-sm border border-border"
    },
    size: {
      sm: "size-8 rounded-full",
      md: "size-10 rounded-full",
      lg: "size-12 rounded-xl"
    },
    position: {
      "bottom-center": "bottom-4 start-1/2 -translate-x-1/2 rtl:translate-x-1/2",
      "bottom-right": "bottom-4 end-4",
      "bottom-left": "bottom-4 start-4"
    }
  },
  defaultVariants: {
    variant: "default",
    size: "md",
    position: "bottom-center"
  }
});
function U({ threshold: c = 50, behavior: s = "smooth", autoScroll: u = !1 } = {}) {
  const i = e.useRef(null), m = e.useRef(null), [b, d] = e.useState(!0), [p, h] = e.useState(!0), n = e.useCallback((t) => {
    const r = t ?? s;
    m.current ? m.current.scrollIntoView({ behavior: r }) : i.current && (i.current.scrollTop = i.current.scrollHeight);
  }, [s]), a = e.useCallback(() => {
    const t = i.current;
    if (!t) return !0;
    const { scrollTop: r, scrollHeight: l, clientHeight: o } = t;
    return l - r - o <= c;
  }, [c]);
  return e.useEffect(() => {
    const t = i.current;
    if (!t) return;
    const r = () => {
      const l = a();
      d((o) => o === l ? o : l), u && l && h((o) => o || !0);
    };
    return r(), t.addEventListener("scroll", r, { passive: !0 }), () => t.removeEventListener("scroll", r);
  }, [a, u]), e.useEffect(() => {
    const t = i.current;
    if (!t) return;
    const r = new ResizeObserver(() => {
      const l = a();
      d((o) => o === l ? o : l);
    });
    return r.observe(t), () => r.disconnect();
  }, [a]), {
    isAtBottom: b,
    scrollToBottom: n,
    containerRef: i,
    anchorRef: m,
    shouldAutoScroll: p,
    handleUserScroll: e.useCallback(() => {
      a() || h(!1);
    }, [a]),
    triggerAutoScroll: e.useCallback(() => {
      u && p && requestAnimationFrame(() => {
        n("smooth");
      });
    }, [
      u,
      p,
      n
    ])
  };
}
function E({ scrollRef: c, containerRef: s, threshold: u = 50, onClick: i, visible: m, variant: b, size: d, position: p, label: h = "Scroll to bottom", unreadCount: n, className: a }) {
  const [t, r] = e.useState(!1);
  e.useEffect(() => {
    if (m !== void 0) return;
    const g = s?.current;
    if (!g) return;
    const v = () => {
      const { scrollTop: A, scrollHeight: B, clientHeight: z } = g;
      r(B - A - z > u);
    };
    return v(), g.addEventListener("scroll", v, { passive: !0 }), () => g.removeEventListener("scroll", v);
  }, [
    s,
    u,
    m
  ]);
  const l = m ?? t, o = () => {
    i ? i() : c?.current ? c.current.scrollIntoView({ behavior: "smooth" }) : s?.current && (s.current.scrollTop = s.current.scrollHeight);
  }, S = d === "sm" ? "size-4" : d === "lg" ? "size-6" : "size-5";
  return /* @__PURE__ */ (0, f.jsx)(_, { children: l && /* @__PURE__ */ (0, f.jsxs)(y.div, {
    "data-slot": "ai-scroll-button",
    initial: {
      opacity: 0,
      y: 20,
      scale: 0.9
    },
    animate: {
      opacity: 1,
      y: 0,
      scale: 1
    },
    exit: {
      opacity: 0,
      y: 20,
      scale: 0.9
    },
    transition: {
      duration: 0.2,
      ease: [
        0.4,
        0,
        0.2,
        1
      ]
    },
    className: x(T({
      variant: b,
      size: d,
      position: p
    }), "motion-reduce:transition-none", a),
    children: [/* @__PURE__ */ (0, f.jsx)(N, {
      appearance: "ghost",
      variant: "secondary",
      size: "icon",
      onClick: o,
      className: "size-full rounded-full",
      "aria-label": h,
      children: /* @__PURE__ */ (0, f.jsx)(I, { className: x(S, b === "primary" ? "text-primary-foreground" : "text-muted-foreground") })
    }), n !== void 0 && n > 0 && /* @__PURE__ */ (0, f.jsx)(y.div, {
      initial: { scale: 0 },
      animate: { scale: 1 },
      transition: {
        type: "spring",
        stiffness: 500,
        damping: 30
      },
      className: "absolute -top-1 -end-1 flex items-center justify-center min-w-[18px] h-[18px] px-1 rounded-full bg-destructive text-destructive-foreground text-xs font-medium motion-reduce:transition-none",
      "aria-label": `${n > 99 ? "99+" : n} new messages`,
      children: n > 99 ? "99+" : n
    })]
  }) });
}
E.displayName = "AIScrollButton";
var V = e.forwardRef(({ className: c }, s) => /* @__PURE__ */ (0, f.jsx)("div", {
  ref: s,
  className: x("h-px w-full", c),
  "aria-hidden": !0
}));
V.displayName = "AIScrollAnchor";
export {
  E as n,
  U as r,
  V as t
};

//# sourceMappingURL=AIScrollButton.js.map