import { upsertResource } from "../resources/upsertResource";
/**
 * Takes an IModel and an array of resources and upserts them to the
 * backing item. Then searches for the resources that were upserted
 * and attaches them to the model, which is returned.
 *
 * @export
 * @param {IModel} model
 * @param {Array<{
 *     resource: Record<string, any>;
 *     filename: string;
 *   }>} resources
 * @param {IUserRequestOptions} requestOptions
 * @return {*}  {Promise<IModel>}
 */
export async function upsertModelResources(model, resources, requestOptions) {
    // Set up promises array
    const upsertPromises = [];
    const expectedResourceNames = [];
    // loop through resources and create them
    resources.forEach((value) => {
        upsertPromises.push(upsertResource(model.item.id, model.item.owner, value.resource, value.filename, requestOptions));
        expectedResourceNames.push(value.filename);
    });
    // Promise.all to wait for all resources to be created
    return Promise.all(upsertPromises).then(() => {
        // Create a new object with the resources
        const updatedResources = resources.reduce((acc, resource) => {
            // Get the property name from the resource name
            const prop = resource.filename.split(".").shift();
            acc[prop] = resource.resource;
            return acc;
        }, {});
        return Object.assign({ resources: updatedResources }, model);
    });
}
//# sourceMappingURL=upsertModelResource.js.map