/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module Settings
 */
import { UiStateEntry } from "@itwin/core-react";
import { SyncUiEventDispatcher } from "../syncui/SyncUiEventDispatcher";
import { UiFramework } from "../UiFramework";
/** These are the UI settings that are stored in the Redux store. They control the color theme,
 * how toolbar group buttons work, and the opacity of floating widget when the cursor
 * is not inside them. It is expect that an IModelApp using App UI components will create and register AppUiSettings with
 * defaults specific to their application. This would be done by calling the following.
 *
 * ```ts
 * UiFramework.registerUserSettingsProvider(new AppUiSettings(defaults));
 * ```
 *
 * @public
 */
class AppUiSettings {
    constructor(defaults) {
        this.providerId = "AppUiSettingsProvider";
        this._settings = [];
        this._applyingLocalSettings = false;
        this.setColorTheme = (theme) => {
            UiFramework.setColorTheme(theme);
            // always store to local storage to avoid flicker during startup if user is not yet logged-in
            window.localStorage.setItem("uifw:defaultTheme", theme);
        };
        this.handleSyncUiEvent = async (args) => {
            if (this._applyingLocalSettings)
                return;
            if (args.eventIds.has("configurableui:set_theme")) {
                await this.colorTheme.saveSetting(UiFramework.getUiStateStorage());
                // always store as default theme in local storage to avoid flicker during startup if user is not yet logged-in
                window.localStorage.setItem("uifw:defaultTheme", UiFramework.getColorTheme());
            }
            if (args.eventIds.has("configurableui:set-drag-interaction"))
                await this.dragInteraction.saveSetting(UiFramework.getUiStateStorage());
            if (args.eventIds.has("configurableui:set-show-widget-icon"))
                await this.showWidgetIcon.saveSetting(UiFramework.getUiStateStorage());
            if (args.eventIds.has("configurableui:set-auto-collapse-unpinned-panels"))
                await this.autoCollapseUnpinnedPanels.saveSetting(UiFramework.getUiStateStorage());
            if (args.eventIds.has("configurableui:set_widget_opacity"))
                await this.widgetOpacity.saveSetting(UiFramework.getUiStateStorage());
            if (args.eventIds.has("configurableui:set-animate-tool-settings"))
                await this.animateToolSettings.saveSetting(UiFramework.getUiStateStorage());
            if (args.eventIds.has("configurableui:set-use-tool-as-tool-settings-label"))
                await this.useToolAsToolSettingsLabel.saveSetting(UiFramework.getUiStateStorage());
            if (args.eventIds.has("configurableui:set-toolbar-opacity"))
                await this.toolbarOpacity.saveSetting(UiFramework.getUiStateStorage());
        };
        this._settings = [];
        this.colorTheme = new UiStateEntry(AppUiSettings._settingNamespace, "ColorTheme", UiFramework.getColorTheme, this.setColorTheme, defaults.colorTheme);
        this._settings.push(this.colorTheme);
        this.dragInteraction = new UiStateEntry(AppUiSettings._settingNamespace, "DragInteraction", () => UiFramework.useDragInteraction, (value) => UiFramework.setUseDragInteraction(value), defaults.dragInteraction);
        this._settings.push(this.dragInteraction);
        this.showWidgetIcon = new UiStateEntry(AppUiSettings._settingNamespace, "ShowWidgetIcon", () => UiFramework.showWidgetIcon, (value) => UiFramework.setShowWidgetIcon(value), defaults.showWidgetIcon);
        this._settings.push(this.showWidgetIcon);
        this.autoCollapseUnpinnedPanels = new UiStateEntry(AppUiSettings._settingNamespace, "AutoCollapseUnpinnedPanels", () => UiFramework.autoCollapseUnpinnedPanels, (value) => UiFramework.setAutoCollapseUnpinnedPanels(value), defaults.autoCollapseUnpinnedPanels);
        this._settings.push(this.autoCollapseUnpinnedPanels);
        this.widgetOpacity = new UiStateEntry(AppUiSettings._settingNamespace, "WidgetOpacity", () => UiFramework.getWidgetOpacity(), (value) => UiFramework.setWidgetOpacity(value), defaults.widgetOpacity);
        this._settings.push(this.widgetOpacity);
        this.animateToolSettings = new UiStateEntry(AppUiSettings._settingNamespace, "AnimateToolSettings", () => UiFramework.animateToolSettings, (value) => UiFramework.setAnimateToolSettings(value), defaults.animateToolSettings);
        this._settings.push(this.animateToolSettings);
        this.useToolAsToolSettingsLabel = new UiStateEntry(AppUiSettings._settingNamespace, "UseToolAsToolSettingsLabel", () => UiFramework.useToolAsToolSettingsLabel, (value) => UiFramework.setUseToolAsToolSettingsLabel(value), defaults.useToolAsToolSettingsLabel);
        this._settings.push(this.useToolAsToolSettingsLabel);
        this.toolbarOpacity = new UiStateEntry(AppUiSettings._settingNamespace, "ToolOpacity", () => UiFramework.getToolbarOpacity(), (value) => UiFramework.setToolbarOpacity(value), defaults.toolbarOpacity);
        this._settings.push(this.toolbarOpacity);
        SyncUiEventDispatcher.onSyncUiEvent.addListener(this.handleSyncUiEvent);
    }
    async apply(storage) {
        this._applyingLocalSettings = true;
        for await (const setting of this._settings) {
            await setting.getSettingAndApplyValue(storage);
        }
        this._applyingLocalSettings = false;
    }
    async loadUserSettings(storage) {
        await this.apply(storage);
    }
}
AppUiSettings._settingNamespace = "AppUiSettings";
export { AppUiSettings };
//# sourceMappingURL=AppUiSettings.js.map