var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.listAppleDevices=exports.launchAppleSimulator=exports.getAppleDevices=void 0;var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));var _inquirer=_interopRequireDefault(require("inquirer"));
var _chalk=_interopRequireDefault(require("chalk"));
var _child_process=_interopRequireDefault(require("child_process"));
var _common=require("../../common");



var _logger=require("../../systemTools/logger");


var _constants=require("../../constants");

var getAppleDevices=function getAppleDevices(c,platform,ignoreDevices,ignoreSimulators){
(0,_logger.logTask)("getAppleDevices:"+platform+",ignoreDevices:"+ignoreDevices+",ignoreSimulators"+ignoreSimulators);
var devices=_child_process.default.execFileSync('xcrun',['instruments','-s'],{
encoding:'utf8'});


var devicesArr=_parseIOSDevicesList(devices,platform,ignoreDevices,ignoreSimulators);
return devicesArr;
};exports.getAppleDevices=getAppleDevices;

var _parseIOSDevicesList=function _parseIOSDevicesList(text,platform){var ignoreDevices=arguments.length>2&&arguments[2]!==undefined?arguments[2]:false;var ignoreSimulators=arguments.length>3&&arguments[3]!==undefined?arguments[3]:false;
var devices=[];
text.split('\n').forEach(function(line){
var s1=line.match(/\[.*?\]/);
var s2=line.match(/\(.*?\)/g);
var s3=line.substring(0,line.indexOf('(')-1);
var s4=line.substring(0,line.indexOf('[')-1);
var isSim=false;
if(s2&&s1){
if(s2[s2.length-1]==='(Simulator)'){
isSim=true;
s2.pop();
}
var version=s2.pop();
var name=""+s4.substring(0,s4.lastIndexOf('(')-1);
name=name||'undefined';
var udid=s1[0].replace(/\[|\]/g,'');
var isDevice=!isSim;

if(isDevice&&!ignoreDevices||!isDevice&&!ignoreSimulators){
switch(platform){
case _constants.IOS:
if(name.includes('iPhone')||name.includes('iPad')||name.includes('iPod')||isDevice){
var icon='Phone 📱';
if(name.includes('iPad'))icon='Tablet 💊';
devices.push({udid:udid,name:name,version:version,isDevice:isDevice,icon:icon});
}
break;
case _constants.TVOS:
if((name.includes('Apple TV')||isDevice)&&!name.includes('iPhone')&&!name.includes('iPad')){
devices.push({udid:udid,name:name,version:version,isDevice:isDevice,icon:'TV 📺'});
}
break;
default:
devices.push({udid:udid,name:name,version:version,isDevice:isDevice});
break;}

}
}
});

return devices;
};

var launchAppleSimulator=function launchAppleSimulator(c,platform,target){var devicesArr,selectedDevice,i,devices,_ref,sim;return _regenerator.default.async(function launchAppleSimulator$(_context){while(1){switch(_context.prev=_context.next){case 0:
(0,_logger.logTask)("launchAppleSimulator:"+platform+":"+target);

devicesArr=getAppleDevices(c,platform,true);

for(i=0;i<devicesArr.length;i++){
if(devicesArr[i].name===target){
selectedDevice=devicesArr[i];
}
}if(!
selectedDevice){_context.next=6;break;}
_launchSimulator(selectedDevice);return _context.abrupt("return",
selectedDevice.name);case 6:


(0,_logger.logWarning)("Your specified simulator target "+_chalk.default.white(target)+" doesn't exists");
devices=devicesArr.map(function(v){return{name:v.name+" | "+v.icon+" | v: "+_chalk.default.green(v.version)+" | udid: "+_chalk.default.grey(v.udid)+(v.isDevice?_chalk.default.red(' (device)'):''),value:v};});_context.next=10;return _regenerator.default.awrap(

_inquirer.default.prompt({
name:'sim',
message:'Select the simulator you want to launch',
type:'list',
choices:devices}));case 10:_ref=_context.sent;sim=_ref.sim;if(!


sim){_context.next=15;break;}
_launchSimulator(sim);return _context.abrupt("return",
sim.name);case 15:return _context.abrupt("return",

Promise.reject('Action canceled!'));case 16:case"end":return _context.stop();}}});};exports.launchAppleSimulator=launchAppleSimulator;


var _launchSimulator=function _launchSimulator(selectedDevice){
try{
_child_process.default.spawnSync('xcrun',['instruments','-w',selectedDevice.udid]);
}catch(e){


}
};

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

var devicesArr=getAppleDevices(c,platform);
var devicesString='';
devicesArr.forEach(function(v,i){
devicesString+=" ["+(i+1)+"]> "+_chalk.default.bold(v.name)+" | "+v.icon+" | v: "+_chalk.default.green(v.version)+" | udid: "+_chalk.default.grey(v.udid)+(
v.isDevice?_chalk.default.red(' (device)'):'')+"\n";

});

(0,_logger.logToSummary)(platform+" Targets:\n\n"+devicesString);

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