import React, { useEffect, useRef } from 'react';
import ReactDOM from 'react-dom/client';
const appContainer = document.createElement('div');
appContainer.className = 'codeblitz-keepalive-portal';
appContainer.style.width = '100%';
appContainer.style.height = '100%';
let appMounted = false;
export const KeepAlive = (props) => {
    const anchorRef = useRef(null);
    useEffect(() => {
        var _a;
        if (!appMounted) {
            appMounted = true;
            ReactDOM.createRoot(appContainer).render(React.createElement(React.Fragment, null, props.children));
        }
        (_a = anchorRef === null || anchorRef === void 0 ? void 0 : anchorRef.current) === null || _a === void 0 ? void 0 : _a.insertAdjacentElement('afterend', appContainer);
        return () => {
            var _a, _b;
            try {
                if (((_a = anchorRef === null || anchorRef === void 0 ? void 0 : anchorRef.current) === null || _a === void 0 ? void 0 : _a.parentNode) !== null) {
                    (_b = anchorRef === null || anchorRef === void 0 ? void 0 : anchorRef.current) === null || _b === void 0 ? void 0 : _b.parentNode.removeChild(appContainer);
                }
            }
            catch (error) {
                console.error('[KeepAlive] unmount container error', error);
            }
        };
    }, []);
    return React.createElement("div", { ref: anchorRef, className: 'codeblitz-keepalive' });
};
//# sourceMappingURL=keepAlive.js.map