"use client";
import { runIfFn } from "../../utils/run-if-fn.js";
import { EnvironmentContextProvider } from "./use-environment-context.js";
import { useMemo, useState } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
import { getDocument, getWindow } from "@zag-js/dom-query";
//#region src/providers/environment/environment-provider.tsx
var EnvironmentProvider = (props) => {
	const { value, children } = props;
	const [spanRef, setSpanRef] = useState();
	const getRootNode = useMemo(() => {
		return () => runIfFn(value) ?? spanRef?.getRootNode() ?? document;
	}, [value, spanRef]);
	return /* @__PURE__ */ jsxs(EnvironmentContextProvider, {
		value: useMemo(() => ({
			getRootNode,
			getWindow: () => getWindow(getRootNode()),
			getDocument: () => getDocument(getRootNode())
		}), [getRootNode]),
		children: [children, !value && /* @__PURE__ */ jsx("span", {
			hidden: true,
			ref: setSpanRef
		})]
	});
};
//#endregion
export { EnvironmentProvider };
