"use strict";

var _interopRequireWildcard = require("@babel/runtime-corejs3/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");

require("core-js/modules/es.array.iterator");

require("core-js/modules/es.promise");

require("core-js/modules/es.string.replace");

var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");

var _mapInstanceProperty = require("@babel/runtime-corejs3/core-js-stable/instance/map");

_Object$defineProperty(exports, "__esModule", {
  value: true
});

exports.default = void 0;

var _stringify = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/json/stringify"));

var _promise = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/promise"));

var _indexOf = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/index-of"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/asyncToGenerator"));

var _fs = _interopRequireDefault(require("fs"));

var _path = _interopRequireWildcard(require("path"));

var _util = require("util");

var _plugin = _interopRequireDefault(require("@start/plugin"));

var _lodash = require("lodash");

const readdir = (0, _util.promisify)(_fs.default.readdir);
const writeFile = (0, _util.promisify)(_fs.default.writeFile);
const lstat = (0, _util.promisify)(_fs.default.lstat);
const mkdir = (0, _util.promisify)(_fs.default.mkdir);
const exists = (0, _util.promisify)(_fs.default.exists);

const tryRequire =
/*#__PURE__*/
function () {
  var _ref = (0, _asyncToGenerator2.default)(function* (file) {
    try {
      return require(file);
    } catch (e) {
      var _context;

      if (e && (0, _indexOf.default)(_context = e.toString()).call(_context, 'Cannot find module' > -1)) return {};
      console.error('f', file, e);
      return {};
    }
  });

  return function tryRequire(_x) {
    return _ref.apply(this, arguments);
  };
}();

var _default = (configsFolder = (0, _path.resolve)(process.cwd(), './app/configs')) => (0, _plugin.default)('CREATE CONFIG', ({
  reporter
}) =>
/*#__PURE__*/
(0, _asyncToGenerator2.default)(function* () {
  const loadConfig =
  /*#__PURE__*/
  function () {
    var _ref3 = (0, _asyncToGenerator2.default)(function* (file) {
      const filePath = _path.default.join(configsFolder, file);

      const context = process.env.SERVER_ENV || 'local';

      const contextFilePath = _path.default.join(configsFolder, context, file);

      const contextConfig = yield tryRequire(contextFilePath);
      const config = yield tryRequire(filePath);
      return (0, _lodash.merge)(config, contextConfig);
    });

    return function loadConfig(_x2) {
      return _ref3.apply(this, arguments);
    };
  }();

  const config = {};

  try {
    const names = yield readdir(configsFolder);
    yield _promise.default.all((0, _mapInstanceProperty(_lodash))(names,
    /*#__PURE__*/
    function () {
      var _ref4 = (0, _asyncToGenerator2.default)(function* (file) {
        if ((0, _indexOf.default)(file).call(file, '.js') === -1) return;
        const configKey = file.replace('.js', '');

        const filePath = _path.default.join(configsFolder, file);

        const isDirectory = (yield lstat(filePath)).isDirectory();

        if (!isDirectory) {
          config[configKey] = yield loadConfig(file);
        }
      });

      return function (_x3) {
        return _ref4.apply(this, arguments);
      };
    }()));
  } catch (e) {
    console.log('e', e);
  }

  const dirname = _path.default.join(process.cwd(), './config');

  if (!(yield exists(dirname))) {
    yield mkdir(dirname, {
      recursive: true
    }).catch(console.error);
  }

  const content = `var cosmicConfig = ${(0, _stringify.default)(config, null, 4)}`;
  yield writeFile((0, _path.resolve)(dirname, 'config.js'), content, 'utf8');

  if (!(yield exists((0, _path.resolve)(dirname, 'override.js')))) {
    const overrideContent = 'var cosmicOverride = {}';
    yield writeFile((0, _path.resolve)(dirname, 'override.js'), overrideContent, 'utf8');
  }
}));

exports.default = _default;
module.exports = exports.default;