/* eslint-disable guard-for-in, complexity, no-try-catch */
import assert from '../utils/assert';
import { loadFile } from './browser-load-file';
import { loadImage } from './browser-load-image';

function noop() {}

/*
 * Loads (Requests) multiple files asynchronously
 */
export function loadFiles(opts = {}) {
  const urls = opts.urls;
  var _opts$onProgress = opts.onProgress;
  const onProgress = _opts$onProgress === undefined ? noop : _opts$onProgress;

  assert(urls.every(url => typeof url === 'string'), 'loadImages: {urls} must be array of strings');
  let count = 0;
  return Promise.all(urls.map(url => {
    const promise = loadFile(Object.assign({ url }, opts));
    promise.then(file => onProgress({
      progress: ++count / urls.length,
      count,
      total: urls.length,
      url
    }));
    return promise;
  }));
}

/*
 * Loads (requests) multiple images asynchronously
 */
export function loadImages(opts = {}) {
  const urls = opts.urls;
  var _opts$onProgress2 = opts.onProgress;
  const onProgress = _opts$onProgress2 === undefined ? noop : _opts$onProgress2;

  assert(urls.every(url => typeof url === 'string'), 'loadImages: {urls} must be array of strings');
  let count = 0;
  return Promise.all(urls.map(url => {
    const promise = loadImage(url, opts);
    promise.then(file => onProgress({
      progress: ++count / urls.length,
      count,
      total: urls.length,
      url
    }));
    return promise;
  }));
}
//# sourceMappingURL=load-files.js.map