/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module ToolSettings
 */
import { IModelApp } from "@itwin/core-frontend";
import { focusIntoContainer } from "@itwin/core-react/internal";
import { SyncUiEventDispatcher } from "../syncui/SyncUiEventDispatcher.js";
import { BeUiEvent } from "@itwin/core-bentley";
/** Tool Settings Manager class. Used to generate UI components for Tool Settings.
 * @internal
 */
export class InternalToolSettingsManager {
    static syncToolSettingsProperties(toolId, syncProperties) {
        InternalToolSettingsManager.onSyncToolSettingsProperties.emit({
            toolId,
            syncProperties,
        });
    }
    static reloadToolSettingsProperties() {
        InternalToolSettingsManager.onReloadToolSettingsProperties.emit();
    }
    static dispatchSyncUiEvent(syncEventId, useImmediateDispatch) {
        if (useImmediateDispatch)
            SyncUiEventDispatcher.dispatchImmediateSyncUiEvent(syncEventId);
        else
            SyncUiEventDispatcher.dispatchSyncUiEvent(syncEventId);
    }
    /** Initializes the ToolSettingsManager
     * @internal
     */
    static initialize() {
        if (IModelApp && IModelApp.toolAdmin) {
            IModelApp.toolAdmin.toolSettingsChangeHandler =
                InternalToolSettingsManager.syncToolSettingsProperties;
            IModelApp.toolAdmin.reloadToolSettingsHandler =
                InternalToolSettingsManager.reloadToolSettingsProperties;
            IModelApp.toolAdmin.toolSyncUiEventDispatcher =
                InternalToolSettingsManager.dispatchSyncUiEvent;
        }
    }
    /** clear cached Tool Settings properties. */
    static clearToolSettingsData() {
        InternalToolSettingsManager.useDefaultToolSettingsProvider = false;
        InternalToolSettingsManager._activeToolLabel = "";
        InternalToolSettingsManager._activeToolDescription = "";
        InternalToolSettingsManager._toolIdForToolSettings = "";
    }
    /** Cache Tool Settings properties */
    static initializeToolSettingsData(toolSettingsProperties, toolId, toolLabel, toolDescription) {
        InternalToolSettingsManager.clearToolSettingsData();
        if (toolLabel)
            InternalToolSettingsManager._activeToolLabel = toolLabel;
        if (toolDescription)
            InternalToolSettingsManager._activeToolDescription = toolDescription;
        if (toolSettingsProperties && toolSettingsProperties.length > 0) {
            if (toolId)
                InternalToolSettingsManager._toolIdForToolSettings = toolId;
            InternalToolSettingsManager._useDefaultToolSettingsProvider = true;
            return true;
        }
        return false;
    }
    /** Set of data used in Tool Settings for the specified tool. The tool specified should be the "active" tool.
     */
    static initializeDataForTool(tool) {
        InternalToolSettingsManager.initializeToolSettingsData(tool.supplyToolSettingsProperties(), tool.toolId, tool.flyover, tool.description);
    }
    /** Returns the toolSettings properties that can be used to populate the tool settings widget. */
    static get toolSettingsProperties() {
        if (IModelApp.toolAdmin &&
            IModelApp.toolAdmin.activeTool &&
            IModelApp.toolAdmin.activeTool.toolId ===
                InternalToolSettingsManager._toolIdForToolSettings) {
            const properties = IModelApp.toolAdmin.activeTool.supplyToolSettingsProperties();
            if (properties)
                return properties;
        }
        return [];
    }
    /** Returns true if the Tool Settings are to be auto populated from the toolSettingsProperties.
     * The setter is chiefly for testing.
     */
    static get useDefaultToolSettingsProvider() {
        return InternalToolSettingsManager._useDefaultToolSettingsProvider;
    }
    static set useDefaultToolSettingsProvider(useDefaultToolSettings) {
        InternalToolSettingsManager._useDefaultToolSettingsProvider =
            useDefaultToolSettings;
    }
    /** The name of the active tool. This is typically the flyover text specified for the tool. */
    static get activeToolLabel() {
        return InternalToolSettingsManager._activeToolLabel;
    }
    static set activeToolLabel(label) {
        InternalToolSettingsManager._activeToolLabel = label;
    }
    /** Returns the description of the active tool. */
    static get activeToolDescription() {
        return InternalToolSettingsManager._activeToolDescription;
    }
    /** Gets the Id of the active tool. If a tool is not active, blank is returned.
     * @return  Id of the active tool, or blank if one is not active.
     */
    static get toolIdForToolSettings() {
        return InternalToolSettingsManager._toolIdForToolSettings;
    }
    static focusIntoToolSettings() {
        let divElement = document.querySelector("div.uifw-toolSettings-docked");
        if (divElement) {
            if (focusIntoContainer(divElement))
                return true;
        }
        divElement = document.querySelector("div.uifw-tool-settings-grid-container");
        if (divElement) {
            if (focusIntoContainer(divElement))
                return true;
        }
        return false;
    }
}
InternalToolSettingsManager._useDefaultToolSettingsProvider = false;
InternalToolSettingsManager._toolIdForToolSettings = "";
InternalToolSettingsManager._activeToolLabel = "";
InternalToolSettingsManager._activeToolDescription = "";
/** Get ToolSettings Properties sync event. */
InternalToolSettingsManager.onSyncToolSettingsProperties = new BeUiEvent(); // eslint-disable-line @typescript-eslint/no-deprecated
// eslint-disable-next-line @typescript-eslint/no-deprecated
InternalToolSettingsManager.onReloadToolSettingsProperties = new BeUiEvent();
//# sourceMappingURL=InternalToolSettingsManager.js.map