"use strict";
/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module Widget
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.useTabTransientState = exports.TabIdContext = exports.WidgetContentRenderer = exports.WidgetContentRenderers = void 0;
require("./ContentRenderer.scss");
const React = require("react");
const ReactDOM = require("react-dom");
const NineZone_1 = require("../base/NineZone");
const ContentManager_1 = require("./ContentManager");
const LayoutStore_1 = require("../base/LayoutStore");
/** @internal */
function WidgetContentRenderers() {
    const widgetContent = React.useContext(NineZone_1.WidgetContentNodeContext);
    const toolSettingsContent = React.useContext(NineZone_1.ToolSettingsNodeContext);
    const tabIds = (0, LayoutStore_1.useLayout)((state) => {
        return Object.keys(state.tabs);
    }, true);
    const toolSettingsTabId = (0, LayoutStore_1.useLayout)((state) => state.toolSettings?.tabId);
    return (React.createElement(React.Fragment, null, tabIds.map((tabId) => {
        const children = tabId === toolSettingsTabId ? toolSettingsContent : widgetContent;
        return (React.createElement(WidgetContentRenderer, { key: tabId, tabId: tabId }, children));
    })));
}
exports.WidgetContentRenderers = WidgetContentRenderers;
/** @internal */
function WidgetContentRenderer(props) {
    const renderTo = (0, ContentManager_1.useContainersStore)((state) => state.containers[props.tabId]);
    const unloaded = (0, LayoutStore_1.useLayout)((state) => !!state.tabs[props.tabId].unloaded);
    const widgetContentManager = React.useContext(ContentManager_1.WidgetContentManagerContext);
    const container = React.useRef(undefined);
    if (!container.current) {
        container.current = document.createElement("div");
        container.current.classList.add("nz-widget-contentRenderer");
    }
    React.useLayoutEffect(() => {
        if (!widgetContentManager)
            return;
        const parent = renderTo;
        if (parent) {
            while (parent.firstChild) {
                parent.removeChild(parent.firstChild);
            }
            parent.appendChild(container.current);
            widgetContentManager.onRestoreTransientState.raiseEvent(props.tabId);
        }
        return () => {
            for (const child of parent?.children || []) {
                if (child === container.current) {
                    parent.removeChild(child);
                    return;
                }
            }
        };
    }, [renderTo, widgetContentManager, props.tabId]);
    return ReactDOM.createPortal(React.createElement("div", { style: { height: "100%", width: "100%" }, id: `content-container:${props.tabId}` },
        React.createElement(exports.TabIdContext.Provider, { value: props.tabId }, unloaded ? null : props.children)), container.current);
}
exports.WidgetContentRenderer = WidgetContentRenderer;
/** @internal */
exports.TabIdContext = React.createContext(undefined);
exports.TabIdContext.displayName = "nz:TabIdContext";
/** @internal */
function useTabTransientState(tabId, onSave, onRestore) {
    const widgetContentManager = React.useContext(ContentManager_1.WidgetContentManagerContext);
    React.useEffect(() => {
        if (!widgetContentManager)
            return;
        return widgetContentManager.onSaveTransientState.addListener((id) => {
            tabId === id && onSave && onSave();
        });
    }, [widgetContentManager, onSave, tabId]);
    React.useEffect(() => {
        if (!widgetContentManager)
            return;
        return widgetContentManager.onRestoreTransientState.addListener((id) => {
            tabId === id && onRestore && onRestore();
        });
    }, [widgetContentManager, onRestore, tabId]);
}
exports.useTabTransientState = useTabTransientState;
//# sourceMappingURL=ContentRenderer.js.map