"use strict";
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module UiStateStorage
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.UserSettingsStorage = void 0;
const core_frontend_1 = require("@itwin/core-frontend");
const core_react_1 = require("@itwin/core-react");
/**
 * Implementation of [[UiStateStorage]] that uses settings admin from [IModelApp.userPreferences]($core-frontend).
 * @public
 * @deprecated in 3.0. Use [IModelApp.userPreferences]($core-frontend) API directly to store user preferences as a replacement.
 */
class UserSettingsStorage {
    async getSetting(namespace, name) {
        if (undefined === core_frontend_1.IModelApp.authorizationClient)
            return { status: core_react_1.UiStateStorageStatus.AuthorizationError };
        const accessToken = await core_frontend_1.IModelApp.getAccessToken();
        if (accessToken === "")
            return { status: core_react_1.UiStateStorageStatus.AuthorizationError };
        // istanbul ignore next
        if (!core_frontend_1.IModelApp.userPreferences)
            return { status: core_react_1.UiStateStorageStatus.Uninitialized };
        const result = await core_frontend_1.IModelApp.userPreferences.get({ accessToken, key: `${namespace}.${name}` });
        return {
            status: core_react_1.UiStateStorageStatus.Success,
            setting: result,
        };
    }
    async saveSetting(namespace, name, setting) {
        // istanbul ignore next
        if (!core_frontend_1.IModelApp.userPreferences)
            return { status: core_react_1.UiStateStorageStatus.Uninitialized };
        const accessToken = await core_frontend_1.IModelApp.getAccessToken();
        if (accessToken === "")
            return { status: core_react_1.UiStateStorageStatus.AuthorizationError };
        const result = await core_frontend_1.IModelApp.userPreferences.save({ accessToken, content: setting, key: `${namespace}.${name}` });
        return {
            status: core_react_1.UiStateStorageStatus.Success,
            setting: result,
        };
    }
    async deleteSetting(namespace, name) {
        // istanbul ignore next
        if (!core_frontend_1.IModelApp.userPreferences)
            return { status: core_react_1.UiStateStorageStatus.Uninitialized };
        const accessToken = await core_frontend_1.IModelApp.getAccessToken();
        if (accessToken === "")
            return { status: core_react_1.UiStateStorageStatus.AuthorizationError };
        const result = await core_frontend_1.IModelApp.userPreferences.delete({ accessToken, key: `${namespace}.${name}` });
        return {
            status: core_react_1.UiStateStorageStatus.Success,
            setting: result,
        };
    }
}
exports.UserSettingsStorage = UserSettingsStorage;
//# sourceMappingURL=UserSettingsStorage.js.map