import _regeneratorRuntime from "@babel/runtime/regenerator";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import uploadFileToTransferSh from "./upload-file-to-transfersh";
import splitFileNameFromFileURL from "./split-file-name-from-file-url";
import getFileURLKey from "./get-file-url-key";
import { setIn } from "seamless-immutable"; // TODO move to utils directory

var idify = function idify(s) {
  return s.toLowerCase().replace(/[^a-z0-9]/g, "_").replace(/_+/g, "_");
};

var transformFileURLsToWebURLs = /*#__PURE__*/function () {
  var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(_ref) {
    var dataset, onChangeDataset, setProgress, remote, newsamples, progressUnit, samplesIndex, samplesItem, fileURLKey, fileURL, halfProgressOfUnit, fileName, webURLOfUploadedFile;
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            dataset = _ref.dataset, onChangeDataset = _ref.onChangeDataset, setProgress = _ref.setProgress, remote = _ref.remote;
            newsamples = [];
            progressUnit = 100 / dataset.samples.length;
            samplesIndex = 0;

          case 4:
            if (!(samplesIndex < dataset.samples.length)) {
              _context.next = 27;
              break;
            }

            samplesItem = dataset.samples[samplesIndex];
            fileURLKey = getFileURLKey(samplesItem);

            if (fileURLKey) {
              _context.next = 10;
              break;
            }

            newsamples.push(samplesItem);
            return _context.abrupt("continue", 24);

          case 10:
            fileURL = samplesItem[fileURLKey];

            if (fileURL.startsWith("file://")) {
              _context.next = 14;
              break;
            }

            newsamples.push(samplesItem);
            return _context.abrupt("continue", 24);

          case 14:
            halfProgressOfUnit = progressUnit * samplesIndex;
            setProgress(halfProgressOfUnit);
            fileName = idify(splitFileNameFromFileURL(fileURL));
            _context.next = 19;
            return uploadFileToTransferSh({
              fileName: fileName,
              fileURL: fileURL,
              remote: remote
            }).catch(function (e) {
              return console.warn(e);
            });

          case 19:
            webURLOfUploadedFile = _context.sent;

            if (webURLOfUploadedFile) {
              _context.next = 23;
              break;
            }

            newsamples.push(samplesItem);
            return _context.abrupt("continue", 24);

          case 23:
            newsamples.push(_objectSpread(_objectSpread({}, samplesItem), {}, _defineProperty({}, fileURLKey, webURLOfUploadedFile)));

          case 24:
            samplesIndex++;
            _context.next = 4;
            break;

          case 27:
            setProgress(100);
            onChangeDataset(setIn(dataset, ["samples"], newsamples));

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

  return function transformFileURLsToWebURLs(_x) {
    return _ref2.apply(this, arguments);
  };
}();

export default transformFileURLsToWebURLs;