/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module Utilities
 */
import { SyncUiEventDispatcher, SyncUiEventId, } from "../syncui/SyncUiEventDispatcher";
import { UiFramework } from "../UiFramework";
import { UiStateStorageStatus } from "../uistate/UiStateStorage";
/** Class that maintain UiShowHide user settings between sessions
 * @internal
 */
export class UiShowHideSettingsProvider {
    constructor() {
        this.providerId = "UiShowHideSettingsProvider";
    }
    static initialize() {
        UiFramework.registerUserSettingsProvider(new UiShowHideSettingsProvider());
    }
    async loadUserSettings(storage) {
        let result = await storage.getSetting(UiShowHideSettingsProvider._settingsNamespace, UiShowHideSettingsProvider._autoHideUiKey);
        if (result.status === UiStateStorageStatus.Success)
            InternalUiShowHideManager.setAutoHideUi(result.setting);
        result = await storage.getSetting(UiShowHideSettingsProvider._settingsNamespace, UiShowHideSettingsProvider._useProximityOpacityKey);
        if (result.status === UiStateStorageStatus.Success)
            InternalUiShowHideManager.setUseProximityOpacity(result.setting);
        result = await storage.getSetting(UiShowHideSettingsProvider._settingsNamespace, UiShowHideSettingsProvider._snapWidgetOpacityKey);
        if (result.status === UiStateStorageStatus.Success)
            InternalUiShowHideManager.setSnapWidgetOpacity(result.setting);
    }
    static async storeAutoHideUi(v, storage) {
        void (storage ?? UiFramework.getUiStateStorage()).saveSetting(this._settingsNamespace, this._autoHideUiKey, v);
    }
    static async storeUseProximityOpacity(v, storage) {
        void (storage ?? UiFramework.getUiStateStorage()).saveSetting(this._settingsNamespace, this._useProximityOpacityKey, v);
    }
    static async storeSnapWidgetOpacity(v, storage) {
        void (storage ?? UiFramework.getUiStateStorage()).saveSetting(this._settingsNamespace, this._snapWidgetOpacityKey, v);
    }
}
UiShowHideSettingsProvider._settingsNamespace = "AppUiSettings";
UiShowHideSettingsProvider._autoHideUiKey = "AutoHideUi";
UiShowHideSettingsProvider._useProximityOpacityKey = "UseProximityOpacity";
UiShowHideSettingsProvider._snapWidgetOpacityKey = "SnapWidgetOpacity";
/** The default inactivity time.
 * @internal
 */
export const INACTIVITY_TIME_DEFAULT = 3500; /** Wait 3.5 seconds */
/** Maintains Ui Show/Hide state. The `Ui` includes widgets, panels and the status bar.
 * @internal
 */
export class InternalUiShowHideManager {
    /** Determines if the Ui is visible */
    static get isUiVisible() {
        return InternalUiShowHideManager._isUiVisible;
    }
    static set isUiVisible(visible) {
        if (this._isUiVisible === visible)
            return;
        InternalUiShowHideManager._isUiVisible = visible;
        UiFramework.onUiVisibilityChanged.emit({ visible });
    }
    static setAutoHideUi(value) {
        InternalUiShowHideManager._autoHideUi = value;
    }
    static setUseProximityOpacity(value) {
        InternalUiShowHideManager._useProximityOpacity = value;
    }
    static setSnapWidgetOpacity(value) {
        InternalUiShowHideManager._snapWidgetOpacity = value;
    }
    static get autoHideUi() {
        return InternalUiShowHideManager._autoHideUi;
    }
    static set autoHideUi(autoHide) {
        if (this._autoHideUi === autoHide)
            return;
        void UiShowHideSettingsProvider.storeAutoHideUi(autoHide);
        if (!autoHide) {
            this.isUiVisible = true;
        }
        this._autoHideUi = autoHide;
        SyncUiEventDispatcher.dispatchImmediateSyncUiEvent(SyncUiEventId.ShowHideManagerSettingChange);
    }
    /** Determines whether the widget panels are shown and hidden. Defaults to false. */
    static get showHidePanels() {
        return InternalUiShowHideManager._showHidePanels;
    }
    static set showHidePanels(showHide) {
        InternalUiShowHideManager._showHidePanels = showHide;
        UiFramework.onUiVisibilityChanged.emit({
            visible: this.isUiVisible,
        });
    }
    /** Determines whether the status bar is shown and hidden. Defaults to false. */
    static get showHideFooter() {
        return InternalUiShowHideManager._showHideFooter;
    }
    static set showHideFooter(showHide) {
        InternalUiShowHideManager._showHideFooter = showHide;
        UiFramework.onUiVisibilityChanged.emit({
            visible: this.isUiVisible,
        });
    }
    /** Determines the amount of inactivity time before the Ui is hidden. Defaults to 3.5 seconds. */
    static get inactivityTime() {
        return InternalUiShowHideManager._inactivityTime;
    }
    static set inactivityTime(time) {
        InternalUiShowHideManager._inactivityTime = time;
    }
    /** Determines whether the proximity of the mouse should alter the opacity of a toolbar. Defaults to true. */
    static get useProximityOpacity() {
        return InternalUiShowHideManager._useProximityOpacity;
    }
    static set useProximityOpacity(value) {
        InternalUiShowHideManager._useProximityOpacity = value;
        void UiShowHideSettingsProvider.storeUseProximityOpacity(value);
        SyncUiEventDispatcher.dispatchImmediateSyncUiEvent(SyncUiEventId.ShowHideManagerSettingChange);
        UiFramework.onUiVisibilityChanged.emit({
            visible: this.isUiVisible,
        });
    }
    /** Determines whether the opacity of a toolbar should snap. Defaults to false. */
    static get snapWidgetOpacity() {
        return InternalUiShowHideManager._snapWidgetOpacity;
    }
    static set snapWidgetOpacity(value) {
        InternalUiShowHideManager._snapWidgetOpacity = value;
        void UiShowHideSettingsProvider.storeSnapWidgetOpacity(value);
        SyncUiEventDispatcher.dispatchImmediateSyncUiEvent(SyncUiEventId.ShowHideManagerSettingChange);
        UiFramework.onUiVisibilityChanged.emit({
            visible: this.isUiVisible,
        });
    }
    /** Handler for when a Frontstage is ready */
    static handleFrontstageReady() {
        if (!InternalUiShowHideManager._autoHideUi)
            return;
        InternalUiShowHideManager.showUiAndResetTimer();
    }
    /** Handler for when the mouse moves over the content area */
    static handleContentMouseMove(_event) {
        if (!InternalUiShowHideManager._autoHideUi)
            return;
        InternalUiShowHideManager.showUiAndResetTimer();
    }
    /** Handler for when the mouse enters a widget */
    static handleWidgetMouseEnter(_event) {
        if (!InternalUiShowHideManager._autoHideUi)
            return;
        InternalUiShowHideManager.showUiAndCancelTimer();
    }
    /** Shows the Ui and resets the inactivity timer */
    static showUiAndResetTimer() {
        this.isUiVisible = true;
        window.clearTimeout(this._timeout);
        if (!this._autoHideUi)
            return;
        this._timeout = window.setTimeout(() => {
            this.isUiVisible = false;
        }, this._inactivityTime);
    }
    /** Shows the Ui and cancels the inactivity timer */
    static showUiAndCancelTimer() {
        this.isUiVisible = true;
        window.clearTimeout(this._timeout);
    }
    static terminate() {
        window.clearTimeout(this._timeout);
        this._isUiVisible = true;
        this._autoHideUi = true;
        this._showHidePanels = false;
        this._showHideFooter = false;
        this._inactivityTime = INACTIVITY_TIME_DEFAULT;
        this._useProximityOpacity = false;
        this._snapWidgetOpacity = false;
    }
}
InternalUiShowHideManager._isUiVisible = true;
InternalUiShowHideManager._autoHideUi = true;
InternalUiShowHideManager._showHidePanels = false;
InternalUiShowHideManager._showHideFooter = false;
InternalUiShowHideManager._inactivityTime = INACTIVITY_TIME_DEFAULT;
InternalUiShowHideManager._useProximityOpacity = false;
InternalUiShowHideManager._snapWidgetOpacity = false;
//# sourceMappingURL=InternalUiShowHideManager.js.map