"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.fixPackageObject = exports.fixPackageJson = exports["default"] = void 0;var _fileutils = require("./fileutils");
var _constants = require("../constants");
var _logger = require("./logger");function _typeof(obj) {"@babel/helpers - typeof";return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) {return typeof obj;} : function (obj) {return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;}, _typeof(obj);}

var getSortedObject = function getSortedObject(obj) {
  if (obj !== null && _typeof(obj) === 'object' && !Array.isArray(obj)) {
    var keys = Object.keys(obj).sort();
    var newObj = {};
    var addedKeys = {};
    keys.forEach(function (v) {
      if (!addedKeys[v]) {
        newObj[v] = obj[v];
        addedKeys[v] = true;
      }
    });
    return newObj;
  }
  if (Array.isArray(obj)) {
    return obj.sort();
  }
  return obj;
};

var checkForDuplicates = function checkForDuplicates(arr) {
  var dupCheck = {};
  arr.forEach(function (v) {
    if (v) {
      Object.keys(v).forEach(function (k) {
        if (dupCheck[k]) {
          (0, _logger.logWarning)("Key ".concat(
          (0, _logger.chalk)().white(
          k), " is duplicated in your package.json"));


        }
        dupCheck[k] = true;
      });
    }
  });
};

var fixPackageJson = function fixPackageJson(c, pkgPath) {return new Promise(function (resolve) {
    var pth = pkgPath || c.paths.project["package"];
    var pp = (0, _fileutils.readObjectSync)(pth);
    var output = fixPackageObject(pp);
    (0, _fileutils.writeFileSync)(pth, output, 4);
    resolve();
  });};exports.fixPackageJson = fixPackageJson;

var fixPackageObject = function fixPackageObject(pp) {
  var output = {};
  var usedKeys = {};

  _constants.PACKAGE_JSON_FILEDS.forEach(function (v) {
    if (pp[v] !== null) {
      output[v] = getSortedObject(pp[v]);
      usedKeys[v] = true;
    }
  });
  Object.keys(pp).forEach(function (k) {
    if (!usedKeys[k]) {
      output[k] = pp[k];
    }
  });

  checkForDuplicates([pp.dependencies, pp.devDependencies]);

  return output;
};exports.fixPackageObject = fixPackageObject;var _default =


{
  fixPackageJson: fixPackageJson,
  fixPackageObject: fixPackageObject };exports["default"] = _default;
//# sourceMappingURL=doctor.js.map