"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (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 (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Portal = exports.PortalHost = void 0;
const react_1 = __importStar(require("react"));
const react_dom_1 = require("react-dom");
const id_1 = require("../../utilities/id");
const Portal_css_1 = __importDefault(require("./Portal.css"));
const PORTAL_HOST_ID = 'PortalHost';
const createId = id_1.createIdCreator('Portal');
function PortalHost() {
    return <div id={PORTAL_HOST_ID}/>;
}
exports.PortalHost = PortalHost;
function Portal({ children }) {
    const [node, setNode] = react_1.useState(null);
    const id = id_1.useId(undefined, createId);
    react_1.useEffect(() => {
        var _a;
        const portal = (_a = document.getElementById(PORTAL_HOST_ID)) !== null && _a !== void 0 ? _a : document.body;
        const node = document.createElement('div');
        node.setAttribute('id', id);
        node.setAttribute('class', Portal_css_1.default.Portal);
        portal.appendChild(node);
        setNode(node);
        return () => {
            portal.removeChild(node);
        };
    }, [id]);
    return node && react_dom_1.createPortal(children, node);
}
exports.Portal = Portal;
