"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

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

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

var _findRoot = _interopRequireDefault(require("find-root"));

var _constants = require("./constants");

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

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { keys.push.apply(keys, Object.getOwnPropertySymbols(object)); } if (enumerableOnly) keys = keys.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var getProjectRoot = function getProjectRoot() {
  var workingDir = process.cwd();
  return (0, _findRoot["default"])(workingDir);
};

var config = {
  clean: function clean() {
    delete this.workingDir;
    delete this.projectRoot;
    delete this.userConfigFilename;
    delete this.userConfigFilepath;
    delete this.userSeedersFolderName;
    delete this.userSeedersFolderPath;
    delete this.userConfigExists;
    delete this.userConfig;
    delete this.seederTemplate;
    delete this.configTemplate;
  },
  getConfigFromPackageJson: function getConfigFromPackageJson() {
    var projectRoot = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : getProjectRoot();

    var packageJsonPath = _path["default"].join(projectRoot, 'package.json');

    var _require = require(packageJsonPath),
        _require$mdSeed = _require.mdSeed,
        mdSeed = _require$mdSeed === void 0 ? {} : _require$mdSeed;

    return mdSeed;
  },
  getUserGeneratorConfig: function getUserGeneratorConfig() {
    var projectRoot = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : getProjectRoot();
    return _objectSpread({}, _constants.defaultUserGeneratorConfig, {}, this.getConfigFromPackageJson(projectRoot));
  },
  update: function update() {
    var projectRoot = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : getProjectRoot();

    var _this$getUserGenerato = this.getUserGeneratorConfig(projectRoot),
        seedersFolder = _this$getUserGenerato.seedersFolder,
        customSeederTemplate = _this$getUserGenerato.customSeederTemplate;

    var userSeedersFolderName = seedersFolder;

    var userSeedersFolderPath = _path["default"].join(projectRoot, userSeedersFolderName);

    var userConfigFilename = _constants.configFilename;

    var userConfigFilepath = _path["default"].join(projectRoot, userConfigFilename);

    var userConfigExists = _fs["default"].existsSync(userConfigFilepath);

    var configTemplate = _constants.systemConfigTemplate;
    var seederTemplate = customSeederTemplate ? _path["default"].join(projectRoot, customSeederTemplate) : _constants.systemSeederTemplate;
    this.projectRoot = projectRoot;
    this.userConfigFilename = userConfigFilename;
    this.userConfigFilepath = userConfigFilepath;
    this.userSeedersFolderName = userSeedersFolderName;
    this.userSeedersFolderPath = userSeedersFolderPath;
    this.userConfigExists = userConfigExists;
    this.seederTemplate = seederTemplate;
    this.configTemplate = configTemplate;
  },
  loadUserConfig: function loadUserConfig() {
    return require(this.userConfigFilepath);
  }
};
config.update();
var _default = config;
exports["default"] = _default;