"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.random=random,exports.default=default_1;const tslib_1=require("tslib"),path_1=tslib_1.__importDefault(require("path")),tools_1=require("../../../../utils/tools"),customError_1=require("../../../../utils/customError"),debug_1=require("../../../../utils/debug");let runInMainProcess=void 0;const sass=async()=>{if(void 0===runInMainProcess&&(runInMainProcess=await(0,debug_1.shouldRunInMainProcess)()),runInMainProcess){return require("sass")}return require("sass")};function random(){return(0,tools_1.generateMD5)(`${Math.random()}${Date.now()}`)}const importWxssReg=/(?:^|\s)?(?:@import)(?:\s*)?(["'])([^"']+.wxss)\1(?:\s*);/g,importCssReg=/(?:^|\s)?(?:@import)(?:\s*)?(["'])([^"']+.css)\1(?:\s*);/g;async function sassRender(s,t){if("undefined"!=typeof WEBIDE_WEBPECK_DEFINE_PLUGIN);else{(await sass()).render(s,t)}}function default_1(s){return{name:"summer-sass",resolveExt:{wxss:["sass","scss"]},async load(t,e){var o,r,a,i;if(e.endsWith(".scss")||e.endsWith(".sass")){const n=(0,tools_1.pathRelative)(s.projectPath,e);let l=s.getFile("",n).toString();const u=s.getTargetPath(s.miniprogramRoot,"global");if(n!==u+".scss"&&n!==u+".sass"){const t=path_1.default.extname(n),e=(null===(r=null===(o=s.setting)||void 0===o?void 0:o.sassSetting)||void 0===r?void 0:r.sassCommonUseFilePath)||"global.sass",u=(null===(i=null===(a=s.setting)||void 0===a?void 0:a.sassSetting)||void 0===i?void 0:i.scssCommonUseFilePath)||"global.scss";let c=".sass"===t?e:u;const d=(0,tools_1.pathRelative)(s.miniprogramRoot,path_1.default.dirname(n))||".";s.exists(s.miniprogramRoot,c)||(c=".sass"===t?u:e),s.exists(s.miniprogramRoot,c)&&(l=".sass"===t?`@use '${"."===d?".":(0,tools_1.pathRelative)(d,"./")}/${c}'\n${l}`:`@use '${"."===d?".":(0,tools_1.pathRelative)(d,"./")}/${c}';\n${l}`)}const c=[];l=l.replace(importWxssReg,(s,t,e)=>(c.push(e),s.replace(e,e.slice(0,-4)+"css")));const d=path_1.default.posix.dirname((0,tools_1.pathRelative)(s.projectPath,e));return new Promise((s,o)=>{const r=Date.now();sassRender({file:e,data:l,sourceMap:!0,sourceMapContents:!0,omitSourceMapUrl:!0,outFile:e.slice(0,-5)+".wxss",includePaths:this.rootPath?[this.rootPath]:[]},(a,i)=>{if(a){const s=(0,customError_1.makeCustomError)(a,customError_1.CustomErrors.SUMMER_PLUGIN_CODE_ERR);return void o(s)}if(!i)return void o(new Error("no result"));const n=i.css.toString("utf-8").replace(importCssReg,(s,t,e)=>{const o=e.slice(0,-3)+"wxss";return c.includes(o)?s.replace(e,o):s});if(i.stats.includedFiles.length>0)for(const s of i.stats.includedFiles)s!==e&&this.addWatchFile(s);const l=i.map?JSON.parse(i.map.toString("utf-8")):void 0;l&&(l.sourceRoot=d),s({targetPath:t,source:{sourceCode:n,inputMap:l},process:[{cost:Date.now()-r,pluginName:"summer-sass",action:"load"}]})})})}}}}