/*---------------------------------------------------------------------------------------------
* 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
 */
import * as React from "react";
import { UiItemsManager } from "@itwin/appui-abstract";
import { ScrollableWidgetContent, TabIdContext } from "@itwin/appui-layout-react";
import { useActiveFrontstageDef } from "../frontstage/Frontstage";
import { useTransientState } from "./useTransientState";
import { InternalFrontstageManager } from "../frontstage/InternalFrontstageManager";
/** @internal */
export 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]);
    useTransientState(onSave, onRestore);
    return (React.createElement(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));
}
/** @internal */
export function useWidgetDef() {
    const tabId = React.useContext(TabIdContext);
    const frontstage = 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.onFrontstageNineZoneStateChangedEvent.addListener(listener);
    }, [frontstage, tabId]);
    React.useEffect(() => {
        // istanbul ignore next
        const handlerActivated = () => {
            setWidgetDef(frontstage === null || frontstage === void 0 ? void 0 : frontstage.findWidgetDef(tabId));
        };
        return UiItemsManager.onUiProviderRegisteredEvent.addListener(handlerActivated); // eslint-disable-line deprecation/deprecation
    }, [frontstage, tabId]);
    return widgetDef;
}
//# sourceMappingURL=Content.js.map