import fetchData from './fetchData';
const fetchDataFiles = async function fetchDataFiles(opts) {
  const {
    values
  } = opts;
  return Promise.all(values.map(async value => {
    const {
      config,
      metadata = {}
    } = value;
    const standardValue = {
      config: {
        ...config,
        key: config.key || config.url
      },
      metadata
    };
    const {
      status,
      statusCode,
      error,
      value: newValue
    } = await fetchData(standardValue);
    if (status === 'update') {
      return newValue;
    }
    if (status === 'error') {
      /* eslint-disable no-console */
      console.error(`Fetch Error: Failed to fetch: ${config.url}`);
      console.error(error);
      /* eslint-enable no-console */
    }

    if (status === 'unknown') {
      /* eslint-disable no-console */
      console.warn('Unknown Fetch Status Code: Unable to handle ' + `status code ${statusCode} (${config.url})`);
      /* eslint-enable no-console */
    }

    return undefined;
  }));
};
export default fetchDataFiles;