"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.compileUseWCC=compileUseWCC,exports.compileWxmlToGlassEasel=compileWxmlToGlassEasel;const tslib_1=require("tslib"),getservice_1=tslib_1.__importDefault(require("../../common/getservice")),nativeModule_1=require("../../nativeModule/common/nativeModule"),genTempFileService_1=require("../../genTempFile/common/genTempFileService"),common_1=require("../../../utils/node/common"),tools_1=require("../../../utils/tools"),fs=require("fs"),path=require("path"),{spawn:spawn}=require("child_process");async function compileUseWCC(e){const{isCut:t,configContent:a,configSplit:n,files:s,genfuncName:i,cwd:o,hasWxs:l,lazyloadConfig:r,isPlugin:c}=e,u=Date.now();let m=["-d","--split",n,t?"-xc":"-cc",a];l&&m.unshift("-ds"),r&&(m=m.concat([t?"-lla":"-llw",r])),m=c?m.concat(s).concat(["-p","-gn",i]):m.concat(s).concat(["-gn",i]);const p=await(0,getservice_1.default)(genTempFileService_1.IGenTempFile).genPath(),g=(0,common_1.shouldUseConfigFile)(m);g&&fs.writeFileSync(p,m.join("\n"));const d=(0,getservice_1.default)(nativeModule_1.INativeModuleService),f=await d.getWXMLParsePath(),_=spawn(f,g?["--config-path",(0,tools_1.normalizePath)(p)]:m,{cwd:o}),w=[],h=[];return _.stdout.on("data",e=>{w.push(e)}),_.stderr.on("data",e=>{h.push(e)}),new Promise((e,t)=>{_.on("close",a=>{if(g&&fs.unlinkSync(p),console.warn("[wcc bindary] compile cost:",Date.now()-u),0===a){const a=Buffer.concat(w).toString();if(!r)return e({code:a,name:i});try{const{generateFunctionContent:t,generateFunctionName:n}=JSON.parse(a);return e({pages:t,names:n})}catch(e){return t(new Error("lazyload parse wxml fail: "+e))}}return t(new Error(Buffer.concat(h).toString()))})})}async function compileWxmlToGlassEasel(e){const t=(0,getservice_1.default)(nativeModule_1.INativeModuleService),a=await t.getWXMLParsePath(),n=(0,getservice_1.default)(genTempFileService_1.IGenTempFile).genPath(),s=path.join(await t.getGlassEaselLazyModulePath(),"glass_easel_template_compiler.js"),i=require("./compileglasseasel.js");return await i({taskName:"compileglasseasel",dataStr:JSON.stringify({wxmlParserPath:a,tempCompileConfigFilePath:n,wxmlAndWxsFiles:e.wxmlAndWxsFiles,glassEaselTemplateCompilerPath:s}),useBackup:!1})}