"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Portal = void 0;
const react_1 = __importStar(require("react"));
const react_dom_1 = __importDefault(require("react-dom"));
const Provider_1 = require("../provider/Provider");
const Theme_1 = require("../theme/Theme");
const utils_external_1 = require("../utils-external");
const hooks_1 = require("../utils/hooks");
const PortalContext = (0, react_1.createContext)(null);
exports.Portal = (0, react_1.forwardRef)((_a, forwardedRef) => {
    var _b;
    var { rootElement, children } = _a, restProps = __rest(_a, ["rootElement", "children"]);
    const providerRootElement = (_b = (0, Provider_1.useProvider)()) === null || _b === void 0 ? void 0 : _b.rootElement;
    const parentPortalNode = (0, react_1.useContext)(PortalContext);
    const [containerElement, setContainerElement] = react_1.default.useState(null);
    const [portalNode, setPortalNode] = react_1.default.useState(null);
    const containerRef = react_1.default.useRef(null);
    const mergedRefs = (0, hooks_1.useMergeRefs)(forwardedRef, setPortalNode);
    /**
     * We update container in effect to avoid SSR mismatches.
     */
    (0, utils_external_1.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 react_dom_1.default.createPortal(react_1.default.createElement(PortalDiv, Object.assign({ ref: mergedRefs }, restProps, { "data-aksel-portal": "" }),
        react_1.default.createElement(PortalContext.Provider, { value: portalNode }, children)), containerElement);
});
const PortalDiv = (0, react_1.forwardRef)((props, forwardedRef) => {
    const themeContext = (0, Theme_1.useThemeInternal)();
    return (react_1.default.createElement(Theme_1.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_1.default.createElement("div", Object.assign({ ref: forwardedRef }, props))));
});
exports.default = exports.Portal;
//# sourceMappingURL=Portal.js.map