/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module ChildWindowManager
 */
import "./ChildWindowRenderer.scss";
import * as React from "react";
import * as ReactDOM from "react-dom";
import { TabIdContext } from "../layout/widget/ContentRenderer.js";
import { ThemeManager } from "../theme/ThemeManager.js";
import { PopupRenderer } from "../popup/PopupManager.js";
import { ModalDialogRenderer } from "../dialog/ModalDialogManager.js";
import { ModelessDialogRenderer } from "../dialog/ModelessDialogManager.js";
import { CursorPopupMenu } from "../cursor/cursormenu/CursorMenu.js";
import { copyStyles } from "./CopyStyles.js";
import { ConfigurableUiContext } from "../configurableui/ConfigurableUiContent.js";
import { CursorPopupRenderer } from "../../appui-react.js";
/** @internal */
export function ChildWindowRenderer({ windowManager, }) {
    const [childWindows, setChildWindows] = React.useState(windowManager.openChildWindows);
    React.useEffect(() => {
        return windowManager.onChildWindowsChanged.addListener(() => {
            setChildWindows([...windowManager.openChildWindows]);
        });
    }, [windowManager]);
    return (React.createElement(React.Fragment, null, childWindows.map((childWindow) => {
        return (React.createElement(ChildWindow, { key: childWindow.childWindowId, content: childWindow.content, tabId: childWindow.tabId, window: childWindow.window }));
    })));
}
function ChildWindow({ content, tabId, window }) {
    const { childWindow } = React.useContext(ConfigurableUiContext);
    const container = React.useMemo(() => {
        return window.document.getElementById("root");
    }, [window]);
    const [styled, setStyled] = React.useState(false);
    React.useEffect(() => {
        void (async () => {
            await stylesUtils.copyStyles(window);
            setStyled(true);
        })();
    }, [window]);
    if (!container)
        return null;
    if (!styled)
        return null;
    const ChildWindowWrapper = childWindow ?? DefaultChildWindowWrapper;
    return ReactDOM.createPortal(React.createElement(ChildWindowContext.Provider, { value: window },
        React.createElement(TabIdContext.Provider, { value: tabId },
            React.createElement(ThemeManager, null,
                React.createElement(ChildWindowWrapper, null,
                    React.createElement("div", { className: "uifw-child-window-container-host" },
                        React.createElement(PopupRenderer, null),
                        React.createElement(ModalDialogRenderer, null),
                        React.createElement(ModelessDialogRenderer, null),
                        React.createElement(CursorPopupMenu, null),
                        React.createElement("div", { className: "uifw-child-window-container nz-widget-widget" }, content),
                        React.createElement(CursorPopupRenderer, null)),
                    React.createElement("div", { className: "uifw-childwindow-internalChildWindowManager_portalContainer" }))))), container);
}
const stylesUtils = (() => {
    // eslint-disable-next-line @typescript-eslint/promise-function-async
    const getCopyStylesPromise = (window) => {
        const w = window;
        return w.__appui_copy_styles;
    };
    return {
        copyStyles: async (window) => {
            let copyStylesPromise = getCopyStylesPromise(window);
            if (copyStylesPromise === undefined) {
                copyStylesPromise = copyStyles(window.document);
                const w = window;
                w.__appui_copy_styles = copyStylesPromise;
            }
            return copyStylesPromise;
        },
    };
})();
function DefaultChildWindowWrapper(props) {
    return React.createElement(React.Fragment, null, props.children);
}
/** @internal */
export const ChildWindowContext = React.createContext(undefined);
//# sourceMappingURL=ChildWindowRenderer.js.map