"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 ConfigurableUi
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.InternalConfigurableUiManager = void 0;
const appui_abstract_1 = require("@itwin/appui-abstract");
const CubeNavigationAidControl_1 = require("../navigationaids/CubeNavigationAidControl");
const DrawingNavigationAidControl_1 = require("../navigationaids/DrawingNavigationAidControl");
const SheetNavigationAid_1 = require("../navigationaids/SheetNavigationAid");
const StandardRotationNavigationAid_1 = require("../navigationaids/StandardRotationNavigationAid");
const UiFramework_1 = require("../UiFramework");
const ConfigurableUiControl_1 = require("./ConfigurableUiControl");
const MessageManager_1 = require("../messages/MessageManager");
const PopupManager_1 = require("../popup/PopupManager");
const ActivityTracker_1 = require("./ActivityTracker");
const core_bentley_1 = require("@itwin/core-bentley");
const InternalFrontstageManager_1 = require("../frontstage/InternalFrontstageManager");
const InternalToolSettingsManager_1 = require("../toolsettings/InternalToolSettingsManager");
const InternalModelessDialogManager_1 = require("../dialog/InternalModelessDialogManager");
const InternalContentDialogManager_1 = require("../dialog/InternalContentDialogManager");
const InternalKeyboardShortcut_1 = require("../keyboardshortcut/InternalKeyboardShortcut");
const InternalModalDialogManager_1 = require("../dialog/InternalModalDialogManager");
const SyncUiEventDispatcher_1 = require("../syncui/SyncUiEventDispatcher");
/* eslint-disable deprecation/deprecation */
/** Configurable Ui Manager maintains controls, Frontstages, Content Groups, Content Layouts, Tasks and Workflows.
 * @internal
 */
class InternalConfigurableUiManager {
    /** Initializes the InternalConfigurableUiManager and registers core controls.
     * @internal
     */
    static initialize() {
        if (this._initialized)
            return;
        // Register core controls
        InternalConfigurableUiManager.register(StandardRotationNavigationAid_1.StandardRotationNavigationAidControl.navigationAidId, StandardRotationNavigationAid_1.StandardRotationNavigationAidControl);
        InternalConfigurableUiManager.register(SheetNavigationAid_1.SheetNavigationAidControl.navigationAidId, SheetNavigationAid_1.SheetNavigationAidControl);
        InternalConfigurableUiManager.register(DrawingNavigationAidControl_1.DrawingNavigationAidControl.navigationAidId, DrawingNavigationAidControl_1.DrawingNavigationAidControl);
        InternalConfigurableUiManager.register(CubeNavigationAidControl_1.CubeNavigationAidControl.navigationAidId, CubeNavigationAidControl_1.CubeNavigationAidControl);
        // Initialize SyncUiEventDispatcher so it can register event callbacks.
        SyncUiEventDispatcher_1.SyncUiEventDispatcher.initialize();
        // Initialize the FrontstageManager
        InternalFrontstageManager_1.InternalFrontstageManager.initialize();
        // Initialize the ToolSettingsManager that manages Tool Settings properties.
        InternalToolSettingsManager_1.InternalToolSettingsManager.initialize();
        // Initialize dialog managers that allow one or more dialogs to be open at a time. These managers adjust the z-indexing
        // to ensure the most recently focused dialog of a specific type displays above its siblings.
        InternalModelessDialogManager_1.InternalModelessDialogManager.initialize();
        // ContentDialog have a z-index just above the fixed content views and below all other UI elements.
        InternalContentDialogManager_1.InternalContentDialogManager.initialize();
        // Initialize the Keyboard Shortcut manager
        InternalKeyboardShortcut_1.InternalKeyboardShortcutManager.initialize();
        this._initialized = true;
    }
    /** Registers a control implementing the [[ConfigurableUiElement]] interface.
     * These controls can be a
     * [[ContentControl]],
     * [[NavigationAidControl]],
     * [[StatusBarWidgetControl]],
     * [[WidgetControl]] or
     * [ToolUiProvider]($appui-react).
     * @param classId the class id of the control to register
     * @param constructor the constructor of the control to register
     */
    static register(classId, constructor) {
        if (this._registeredControls.get(classId) !== undefined) {
            throw new appui_abstract_1.UiError(UiFramework_1.UiFramework.loggerCategory(this), `registerControl: classId '${classId}' already registered`);
        }
        this._registeredControls.set(classId, constructor);
    }
    /** Determines if a control has been registered based on its classId.
     * @param classId   the class id of the control to test
     * @returns  true if the control is registered or false if not
     */
    static isRegistered(classId) {
        const constructor = this._registeredControls.get(classId);
        return constructor !== undefined;
    }
    /** Determines if a control has been registered.
     * @internal
     */
    static getConstructorClassId(constructor) {
        for (const [key, value] of this._registeredControls.entries()) {
            if (value === constructor)
                return key;
        }
        return undefined;
    }
    /** Unregisters a control that has been registered.
     * @param classId   the class id of the control to unregister
     */
    static unregister(classId) {
        const constructor = this._registeredControls.get(classId);
        if (constructor)
            this._registeredControls.delete(classId);
    }
    /** Creates a control registered by calling registerControl.
     * @param classId   the class id of the control to create
     * @param uniqueId  a unique id for the control
     * @param options   options passed to the constructor of the control
     * @param controlId controlId which may not be unique across all control instances.
     * @returns  the created control
     */
    static create(classId, uniqueId, options, controlId) {
        const info = new ConfigurableUiControl_1.ConfigurableCreateInfo(classId, uniqueId, controlId ?? uniqueId);
        const constructor = this._registeredControls.get(info.classId);
        if (!constructor) {
            throw new appui_abstract_1.UiError(UiFramework_1.UiFramework.loggerCategory(this), `createControl: classId '${classId}' not registered`);
        }
        const control = new constructor(info, options);
        return control;
    }
    /** Gets the HTML wrapper element for Configurable UI */
    static getWrapperElement() {
        const wrapper = document.getElementById("uifw-configurableui-wrapper");
        const htmlElement = wrapper;
        return htmlElement;
    }
    /** Assists in the transition to context wrapper exported from Configurable UI */
    static getWrapperDocument() {
        const wrapper = document.getElementById("uifw-configurableui-wrapper");
        return wrapper?.ownerDocument ?? document;
    }
    /** Closes all UI popups currently open */
    static closeUi() {
        MessageManager_1.MessageManager.closeAllMessages();
        InternalModelessDialogManager_1.InternalModelessDialogManager.closeAll();
        InternalModalDialogManager_1.InternalModalDialogManager.closeAll();
        InternalContentDialogManager_1.InternalContentDialogManager.closeAll();
        UiFramework_1.UiFramework.keyboardShortcuts.closeMenu();
        UiFramework_1.UiFramework.closeCursorMenu();
        PopupManager_1.PopupManager.clearPopups();
    }
}
exports.InternalConfigurableUiManager = InternalConfigurableUiManager;
InternalConfigurableUiManager._registeredControls = new Map();
InternalConfigurableUiManager._initialized = false;
/** @internal */
InternalConfigurableUiManager.activityTracker = new ActivityTracker_1.ActivityTracker();
/** @internal */
InternalConfigurableUiManager.onUiActivityEvent = new core_bentley_1.BeUiEvent();
/** @internal */
InternalConfigurableUiManager.onUiIntervalEvent = new core_bentley_1.BeUiEvent();
//# sourceMappingURL=InternalConfigurableUiManager.js.map