"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 StatusBar
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.useUiItemsProviderStatusBarItems = void 0;
const React = require("react");
const useActiveStageId_1 = require("../hooks/useActiveStageId");
const useAvailableUiItemsProviders_1 = require("../hooks/useAvailableUiItemsProviders");
const UiFramework_1 = require("../UiFramework");
const UiItemsManager_1 = require("../ui-items-provider/UiItemsManager");
/** Hook that returns items from [[StatusBarItemsManager]].
 * @public
 */
const useUiItemsProviderStatusBarItems = (manager) => {
    const uiItemProviderIds = (0, useAvailableUiItemsProviders_1.useAvailableUiItemsProviders)();
    const stageId = (0, useActiveStageId_1.useActiveStageId)();
    const [items, setItems] = React.useState(manager.items);
    const providersRef = React.useRef("");
    const currentStageRef = React.useRef("");
    // gathers items from registered extensions - dependent on when a UiItemsProvider is register or unregistered and if the
    // current stage's composer allows entries from extensions.
    React.useEffect(() => {
        const uiProviders = uiItemProviderIds.join("-");
        // istanbul ignore else
        if (providersRef.current !== uiProviders ||
            currentStageRef.current !== stageId) {
            currentStageRef.current = stageId;
            const frontstageDef = UiFramework_1.UiFramework.frontstages.activeFrontstageDef;
            // istanbul ignore else
            if (frontstageDef) {
                providersRef.current = uiProviders;
                const statusBarItems = UiItemsManager_1.UiItemsManager.getStatusBarItems(stageId, frontstageDef.usage);
                manager.loadItems(statusBarItems);
                setItems(manager.items);
            }
        }
    }, [manager, uiItemProviderIds, stageId]);
    // handle item changes caused by setter calls to UiFramework.addonStatusBarItemsManager
    React.useEffect(() => {
        return manager.onItemsChanged.addListener((args) => {
            setItems(args.items);
        });
    }, [manager]);
    return items;
};
exports.useUiItemsProviderStatusBarItems = useUiItemsProviderStatusBarItems;
//# sourceMappingURL=useUiItemsProviderStatusBarItems.js.map