import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import { Storage } from "aws-amplify";
import isEmpty from "lodash/isEmpty";
import * as datasetHelper from "../../dataset-helper";
import { setIn } from "seamless-immutable";
export default (function (file) {
  function fetchAFile(_x) {
    return _fetchAFile.apply(this, arguments);
  }

  function _fetchAFile() {
    _fetchAFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(element) {
      var proxyUrl, response, url, blob;
      return _regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              proxyUrl = "https://cors-anywhere.herokuapp.com/";
              if (datasetHelper.getSampleUrl(element) !== undefined) url = proxyUrl + datasetHelper.getSampleUrl(element);
              _context2.next = 4;
              return fetch(url, {
                method: "GET",
                headers: {
                  "X-Requested-With": "xmlhttprequest"
                }
              }).catch(function (error) {
                console.log("Looks like there was a problem: \n", error);
              });

            case 4:
              response = _context2.sent;
              _context2.next = 7;
              return response.blob();

            case 7:
              blob = _context2.sent;
              return _context2.abrupt("return", blob);

            case 9:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2);
    }));
    return _fetchAFile.apply(this, arguments);
  }

  function fileNameExist(file) {
    if (file !== "undefined" && file.fileName !== "undefined") return true;
    return false;
  }

  function createOrReplaceProjectFile(file) {
    Storage.put("".concat(file.fileName, "/"), null, {
      level: "private"
    }).catch(function (err) {
      return console.log(err);
    });
  }

  function createOrReplaceAnnotations(file, json) {
    Storage.put("".concat(file.fileName, "/annotations/annotations.json"), json, {
      level: "private"
    }).catch(function (err) {
      return console.log(err);
    });
  }

  function createOrReplaceImages(file) {
    // TODO you can't have async in a forEach loop
    if (!isEmpty(file.content.samples)) {
      file.content.samples.forEach( /*#__PURE__*/function () {
        var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(element) {
          var blob, imageOrVideoName, pathToFile;
          return _regeneratorRuntime.wrap(function _callee$(_context) {
            while (1) {
              switch (_context.prev = _context.next) {
                case 0:
                  _context.prev = 0;

                  if (isEmpty(datasetHelper.getSampleUrl(element))) {
                    _context.next = 7;
                    break;
                  }

                  _context.next = 4;
                  return fetchAFile(element);

                case 4:
                  blob = _context.sent;
                  _context.next = 8;
                  break;

                case 7:
                  if (!isEmpty(element.document)) {
                    blob = element.document;
                  }

                case 8:
                  if (isEmpty(element.sampleName)) {
                    imageOrVideoName = datasetHelper.getSampleName(element);
                  } else {
                    imageOrVideoName = element.sampleName;
                  }

                  pathToFile = "".concat(file.fileName, "/data/").concat(imageOrVideoName);
                  Storage.put(pathToFile, blob, {
                    level: "private"
                  }).catch(function (err) {
                    return console.log(err);
                  });
                  _context.next = 16;
                  break;

                case 13:
                  _context.prev = 13;
                  _context.t0 = _context["catch"](0);
                  console.log(_context.t0);

                case 16:
                case "end":
                  return _context.stop();
              }
            }
          }, _callee, null, [[0, 13]]);
        }));

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

  if (fileNameExist(file)) {
    var dataset = file.content; // TODO datasetHelper.setSamplesName is returning an incorrect object, it
    // should be returning an array of samples

    console.log("this should be an array of samples", datasetHelper.setSamplesName(dataset));
    file = setIn(file, ["content"], setIn(dataset, ["samples"], datasetHelper.setSamplesName(dataset)));
    var json = JSON.stringify(file);
    createOrReplaceProjectFile(file);
    createOrReplaceAnnotations(file, json);
    createOrReplaceImages(file);
  }
});