"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Portal = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const react_dom_1 = require("react-dom");
const context_1 = require("nice-ui/lib/utils/portal/context");
const PortalState_1 = require("nice-ui/lib/utils/portal/PortalState");
const Portal = ({ children, parent }) => {
    const parentState = (0, context_1.usePortal)();
    // biome-ignore lint: hook dependency list manually managed
    const state = React.useMemo(() => {
        const state = new PortalState_1.PortalState();
        state.parent = parentState;
        return state;
    }, [parent]);
    const [el] = React.useState(() => document.createElement('div'));
    // biome-ignore lint: hook dependency list manually managed
    React.useLayoutEffect(() => {
        const container = parent || document.body;
        container.appendChild(el);
        state.addRoot(el);
        return () => {
            state.delRoot(el);
            container.removeChild(el);
        };
    }, [parent]);
    return React.createElement(context_1.context.Provider, { value: state }, (0, react_dom_1.createPortal)(children, el));
};
exports.Portal = Portal;
//# sourceMappingURL=Portal.js.map