"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 Hooks
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.useActiveIModelConnection = void 0;
const react_1 = require("react");
const SessionState_1 = require("../redux/SessionState");
const SyncUiEventDispatcher_1 = require("../syncui/SyncUiEventDispatcher");
const UiFramework_1 = require("../UiFramework");
/** React hook that maintains the active IModelConnection. For this hook to work properly the
 * IModelConnection must be set using UiFramework.setIModelConnection method. This also requires
 * that the host app includes the UiFramework reducer into its Redux store.
 * @public
 */
function useActiveIModelConnection() {
    const [activeConnection, setActiveConnection] = (0, react_1.useState)(UiFramework_1.UiFramework.getIModelConnection());
    (0, react_1.useEffect)(() => {
        const handleSyncUiEvent = (args) => {
            const eventIds = [SessionState_1.SessionStateActionId.SetIModelConnection];
            // istanbul ignore else
            if (eventIds.some((value) => args.eventIds.has(value.toLowerCase()))) {
                setActiveConnection(UiFramework_1.UiFramework.getIModelConnection());
            }
        };
        SyncUiEventDispatcher_1.SyncUiEventDispatcher.onSyncUiEvent.addListener(handleSyncUiEvent);
        return () => {
            SyncUiEventDispatcher_1.SyncUiEventDispatcher.onSyncUiEvent.removeListener(handleSyncUiEvent);
        };
    }, [activeConnection]);
    return activeConnection;
}
exports.useActiveIModelConnection = useActiveIModelConnection;
//# sourceMappingURL=useActiveIModelConnection.js.map