import fetchDataFiles from './fetchDataFiles';
const fetchFlatpackStaticData = async function fetchFlatpackStaticData(opts) {
  const {
    flatpackConfig = {},
    data = []
  } = opts;
  const {
    manifestUrl,
    pageConfigUrl,
    pageConfigInterval = 10000,
    baseFileUrl
  } = flatpackConfig;
  const allFiles = [
  // Flatpack manifest
  manifestUrl && {
    url: manifestUrl,
    type: 'pages-manifest'
  },
  // Flatpack page config
  pageConfigUrl && {
    url: pageConfigUrl,
    type: 'current-page',
    refreshRate: pageConfigInterval
  }, ...data];
  const dataFiles = allFiles.filter(Boolean).map(config => {
    const {
      key,
      url: fileUrl,
      ...restConfig
    } = config;
    const url = baseFileUrl ? new URL(fileUrl, baseFileUrl).href : fileUrl;
    return {
      config: {
        url,
        key: key || url,
        ...restConfig
      }
    };
  });
  const fetchedData = await fetchDataFiles({
    values: dataFiles
  });
  return fetchedData;
};
export default fetchFlatpackStaticData;