var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.listAppleDevices=exports.launchAppleSimulator=exports.getAppleDevices=void 0;var _path=_interopRequireDefault(require("path"));
var _fs=_interopRequireDefault(require("fs"));
var _chalk=_interopRequireDefault(require("chalk"));
var _readline=_interopRequireDefault(require("readline"));
var _child_process=_interopRequireDefault(require("child_process"));
var _common=require("../../common");







var _prompt=require("../../systemTools/prompt");
var _constants=require("../../constants");

var getAppleDevices=function getAppleDevices(c,platform,ignoreDevices,ignoreSimulators){
(0,_common.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){return new Promise(function(resolve){
(0,_common.logTask)("launchAppleSimulator:"+platform+":"+target);

var devicesArr=getAppleDevices(c,platform,true);
var selectedDevice;
for(var i=0;i<devicesArr.length;i++){
if(devicesArr[i].name===target){
selectedDevice=devicesArr[i];
}
}
if(selectedDevice){
_launchSimulator(selectedDevice);
resolve(selectedDevice.name);
}else{
(0,_common.logWarning)("Your specified simulator target "+_chalk.default.white(target)+" doesn't exists");
var readlineInterface=_readline.default.createInterface({
input:process.stdin,
output:process.stdout});

var devicesString='\n';
devicesArr.forEach(function(v,i){
devicesString+="-["+(i+1)+"] "+_chalk.default.white(v.name)+" | "+v.icon+" | v: "+_chalk.default.green(v.version)+" | udid: "+_chalk.default.blue(
v.udid)+(
v.isDevice?_chalk.default.red(' (device)'):'')+"\n";
});
readlineInterface.question((0,_prompt.getQuestion)(devicesString+"\nType number of the simulator you want to launch"),function(v){
var chosenDevice=devicesArr[parseInt(v,10)-1];
if(chosenDevice){
_launchSimulator(chosenDevice);
resolve(chosenDevice.name);
}else{
(0,_common.logError)("Wrong choice "+v+"! Ingoring");
}
});
}
});};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,_common.logTask)("listAppleDevices:"+platform);

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

});
console.log(devicesString);
});};exports.listAppleDevices=listAppleDevices;