"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 SyncUi
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.SyncUiEventDispatcher = exports.SyncUiEventId = void 0;
const core_bentley_1 = require("@itwin/core-bentley");
const core_frontend_1 = require("@itwin/core-frontend");
const UiFramework_1 = require("../UiFramework");
const InternalSyncUiEventDispatcher_1 = require("./InternalSyncUiEventDispatcher");
// cSpell:ignore activecontentchanged backstageevent contentlayoutactivated contentcontrolactivated
// cSpell:ignore frontstageactivated modalfrontstagechanged modaldialogchanged
// cSpell:ignore navigationaidactivated toolactivated widgetstatechanged frontstageactivating
// cSpell:ignore frontstageready activeviewportchanged selectionsetchanged viewstatechanged
// cSpell:ignore accudrawcompassmodechanged accudrawrotationchanged
/** Event Id used to sync UI components. Used to refresh visibility or enable state of control.
 * @public
 */
var SyncUiEventId;
(function (SyncUiEventId) {
    /** AccuDraw compass mode has changed. */
    SyncUiEventId["AccuDrawCompassModeChanged"] = "accudrawcompassmodechanged";
    /** AccuDraw rotation has changed. */
    SyncUiEventId["AccuDrawRotationChanged"] = "accudrawrotationchanged";
    /** The active content as maintained by the ContentViewManager has changed. */
    SyncUiEventId["ActiveContentChanged"] = "activecontentchanged";
    /** The active view maintained by the ViewManager has changed. */
    SyncUiEventId["ActiveViewportChanged"] = "activeviewportchanged";
    /** Backstage has been toggled. */
    SyncUiEventId["BackstageEvent"] = "backstageevent";
    /** A Content Layout has been activated.  */
    SyncUiEventId["ContentLayoutActivated"] = "contentlayoutactivated";
    /** A Content Control maintained by UiFramework.frontstages has been activated.
     * @deprecated in 4.16.0. Use {@link SyncUiEventId.ActiveContentChanged} instead.
     */
    SyncUiEventId["ContentControlActivated"] = "contentcontrolactivated";
    /** A Frontstage is activating. */
    SyncUiEventId["FrontstageActivating"] = "frontstageactivating";
    /** A Frontstage has been activated and the content has been assigned. */
    SyncUiEventId["FrontstageReady"] = "frontstageready";
    /** A Modal Frontstage has been opened or closed. */
    SyncUiEventId["ModalFrontstageChanged"] = "modalfrontstagechanged";
    /** A Modal Dialog has been opened or closed. */
    SyncUiEventId["ModalDialogChanged"] = "modaldialogchanged";
    /** A NavigationAid has been activated. */
    SyncUiEventId["NavigationAidActivated"] = "navigationaidactivated";
    /** An InteractiveTool has been activated via the ToolAdmin. */
    SyncUiEventId["ToolActivated"] = "toolactivated";
    /** The state of a Widget has changed. */
    SyncUiEventId["WidgetStateChanged"] = "widgetstatechanged";
    /** The SelectionSet for the active IModelConnection has changed. */
    SyncUiEventId["SelectionSetChanged"] = "selectionsetchanged";
    /** The list of settings providers registered with SettingsManager has changed. */
    SyncUiEventId["SettingsProvidersChanged"] = "settingsproviderschanged";
    /** The current view state has changed (used by view undo/redo toolbar buttons). */
    SyncUiEventId["ViewStateChanged"] = "viewstatechanged";
    /** The current object the reads and write UI State has changed. */
    SyncUiEventId["UiStateStorageChanged"] = "uistatestoragechanged";
    SyncUiEventId["ShowHideManagerSettingChange"] = "show-hide-setting-change";
    /** The list of feature overrides applied has been changed */
    SyncUiEventId["FeatureOverridesChanged"] = "featureoverrideschanged";
    SyncUiEventId["ViewedModelsChanged"] = "viewedmodelschanged";
})(SyncUiEventId || (exports.SyncUiEventId = SyncUiEventId = {}));
/** This class is used to send eventIds to interested UI components so the component can determine if it needs
 * to refresh its display by calling setState on itself.
 * @public
 */
class SyncUiEventDispatcher {
    static setTimeoutPeriod(period) {
        SyncUiEventDispatcher._uiEventDispatcher.setTimeoutPeriod(period);
    }
    /** Return set of event ids that will be sent to listeners/. */
    static get syncEventIds() {
        return SyncUiEventDispatcher._uiEventDispatcher.syncEventIds;
    }
    /** Return SyncUiEvent so callers can register an event callback. */
    // eslint-disable-next-line deprecation/deprecation
    static get onSyncUiEvent() {
        return SyncUiEventDispatcher._uiEventDispatcher.onSyncUiEvent;
    }
    /** Immediately trigger sync event processing. */
    static dispatchImmediateSyncUiEvent(eventId) {
        SyncUiEventDispatcher._uiEventDispatcher.dispatchImmediateSyncUiEvent(eventId);
    }
    /** Save eventId in Set for processing. */
    static dispatchSyncUiEvent(eventId) {
        if (0 === SyncUiEventDispatcher._uiEventDispatcher.timeoutPeriod) {
            core_bentley_1.Logger.logInfo(UiFramework_1.UiFramework.loggerCategory(this), `[dispatchSyncUiEvent] not processed because timeoutPeriod=0`);
            return;
        }
        SyncUiEventDispatcher._uiEventDispatcher.dispatchSyncUiEvent(eventId);
    }
    /** Save multiple eventIds in Set for processing. */
    static dispatchSyncUiEvents(eventIds) {
        if (0 === SyncUiEventDispatcher._uiEventDispatcher.timeoutPeriod) {
            core_bentley_1.Logger.logInfo(UiFramework_1.UiFramework.loggerCategory(this), `[dispatchSyncUiEvents] not processed because _timeoutPeriod=0`);
        }
        SyncUiEventDispatcher._uiEventDispatcher.dispatchSyncUiEvents(eventIds);
    }
    /** Checks to see if an eventId of interest is contained in the set of eventIds */
    static hasEventOfInterest(eventIds, idsOfInterest) {
        return SyncUiEventDispatcher._uiEventDispatcher.hasEventOfInterest(eventIds, idsOfInterest);
    }
    static _dispatchViewChange() {
        SyncUiEventDispatcher._uiEventDispatcher.dispatchSyncUiEvent(SyncUiEventId.ViewStateChanged);
    }
    static _dispatchFeatureOverridesChange() {
        SyncUiEventDispatcher._uiEventDispatcher.dispatchSyncUiEvent(SyncUiEventId.FeatureOverridesChanged);
    }
    static _dispatchViewedModelsChanged() {
        SyncUiEventDispatcher._uiEventDispatcher.dispatchSyncUiEvent(SyncUiEventId.ViewedModelsChanged);
    }
    /** Initializes the Monitoring of Events that trigger dispatching sync events */
    static initialize() {
        // clear any registered listeners - this should only be encountered in unit test scenarios
        this._unregisterFuncs.forEach((unregister) => unregister());
        this._unregisterFuncs = [];
        this._unregisterFuncs.push(
        // eslint-disable-next-line deprecation/deprecation
        UiFramework_1.UiFramework.frontstages.onContentControlActivatedEvent.addListener(() => {
            SyncUiEventDispatcher.dispatchSyncUiEvent(
            // eslint-disable-next-line deprecation/deprecation
            SyncUiEventId.ContentControlActivated);
        }), UiFramework_1.UiFramework.frontstages.onContentLayoutActivatedEvent.addListener(() => {
            SyncUiEventDispatcher._uiEventDispatcher.dispatchSyncUiEvent(SyncUiEventId.ContentLayoutActivated);
        }), UiFramework_1.UiFramework.frontstages.onFrontstageActivatedEvent.addListener(() => {
            SyncUiEventDispatcher._uiEventDispatcher.dispatchSyncUiEvent(SyncUiEventId.FrontstageActivating);
        }), UiFramework_1.UiFramework.frontstages.onFrontstageReadyEvent.addListener(() => {
            SyncUiEventDispatcher._uiEventDispatcher.dispatchSyncUiEvent(SyncUiEventId.FrontstageReady);
        }), UiFramework_1.UiFramework.frontstages.onModalFrontstageChangedEvent.addListener(() => {
            SyncUiEventDispatcher._uiEventDispatcher.dispatchSyncUiEvent(SyncUiEventId.ModalFrontstageChanged);
        }), UiFramework_1.UiFramework.frontstages.onNavigationAidActivatedEvent.addListener(() => {
            SyncUiEventDispatcher._uiEventDispatcher.dispatchSyncUiEvent(SyncUiEventId.NavigationAidActivated);
        }), UiFramework_1.UiFramework.frontstages.onToolActivatedEvent.addListener(() => {
            SyncUiEventDispatcher._uiEventDispatcher.dispatchSyncUiEvent(SyncUiEventId.ToolActivated);
        }), UiFramework_1.UiFramework.frontstages.onWidgetStateChangedEvent.addListener(() => {
            SyncUiEventDispatcher._uiEventDispatcher.dispatchSyncUiEvent(SyncUiEventId.WidgetStateChanged);
        }), UiFramework_1.UiFramework.backstage.onToggled.addListener(() => {
            SyncUiEventDispatcher._uiEventDispatcher.dispatchSyncUiEvent(SyncUiEventId.BackstageEvent);
        }), UiFramework_1.UiFramework.content.onActiveContentChangedEvent.addListener(() => {
            SyncUiEventDispatcher._uiEventDispatcher.dispatchSyncUiEvent(SyncUiEventId.ActiveContentChanged);
        }), core_frontend_1.IModelApp.viewManager.onSelectedViewportChanged.addListener((args) => {
            SyncUiEventDispatcher._uiEventDispatcher.dispatchSyncUiEvent(SyncUiEventId.ActiveViewportChanged);
            // if this is the first view being opened up start the default tool so tool admin is happy.
            if (undefined === args.previous) {
                void core_frontend_1.IModelApp.toolAdmin.startDefaultTool();
            }
            else {
                args.previous.onViewChanged.removeListener(SyncUiEventDispatcher._dispatchViewChange);
                args.previous.onFeatureOverridesChanged.removeListener(SyncUiEventDispatcher._dispatchFeatureOverridesChange);
                args.previous.onViewedModelsChanged.removeListener(SyncUiEventDispatcher._dispatchViewedModelsChanged);
            }
            if (args.current) {
                args.current.onViewChanged.addListener(SyncUiEventDispatcher._dispatchViewChange);
                args.current.onFeatureOverridesChanged.addListener(SyncUiEventDispatcher._dispatchFeatureOverridesChange);
                args.current.onViewedModelsChanged.addListener(SyncUiEventDispatcher._dispatchViewedModelsChanged);
            }
        }));
    }
    /** This should be called by IModelApp when the active IModelConnection is closed. */
    static clearConnectionEvents(iModelConnection) {
        if (this._iModelConnection !== iModelConnection)
            return;
        this._connectionUnregisterFuncs.forEach((unregister) => unregister());
        this._connectionUnregisterFuncs = [];
    }
    /** This should be called by IModelApp when the active IModelConnection is established. */
    static initializeConnectionEvents(iModelConnection) {
        this._iModelConnection &&
            this.clearConnectionEvents(this._iModelConnection);
        this._iModelConnection = iModelConnection;
        this._connectionUnregisterFuncs.push(iModelConnection.selectionSet.onChanged.addListener(() => {
            SyncUiEventDispatcher._uiEventDispatcher.dispatchSyncUiEvent(SyncUiEventId.SelectionSetChanged);
        }), iModelConnection.selectionSet.onChanged.addListener((ev) => {
            const numSelected = ev.set.elements.size;
            UiFramework_1.UiFramework.setNumItemsSelected(numSelected);
        }));
    }
}
exports.SyncUiEventDispatcher = SyncUiEventDispatcher;
SyncUiEventDispatcher._uiEventDispatcher = new InternalSyncUiEventDispatcher_1.InternalSyncUiEventDispatcher();
SyncUiEventDispatcher._unregisterFuncs = new Array();
SyncUiEventDispatcher._connectionUnregisterFuncs = new Array();
//# sourceMappingURL=SyncUiEventDispatcher.js.map