"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 core_bentley_1 = require("@itwin/core-bentley");
const itwinui_illustrations_react_1 = require("@itwin/itwinui-illustrations-react");
const itwinui_react_1 = require("@itwin/itwinui-react");
const React = require("react");
const react_error_boundary_1 = require("react-error-boundary");
const FrontstageDef_1 = require("../frontstage/FrontstageDef");
const Content_1 = require("../layout/widget/Content");
const ContentRenderer_1 = require("../layout/widget/ContentRenderer");
const UiItemsManager_1 = require("../ui-items-provider/UiItemsManager");
const isProviderItem_1 = require("../ui-items-provider/isProviderItem");
const useTransientState_1 = require("./useTransientState");
const useTranslation_1 = require("../hooks/useTranslation");
const PopoutThemeProvider_1 = require("../preview/reparent-popout-widgets/PopoutThemeProvider");
function WidgetFallback() {
    const { translate } = (0, useTranslation_1.useTranslation)();
    return (React.createElement("div", { role: "alert", style: { position: "relative", minHeight: 400 } },
        React.createElement(itwinui_react_1.NonIdealState, { svg: React.createElement(itwinui_illustrations_react_1.SvgError, null), heading: translate("widget.errorMessage.unknownError") })));
}
/** @internal */
function WidgetContent() {
    const widget = useWidgetDef();
    const itemId = widget?.id ?? widget?.label ?? "unknown";
    const onSave = React.useCallback(() => {
        widget?.saveTransientState();
    }, [widget]);
    const onRestore = React.useCallback(() => {
        widget?.restoreTransientState();
    }, [widget]);
    (0, useTransientState_1.useTransientState)(onSave, onRestore);
    const providerId = widget?.initialConfig && (0, isProviderItem_1.isProviderItem)(widget?.initialConfig)
        ? widget?.initialConfig.providerId
        : undefined;
    return (React.createElement(PopoutThemeProvider_1.PopoutThemeProvider, null,
        React.createElement(Content_1.ScrollableWidgetContent, { itemId: itemId, providerId: providerId },
            React.createElement(react_error_boundary_1.ErrorBoundary, { FallbackComponent: WidgetFallback }, widget?.reactNode))));
}
exports.WidgetContent = WidgetContent;
/** @internal */
function useWidgetDef() {
    const frontstage = (0, FrontstageDef_1.useActiveFrontstageDef)();
    const tabId = React.useContext(ContentRenderer_1.TabIdContext);
    (0, core_bentley_1.assert)(!!tabId);
    const [widgetDef, setWidgetDef] = React.useState(() => frontstage?.findWidgetDef(tabId));
    const [prevTabId, setPrevTabId] = React.useState(tabId);
    if (prevTabId !== tabId) {
        setPrevTabId(tabId);
        setWidgetDef(frontstage?.findWidgetDef(tabId));
    }
    React.useEffect(() => {
        return UiItemsManager_1.UiItemsManager.onUiProviderRegisteredEvent.addListener(() => {
            setWidgetDef(frontstage?.findWidgetDef(tabId));
        });
    }, [frontstage, tabId]);
    return widgetDef?.id === tabId ? widgetDef : undefined;
}
exports.useWidgetDef = useWidgetDef;
//# sourceMappingURL=Content.js.map