'use strict';

/* eslint-disable promise/catch-or-return */
/* eslint-disable promise/prefer-await-to-then */
var extractModuleName = function extractModuleName(importFn) {
  var code = importFn.toString();
  var match = code.match(/import\((['"])(.*?)\1/);
  var rawPath = match ? match[2] : "unknown_chunk";
  return rawPath.replace(/^\.\//, "").replace(/\.\.\//g, "").replace(/[/.]+/g, "_");
};
var _retryImport = function retryImport(importFn, moduleName) {
  var retriesLeft = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 3;
  var delay = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 1000;
  var resolvedModuleName = moduleName || extractModuleName(importFn);
  return new Promise(function (resolve, reject) {
    return importFn().then(resolve)["catch"](function (error) {
      setTimeout(function () {
        if (retriesLeft === 1) {
          var reloadKey = "reload_".concat(resolvedModuleName);
          var hasReloaded = sessionStorage.getItem(reloadKey);
          if (!hasReloaded) {
            sessionStorage.setItem(reloadKey, "true");
            window.location.reload();
          } else {
            sessionStorage.removeItem(reloadKey);
            reject(error);
          }
          return;
        }
        _retryImport(importFn, resolvedModuleName, retriesLeft - 1, delay).then(resolve, reject);
      }, delay);
    });
  });
};

module.exports = _retryImport;
//# sourceMappingURL=retryImport.js.map
