import * as e from "react";
import { createContext as t, useCallback as n, useContext as r, useEffect as i, useImperativeHandle as a, useLayoutEffect as o, useMemo as s, useRef as c, useState as l } from "react";
//#region ../../node_modules/.bun/use-stick-to-bottom@1.1.4+e14d3f224186685e/node_modules/use-stick-to-bottom/dist/useStickToBottom.js
var u = {
	damping: .7,
	stiffness: .05,
	mass: 1.25
}, d = 70, f = 1e3 / 60, p = 350, m = !1;
globalThis.document?.addEventListener("mousedown", () => {
	m = !0;
}), globalThis.document?.addEventListener("mouseup", () => {
	m = !1;
}), globalThis.document?.addEventListener("click", () => {
	m = !1;
});
var h = (e = {}) => {
	let [t, r] = l(!1), [i, a] = l(e.initial !== !1), [o, u] = l(!1), h = c(null);
	h.current = e;
	let _ = n(() => {
		if (!m) return !1;
		let e = window.getSelection();
		if (!e || !e.rangeCount) return !1;
		let t = e.getRangeAt(0);
		return t.commonAncestorContainer.contains(E.current) || E.current?.contains(t.commonAncestorContainer);
	}, []), y = n((e) => {
		x.isAtBottom = e, a(e);
	}, []), b = n((e) => {
		x.escapedFromLock = e, r(e);
	}, []), x = s(() => {
		let n;
		return {
			escapedFromLock: t,
			isAtBottom: i,
			resizeDifference: 0,
			accumulated: 0,
			velocity: 0,
			listeners: /* @__PURE__ */ new Set(),
			get scrollTop() {
				return E.current?.scrollTop ?? 0;
			},
			set scrollTop(e) {
				if (E.current) {
					let { scrollBehavior: t } = getComputedStyle(E.current);
					t !== "auto" && (E.current.style.scrollBehavior = "auto"), E.current.scrollTop = e, x.ignoreScrollToTop = E.current.scrollTop, t !== "auto" && (E.current.style.scrollBehavior = t);
				}
			},
			get targetScrollTop() {
				return !E.current || !D.current ? 0 : E.current.scrollHeight - 1 - E.current.clientHeight;
			},
			get calculatedTargetScrollTop() {
				if (!E.current || !D.current) return 0;
				let { targetScrollTop: t } = this;
				if (!e.targetScrollTop) return t;
				if (n?.targetScrollTop === t) return n.calculatedScrollTop;
				let r = Math.max(Math.min(e.targetScrollTop(t, {
					scrollElement: E.current,
					contentElement: D.current
				}), t), 0);
				return n = {
					targetScrollTop: t,
					calculatedScrollTop: r
				}, requestAnimationFrame(() => {
					n = void 0;
				}), r;
			},
			get scrollDifference() {
				return this.calculatedTargetScrollTop - this.scrollTop;
			},
			get isNearBottom() {
				return this.scrollDifference <= d;
			}
		};
	}, []), S = n((e = {}) => {
		typeof e == "string" && (e = { animation: e }), e.preserveScrollPosition || y(!0);
		let t = Date.now() + (Number(e.wait) || 0), n = v(h.current, e.animation), { ignoreEscapes: r = !1 } = e, i, a = x.calculatedTargetScrollTop;
		e.duration instanceof Promise ? e.duration.finally(() => {
			i = Date.now();
		}) : i = t + (e.duration ?? 0);
		let o = async () => {
			let e = new Promise(requestAnimationFrame).then(() => {
				if (!x.isAtBottom) return x.animation = void 0, !1;
				let { scrollTop: s } = x, c = performance.now(), l = (c - (x.lastTick ?? c)) / f;
				if (x.animation ||= {
					behavior: n,
					promise: e,
					ignoreEscapes: r
				}, x.animation.behavior === n && (x.lastTick = c), _() || t > Date.now()) return o();
				if (s < Math.min(a, x.calculatedTargetScrollTop)) {
					if (x.animation?.behavior === n) {
						if (n === "instant") return x.scrollTop = x.calculatedTargetScrollTop, o();
						x.velocity = (n.damping * x.velocity + n.stiffness * x.scrollDifference) / n.mass, x.accumulated += x.velocity * l, x.scrollTop += x.accumulated, x.scrollTop !== s && (x.accumulated = 0);
					}
					return o();
				}
				return i > Date.now() ? (a = x.calculatedTargetScrollTop, o()) : (x.animation = void 0, x.scrollTop < x.calculatedTargetScrollTop ? S({
					animation: v(h.current, h.current.resize),
					ignoreEscapes: r,
					duration: Math.max(0, i - Date.now()) || void 0
				}) : x.isAtBottom);
			});
			return e.then((e) => (requestAnimationFrame(() => {
				x.animation || (x.lastTick = void 0, x.velocity = 0);
			}), e));
		};
		return e.wait !== !0 && (x.animation = void 0), x.animation?.behavior === n ? x.animation.promise : o();
	}, [
		y,
		_,
		x
	]), C = n(() => {
		b(!0), y(!1);
	}, [b, y]), w = n(({ target: e }) => {
		if (e !== E.current) return;
		let { scrollTop: t, ignoreScrollToTop: n } = x, { lastScrollTop: r = t } = x;
		x.lastScrollTop = t, x.ignoreScrollToTop = void 0, n && n > t && (r = n), u(x.isNearBottom), setTimeout(() => {
			if (x.resizeDifference || t === n) return;
			if (_()) {
				b(!0), y(!1);
				return;
			}
			let e = t > r, i = t < r;
			if (x.animation?.ignoreEscapes) {
				x.scrollTop = r;
				return;
			}
			i && (b(!0), y(!1)), e && b(!1), !x.escapedFromLock && x.isNearBottom && y(!0);
		}, 1);
	}, [
		b,
		y,
		_,
		x
	]), T = n(({ target: e, deltaY: t }) => {
		let n = e;
		for (; !["scroll", "auto"].includes(getComputedStyle(n).overflow);) {
			if (!n.parentElement) return;
			n = n.parentElement;
		}
		n === E.current && t < 0 && E.current.scrollHeight > E.current.clientHeight && !x.animation?.ignoreEscapes && (b(!0), y(!1));
	}, [
		b,
		y,
		x
	]), E = g((e) => {
		E.current?.removeEventListener("scroll", w), E.current?.removeEventListener("wheel", T), e?.addEventListener("scroll", w, { passive: !0 }), e?.addEventListener("wheel", T, { passive: !0 });
	}, []), D = g((e) => {
		if (x.resizeObserver?.disconnect(), !e) return;
		let t;
		x.resizeObserver = new ResizeObserver(([e]) => {
			let { height: n } = e.contentRect, r = n - (t ?? n);
			if (x.resizeDifference = r, x.scrollTop > x.targetScrollTop && (x.scrollTop = x.targetScrollTop), u(x.isNearBottom), r >= 0) {
				let e = v(h.current, t ? h.current.resize : h.current.initial);
				S({
					animation: e,
					wait: !0,
					preserveScrollPosition: !0,
					duration: e === "instant" ? void 0 : p
				});
			} else x.isNearBottom && (b(!1), y(!0));
			t = n, requestAnimationFrame(() => {
				setTimeout(() => {
					x.resizeDifference === r && (x.resizeDifference = 0);
				}, 1);
			});
		}), x.resizeObserver?.observe(e);
	}, []);
	return {
		contentRef: D,
		scrollRef: E,
		scrollToBottom: S,
		stopScroll: C,
		isAtBottom: i || o,
		isNearBottom: o,
		escapedFromLock: t,
		state: x
	};
};
function g(e, t) {
	let r = n((t) => (r.current = t, e(t)), t);
	return r;
}
var _ = /* @__PURE__ */ new Map();
function v(...e) {
	let t = { ...u }, n = !1;
	for (let r of e) {
		if (r === "instant") {
			n = !0;
			continue;
		}
		typeof r == "object" && (n = !1, t.damping = r.damping ?? t.damping, t.stiffness = r.stiffness ?? t.stiffness, t.mass = r.mass ?? t.mass);
	}
	let r = JSON.stringify(t);
	return _.has(r) || _.set(r, Object.freeze(t)), n ? "instant" : _.get(r);
}
//#endregion
//#region ../../node_modules/.bun/use-stick-to-bottom@1.1.4+e14d3f224186685e/node_modules/use-stick-to-bottom/dist/StickToBottom.js
var y = t(null), b = typeof window < "u" ? o : i;
function x({ instance: t, children: n, resize: r, initial: i, mass: o, damping: l, stiffness: u, targetScrollTop: d, contextRef: f, ...p }) {
	let m = c(null), g = h({
		mass: o,
		damping: l,
		stiffness: u,
		resize: r,
		initial: i,
		targetScrollTop: e.useCallback((e, t) => (E?.targetScrollTop ?? d)?.(e, t) ?? e, [d])
	}), { scrollRef: _, contentRef: v, scrollToBottom: x, stopScroll: S, isAtBottom: C, escapedFromLock: w, state: T } = t ?? g, E = s(() => ({
		scrollToBottom: x,
		stopScroll: S,
		scrollRef: _,
		isAtBottom: C,
		escapedFromLock: w,
		contentRef: v,
		state: T,
		get targetScrollTop() {
			return m.current;
		},
		set targetScrollTop(e) {
			m.current = e;
		}
	}), [
		x,
		C,
		v,
		_,
		S,
		w,
		T
	]);
	return a(f, () => E, [E]), b(() => {
		_.current && getComputedStyle(_.current).overflow === "visible" && (_.current.style.overflow = "auto");
	}, []), e.createElement(y.Provider, { value: E }, e.createElement("div", { ...p }, typeof n == "function" ? n(E) : n));
}
(function(t) {
	function n({ children: t, scrollClassName: n, ...r }) {
		let i = S();
		return e.createElement("div", {
			ref: i.scrollRef,
			style: {
				height: "100%",
				width: "100%",
				scrollbarGutter: "stable both-edges"
			},
			className: n
		}, e.createElement("div", {
			...r,
			ref: i.contentRef
		}, typeof t == "function" ? t(i) : t));
	}
	t.Content = n;
})(x ||= {});
function S() {
	let e = r(y);
	if (!e) throw Error("use-stick-to-bottom component context must be used within a StickToBottom component");
	return e;
}
//#endregion
export { S as n, x as t };
