const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_objectWithoutProperties = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js");
let react = require("react");
react = require_runtime.__toESM(react);
//#region src/plus/productSelect/components/IntersectionObserver.tsx
const _excluded = [
	"children",
	"className",
	"style"
];
const IntersectionObserverContext = (0, react.createContext)({
	initialized: false,
	register: () => {},
	unregister: () => {}
});
const { Provider, Consumer } = IntersectionObserverContext;
const IntersectionObserverProvider = ({ children, root = null, rootMargin = "0px", threshold = 0, priorityMode }) => {
	const [mounted, setMounted] = (0, react.useState)(false);
	const [initialized, setInitialized] = (0, react.useState)(false);
	const observerRef = (0, react.useRef)(null);
	const callbackMapRef = (0, react.useRef)(/* @__PURE__ */ new Map());
	const findCallback = (0, react.useCallback)((target) => {
		return callbackMapRef.current.get(target);
	}, []);
	const selectPriorityEntry = (0, react.useCallback)((entries, mode) => {
		if (entries.length === 0) return void 0;
		if (entries.length === 1) return entries[0];
		switch (mode) {
			case "top": return entries.reduce((prev, curr) => curr.boundingClientRect.top < prev.boundingClientRect.top ? curr : prev);
			case "bottom": return entries.reduce((prev, curr) => curr.boundingClientRect.bottom > prev.boundingClientRect.bottom ? curr : prev);
			case "left": return entries.reduce((prev, curr) => curr.boundingClientRect.left < prev.boundingClientRect.left ? curr : prev);
			case "right": return entries.reduce((prev, curr) => curr.boundingClientRect.right > prev.boundingClientRect.right ? curr : prev);
			default: return entries[0];
		}
	}, []);
	const observerOptions = (0, react.useMemo)(() => ({
		root,
		rootMargin,
		threshold
	}), [
		root,
		rootMargin,
		threshold
	]);
	const createObserver = (0, react.useCallback)(() => {
		if (typeof window !== "undefined" && !window.IntersectionObserver) {
			console.warn("IntersectionObserver is not supported in this browser");
			return null;
		}
		return new IntersectionObserver((entries) => {
			if (entries.length === 0) return;
			try {
				if (!priorityMode) {
					entries.forEach((entry) => {
						const callback = findCallback(entry.target);
						if (callback) callback(entry);
					});
					return;
				}
				const visibleEntries = [];
				const invisibleEntries = [];
				entries.forEach((entry) => {
					if (entry.isIntersecting) visibleEntries.push(entry);
					else invisibleEntries.push(entry);
				});
				invisibleEntries.forEach((entry) => {
					const callback = findCallback(entry.target);
					if (callback) callback(entry);
				});
				if (visibleEntries.length > 0) {
					const selectedEntry = selectPriorityEntry(visibleEntries, priorityMode);
					if (selectedEntry) {
						const callback = findCallback(selectedEntry.target);
						if (callback) callback(selectedEntry);
					}
				}
			} catch (error) {
				console.error("IntersectionObserver callback error:", error);
			}
		}, observerOptions);
	}, [
		observerOptions,
		priorityMode,
		findCallback,
		selectPriorityEntry
	]);
	const register = (0, react.useCallback)(({ ref, callback }) => {
		if ((ref === null || ref === void 0 ? void 0 : ref.current) && (observerRef === null || observerRef === void 0 ? void 0 : observerRef.current)) {
			const element = ref.current;
			if (callbackMapRef.current.has(element)) return;
			callbackMapRef.current.set(element, callback);
			observerRef.current.observe(element);
		}
	}, []);
	const unregister = (0, react.useCallback)(({ ref }) => {
		if ((ref === null || ref === void 0 ? void 0 : ref.current) && (observerRef === null || observerRef === void 0 ? void 0 : observerRef.current)) {
			const element = ref.current;
			callbackMapRef.current.delete(element);
			observerRef.current.unobserve(element);
		}
	}, []);
	(0, react.useEffect)(() => {
		if (mounted) {
			if (observerRef.current) observerRef.current.disconnect();
			observerRef.current = createObserver();
			setInitialized(true);
		}
	}, [mounted, createObserver]);
	(0, react.useEffect)(() => {
		setMounted(true);
		return () => {
			if (observerRef.current) {
				observerRef.current.disconnect();
				callbackMapRef.current.clear();
				setInitialized(false);
			}
		};
	}, []);
	const contextValue = {
		initialized,
		register,
		unregister
	};
	return /* @__PURE__ */ react.default.createElement(Provider, { value: contextValue }, children);
};
const useIntersectionTrigger = (elementRef, options = {}) => {
	const { once = false, onHide, onShow } = options;
	const { initialized, register, unregister } = (0, react.useContext)(IntersectionObserverContext);
	const [isIntersecting, setIsIntersecting] = (0, react.useState)(false);
	const cleanup = (0, react.useCallback)(() => unregister({ ref: elementRef }), [elementRef, unregister]);
	const intersectionCallback = (0, react.useCallback)((entry) => {
		if (entry.isIntersecting) {
			onShow === null || onShow === void 0 || onShow(cleanup);
			if (once) unregister({ ref: elementRef });
		} else onHide === null || onHide === void 0 || onHide(cleanup);
		setIsIntersecting(entry.isIntersecting);
	}, [
		onShow,
		onHide,
		once,
		cleanup,
		unregister,
		elementRef
	]);
	const registerRef = (0, react.useRef)();
	registerRef.current = () => {
		return register({
			ref: elementRef,
			callback: intersectionCallback
		});
	};
	(0, react.useEffect)(() => {
		if (initialized) {
			var _registerRef$current;
			(_registerRef$current = registerRef.current) === null || _registerRef$current === void 0 || _registerRef$current.call(registerRef);
		}
		return () => {
			cleanup();
		};
	}, [initialized, cleanup]);
	return { isIntersecting };
};
const IntersectionTrigger = (_ref) => {
	let { children, className, style = {} } = _ref, options = require_objectWithoutProperties._objectWithoutProperties(_ref, _excluded);
	const elementRef = (0, react.useRef)(null);
	const { isIntersecting } = useIntersectionTrigger(elementRef, options);
	return /* @__PURE__ */ react.default.createElement("div", {
		className,
		ref: elementRef,
		style: require_objectSpread2._objectSpread2({
			width: "100%",
			height: "100%"
		}, style)
	}, children);
};
//#endregion
exports.IntersectionTrigger = IntersectionTrigger;
exports.default = IntersectionObserverProvider;
