var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.default=void 0;var _slicedToArray2=_interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _consoleTablePrinter=require("console-table-printer");
var _fs=_interopRequireDefault(require("fs"));

var _config=_interopRequireDefault(require("../config"));
var _logger=require("./logger");
var _fileutils=require("./fileutils");

var configSchema={
analytics:{
values:['true','false'],
key:'disableAnalytics',
reverse:true}};



var _getValue=function _getValue(key,global){var
paths=_config.default.paths;
var globalConfig=require(paths.GLOBAL_RNV_CONFIG);
var localConfig;
if(_fs.default.existsSync(paths.project.config)){
localConfig=require(paths.project.config);
}

if(!global&&!localConfig)return'N/A';

var value=global?globalConfig[key]:localConfig[key];

if(configSchema[key].reverse)value=!value;
return value;
};

var _listValue=function _listValue(key){
var table=[{
Key:key,
'Global Value':_getValue(key,true)}];


var localValue=_getValue(key);

if(localValue!=='N/A'){
table[0]['Project Value']=_getValue(key);
}

return table;
};

var _isValid=function _isValid(key,value){
var keySchema=configSchema[key];
if(!keySchema){
(0,_logger.logWarning)("Unknown config param "+key);
return false;
}

if(keySchema.values&&!keySchema.values.includes(value)){
(0,_logger.logWarning)("Unsupported value provided for "+key+". Correct values are "+keySchema.values.join(', '));
return false;
}

return true;
};

var _setValue=function _setValue(key,value){var
global=_config.default.program.global,paths=_config.default.paths;

if(_isValid(key,value)){
var configPath=global?paths.GLOBAL_RNV_CONFIG:paths.project.config;
var config=require(configPath);

if(['true','false'].includes(value))value=value==='true';

config[configSchema[key].key]=configSchema[key].reverse?!value:value;
(0,_fileutils.writeObjectSync)(configPath,config);
}
};

var rnvConfig=function rnvConfig(){var _Config$rnvArguments=(0,_slicedToArray2.default)(
_config.default.rnvArguments,3),key=_Config$rnvArguments[1],value=_Config$rnvArguments[2];

if(key==='list'){var _ref;
var rows=[];
Object.keys(configSchema).forEach(function(k){return rows.push(_listValue(k));});

(0,_consoleTablePrinter.printTable)((_ref=[]).concat.apply(_ref,rows));
return true;
}


if(!key){
(0,_logger.logWarning)('Please specify a config');
return true;
}
if(!configSchema[key]){
(0,_logger.logWarning)("Unknown config "+key);
return true;
}

if(!value){

(0,_consoleTablePrinter.printTable)(_listValue(key));
}else{
_setValue(key,value);
}

return true;
};var _default=

rnvConfig;exports.default=_default;
//# sourceMappingURL=config.js.map