"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _three = require("three");

var textureLoader = new _three.TextureLoader();
var SIZE_TEXTURE_TILE = 256;

function checkResponse(response) {
  if (!response.ok) {
    var error = new Error("Error loading ".concat(response.url, ": status ").concat(response.status));
    error.response = response;
    throw error;
  }
}

var arrayBuffer = function (url) {
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  return fetch(url, options).then(function (response) {
    checkResponse(response);
    return response.arrayBuffer();
  });
};

var getTextureFloat = function (buffer) {
  var texture = new _three.DataTexture(buffer, SIZE_TEXTURE_TILE, SIZE_TEXTURE_TILE, _three.AlphaFormat, _three.FloatType);
  texture.needsUpdate = true;
  return texture;
};
/**
 * Utilitary to fetch resources from a server using the [fetch API]{@link
 * https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch}.
 *
 * @module Fetcher
 */


var _default = {
  /**
   * Wrapper over fetch to get some text.
   *
   * @param {string} url - The URL of the resources to fetch.
   * @param {Object} options - Fetch options (passed directly to `fetch()`),
   * see [the syntax for more information]{@link
   * https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Syntax}.
   *
   * @return {Promise<string>} Promise containing the text.
   */
  text: function text(url) {
    var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
    return fetch(url, options).then(function (response) {
      checkResponse(response);
      return response.text();
    });
  },

  /**
   * Little wrapper over fetch to get some JSON.
   *
   * @param {string} url - The URL of the resources to fetch.
   * @param {Object} options - Fetch options (passed directly to `fetch()`),
   * see [the syntax for more information]{@link
   * https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Syntax}.
   *
   * @return {Promise<Object>} Promise containing the JSON object.
   */
  json: function json(url) {
    var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
    return fetch(url, options).then(function (response) {
      checkResponse(response);
      return response.json();
    });
  },

  /**
   * Wrapper over fetch to get some XML.
   *
   * @param {string} url - The URL of the resources to fetch.
   * @param {Object} options - Fetch options (passed directly to `fetch()`),
   * see [the syntax for more information]{@link
   * https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Syntax}.
   *
   * @return {Promise<Document>} Promise containing the XML Document.
   */
  xml: function xml(url) {
    var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
    return fetch(url, options).then(function (response) {
      checkResponse(response);
      return response.text();
    }).then(function (text) {
      return new window.DOMParser().parseFromString(text, 'text/xml');
    });
  },

  /**
   * Wrapper around {@link THREE.TextureLoader}.
   *
   * @param {string} url - The URL of the resources to fetch.
   * @param {Object} options - Fetch options (passed directly to `fetch()`),
   * see [the syntax for more information]{@link
   * https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Syntax}.
   * Note that THREE.js docs mentions `withCredentials`, but it is not
   * actually used in {@link THREE.TextureLoader}.
   *
   * @return {Promise<THREE.Texture>} Promise containing the {@link
   * THREE.Texture}.
   */
  texture: function texture(url) {
    var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
    var res;
    var rej;
    textureLoader.crossOrigin = options.crossOrigin;
    var promise = new Promise(function (resolve, reject) {
      res = resolve;
      rej = reject;
    });
    textureLoader.load(url, res, function () {}, rej);
    return promise;
  },

  /**
   * Wrapper over fetch to get some ArrayBuffer.
   *
   * @param {string} url - The URL of the resources to fetch.
   * @param {Object} options - Fetch options (passed directly to `fetch()`),
   * see [the syntax for more information]{@link
   * https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Syntax}.
   *
   * @return {Promise<ArrayBuffer>} Promise containing the ArrayBuffer.
   */
  arrayBuffer: arrayBuffer,

  /**
   * Wrapper over fetch to get some {@link THREE.DataTexture}.
   *
   * @param {string} url - The URL of the resources to fetch.
   * @param {Object} options - Fetch options (passed directly to `fetch()`),
   * see [the syntax for more information]{@link
   * https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Syntax}.
   *
   * @return {Promise<THREE.DataTexture>} Promise containing the DataTexture.
   */
  textureFloat: function textureFloat(url) {
    var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
    return arrayBuffer(url, options).then(function (buffer) {
      var floatArray = new Float32Array(buffer);
      var texture = getTextureFloat(floatArray);
      return texture;
    });
  },

  /**
   * Wrapper over fetch to get a bunch of files sharing the same name, but
   * different extensions.
   *
   * @param {string} baseUrl - The shared URL of the resources to fetch.
   * @param {Object} extensions - An object containing arrays. The keys of
   * each of this array are available fetch type, such as `text`, `json` or
   * even `arrayBuffer`. The arrays contains the extensions to append after
   * the `baseUrl` (see example below).
   * @param {Object} options - Fetch options (passed directly to `fetch()`),
   * see [the syntax for more information]{@link
   * https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Syntax}.
   *
   * @return {Promise[]} An array of promises, containing all the files,
   * organized by their extensions (see the example below).
   *
   * @example
   * itowns.Fetcher.multiple('http://geo.server/shapefile', {
   *     // will fetch:
   *     // - http://geo.server/shapefile.shp
   *     // - http://geo.server/shapefile.dbf
   *     // - http://geo.server/shapefile.shx
   *     // - http://geo.server/shapefile.prj
   *     arrayBuffer: ['shp', 'dbf', 'shx'],
   *     text: ['prj'],
   * }).then(function _(result) {
   *     // result looks like:
   *     result = {
   *         shp: ArrayBuffer
   *         dbf: ArrayBuffer
   *         shx: ArrayBuffer
   *         prj: string
   *     };
   * });
   */
  multiple: function multiple(baseUrl, extensions) {
    var _this = this;

    var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
    var promises = [];
    var url;

    for (var fetchType in extensions) {
      if (!this[fetchType]) {
        throw new Error("".concat(fetchType, " is not a valid Fetcher method."));
      } else {
        var _iteratorNormalCompletion = true;
        var _didIteratorError = false;
        var _iteratorError = undefined;

        try {
          var _loop = function () {
            var extension = _step.value;
            url = "".concat(baseUrl, ".").concat(extension);
            promises.push(_this[fetchType](url, options).then(function (result) {
              return {
                type: extension,
                result: result
              };
            }));
          };

          for (var _iterator = extensions[fetchType][Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
            _loop();
          }
        } catch (err) {
          _didIteratorError = true;
          _iteratorError = err;
        } finally {
          try {
            if (!_iteratorNormalCompletion && _iterator["return"] != null) {
              _iterator["return"]();
            }
          } finally {
            if (_didIteratorError) {
              throw _iteratorError;
            }
          }
        }
      }
    }

    return Promise.all(promises).then(function (result) {
      var all = {};
      var _iteratorNormalCompletion2 = true;
      var _didIteratorError2 = false;
      var _iteratorError2 = undefined;

      try {
        for (var _iterator2 = result[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
          var res = _step2.value;
          all[res.type] = res.result;
        }
      } catch (err) {
        _didIteratorError2 = true;
        _iteratorError2 = err;
      } finally {
        try {
          if (!_iteratorNormalCompletion2 && _iterator2["return"] != null) {
            _iterator2["return"]();
          }
        } finally {
          if (_didIteratorError2) {
            throw _iteratorError2;
          }
        }
      }

      return Promise.resolve(all);
    });
  }
};
exports["default"] = _default;