"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_layout_react_1 = require("@itwin/appui-layout-react");
const FrontstageDef_1 = require("../frontstage/FrontstageDef");
const useTransientState_1 = require("./useTransientState");
const core_bentley_1 = require("@itwin/core-bentley");
const UiItemsManager_1 = require("../ui-items-provider/UiItemsManager");
const isProviderItem_1 = require("../ui-items-provider/isProviderItem");
const InternalFrontstageManager_1 = require("../frontstage/InternalFrontstageManager");
const react_error_boundary_1 = require("react-error-boundary");
const itwinui_illustrations_react_1 = require("@itwin/itwinui-illustrations-react");
const itwinui_react_1 = require("@itwin/itwinui-react");
const UiFramework_1 = require("../UiFramework");
function WidgetFallback() {
    const errorMessage = UiFramework_1.UiFramework.translate("widget.errorMessage.unknownError");
    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: errorMessage })));
}
/** @internal */
function WidgetContent() {
    var _a, _b;
    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);
    const providerId = (widget === null || widget === void 0 ? void 0 : widget.initialConfig) && (0, isProviderItem_1.isProviderItem)(widget === null || widget === void 0 ? void 0 : widget.initialConfig)
        ? widget === null || widget === void 0 ? void 0 : widget.initialConfig.providerId
        : undefined;
    return (React.createElement(appui_layout_react_1.ScrollableWidgetContent, { itemId: itemId, providerId: providerId },
        React.createElement(react_error_boundary_1.ErrorBoundary, { FallbackComponent: WidgetFallback }, widget === null || widget === void 0 ? void 0 : widget.reactNode)));
}
exports.WidgetContent = WidgetContent;
/** @internal */
function useWidgetDef() {
    const tabId = React.useContext(appui_layout_react_1.TabIdContext);
    (0, core_bentley_1.assert)(!!tabId);
    const frontstage = (0, FrontstageDef_1.useActiveFrontstageDef)();
    const [widgetDef, setWidgetDef] = React.useState(() => frontstage === null || frontstage === void 0 ? void 0 : frontstage.findWidgetDef(tabId));
    React.useEffect(() => {
        return InternalFrontstageManager_1.InternalFrontstageManager.onFrontstageNineZoneStateChangedEvent.addListener((args) => {
            if (args.frontstageDef !== frontstage ||
                !frontstage ||
                frontstage.isStageClosing ||
                frontstage.isApplicationClosing)
                return;
            setWidgetDef(frontstage.findWidgetDef(tabId));
        });
    }, [frontstage, tabId]);
    React.useEffect(() => {
        return UiItemsManager_1.UiItemsManager.onUiProviderRegisteredEvent.addListener(() => {
            setWidgetDef(frontstage === null || frontstage === void 0 ? void 0 : frontstage.findWidgetDef(tabId));
        });
    }, [frontstage, tabId]);
    return widgetDef;
}
exports.useWidgetDef = useWidgetDef;
//# sourceMappingURL=Content.js.map