"use client";
const require_use_did_update = require("../use-did-update/use-did-update.cjs");
let react = require("react");
//#region packages/@mantine/hooks/src/use-focus-return/use-focus-return.ts
function useFocusReturn({ opened, shouldReturnFocus = true }) {
	const lastActiveElement = (0, react.useRef)(null);
	const returnFocus = () => {
		if (lastActiveElement.current && "focus" in lastActiveElement.current && typeof lastActiveElement.current.focus === "function") lastActiveElement.current?.focus({ preventScroll: true });
	};
	require_use_did_update.useDidUpdate(() => {
		let timeout = -1;
		const clearFocusTimeout = (event) => {
			if (event.key === "Tab") window.clearTimeout(timeout);
		};
		document.addEventListener("keydown", clearFocusTimeout);
		if (opened) lastActiveElement.current = document.activeElement;
		else if (shouldReturnFocus) {
			const activeElementAtClose = document.activeElement;
			timeout = window.setTimeout(() => {
				const currentActiveElement = document.activeElement;
				if (currentActiveElement === null || currentActiveElement === document.body || currentActiveElement === activeElementAtClose) returnFocus();
			}, 10);
		}
		return () => {
			window.clearTimeout(timeout);
			document.removeEventListener("keydown", clearFocusTimeout);
		};
	}, [opened, shouldReturnFocus]);
	return returnFocus;
}
//#endregion
exports.useFocusReturn = useFocusReturn;

//# sourceMappingURL=use-focus-return.cjs.map