'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getFiles = getFiles;
exports.saveFile = saveFile;

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _fsExtra = require('fs-extra');

var _fsExtra2 = _interopRequireDefault(_fsExtra);

var _ = require('../../');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function getFiles() {
  var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';

  var pathToReferences = _path2.default.join(_.config.root, _.config.reference.url);
  var res = {};

  if (name !== '') res[name] = _.cmsData.file.get(name);else {
    var files = _.coreUtils.file.getFilesSync(pathToReferences, true, '.json');
    Array.prototype.forEach.call(files, function (pathFile) {
      var fileName = pathFile.split(_path2.default.sep);
      res[fileName[fileName.length - 1]] = _.cmsData.file.get(pathFile);
    });
  }

  return res;
}

function saveFile(url, json) {
  _fsExtra2.default.writeJson(_path2.default.join(_.config.root, _.config.reference.url, url), JSON.parse(json), function (err) {
    if (err) console.log('saveFile reference error: ', err);
  });
}