import saveAs from 'file-saver';
import metaInfo from './../../../meta';
import { FOLDER_OPEN_STATE } from '../../constants';
export var getFoldersForPaths = function getFoldersForPaths(paths, openedFolders, override, sep) {
  return paths.reduce(function (res, path) {
    var folders = path.split(sep);
    folders.pop(); //remove file

    folders.forEach(function (f, i, l) {
      var key = l.slice(0, i + 1).join(sep);
      res[key] = override || !openedFolders[key] ? FOLDER_OPEN_STATE.OPEN_ACTIVE_CHILDREN_ONLY : openedFolders[key];
    });
    return res;
  }, {});
};
export var downloadObjectAsJsonFile = function downloadObjectAsJsonFile(data) {
  var fileName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'codecrumbs-showcase.json';
  var fileToSave = new Blob([JSON.stringify({
    version: metaInfo.version,
    data: data
  }, undefined, 2)], {
    type: 'application/json',
    name: fileName
  });
  saveAs(fileToSave, fileName);
};
export var uploadFileAsObject = function uploadFileAsObject(file) {
  return new Promise(function (resolve, reject) {
    var reader = new FileReader();

    reader.onload = function (e) {
      return resolve(JSON.parse(e.target.result));
    };

    reader.onerror = reject;
    reader.readAsText(file);
  });
};