"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.useUiItemsProviderBackstageItems = void 0;
const React = require("react");
const useAvailableUiItemsProviders_1 = require("../hooks/useAvailableUiItemsProviders");
const UiItemsManager_1 = require("../ui-items-provider/UiItemsManager");
/** Hook that returns items from [[BackstageItemsManager]].
 * @public
 */
const useUiItemsProviderBackstageItems = (manager) => {
    const uiItemProviderIds = (0, useAvailableUiItemsProviders_1.useAvailableUiItemsProviders)();
    const [items, setItems] = React.useState(manager ? manager.items : /* istanbul ignore next */ []);
    const providersRef = 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) {
            providersRef.current = uiProviders;
            const backstageItems = UiItemsManager_1.UiItemsManager.getBackstageItems();
            manager.loadItems(backstageItems);
            setItems(manager.items);
        }
    }, [manager, uiItemProviderIds]);
    // handle item changes caused by calls to UiFramework.backstageManager
    React.useEffect(() => {
        return manager.onItemsChanged.addListener((args) => {
            setItems(args.items);
        });
    }, [manager]);
    return items;
};
exports.useUiItemsProviderBackstageItems = useUiItemsProviderBackstageItems;
//# sourceMappingURL=useUiItemsProviderBackstageItems.js.map