import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import isEmpty from "lodash/isEmpty";
import Amplify, { Storage } from "aws-amplify";
import * as datasetHelper from "../../utils//dataset-helper";

function CheckIfAnnotationExist(result, folderToFetch) {
  return result.find(function (element) {
    return element.key === "".concat(folderToFetch, "/annotations/annotations.json");
  });
}

function GetSampleFromAnnotation(json, samples) {
  if (isEmpty(json.content.samples)) return;
  var newSamples = [];

  for (var i = 0; i < json.content.samples.length; i++) {
    var sampleName = datasetHelper.getSampleName(json.content.samples[i]);
    var annotation = json.content.samples[i].annotation;
    var sampleFound = datasetHelper.getSampleWithThisSampleName(sampleName, samples);
    var url;

    if (sampleFound === null) {
      url = datasetHelper.getSampleUrl(samples);
    } else {
      url = datasetHelper.getSampleUrl(sampleFound);
    }

    newSamples.push(datasetHelper.createOneNewSample(sampleName, url, annotation));
  }

  json.content.samples = newSamples;
}

export default /*#__PURE__*/(function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(result, samples, folderToFetch, authConfig) {
    var json;
    return _regeneratorRuntime.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            Amplify.configure(authConfig);
            json = null;

            if (!CheckIfAnnotationExist(result, folderToFetch)) {
              _context4.next = 5;
              break;
            }

            _context4.next = 5;
            return Storage.get("".concat(folderToFetch, "/annotations/annotations.json"), {
              expires: 24 * 60 * 60 * 2000,
              level: "private"
            }).then( /*#__PURE__*/function () {
              var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(result) {
                return _regeneratorRuntime.wrap(function _callee3$(_context3) {
                  while (1) {
                    switch (_context3.prev = _context3.next) {
                      case 0:
                        _context3.next = 2;
                        return fetch(result).then( /*#__PURE__*/function () {
                          var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(data) {
                            return _regeneratorRuntime.wrap(function _callee2$(_context2) {
                              while (1) {
                                switch (_context2.prev = _context2.next) {
                                  case 0:
                                    _context2.next = 2;
                                    return data.json().then( /*#__PURE__*/function () {
                                      var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(result) {
                                        return _regeneratorRuntime.wrap(function _callee$(_context) {
                                          while (1) {
                                            switch (_context.prev = _context.next) {
                                              case 0:
                                                if (!(typeof result.content === "undefined")) {
                                                  _context.next = 2;
                                                  break;
                                                }

                                                return _context.abrupt("return");

                                              case 2:
                                                json = result;
                                                GetSampleFromAnnotation(json, samples);

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

                                      return function (_x7) {
                                        return _ref4.apply(this, arguments);
                                      };
                                    }());

                                  case 2:
                                    return _context2.abrupt("return", _context2.sent);

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

                          return function (_x6) {
                            return _ref3.apply(this, arguments);
                          };
                        }());

                      case 2:
                      case "end":
                        return _context3.stop();
                    }
                  }
                }, _callee3);
              }));

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

          case 5:
            return _context4.abrupt("return", json);

          case 6:
          case "end":
            return _context4.stop();
        }
      }
    }, _callee4);
  }));

  return function (_x, _x2, _x3, _x4) {
    return _ref.apply(this, arguments);
  };
})();