const settleAllPromises = (promiseResultMap) => {
    // Pull our keys out of the promise 'table'
    const promiseNames = Object.entries(promiseResultMap);

    return Promise.allSettled(promiseNames.map((e) => e[1]).map((n) => n.promise)).then((results) => {
        const resultMap = {};

        // Build a map of the results
        for (let i = 0; i < results.length; ++i) {
            const promiseName = promiseNames[i][1].resultKey;

            // Some Promises like the cms content call don't have a 'data' field
            // when returned, so other promises do. Map the results to the object
            // so that the object is the return data.

            if (results[i]?.value?.data === undefined) {
                resultMap[promiseName] = results[i]?.value;
            } else {
                resultMap[promiseName] = results[i]?.value?.data;
            }
        }

        return resultMap;
    });
};

export default settleAllPromises;
