"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.WidgetContentManagerContext = exports.useContainersStore = exports.WidgetContentManager = void 0;
const immer_1 = require("immer");
const React = require("react");
const zustand_1 = require("zustand");
const core_bentley_1 = require("@itwin/core-bentley");
/** @internal */
function WidgetContentManager(props) {
    const setContentContainer = (0, exports.useContainersStore)((state) => state.setContainer);
    const saveTransientStateRef = React.useRef(new core_bentley_1.BeEvent());
    const restoreTransientStateRef = React.useRef(new core_bentley_1.BeEvent());
    const setContainer = React.useCallback((tabId, container) => {
        if (!container) {
            saveTransientStateRef.current.raiseEvent(tabId);
        }
        setContentContainer(tabId, container);
    }, [setContentContainer]);
    const widgetContentManagerContextValue = React.useMemo(() => ({
        setContainer,
        onSaveTransientState: saveTransientStateRef.current,
        onRestoreTransientState: restoreTransientStateRef.current,
    }), [setContainer]);
    return (React.createElement(exports.WidgetContentManagerContext.Provider, { value: widgetContentManagerContextValue }, props.children));
}
exports.WidgetContentManager = WidgetContentManager;
/** @internal */
exports.useContainersStore = (0, zustand_1.create)((set) => ({
    containers: {},
    setContainer: (tabId, container) => {
        set((state) => (0, immer_1.default)(state, (draft) => {
            if (!container) {
                delete draft.containers[tabId];
                return;
            }
            draft.containers[tabId] = (0, immer_1.castDraft)(container);
        }));
    },
}));
/** @internal */
exports.WidgetContentManagerContext = React.createContext(undefined);
exports.WidgetContentManagerContext.displayName = "nz:WidgetContentManagerContext";
//# sourceMappingURL=ContentManager.js.map