'use strict';

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

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

var _fsExtra2 = _interopRequireDefault(_fsExtra);

var _cliColor = require('cli-color');

var _cliColor2 = _interopRequireDefault(_cliColor);

var _extend = require('extend');

var _extend2 = _interopRequireDefault(_extend);

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _config = require('./config.json');

var _config2 = _interopRequireDefault(_config);

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

var result = _config2.default;
result.root = process.cwd();
if (process.env.ROOT) {
  result.root = process.env.ROOT.replace(/\/$/, '');
}

var hintAbeJson = false;

var loadLocalConfig = function loadLocalConfig(result) {
  if (result.root !== '') {
    try {
      var stat = _fsExtra2.default.statSync(result.root);
      if (stat && stat.isDirectory()) {
        try {
          stat = _fsExtra2.default.statSync(_path2.default.join(result.root, 'abe.json'));
          if (stat) {
            var json = _fsExtra2.default.readJsonSync(_path2.default.join(result.root, 'abe.json'));
            result = (0, _extend2.default)(true, result, json);
          }
        } catch (e) {
          if (!hintAbeJson) {
            hintAbeJson = true;
            console.log(_cliColor2.default.green('[ Hint ]'), 'you can create a specific config file named abe.json to customize your abe install', _cliColor2.default.cyan.underline('https://github.com/AdFabConnect/abejs/blob/master/docs/abe-config.md'));
          }
        }
      }
    } catch (e) {
      console.log('LoadConfig Error');
      console.log(e);
    }
  }
};

loadLocalConfig(result);

result.exist = function (conf, json) {
  var c = conf.split('.');
  var current = json;
  if (typeof current !== 'undefined' && current !== null) {
    Array.prototype.forEach.call(c, function (c) {
      if (current !== false && typeof current[c] !== 'undefined' && current[c] !== null) {
        current = current[c];
      } else {
        current = false;
        return false;
      }
    });
    return current;
  } else {
    return false;
  }
};

result.localConfigExist = function () {
  if (result.root !== '') {
    try {
      var stat = _fsExtra2.default.statSync(result.root);
      if (stat && stat.isDirectory()) {
        try {
          stat = _fsExtra2.default.statSync(_path2.default.join(result.root, 'abe.json'));
          if (stat) {
            return true;
          }
        } catch (e) {
          return false;
        }
      }
    } catch (e) {

      return false;
    }
  }

  return false;
};

result.getLocalConfig = function () {
  if (result.localConfigExist()) {
    return _fsExtra2.default.readJsonSync(_path2.default.join(result.root, 'abe.json'));
  } else {
    return {};
  }
};

result.getDefault = function (conf) {
  return result[conf];
};

result.get = function (conf) {

  return result.exist(conf, result);
};

result.set = function (json) {
  (0, _extend2.default)(true, result, json);
  loadLocalConfig(result);
};

result.save = function (json) {
  // extend(true, result, json)

  if (result.localConfigExist()) {
    // var abeJson = fse.readJsonSync(path.join(result.root,'abe.json'))
    // extend(true, abeJson, json)
    var confPath = _path2.default.join(result.root, 'abe.json');
    _fsExtra2.default.writeJsonSync(confPath, json, { space: 2, encoding: 'utf-8' });
  }
};

result.getConfigByWebsite = function () {
  var defaultConfig = (0, _extend2.default)(true, {}, result);
  var configBySite = {
    default: {}
  };

  var localConfig = (0, _extend2.default)(true, {}, defaultConfig);
  for (var item in localConfig) {
    switch (item) {
      case 'intlData':
        configBySite.default.intlData = localConfig[item];
        break;
      case 'templates':
        configBySite.default.templates = localConfig[item];
        break;
      case 'structure':
        configBySite.default.structure = localConfig[item];
        break;
      case 'data':
        configBySite.default.data = localConfig[item];
        break;
      case 'publish':
        configBySite.default.publish = localConfig[item];
        break;
      case 'files':
        configBySite.default.files = {
          templates: {
            extension: localConfig[item].templates.extension
          }
        };
        break;
      case 'upload':
        configBySite.default.upload = localConfig[item];
        break;
    }
  }

  return configBySite;
};

exports.default = result;