import { jsx as _jsx } from "react/jsx-runtime";
import { flushSync } from "react-dom";
import { EventDispatcher } from "./event-dispatcher";
import { createRoot } from "react-dom/client";
export class PortalProviderAPI extends EventDispatcher {
    constructor() {
        super();
        this.portals = new Map();
        this.roots = new Map();
    }
    render(Component, container) {
        const root = this.roots.get(container) || createRoot(container);
        flushSync(() => root.render(_jsx(Component, {})));
        this.roots.set(container, root);
    }
    remove(container) {
        // if container is already unmounted (maybe by prosemirror),
        // no need to proceed
        if (!container.parentNode)
            return;
        const root = this.roots.get(container);
        if (!root)
            return;
        this.roots.delete(container);
        try {
            root.unmount();
        }
        catch (_a) {
            // ignore
        }
    }
}
