var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.createPlatformBuild=exports.cleanPlatformBuild=exports.rnvPlatformConnect=exports.rnvPlatformEject=exports.rnvPlatformConfigure=exports.rnvPlatformList=void 0;var _defineProperty2=_interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));

var _chalk=_interopRequireDefault(require("chalk"));
var _path=_interopRequireDefault(require("path"));
var _inquirer=_interopRequireDefault(require("inquirer"));
var _common=require("../common");


var _logger=require("../systemTools/logger");
var _prompt=require("../systemTools/prompt");
var _fileutils=require("../systemTools/fileutils");
var _buildHooks=require("../projectTools/buildHooks");
var _projectParser=require("../projectTools/projectParser");
var _constants=require("../constants");function ownKeys(object,enumerableOnly){var keys=Object.keys(object);if(Object.getOwnPropertySymbols){var symbols=Object.getOwnPropertySymbols(object);if(enumerableOnly)symbols=symbols.filter(function(sym){return Object.getOwnPropertyDescriptor(object,sym).enumerable;});keys.push.apply(keys,symbols);}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){(0,_defineProperty2.default)(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;}

var rnvPlatformList=function rnvPlatformList(c){return new Promise(function(resolve,reject){
var opts=_genPlatOptions(c);
(0,_logger.logToSummary)("Platforms:\n\n"+opts.asString);
resolve();
});};exports.rnvPlatformList=rnvPlatformList;

var rnvPlatformConfigure=function rnvPlatformConfigure(c){return _regenerator.default.async(function rnvPlatformConfigure$(_context){while(1){switch(_context.prev=_context.next){case 0:
c.runtime.platform=c.program.platform||'all';
(0,_logger.logTask)("rnvPlatformConfigure:"+c.runtime.platform);_context.next=4;return _regenerator.default.awrap(

(0,_common.isPlatformSupported)(c));case 4:_context.next=6;return _regenerator.default.awrap(
cleanPlatformBuild(c,c.runtime.platform));case 6:_context.next=8;return _regenerator.default.awrap(
(0,_projectParser.cleanPlaformAssets)(c,c.runtime.platform));case 8:_context.next=10;return _regenerator.default.awrap(
_runCopyPlatforms(c,c.runtime.platform));case 10:case"end":return _context.stop();}}});};exports.rnvPlatformConfigure=rnvPlatformConfigure;


var _generatePlatformChoices=function _generatePlatformChoices(c){return c.buildConfig.defaults.supportedPlatforms.map(function(platform){
var isConnected=c.paths.project.platformTemplatesDirs[platform].includes(c.paths.rnv.platformTemplates.dir);
return{name:platform+" - "+(isConnected?_chalk.default.green('(connected)'):_chalk.default.yellow('(ejected)')),value:platform,isConnected:isConnected};
});};

var rnvPlatformEject=function rnvPlatformEject(c){var _ref,ejectedPlatforms,ptfn,rptf,prf,copyShared;return _regenerator.default.async(function rnvPlatformEject$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:
(0,_logger.logTask)('rnvPlatformEject');_context2.next=3;return _regenerator.default.awrap(

_inquirer.default.prompt({
name:'ejectedPlatforms',
message:'This will copy platformTemplates folders from ReNative managed directly to your project Select platforms you would like to connect',
type:'checkbox',
choices:_generatePlatformChoices(c).map(function(choice){return _objectSpread({},choice,{disabled:!choice.isConnected});})}));case 3:_ref=_context2.sent;ejectedPlatforms=_ref.ejectedPlatforms;


if(ejectedPlatforms.length){
ptfn='platformTemplates';
rptf=c.paths.rnv.platformTemplates.dir;
prf=c.paths.project.dir;

copyShared=false;

ejectedPlatforms.forEach(function(platform){
if(_constants.PLATFORMS[platform].requiresSharedConfig){
copyShared=true;
}

(0,_fileutils.copyFolderContentsRecursiveSync)(_path.default.join(rptf,platform),_path.default.join(prf,ptfn,platform));

if(copyShared){
(0,_fileutils.copyFolderContentsRecursiveSync)(_path.default.join(rptf,'_shared'),_path.default.join(prf,ptfn,'_shared'));
}

c.files.project.config.platformTemplatesDirs=c.files.project.config.platformTemplatesDirs||{};
c.files.project.config.platformTemplatesDirs[platform]="./"+ptfn;

(0,_fileutils.writeObjectSync)(c.paths.project.config,c.files.project.config);
});
}

(0,_logger.logSuccess)(
_chalk.default.white(ejectedPlatforms.join(','))+" platform templates are located in "+_chalk.default.white(
c.files.project.config.platformTemplatesDirs[ejectedPlatforms[0]])+" now. You can edit them directly!");case 7:case"end":return _context2.stop();}}});};exports.rnvPlatformEject=rnvPlatformEject;




var _genPlatOptions=function _genPlatOptions(c){
var opts=(0,_prompt.generateOptions)(c.buildConfig.defaults.supportedPlatforms,true,null,function(i,obj,mapping,defaultVal){
var isEjected=c.paths.project.platformTemplatesDirs[obj].includes(c.paths.rnv.platformTemplates.dir)?_chalk.default.green('(connected)'):_chalk.default.yellow('(ejected)');
return" ["+_chalk.default.white(i+1)+"]> "+_chalk.default.bold(defaultVal)+" - "+isEjected+" \n";
});
return opts;
};

var rnvPlatformConnect=function rnvPlatformConnect(c){var _ref2,connectedPlatforms,_ref3,deletePlatformFolder,pathsToRemove;return _regenerator.default.async(function rnvPlatformConnect$(_context3){while(1){switch(_context3.prev=_context3.next){case 0:
(0,_logger.logTask)('rnvPlatformConnect');_context3.next=3;return _regenerator.default.awrap(

_inquirer.default.prompt({
name:'connectedPlatforms',
message:'This will point platformTemplates folders from your local project to ReNative managed one. Select platforms you would like to connect',
type:'checkbox',
choices:_generatePlatformChoices(c).map(function(choice){return _objectSpread({},choice,{disabled:choice.isConnected});})}));case 3:_ref2=_context3.sent;connectedPlatforms=_ref2.connectedPlatforms;



if(connectedPlatforms.length){
connectedPlatforms.forEach(function(platform){var _c$files$project$conf;
if((_c$files$project$conf=c.files.project.config.platformTemplatesDirs)==null?void 0:_c$files$project$conf[platform]){
delete c.files.project.config.platformTemplatesDirs[platform];
}

if(!Object.keys(c.files.project.config.platformTemplatesDirs).length){
delete c.files.project.config.platformTemplatesDirs;
}

(0,_fileutils.writeObjectSync)(c.paths.project.config,c.files.project.config);
});
}_context3.next=8;return _regenerator.default.awrap(

_inquirer.default.prompt({
name:'deletePlatformFolder',
type:'confirm',
message:'Would you also like to delete the previously used platform folder?'}));case 8:_ref3=_context3.sent;deletePlatformFolder=_ref3.deletePlatformFolder;if(!


deletePlatformFolder){_context3.next=15;break;}
pathsToRemove=[];
connectedPlatforms.forEach(function(platform){
pathsToRemove.push(_path.default.join(c.paths.project.platformTemplatesDirs[platform],platform));
});_context3.next=15;return _regenerator.default.awrap(



(0,_fileutils.removeDirs)(pathsToRemove));case 15:


(0,_logger.logSuccess)(
_chalk.default.white(connectedPlatforms.join(','))+" now using ReNative platformTemplates located in "+_chalk.default.white(c.paths.rnv.platformTemplates.dir)+" now!");case 16:case"end":return _context3.stop();}}});};exports.rnvPlatformConnect=rnvPlatformConnect;



var _runCopyPlatforms=function _runCopyPlatforms(c,platform){return new Promise(function(resolve,reject){
(0,_logger.logTask)("_runCopyPlatforms:"+platform);
var copyPlatformTasks=[];
if(platform==='all'){
for(var k in c.buildConfig.platforms){
if((0,_common.isPlatformSupportedSync)(k)){
var ptPath=_path.default.join(c.paths.project.platformTemplatesDirs[k],""+k);
var pPath=_path.default.join(c.paths.project.builds.dir,c.runtime.appId+"_"+k);
copyPlatformTasks.push((0,_fileutils.copyFolderContentsRecursiveSync)(ptPath,pPath));
}
}
}else if((0,_common.isPlatformSupportedSync)(platform)){
var _ptPath=_path.default.join(c.paths.project.platformTemplatesDirs[platform],""+platform);
var _pPath=_path.default.join(c.paths.project.builds.dir,c.runtime.appId+"_"+platform);
copyPlatformTasks.push((0,_fileutils.copyFolderContentsRecursiveSync)(_ptPath,_pPath));
}else{
logWarning("Your platform "+_chalk.default.white(platform)+" config is not present. Check "+_chalk.default.white(c.paths.appConfig.config));
}

Promise.all(copyPlatformTasks).then(function(values){
resolve();
});
});};

var cleanPlatformBuild=function cleanPlatformBuild(c,platform){return new Promise(function(resolve,reject){
(0,_logger.logTask)("cleanPlatformBuild:"+platform);

var cleanTasks=[];

if(platform==='all'){
for(var k in c.buildConfig.platforms){
if((0,_common.isPlatformSupportedSync)(k)){
var pPath=_path.default.join(c.paths.project.builds.dir,c.runtime.appId+"_"+k);
cleanTasks.push((0,_fileutils.cleanFolder)(pPath));
}
}
}else if((0,_common.isPlatformSupportedSync)(platform)){
var _pPath2=_path.default.join(c.paths.project.builds.dir,c.runtime.appId+"_"+platform);
cleanTasks.push((0,_fileutils.cleanFolder)(_pPath2));
}

Promise.all(cleanTasks).then(function(values){
resolve();
});
});};exports.cleanPlatformBuild=cleanPlatformBuild;

var createPlatformBuild=function createPlatformBuild(c,platform){return new Promise(function(resolve,reject){
(0,_logger.logTask)("createPlatformBuild:"+platform);

if(!(0,_common.isPlatformSupportedSync)(platform,null,reject))return;

var pPath=_path.default.join(c.paths.project.builds.dir,c.runtime.appId+"_"+platform);
var ptPath=_path.default.join(c.paths.project.platformTemplatesDirs[platform],""+platform);
(0,_fileutils.copyFolderContentsRecursiveSync)(ptPath,pPath,false,[_path.default.join(ptPath,'_privateConfig')]);

resolve();
});};exports.createPlatformBuild=createPlatformBuild;