"use strict";
/* eslint-disable @typescript-eslint/no-unsafe-return */
// These Types and Functions can land in rest-js
// a friendly name for the scope of the resource
// the API deals with clientIds, which are
// listed here https://confluencewikidev.esri.com/pages/viewpage.action?pageId=50561461
// Generally speaking, the UserResourceScope is a downcased version of the
// clientId. Hub is unique b/c Sites have their own clientId,
Object.defineProperty(exports, "__esModule", { value: true });
exports.setUserResource = setUserResource;
exports.getUserResource = getUserResource;
exports.removeUserResource = removeUserResource;
exports.listUserResources = listUserResources;
const arcgis_rest_request_1 = require("@esri/arcgis-rest-request");
const fail_safe_1 = require("../fail-safe");
const getObjectSize_1 = require("../getObjectSize");
/**
 * @private
 * Set a User-App-Resource
 * By default this will fetch and merge with an existing resource.
 * Passing `true` as the last parameter,
 * @param resource
 * @param username
 * @param portalUrl
 * @param token
 * @param replace
 * @returns
 */
async function setUserResource(resource, username, portalUrl, token, replace = false) {
    // Ensure we are below 5MB max size
    if ((0, getObjectSize_1.getObjectSize)(resource.data).megabytes > 4.95) {
        throw new Error(`User Resource is too large to store. Please ensure it is less than 5MB in size`);
    }
    let payload = resource.data;
    if (!replace) {
        const fsGetResource = (0, fail_safe_1.failSafe)(getUserResource, {});
        // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
        const currentResource = await fsGetResource(username, resource.key, portalUrl, token);
        // extend current object witn updated object
        // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
        payload = Object.assign(Object.assign({}, currentResource), resource.data);
    }
    const ro = {
        portal: portalUrl,
        httpMethod: "POST",
        params: {
            text: JSON.stringify(payload),
            access: resource.access,
            key: resource.key,
            token,
        },
    };
    // TODO Experiment w/ how we can make this call w/o creating a ArcGISIdentityManager with the token
    return (0, arcgis_rest_request_1.request)(`${portalUrl}/sharing/rest/community/users/${username}/addResource`, ro);
}
/**
 * @private
 * Gets a user app resource
 * If the resource does not exist, this will throw. This can be wrapped in `failSafe`
 * configured to return an empty object.
 * @param username
 * @param key
 * @param portalUrl
 * @param token
 * @returns
 */
function getUserResource(username, key, portalUrl, token) {
    const ro = {
        portal: portalUrl,
    };
    return (0, arcgis_rest_request_1.request)(`${portalUrl}/sharing/rest/community/users/${username}/resources/${key}?token=${token}`, ro);
}
/**
 * @private
 * Remove a resource
 * Used primarily in tests, but can be useful if you need to clean up
 * settings that are no longer used
 * @param username
 * @param key
 * @param portalUrl
 * @param token
 * @returns
 */
function removeUserResource(username, key, portalUrl, token) {
    const ro = {
        portal: portalUrl,
        httpMethod: "POST",
        params: {
            key,
            token,
        },
    };
    return (0, arcgis_rest_request_1.request)(`${portalUrl}/sharing/rest/community/users/${username}/removeResource`, ro);
}
/**
 * @private
 * List user resources associated with the user/token.
 * Defaults to listing only resources associated with the app the token is associated with
 * @param username
 * @param portalUrl
 * @param token
 * @returns
 */
function listUserResources(username, portalUrl, token, returnAllApps = false) {
    const ro = {
        portal: portalUrl,
    };
    return (0, arcgis_rest_request_1.request)(`${portalUrl}/sharing/rest/community/users/${username}/resources?f=json&token=${token}&returnAllApps=${returnAllApps.toString()}`, ro);
}
//# sourceMappingURL=userAppResources.js.map