"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchModelResources = fetchModelResources;
const arcgis_rest_portal_1 = require("@esri/arcgis-rest-portal");
const fail_safe_1 = require("../utils/fail-safe");
/**
 * Given an item, and a list of resource name/prop pairs,
 * fetch the resources and return as an object for the IModel
 *
 * @export
 * @param {IItem} item
 * @param {{
 *     [key: string]: string
 *   }} resourceNamePairs
 * @param {IRequestOptions} requestOptions
 * @return {*}  {Promise<Record<string, any>>}
 */
// we should actually resolve this lint error
// eslint-disable-next-line @typescript-eslint/require-await
async function fetchModelResources(item, resourceNamePairs, requestOptions) {
    // Iterate through the resource name/prop pairs and fetch the resources
    return Object.entries(resourceNamePairs).reduce(async (acc, [key, value]) => {
        // failsafe to prevent errors returns falsy if error
        const failSafeGetResource = (0, fail_safe_1.failSafe)(arcgis_rest_portal_1.getItemResource, null);
        // get the resource
        const resp = await failSafeGetResource(item.id, Object.assign({ fileName: value, 
            // Must be "arrayBuffer" | "blob" | "formData" | "json" | "text";
            readAs: value.split(".").pop() }, requestOptions));
        // if the failsafe succeeds
        if (resp) {
            // Update the acc with the prop and resource
            acc[key] = resp;
        }
        return acc;
    }, {});
}
//# sourceMappingURL=fetchModelResource.js.map