import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import JAC from "jac-format";
var alternativeSpellings = {
  imageUrl: ["image_url", "image_urls", "image", "image_src", "img_url", "img", "img_src"],
  document: ["text"]
};
var badSpellingMap = {};

for (var _i = 0, _Object$entries = Object.entries(alternativeSpellings); _i < _Object$entries.length; _i++) {
  var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
      correct = _Object$entries$_i[0],
      badSpellings = _Object$entries$_i[1];

  var _iterator = _createForOfIteratorHelper(badSpellings),
      _step;

  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var badSpelling = _step.value;
      badSpellingMap[badSpelling] = correct;
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }
}

var convertTaskDatumMistakes = function convertTaskDatumMistakes(sample) {
  for (var key in sample) {
    var normalizedKey = key.toLowerCase().replace(/ /g, "_");
    var correction = badSpellingMap[normalizedKey];

    if (correction) {
      sample[correction] = sample[key];
      delete sample[key];
    }
  }

  return sample;
};

export default (function (csv) {
  var json = JAC.fromCSV(csv, {
    derivePath: function derivePath() {
      return "samples.*";
    }
  });
  if (!json.samples) throw new Error("No samples");
  return {
    interface: json.interface,
    taskOutput: json.samples.map(function (sample) {
      return sample.output;
    }),
    samples: json.samples.map(convertTaskDatumMistakes).map(function (sample) {
      delete sample.output;
      return sample;
    })
  };
});