"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useWidgetDef = exports.WidgetContent = void 0;
/*---------------------------------------------------------------------------------------------
* 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
 */
const React = require("react");
const appui_abstract_1 = require("@itwin/appui-abstract");
const appui_layout_react_1 = require("@itwin/appui-layout-react");
const Frontstage_1 = require("../frontstage/Frontstage");
const useTransientState_1 = require("./useTransientState");
const InternalFrontstageManager_1 = require("../frontstage/InternalFrontstageManager");
/** @internal */
function WidgetContent() {
    var _a, _b, _c;
    const widget = useWidgetDef();
    // istanbul ignore next
    const itemId = (_b = (_a = widget === null || widget === void 0 ? void 0 : widget.id) !== null && _a !== void 0 ? _a : widget === null || widget === void 0 ? void 0 : widget.label) !== null && _b !== void 0 ? _b : "unknown";
    const onSave = React.useCallback(() => {
        // istanbul ignore next
        widget === null || widget === void 0 ? void 0 : widget.saveTransientState();
    }, [widget]);
    const onRestore = React.useCallback(() => {
        // istanbul ignore next
        widget === null || widget === void 0 ? void 0 : widget.restoreTransientState();
    }, [widget]);
    (0, useTransientState_1.useTransientState)(onSave, onRestore);
    return (React.createElement(appui_layout_react_1.ScrollableWidgetContent, { itemId: itemId, providerId: (_c = widget === null || widget === void 0 ? void 0 : widget.initialProps) === null || _c === void 0 ? void 0 : _c.providerId }, widget === null || widget === void 0 ? void 0 : widget.reactNode));
}
exports.WidgetContent = WidgetContent;
/** @internal */
function useWidgetDef() {
    const tabId = React.useContext(appui_layout_react_1.TabIdContext);
    const frontstage = (0, Frontstage_1.useActiveFrontstageDef)();
    const [widgetDef, setWidgetDef] = React.useState(() => frontstage === null || frontstage === void 0 ? void 0 : frontstage.findWidgetDef(tabId));
    React.useEffect(() => {
        const listener = (args) => {
            // istanbul ignore next
            if (args.frontstageDef !== frontstage || !frontstage || frontstage.isStageClosing || frontstage.isApplicationClosing)
                return;
            setWidgetDef(frontstage.findWidgetDef(tabId));
        };
        return InternalFrontstageManager_1.InternalFrontstageManager.onFrontstageNineZoneStateChangedEvent.addListener(listener);
    }, [frontstage, tabId]);
    React.useEffect(() => {
        // istanbul ignore next
        const handlerActivated = () => {
            setWidgetDef(frontstage === null || frontstage === void 0 ? void 0 : frontstage.findWidgetDef(tabId));
        };
        return appui_abstract_1.UiItemsManager.onUiProviderRegisteredEvent.addListener(handlerActivated); // eslint-disable-line deprecation/deprecation
    }, [frontstage, tabId]);
    return widgetDef;
}
exports.useWidgetDef = useWidgetDef;
//# sourceMappingURL=Content.js.map