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


















var _projectParser=require("../../projectTools/projectParser");
var _constants=require("../../constants");
var _pluginTools=require("../../pluginTools");
var _apple=require("../apple");
var _fileutils=require("../../systemTools/fileutils");

var xcode=require('xcode');


var parseXcodeProject=function parseXcodeProject(c,platform){return new Promise(function(resolve,reject){

var appFolder=(0,_common.getAppFolder)(c,platform);
var appFolderName=(0,_apple.getAppFolderName)(c,platform);
var projectPath=_path.default.join(appFolder,appFolderName+".xcodeproj/project.pbxproj");
var xcodeProj=xcode.project(projectPath);

xcodeProj.parse(function(){
var appId=(0,_common.getAppId)(c,platform);

var tId=(0,_common.getConfigProp)(c,platform,'teamID');
if(tId){
xcodeProj.updateBuildProperty('DEVELOPMENT_TEAM',tId);
}else{
xcodeProj.updateBuildProperty('DEVELOPMENT_TEAM','""');
}
var provisioningStyle=(0,_common.getConfigProp)(c,platform,'provisioningStyle','Automatic');
xcodeProj.addTargetAttribute('ProvisioningStyle',provisioningStyle);
xcodeProj.addBuildProperty('CODE_SIGN_STYLE',provisioningStyle);
xcodeProj.updateBuildProperty('PRODUCT_BUNDLE_IDENTIFIER',appId);

var deploymentTarget=(0,_common.getConfigProp)(c,platform,'deploymentTarget','10.0');
if(platform===_constants.IOS){
xcodeProj.updateBuildProperty('IPHONEOS_DEPLOYMENT_TARGET',deploymentTarget);
}else if(platform===_constants.TVOS){
xcodeProj.updateBuildProperty('TVOS_DEPLOYMENT_TARGET',deploymentTarget);
}

var provisionProfileSpecifier=(0,_common.getConfigProp)(c,platform,'provisionProfileSpecifier');
if(provisionProfileSpecifier){
xcodeProj.updateBuildProperty('PROVISIONING_PROFILE_SPECIFIER',"\""+provisionProfileSpecifier+"\"");
}

var codeSignIdentity=(0,_common.getConfigProp)(c,platform,'codeSignIdentity');
if(codeSignIdentity){
var runScheme=(0,_common.getConfigProp)(c,platform,'runScheme');
var bc=xcodeProj.pbxXCBuildConfigurationSection();



var cs1='CODE_SIGN_IDENTITY';
var cs2='"CODE_SIGN_IDENTITY[sdk=iphoneos*]"';
for(var configName in bc){
var config=bc[configName];
if(runScheme&&config.name===runScheme||!runScheme){
if(config.buildSettings[cs1])config.buildSettings[cs1]="\""+codeSignIdentity+"\"";
if(config.buildSettings[cs2])config.buildSettings[cs2]="\""+codeSignIdentity+"\"";
}
}
}


var systemCapabilities=(0,_common.getConfigProp)(c,platform,'systemCapabilities');
if(systemCapabilities){
var sysCapObj={};
for(var sk in systemCapabilities){
var val=systemCapabilities[sk];
sysCapObj[sk]={enabled:val===true?1:0};
}

xcodeProj.addTargetAttribute('SystemCapabilities',sysCapObj);
}


c.pluginConfigiOS.embeddedFontSources.forEach(function(v){
xcodeProj.addResourceFile(v);
});



(0,_pluginTools.parsePlugins)(c,platform,function(plugin,pluginPlat,key){
if(pluginPlat.xcodeproj){
if(pluginPlat.xcodeproj.resourceFiles){
pluginPlat.xcodeproj.resourceFiles.forEach(function(v){
xcodeProj.addResourceFile(_path.default.join(appFolder,v));
});
}
if(pluginPlat.xcodeproj.sourceFiles){
pluginPlat.xcodeproj.sourceFiles.forEach(function(v){

xcodeProj.addSourceFile(v,null,'200132F21F6BF9CF00450340');
});
}
if(pluginPlat.xcodeproj.headerFiles){
pluginPlat.xcodeproj.headerFiles.forEach(function(v){
xcodeProj.addHeaderFile(v,null,'200132F21F6BF9CF00450340');
});
}
if(pluginPlat.xcodeproj.buildPhases){
pluginPlat.xcodeproj.buildPhases.forEach(function(v){
xcodeProj.addBuildPhase([],'PBXShellScriptBuildPhase','ShellScript',null,{
shellPath:v.shellPath||'/bin/sh',
shellScript:v.shellScript,
inputPaths:v.inputPaths||['"$(SRCROOT)/$(BUILT_PRODUCTS_DIR)/$(INFOPLIST_PATH)"']});

});
}
}
});

_fs.default.writeFileSync(projectPath,xcodeProj.writeSync());
resolve();
});
});};exports.parseXcodeProject=parseXcodeProject;