import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
import JAC from "jac-format";
import range from "lodash/range";
import { flatten } from "flat";
export default (function (obj) {
  var AddSamplesKeys = function AddSamplesKeys(k) {
    sampleKeys.add(k);
  }; // Find all possible sample keys


  var sampleKeys = new Set();

  var _iterator = _createForOfIteratorHelper(obj.samples),
      _step;

  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var sample = _step.value;
      Object.keys(flatten(sample)).forEach(function (k) {
        return AddSamplesKeys(k);
      });
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }

  sampleKeys = Array.from(sampleKeys).sort(function (a, b) {
    return a.split(".").length - b.split(".").length;
  });

  if (sampleKeys.some(function (k) {
    return k.includes("output.keyframes");
  })) {
    sampleKeys = sampleKeys.filter(function (k) {
      return k.includes("output.keyframes");
    });
    sampleKeys.push("output.keyframes");
  }

  var options = {
    rows: ["interface"].concat(_toConsumableArray(range(obj.samples.length).map(function (i) {
      return "samples.".concat(i);
    }))),
    columns: ["."].concat(_toConsumableArray(sampleKeys)),
    validate: false
  };
  return JAC.toCSV(obj, options);
});