var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import React, { createContext, forwardRef, useContext, } from "react";
import ReactDOM from "react-dom";
import { useProvider } from "../provider/Provider.js";
import { Theme, useThemeInternal } from "../theme/Theme.js";
import { useClientLayoutEffect } from "../utils-external/index.js";
import { useMergeRefs } from "../utils/hooks/index.js";
const PortalContext = createContext(null);
export const Portal = forwardRef((_a, forwardedRef) => {
    var _b;
    var { rootElement, children } = _a, restProps = __rest(_a, ["rootElement", "children"]);
    const providerRootElement = (_b = useProvider()) === null || _b === void 0 ? void 0 : _b.rootElement;
    const parentPortalNode = useContext(PortalContext);
    const [containerElement, setContainerElement] = React.useState(null);
    const [portalNode, setPortalNode] = React.useState(null);
    const containerRef = React.useRef(null);
    const mergedRefs = useMergeRefs(forwardedRef, setPortalNode);
    /**
     * We update container in effect to avoid SSR mismatches.
     */
    useClientLayoutEffect(() => {
        var _a, _b, _c;
        /* Wait for the container to be resolved if explicitly `null`. */
        if ((rootElement !== null && rootElement !== void 0 ? rootElement : providerRootElement) === null) {
            if (containerRef.current) {
                containerRef.current = null;
                setPortalNode(null);
                setContainerElement(null);
            }
            return;
        }
        const resolvedContainer = (_b = (_a = rootElement !== null && rootElement !== void 0 ? rootElement : parentPortalNode) !== null && _a !== void 0 ? _a : providerRootElement) !== null && _b !== void 0 ? _b : (_c = globalThis === null || globalThis === void 0 ? void 0 : globalThis.document) === null || _c === void 0 ? void 0 : _c.body;
        if (resolvedContainer === null) {
            if (containerRef.current) {
                containerRef.current = null;
                setPortalNode(null);
                setContainerElement(null);
            }
            return;
        }
        if (containerRef.current !== resolvedContainer) {
            containerRef.current = resolvedContainer;
            setPortalNode(null);
            setContainerElement(resolvedContainer);
        }
    }, [parentPortalNode, providerRootElement, rootElement]);
    if (!containerElement) {
        return null;
    }
    return ReactDOM.createPortal(React.createElement(PortalDiv, Object.assign({ ref: mergedRefs }, restProps, { "data-aksel-portal": "" }),
        React.createElement(PortalContext.Provider, { value: portalNode }, children)), containerElement);
});
const PortalDiv = forwardRef((props, forwardedRef) => {
    const themeContext = useThemeInternal();
    return (React.createElement(Theme, { theme: themeContext === null || themeContext === void 0 ? void 0 : themeContext.theme, asChild: true, hasBackground: false, "data-color": themeContext === null || themeContext === void 0 ? void 0 : themeContext.color },
        React.createElement("div", Object.assign({ ref: forwardedRef }, props))));
});
export default Portal;
//# sourceMappingURL=Portal.js.map