"use client";
import { jsx as _jsx } from "react/jsx-runtime";
import { createContext, useContext, useEffect, useState } from "react";
export const PORTAL_CONTAINER_ID = "rmd-portal-container";
let portalContainer = null;
const getPortalContainer = ()=>typeof window === "undefined" ? null : document.body;
const context = /*#__PURE__*/ createContext(getPortalContainer());
context.displayName = "PortalContainer";
const { Provider } = context;
/**
 * @internal
 * @since 6.0.0
 */ export function usePortalContainer() {
    return useContext(context);
}
/**
 * **Client Component**
 *
 * This component allows for all child `Portal` components to render within the
 * same container element. If a custom `container` element is not provided, a
 * `<div id="rmd-portal-container"></div>` will be added as the last child to
 * the `document.body` and be used as the container element.
 *
 * @see {@link Portal}
 * @since 6.0.0
 */ export function PortalContainerProvider(props) {
    const { container, children } = props;
    const [value, setValue] = useState(portalContainer);
    useEffect(()=>{
        if (container && "current" in container) {
            setValue(container.current);
            return;
        }
        if (typeof container !== "undefined") {
            setValue(container);
            return;
        }
        if (!portalContainer) {
            portalContainer = document.createElement("div");
            portalContainer.id = PORTAL_CONTAINER_ID;
        }
        if (!document.body.contains(portalContainer)) {
            document.body.appendChild(portalContainer);
        }
        setValue(portalContainer);
        return ()=>{
            if (portalContainer && document.body.contains(portalContainer)) {
                document.body.removeChild(portalContainer);
            }
        };
    }, [
        container
    ]);
    const containerValue = container && "current" in container || !container ? value : container;
    return /*#__PURE__*/ _jsx(Provider, {
        value: containerValue,
        children: children
    });
}

//# sourceMappingURL=PortalContainerProvider.js.map