"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.runWcscWasm=runWcscWasm,exports.runWcsc=runWcsc;const tslib_1=require("tslib"),common_1=require("../../../../../utils/web/common"),fs_1=tslib_1.__importDefault(require("fs")),path_1=tslib_1.__importDefault(require("path")),wasmSpliter="_split_<><><>_split_";let ModuleWcsc=null,wxssCompileFunc=null;async function runWcscWasm(e){if(!ModuleWcsc){const e=await Promise.resolve().then(()=>tslib_1.__importStar(require("./wcsc.wasm"))),t=await Promise.resolve().then(()=>tslib_1.__importStar(require("./wcsc")));ModuleWcsc=await t.default({wasmBinary:e.default})}wxssCompileFunc||(wxssCompileFunc=ModuleWcsc.wxss_compile);const{files:t,pageCount:s,lazyload:r,subPackage:o}=e,c=e.contents||[],l=[wasmSpliter,t.join(wasmSpliter),c.join(wasmSpliter),t.length,"",s,0,r?1:0,o?1:0,o||""],n=wxssCompileFunc(...l);return(0,common_1.splitQuoteFromWASM)(n)}function tranWcscResultToObject(e){const t=e.split("="),s={};for(let e=0,r=t.length;e<r&&t[e+1];e+=2)s[t[e]]=t[e+1];return s}async function runWcsc(e){var t;if(!e)throw Error("options is required");if("string"!=typeof e.subPackage&&delete e.subPackage,"boolean"!=typeof e.lazyload&&delete e.lazyload,!(null===(t=(e=Object.assign({files:[],contents:[],pageCount:0,cwd:"",replaceContent:{},debug:!1,classPrefix:"",lazyload:!1},e)).contents)||void 0===t?void 0:t.length)){const t=e.files.map(t=>"string"==typeof e.replaceContent[t]?e.replaceContent[t]:fs_1.default.readFileSync(path_1.default.resolve(e.cwd,t),"utf8"));e.contents=await Promise.all(t)||[]}let s;try{s=await runWcscWasm(e)}catch(e){throw new Error(e)}if("true"!==s.success){const e="[wcc-node] compile error: "+s.errmsg;throw new Error(e)}s=s.content;return e.lazyload?s:tranWcscResultToObject(s)}