"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.useActiveViewport = void 0;
const react_1 = require("react");
const core_frontend_1 = require("@itwin/core-frontend");
const SyncUiEventDispatcher_1 = require("../syncui/SyncUiEventDispatcher");
const UiFramework_1 = require("../UiFramework");
/** React hook that maintains the active viewport.
 * @public
 */
function useActiveViewport() {
    const [activeViewport, setActiveViewport] = (0, react_1.useState)(core_frontend_1.IModelApp.viewManager.selectedView);
    (0, react_1.useEffect)(() => {
        // eslint-disable-next-line deprecation/deprecation
        const onActiveContentChanged = (_args) => {
            setActiveViewport(core_frontend_1.IModelApp.viewManager.selectedView);
        };
        // IModelApp.viewManager.onSelectedViewportChanged will often fire before UI components have mounted
        // so use UiFramework.content.onActiveContentChangedEvent which will always trigger once all stage components
        // are loaded and when the IModelApp.viewManager.selectedView changes.
        UiFramework_1.UiFramework.content.onActiveContentChangedEvent.addListener(onActiveContentChanged);
        return () => {
            UiFramework_1.UiFramework.content.onActiveContentChangedEvent.removeListener(onActiveContentChanged);
        };
    }, []);
    (0, react_1.useEffect)(() => {
        const syncIdsOfInterest = [
            SyncUiEventDispatcher_1.SyncUiEventId.ActiveContentChanged,
            SyncUiEventDispatcher_1.SyncUiEventId.ContentControlActivated,
            SyncUiEventDispatcher_1.SyncUiEventId.FrontstageReady,
        ];
        // eslint-disable-next-line deprecation/deprecation
        const handleSyncUiEvent = (args) => {
            if (syncIdsOfInterest.some((value) => args.eventIds.has(value))) {
                const activeContentControl = UiFramework_1.UiFramework.content.getActiveContentControl();
                setActiveViewport(activeContentControl && activeContentControl.viewport);
            }
        };
        return SyncUiEventDispatcher_1.SyncUiEventDispatcher.onSyncUiEvent.addListener(handleSyncUiEvent);
    }, []);
    return activeViewport;
}
exports.useActiveViewport = useActiveViewport;
//# sourceMappingURL=useActiveViewport.js.map