"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateUserSiteSettings = updateUserSiteSettings;
exports.fetchUserSiteSettings = fetchUserSiteSettings;
exports.updateUserHubSettings = updateUserHubSettings;
exports.fetchUserHubSettings = fetchUserHubSettings;
const clearUserSiteSettings_1 = require("./internal/clearUserSiteSettings");
const clearUserHubSettings_1 = require("./internal/clearUserHubSettings");
const siteSettingsMigrations_1 = require("./internal/siteSettingsMigrations");
const userAppResources_1 = require("./internal/userAppResources");
const fetchAndMigrateUserHubSettings_1 = require("./internal/fetchAndMigrateUserHubSettings");
/**
 * Store User settings in the Site App's cache
 * @param settings
 * @param context
 * @param replace
 * @returns
 */
async function updateUserSiteSettings(settings, context, replace = false) {
    const token = context.tokenFor(clearUserSiteSettings_1.USER_SITE_SETTINGS_APP);
    if (token) {
        // always stamp in the updated and username properties
        settings.updated = new Date().getTime();
        settings.username = context.currentUser.username;
        const resource = {
            key: clearUserSiteSettings_1.USER_SITE_SETTINGS_KEY,
            data: settings,
            access: "userappprivate",
        };
        return (0, userAppResources_1.setUserResource)(resource, context.currentUser.username, context.portalUrl, token, replace);
    }
    else {
        throw new Error(`No user-app-resource token available to store Site Settings`);
    }
}
/**
 * Fetch the user's settings for the current Site
 * @param context
 * @returns
 */
async function fetchUserSiteSettings(context) {
    const token = context.tokenFor(clearUserSiteSettings_1.USER_SITE_SETTINGS_APP);
    if (token) {
        const settings = (await (0, userAppResources_1.getUserResource)(context.currentUser.username, clearUserSiteSettings_1.USER_SITE_SETTINGS_KEY, context.portalUrl, token));
        // run though schema upgrades
        return (0, siteSettingsMigrations_1.applySiteSettingsMigrations)(settings);
    }
    else {
        // eslint-disable-next-line @typescript-eslint/no-unsafe-return
        return Promise.resolve(null);
    }
}
/**
 * Store the current user's Hub settings as a User App Resource
 * @param settings
 * @param context
 * @param replace
 * @returns
 */
async function updateUserHubSettings(settings, context, replace = false) {
    const token = context.tokenFor(clearUserHubSettings_1.USER_HUB_SETTINGS_APP);
    if (token) {
        // always stamp in the updated and username properties
        settings.updated = new Date().getTime();
        settings.username = context.currentUser.username;
        const resource = {
            key: clearUserHubSettings_1.USER_HUB_SETTINGS_KEY,
            data: settings,
            access: "userappprivate",
        };
        return (0, userAppResources_1.setUserResource)(resource, context.currentUser.username, context.portalUrl, token, replace);
    }
    else {
        throw new Error(`No user-app-resource token available to store Site Settings`);
    }
}
/**
 * Fetch the current user's settings for ArcGIS Hub
 * Will return null if the hubforarcgis token is not available
 * @param context
 * @returns
 */
async function fetchUserHubSettings(context) {
    const token = context.tokenFor(clearUserHubSettings_1.USER_HUB_SETTINGS_APP);
    if (token) {
        return await (0, fetchAndMigrateUserHubSettings_1.fetchAndMigrateUserHubSettings)(context.currentUser.username, context.portalUrl, token);
    }
    else {
        // eslint-disable-next-line @typescript-eslint/no-unsafe-return
        return Promise.resolve(null);
    }
}
//# sourceMappingURL=hubUserAppResources.js.map