"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchAndMigrateUserHubSettings = fetchAndMigrateUserHubSettings;
const fail_safe_1 = require("../fail-safe");
const clearUserHubSettings_1 = require("./clearUserHubSettings");
const siteSettingsMigrations_1 = require("./siteSettingsMigrations");
const userAppResources_1 = require("./userAppResources");
/**
 * @private
 * Fetch the User Hub Settings and apply migrations
 * Caller is responsible for ensuring the passed token
 * is assoicated with the `hubforarcgis` client id
 * @param username
 * @param portalUrl
 * @param token
 * @returns
 */
async function fetchAndMigrateUserHubSettings(username, portalUrl, token) {
    // create failsafe function that returns a default Hub Settings object
    const fsGetResource = (0, fail_safe_1.failSafe)(userAppResources_1.getUserResource, getDefaultUserHubSettings(username));
    // fetch the user's Hub Settings
    const settings = (await fsGetResource(username, clearUserHubSettings_1.USER_HUB_SETTINGS_KEY, portalUrl, token));
    // apply any migrations and return the result
    return (0, siteSettingsMigrations_1.applyHubSettingsMigrations)(settings);
}
/**
 * Get a default Hub Settings object for a user
 * This will be run through migrations, so it's ok if it's not the latest version
 * @param username
 * @returns
 */
function getDefaultUserHubSettings(username) {
    // This is current for schemaVersion 1.1 but can be updated as needed
    // but it is run through migrations after being returned
    // so it doesn't need to be the latest version
    return {
        schemaVersion: 1.1,
        username,
        updated: new Date().getTime(),
        features: {
            workspace: true,
        },
    };
}
//# sourceMappingURL=fetchAndMigrateUserHubSettings.js.map