var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.getWorkspaceOptions=exports.rnvWorkspaceUpdate=exports.rnvWorkspaceConnect=exports.rnvWorkspaceAdd=exports.rnvWorkspaceList=void 0;var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));var _chalk=_interopRequireDefault(require("chalk"));
var _path=_interopRequireDefault(require("path"));
var _fs=_interopRequireDefault(require("fs"));
var _prompt=require("../systemTools/prompt");
var _logger=require("../systemTools/logger");




var _fileutils=require("../systemTools/fileutils");

var rnvWorkspaceList=function rnvWorkspaceList(c){var _c$files$rnv$configWo;var opts;return _regenerator.default.async(function rnvWorkspaceList$(_context){while(1){switch(_context.prev=_context.next){case 0:
(0,_logger.logTask)('rnvWorkspaceList');


opts=(0,_prompt.generateOptions)((_c$files$rnv$configWo=c.files.rnv.configWorkspaces)==null?void 0:_c$files$rnv$configWo.workspaces,true,null,function(i,obj,mapping,defaultVal){
var isConnected='';
return" ["+_chalk.default.grey(i+1)+"]> "+_chalk.default.bold(defaultVal)+isConnected+" \n";
});

(0,_logger.logToSummary)("Workspaces:\n\n"+opts.asString);case 3:case"end":return _context.stop();}}});};exports.rnvWorkspaceList=rnvWorkspaceList;


var rnvWorkspaceAdd=function rnvWorkspaceAdd(c){var _c$files$defaultWorks,_c$files$defaultWorks2,_c$files$defaultWorks3,_c$files$defaultWorks4;var _ref,workspace,workspacePath,_ref2,confirm,workspaceID,_ref3,workspaceIDInput,workspaceConfig;return _regenerator.default.async(function rnvWorkspaceAdd$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:
(0,_logger.logTask)('rnvWorkspaceAdd');_context2.next=3;return _regenerator.default.awrap(

(0,_prompt.inquirerPrompt)({
name:'workspace',
type:'input',
message:'absolute path to new workspace',
validate:function validate(i){return!!i||'No path provided';}}));case 3:_ref=_context2.sent;workspace=_ref.workspace;


workspacePath=_path.default.join(workspace);if(!

_fs.default.existsSync(workspacePath)){_context2.next=13;break;}_context2.next=9;return _regenerator.default.awrap(
(0,_prompt.inquirerPrompt)({
name:'confirm',
type:'confirm',
message:"Folder "+workspacePath+" already exists are you sure you want to override it?"}));case 9:_ref2=_context2.sent;confirm=_ref2.confirm;if(

confirm){_context2.next=13;break;}return _context2.abrupt("return");case 13:


workspaceID=workspacePath.split('/').pop().replace(/@|\./g,'');_context2.next=16;return _regenerator.default.awrap(

(0,_prompt.inquirerPrompt)({
name:'workspaceIDInput',
type:'input',
message:"ID of the workspace ("+workspaceID+")"}));case 16:_ref3=_context2.sent;workspaceIDInput=_ref3.workspaceIDInput;


workspaceID=workspaceIDInput||workspaceID;
c.files.rnv.configWorkspaces.workspaces[workspaceID]={
path:workspacePath};


workspaceConfig={
sdks:(_c$files$defaultWorks=c.files.defaultWorkspace)==null?void 0:(_c$files$defaultWorks2=_c$files$defaultWorks.config)==null?void 0:_c$files$defaultWorks2.sdks,
defaultTargets:(_c$files$defaultWorks3=c.files.defaultWorkspace)==null?void 0:(_c$files$defaultWorks4=_c$files$defaultWorks3.config)==null?void 0:_c$files$defaultWorks4.defaultTargets};


(0,_fileutils.mkdirSync)(workspacePath);
(0,_fileutils.writeObjectSync)(_path.default.join(workspacePath,'renative.json'),workspaceConfig);

(0,_fileutils.writeObjectSync)(c.paths.rnv.configWorkspaces,c.files.rnv.configWorkspaces);case 24:case"end":return _context2.stop();}}});};exports.rnvWorkspaceAdd=rnvWorkspaceAdd;


var rnvWorkspaceConnect=function rnvWorkspaceConnect(c){var _c$files$rnv$configWo2;var opts,_ref4,selectedWS;return _regenerator.default.async(function rnvWorkspaceConnect$(_context3){while(1){switch(_context3.prev=_context3.next){case 0:
(0,_logger.logTask)('rnvWorkspaceConnect');

opts=Object.keys((_c$files$rnv$configWo2=c.files.rnv.configWorkspaces)==null?void 0:_c$files$rnv$configWo2.workspaces).map(function(v){var _c$files$rnv$configWo3;return v+" "+_getConnectionString((_c$files$rnv$configWo3=c.files.rnv.configWorkspaces)==null?void 0:_c$files$rnv$configWo3.workspaces[v]);});_context3.next=4;return _regenerator.default.awrap(

(0,_prompt.inquirerPrompt)({
type:'list',
name:'selectedWS',
message:'Pick a workspace',
choices:opts}));case 4:_ref4=_context3.sent;selectedWS=_ref4.selectedWS;case 6:case"end":return _context3.stop();}}});};exports.rnvWorkspaceConnect=rnvWorkspaceConnect;



var _getConnectionString=function _getConnectionString(obj){var _obj$remote;
var remoteUrl=(_obj$remote=obj.remote)==null?void 0:_obj$remote.url;
var connectMsg=remoteUrl?_chalk.default.green("("+obj.remote.type+":"+remoteUrl+")"):'';
return connectMsg;
};

var rnvWorkspaceUpdate=function rnvWorkspaceUpdate(c){return _regenerator.default.async(function rnvWorkspaceUpdate$(_context4){while(1){switch(_context4.prev=_context4.next){case 0:
(0,_logger.logTask)('rnvWorkspaceUpdate');case 1:case"end":return _context4.stop();}}});};exports.rnvWorkspaceUpdate=rnvWorkspaceUpdate;



var getWorkspaceOptions=function getWorkspaceOptions(c){var _c$files$rnv$configWo4;return(0,_prompt.generateOptions)((_c$files$rnv$configWo4=c.files.rnv.configWorkspaces)==null?void 0:_c$files$rnv$configWo4.workspaces,false,null,function(i,obj,mapping,defaultVal){
(0,_logger.logDebug)('getWorkspaceOptions');

return" ["+_chalk.default.grey(i+1)+"]> "+_chalk.default.bold(defaultVal)+" "+_getConnectionString(obj)+"\n";
});};exports.getWorkspaceOptions=getWorkspaceOptions;