import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import RecognizeFileExtension from "../../utils/RecognizeFileExtension";
import Amplify, { Storage } from "aws-amplify";

function setUrl(_x, _x2) {
  return _setUrl.apply(this, arguments);
}

function _setUrl() {
  _setUrl = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(result, configImport) {
    var texte;
    return _regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            if (!configImport.loadProjectIsSelected) {
              _context3.next = 17;
              break;
            }

            if (!(RecognizeFileExtension(result) === "Image")) {
              _context3.next = 5;
              break;
            }

            return _context3.abrupt("return", {
              imageUrl: "".concat(result)
            });

          case 5:
            if (!(RecognizeFileExtension(result) === "Video")) {
              _context3.next = 9;
              break;
            }

            return _context3.abrupt("return", {
              videoUrl: "".concat(result)
            });

          case 9:
            if (!(RecognizeFileExtension(result) === "Audio")) {
              _context3.next = 13;
              break;
            }

            return _context3.abrupt("return", {
              audioUrl: "".concat(result)
            });

          case 13:
            if (!(RecognizeFileExtension(result) === "PDF")) {
              _context3.next = 15;
              break;
            }

            return _context3.abrupt("return", {
              pdfUrl: "".concat(result)
            });

          case 15:
            _context3.next = 38;
            break;

          case 17:
            if (!(RecognizeFileExtension(result) === configImport.typeOfFileToLoad && configImport.typeOfFileToLoad === "Image")) {
              _context3.next = 21;
              break;
            }

            return _context3.abrupt("return", {
              imageUrl: "".concat(result)
            });

          case 21:
            if (!(RecognizeFileExtension(result) === configImport.typeOfFileToLoad && configImport.typeOfFileToLoad === "Video")) {
              _context3.next = 25;
              break;
            }

            return _context3.abrupt("return", {
              videoUrl: "".concat(result)
            });

          case 25:
            if (!(RecognizeFileExtension(result) === configImport.typeOfFileToLoad && configImport.typeOfFileToLoad === "Audio")) {
              _context3.next = 29;
              break;
            }

            return _context3.abrupt("return", {
              audioUrl: "".concat(result)
            });

          case 29:
            if (!(RecognizeFileExtension(result) === configImport.typeOfFileToLoad && configImport.typeOfFileToLoad === "PDF")) {
              _context3.next = 33;
              break;
            }

            return _context3.abrupt("return", {
              pdfUrl: "".concat(result)
            });

          case 33:
            if (!(RecognizeFileExtension(result) === configImport.typeOfFileToLoad && configImport.typeOfFileToLoad === "Texte")) {
              _context3.next = 38;
              break;
            }

            _context3.next = 36;
            return fetchTextInFile(result);

          case 36:
            texte = _context3.sent;
            return _context3.abrupt("return", {
              document: "".concat(texte)
            });

          case 38:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  }));
  return _setUrl.apply(this, arguments);
}

function fetchTextInFile(_x3) {
  return _fetchTextInFile.apply(this, arguments);
}

function _fetchTextInFile() {
  _fetchTextInFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(urlSource) {
    var proxyUrl, response, url, texte;
    return _regeneratorRuntime.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            proxyUrl = "https://cors-anywhere.herokuapp.com/";
            url = proxyUrl + urlSource;
            _context4.next = 4;
            return fetch(url, {
              method: "GET"
            }).catch(function (error) {
              console.log("Looks like there was a problem: \n", error);
            });

          case 4:
            response = _context4.sent;
            _context4.next = 7;
            return response.text();

          case 7:
            texte = _context4.sent;
            return _context4.abrupt("return", texte);

          case 9:
          case "end":
            return _context4.stop();
        }
      }
    }, _callee4);
  }));
  return _fetchTextInFile.apply(this, arguments);
}

export default /*#__PURE__*/(function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(result, folderToFetch, configImport, authConfig) {
    var samples, i;
    return _regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            Amplify.configure(authConfig);
            samples = [];
            i = 0;

          case 3:
            if (!(i < result.length)) {
              _context2.next = 10;
              break;
            }

            if (!result[i].key.match("(".concat(folderToFetch, "/data).*(\\.).*"))) {
              _context2.next = 7;
              break;
            }

            _context2.next = 7;
            return Storage.get(result[i].key, {
              expires: 24 * 60 * 60,
              level: "private"
            }).then( /*#__PURE__*/function () {
              var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(result) {
                return _regeneratorRuntime.wrap(function _callee$(_context) {
                  while (1) {
                    switch (_context.prev = _context.next) {
                      case 0:
                        _context.t0 = samples;
                        _context.next = 3;
                        return setUrl(result, configImport);

                      case 3:
                        _context.t1 = _context.sent;

                        _context.t0.push.call(_context.t0, _context.t1);

                      case 5:
                      case "end":
                        return _context.stop();
                    }
                  }
                }, _callee);
              }));

              return function (_x8) {
                return _ref2.apply(this, arguments);
              };
            }()).catch(function (err) {
              console.log("error getting link for s3 image", err);
              return null;
            });

          case 7:
            i++;
            _context2.next = 3;
            break;

          case 10:
            return _context2.abrupt("return", samples);

          case 11:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));

  return function (_x4, _x5, _x6, _x7) {
    return _ref.apply(this, arguments);
  };
})();