/*---------------------------------------------------------------------------------------------
 * 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 { UiEvent } from "@itwin/appui-abstract";
import { focusIntoContainer } from "@itwin/core-react";
import { SyncToolSettingsPropertiesEvent } from "../framework/FrameworkToolSettings";
import { SyncUiEventDispatcher } from "../syncui/SyncUiEventDispatcher";
/** Tool Settings Manager class. Used to generate UI components for Tool Settings.
 * @internal
 */
class InternalToolSettingsManager {
    // istanbul ignore next
    static syncToolSettingsProperties(toolId, syncProperties) {
        InternalToolSettingsManager.onSyncToolSettingsProperties.emit({
            toolId,
            syncProperties,
        });
    }
    // istanbul ignore next
    static reloadToolSettingsProperties() {
        InternalToolSettingsManager.onReloadToolSettingsProperties.emit();
    }
    static dispatchSyncUiEvent(syncEventId, useImmediateDispatch) {
        if (useImmediateDispatch)
            SyncUiEventDispatcher.dispatchImmediateSyncUiEvent(syncEventId);
        else
            SyncUiEventDispatcher.dispatchSyncUiEvent(syncEventId);
    }
    /** Initializes the ToolSettingsManager
     * @internal
     */
    static initialize() {
        // istanbul ignore else
        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();
        // istanbul ignore else
        if (toolLabel)
            InternalToolSettingsManager._activeToolLabel = toolLabel;
        // istanbul ignore else
        if (toolDescription)
            InternalToolSettingsManager._activeToolDescription = toolDescription;
        /* istanbul ignore else */
        if (toolSettingsProperties && toolSettingsProperties.length > 0) {
            // istanbul ignore else
            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();
            // istanbul ignore else
            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.nz-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 SyncToolSettingsPropertiesEvent();
InternalToolSettingsManager.onReloadToolSettingsProperties = new UiEvent();
export { InternalToolSettingsManager };
//# sourceMappingURL=InternalToolSettingsManager.js.map