/*---------------------------------------------------------------------------------------------
 * 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 { UiStateStorageStatus } from "@itwin/core-react";
import { SyncUiEventDispatcher, SyncUiEventId, } from "../syncui/SyncUiEventDispatcher";
import { UiFramework } from "../UiFramework";
/** Class that maintain UiShowHide user settings between sessions
 * @internal
 */
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 !== null && storage !== void 0 ? storage : UiFramework.getUiStateStorage()).saveSetting(this._settingsNamespace, this._autoHideUiKey, v);
    }
    static async storeUseProximityOpacity(v, storage) {
        void (storage !== null && storage !== void 0 ? storage : UiFramework.getUiStateStorage()).saveSetting(this._settingsNamespace, this._useProximityOpacityKey, v);
    }
    static async storeSnapWidgetOpacity(v, storage) {
        void (storage !== null && storage !== void 0 ? storage : UiFramework.getUiStateStorage()).saveSetting(this._settingsNamespace, this._snapWidgetOpacityKey, v);
    }
}
UiShowHideSettingsProvider._settingsNamespace = "AppUiSettings";
UiShowHideSettingsProvider._autoHideUiKey = "AutoHideUi";
UiShowHideSettingsProvider._useProximityOpacityKey = "UseProximityOpacity";
UiShowHideSettingsProvider._snapWidgetOpacityKey = "SnapWidgetOpacity";
export { UiShowHideSettingsProvider };
/** 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
 */
class InternalUiShowHideManager {
    /** Determines if the Ui is visible */
    static get isUiVisible() {
        return InternalUiShowHideManager._isUiVisible;
    }
    static set isUiVisible(visible) {
        InternalUiShowHideManager._isUiVisible = visible;
    }
    /** @public */
    static setAutoHideUi(value) {
        InternalUiShowHideManager._autoHideUi = value;
    }
    /** @internal */
    static setUseProximityOpacity(value) {
        InternalUiShowHideManager._useProximityOpacity = value;
    }
    /** @internal */
    static setSnapWidgetOpacity(value) {
        InternalUiShowHideManager._snapWidgetOpacity = value;
    }
    /** Determines whether the `auto-hide Ui` feature is on. Defaults to false.
     * When true, the Ui automatically hides after a few seconds of inactivity.
     */
    static get autoHideUi() {
        return InternalUiShowHideManager._autoHideUi;
    }
    static set autoHideUi(autoHide) {
        void UiShowHideSettingsProvider.storeAutoHideUi(autoHide);
        InternalUiShowHideManager._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: UiFramework.getIsUiVisible(),
        });
    }
    /** 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: UiFramework.getIsUiVisible(),
        });
    }
    /** 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: UiFramework.getIsUiVisible(),
        });
    }
    /** 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: UiFramework.getIsUiVisible(),
        });
    }
    /** Handler for when a Frontstage is ready */
    static handleFrontstageReady() {
        // istanbul ignore next
        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() {
        setTimeout(() => {
            InternalUiShowHideManager.showUi();
            InternalUiShowHideManager.resetTimer();
        });
    }
    /** Shows the Ui and cancels the inactivity timer */
    static showUiAndCancelTimer() {
        setTimeout(() => {
            InternalUiShowHideManager.showUi();
            InternalUiShowHideManager.cancelTimer();
        });
    }
    static cancelTimer() {
        clearTimeout(InternalUiShowHideManager._timeout);
    }
    static resetTimer() {
        InternalUiShowHideManager.cancelTimer();
        InternalUiShowHideManager._timeout = setTimeout(InternalUiShowHideManager.hideUi, InternalUiShowHideManager._inactivityTime);
    }
    static showUi() {
        UiFramework.setIsUiVisible(true);
    }
    static hideUi() {
        UiFramework.setIsUiVisible(false);
    }
    /** @internal */
    static terminate() {
        InternalUiShowHideManager.cancelTimer();
        // Ensure that next use will have default values for tests.
        InternalUiShowHideManager._isUiVisible = true;
        InternalUiShowHideManager._autoHideUi = true;
        InternalUiShowHideManager._showHidePanels = false;
        InternalUiShowHideManager._showHideFooter = false;
        InternalUiShowHideManager._inactivityTime = INACTIVITY_TIME_DEFAULT;
        InternalUiShowHideManager._useProximityOpacity = false;
        InternalUiShowHideManager._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;
export { InternalUiShowHideManager };
//# sourceMappingURL=InternalUiShowHideManager.js.map