'use strict';

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

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

exports.saveJson = saveJson;
exports.saveHtml = saveHtml;

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

var _fsExtra2 = _interopRequireDefault(_fsExtra);

var _mkdirp = require('mkdirp');

var _mkdirp2 = _interopRequireDefault(_mkdirp);

var _xss = require('xss');

var _xss2 = _interopRequireDefault(_xss);

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

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

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

function saveJson(url, json) {
  _mkdirp2.default.sync(_path2.default.dirname(url));

  if (typeof json.abe_source !== 'undefined' && json.abe_source !== null) {
    delete json.abe_source;
  }

  var eachRecursive = function eachRecursive(obj) {
    for (var k in obj) {
      if (_typeof(obj[k]) === 'object' && obj[k] !== null) {
        eachRecursive(obj[k]);
      } else if (typeof obj[k] !== 'undefined' && obj[k] !== null) {
        obj[k] = (0, _xss2.default)(obj[k].toString().replace(/&quot;/g, '"'), { 'whiteList': _.config.htmlWhiteList });
      }
    }
  };

  eachRecursive(json);

  _fsExtra2.default.writeJsonSync(url, json, {
    space: 2,
    encoding: 'utf-8'
  });
  return true;
}

function saveHtml(url, html) {
  _mkdirp2.default.sync(_path2.default.dirname(url));
  _fsExtra2.default.writeFileSync(url, html);

  return true;
}