import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE_react_dom__ from "react-dom";
import * as __WEBPACK_EXTERNAL_MODULE__render_mjs__ from "./render.mjs";
const PortalMap = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observable)(new Map());
const createPortalProvider = (id)=>{
    const Portal = (props)=>{
        if (props.id && !PortalMap.has(props.id)) PortalMap.set(props.id, null);
        return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_react__.Fragment, {
            children: [
                props.children,
                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.Observer, {
                    children: ()=>{
                        if (!props.id) return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.Fragment, {});
                        const portal = PortalMap.get(props.id);
                        if (portal) return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_dom__.createPortal)(portal, document.body);
                        return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.Fragment, {});
                    }
                })
            ]
        });
    };
    Portal.defaultProps = {
        id
    };
    return Portal;
};
function createPortalRoot(host, id) {
    function render(renderer) {
        if (PortalMap.has(id)) PortalMap.set(id, null == renderer ? void 0 : renderer());
        else if (host) (0, __WEBPACK_EXTERNAL_MODULE__render_mjs__.render)(/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_react__.Fragment, {
            children: null == renderer ? void 0 : renderer()
        }), host);
    }
    function unmount() {
        if (PortalMap.has(id)) PortalMap.set(id, null);
        if (host) {
            const unmountResult = (0, __WEBPACK_EXTERNAL_MODULE__render_mjs__.unmount)(host);
            if (unmountResult && host.parentNode) {
                var _host_parentNode;
                null === (_host_parentNode = host.parentNode) || void 0 === _host_parentNode || _host_parentNode.removeChild(host);
            }
        }
    }
    return {
        render,
        unmount
    };
}
export { createPortalProvider, createPortalRoot };
