"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.generateMiniappIOSResources=generateMiniappIOSResources;const tslib_1=require("tslib"),zip_1=require("../../utils/zip"),request_1=require("../../utils/request"),tools_1=require("../../utils/tools"),log=tslib_1.__importStar(require("../../utils/log")),tools_2=require("../utils/tools"),fse=require("fs-extra"),path=require("path"),CDN_PREFIX="https://dldir1.qq.com/WechatWebDev/";class SDKVersionManager{constructor(e){this.project=e,this.iosBaseDir=(0,tools_2.getMiniappIOSCacheDir)(),this.downloadBigFile=async(e,i)=>{const o=fse.createWriteStream(i,{mode:511});return new Promise((i,s)=>{(0,request_1.simpleRequest)({url:""+e,method:"get"},e=>{e&&s(e)}).pipe(o),o.on("error",e=>{s(e)}),o.on("close",()=>{i({})})})},this.downloadMiniappIOSResources=async(e,i=!1)=>{log.info("Download ios resources...");const o=path.join(this.iosBaseDir,"v2-ipa",e);fse.existsSync(o)||(fse.emptyDirSync(path.dirname(o)),fse.ensureDirSync(o));const s=["x86.zip","arm64.zip",`WeAppSDK-${e}.zip`];i&&(s.push("x86_ipad.zip"),s.push("arm64_ipad.zip"));try{const i=[];s.forEach(s=>{const t=`${CDN_PREFIX}donut/ios/${e}/${s}`,r=path.join(o,s);fse.existsSync(r)||i.push(this.downloadBigFile(t,r))}),await Promise.all(i),log.info("Download ios resources successfully")}catch(e){throw fse.removeSync(o),new Error(`download ios resources resources failed: ${e.message}. Network issue or SDK Version may not suitable for IPad beta`)}return o},this.generateMiniappIOSResources=async(e,i,o=!1,s=!1)=>{try{log.info((o?"is":"not")+" using iPad Beta");const s=await this.downloadMiniappIOSResources(e,o);log.info("Archive ios resources..."),await this.downloadSwiftSupport(e);let t="";t=o?path.join(s,"arm64"===i?"arm64_ipad.zip":"x86_ipad.zip"):path.join(s,"arm64"===i?"arm64.zip":"x86.zip");const r=path.join(s,`WeAppSDK-${e}.zip`),a=path.join(this.iosBaseDir,"v2-ipa",e,`ipa-${i}${o?"-iPad":""}-${(0,tools_1.generateMD5)(this.project.projectPath)}/Payload`);fse.existsSync(a)||await(0,zip_1.unzip)(t,a);const n=path.join(this.iosBaseDir,"v2-ipa",e,"sdk-"+(0,tools_1.generateMD5)(this.project.projectPath));return fse.existsSync(n)||await(0,zip_1.unzip)(r,n),log.info("Archive ios resources successfully"),{demoIpaPath:path.join(a,"demo.app"),sdkPath:n}}catch(t){const r=path.join(this.iosBaseDir,"v2-ipa",e);if(fse.existsSync(r)&&fse.removeSync(r),s){log.info(`Start retry, Process resources failed: ${t.message}.`);return await this.generateMiniappIOSResources(e,i,o,!1)}throw log.error("Archive ios resources failed, please retry and check whether your network is running smoothly..."),new Error("Download and unzip the ios resources failed: "+t.message)}},this.project=e}async downloadSwiftSupport(e){if((0,tools_1.compareVersion)(e,"1.6.0")>=0){log.info("Downloading SwiftSupport...");const e="1",i="libswift_Concurrency.dylib",o=path.join(this.iosBaseDir,"v2-ipa","SwiftSupport"),s=path.join(o,i),t=`${CDN_PREFIX}donut/ios/SwiftSupport/${e}/${i}`;if(fse.ensureDirSync(o),log.info(`SwiftSupport remote: ${t}, local: ${s}`),await this.downloadBigFile(t,s),!fse.existsSync(s))throw log.info("cant find localFile: "+s),new Error("cant find localFile: "+s);log.info("download SwiftSupport done: "+s)}}}async function generateMiniappIOSResources(e,i,o,s=!1){return await new SDKVersionManager(e).generateMiniappIOSResources(i,o,s,!0)}