var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.getWorkspaceOptions=exports.getWorkspaceConnectionString=exports.getWorkspaceDirPath=exports.createWorkspace=void 0;var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));var _path=_interopRequireDefault(require("path"));
var _prompt=require("../../cli/prompt");
var _logger=require("../systemManager/logger");






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


var createWorkspace=function createWorkspace(c,workspaceID,workspacePath){var _c$files$defaultWorks,_c$files$defaultWorks2,_c$files$defaultWorks3,_c$files$defaultWorks4;var workspaceConfig;return _regenerator.default.async(function createWorkspace$(_context){while(1){switch(_context.prev=_context.next){case 0:
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.writeFileSync)(_path.default.join(workspacePath,'renative.json'),workspaceConfig);

(0,_fileutils.writeFileSync)(c.paths.rnv.configWorkspaces,c.files.rnv.configWorkspaces);return _context.abrupt("return",
true);case 6:case"end":return _context.stop();}}},null,null,null,Promise);};exports.createWorkspace=createWorkspace;


var getWorkspaceDirPath=function getWorkspaceDirPath(c){var _c$buildConfig,_c$buildConfig2,_c$buildConfig2$paths;var wss,ws,dirPath,_wss$workspaces$ws,wsDir,confirm,_await$inquirerPrompt,conf,_c$buildConfig3,_c$buildConfig3$paths;return _regenerator.default.async(function getWorkspaceDirPath$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:
(0,_logger.logTask)('getWorkspaceDirPath');
wss=c.files.rnv.configWorkspaces;
ws=c.runtime.selectedWorkspace||((_c$buildConfig=c.buildConfig)==null?void 0:_c$buildConfig.workspaceID);if(!(

wss!=null&&wss.workspaces&&ws)){_context2.next=25;break;}
dirPath=(_wss$workspaces$ws=wss.workspaces[ws])==null?void 0:_wss$workspaces$ws.path;if(
dirPath){_context2.next=25;break;}
wsDir=_path.default.join(c.paths.home.dir,"."+ws);if(!
(0,_fileutils.fsExistsSync)(wsDir)){_context2.next=13;break;}
wss.workspaces[ws]={
path:wsDir};

(0,_fileutils.writeFileSync)(c.paths.rnv.configWorkspaces,wss);
(0,_logger.logInfo)("Found workspace id "+
ws+" and compatible directory "+wsDir+". Your "+
c.paths.rnv.configWorkspaces+" has been updated.");_context2.next=25;break;case 13:if(!(

!c.runtime.isWSConfirmed||c.program.ci===true)){_context2.next=25;break;}
confirm=true;if(!(
c.program.ci!==true)){_context2.next=22;break;}_context2.next=18;return _regenerator.default.awrap(
(0,_prompt.inquirerPrompt)({
name:'conf',
type:'confirm',
message:"Your project belongs to workspace "+(0,_logger.chalk)().white(
ws)+". do you want to add new workspace "+
(0,_logger.chalk)().white(
ws)+" to your local system at "+
(0,_logger.chalk)().white(wsDir)+"?",
warningMessage:'No app configs found for this project'}));case 18:_await$inquirerPrompt=_context2.sent;conf=_await$inquirerPrompt.conf;

confirm=conf;
c.runtime.isWSConfirmed=true;case 22:if(!

confirm){_context2.next=25;break;}_context2.next=25;return _regenerator.default.awrap(
createWorkspace(c,ws,wsDir));case 25:




if((_c$buildConfig2=c.buildConfig)!=null&&(_c$buildConfig2$paths=_c$buildConfig2.paths)!=null&&_c$buildConfig2$paths.globalConfigDir){
(0,_logger.logWarning)("paths.globalConfigDir in "+

c.paths.project.config+" is DEPRECATED. use workspaceID instead. more info at https://renative.org/docs/workspaces");


}if(
dirPath){_context2.next=28;break;}return _context2.abrupt("return",
((_c$buildConfig3=c.buildConfig)==null?void 0:(_c$buildConfig3$paths=_c$buildConfig3.paths)==null?void 0:_c$buildConfig3$paths.globalConfigDir)||c.paths.GLOBAL_RNV_DIR);case 28:return _context2.abrupt("return",

dirPath);case 29:case"end":return _context2.stop();}}},null,null,null,Promise);};exports.getWorkspaceDirPath=getWorkspaceDirPath;


var getWorkspaceConnectionString=function getWorkspaceConnectionString(obj){var _obj$remote;
var remoteUrl=(_obj$remote=obj.remote)==null?void 0:_obj$remote.url;
var connectMsg=remoteUrl?
(0,_logger.chalk)().green("("+obj.remote.type+":"+remoteUrl+")"):
'';
return connectMsg;
};exports.getWorkspaceConnectionString=getWorkspaceConnectionString;

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

return" ["+(0,_logger.chalk)().grey(i+1)+"]> "+(0,_logger.chalk)().bold(
defaultVal)+" "+
getWorkspaceConnectionString(obj)+"\n";
});};exports.getWorkspaceOptions=getWorkspaceOptions;
//# sourceMappingURL=workspace.js.map