"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.PackOptionsHelper=void 0;const tslib_1=require("tslib"),path=tslib_1.__importStar(require("path")),filerules_1=tslib_1.__importDefault(require("../utils/filerules")),tools_1=require("./tools"),config_1=require("../config/config");class PackOptionsHelper{constructor(){this.packOptionIgnores=[],this.packOptionIncludes=[],this.ignoreUploadUnusedFiles=!1,this.ignoreDevUnusedFiles=!1,this.unusedFiles=new Set,this.projectRoot="",this.extOpts={include:[],ignore:[]}}setExtOpts(e={}){this.extOpts.include=e.include||[],this.extOpts.ignore=e.ignore||[]}updateState(e){var s,i;this.packOptionIgnores=(null===(s=e.packOptions)||void 0===s?void 0:s.ignore)||[],this.packOptionIncludes=(null===(i=e.packOptions)||void 0===i?void 0:i.include)||[],this.projectRoot=e.miniprogramRoot||"",this.ignoreUploadUnusedFiles=!1,this.ignoreDevUnusedFiles=!1,e.type===config_1.COMPILE_TYPE.miniProgram&&(this.ignoreUploadUnusedFiles=Boolean(e.setting.ignoreUploadUnusedFiles),this.ignoreDevUnusedFiles=Boolean(e.setting.ignoreDevUnusedFiles)),(0,tools_1.isPluginType)(e)&&(this.projectRoot=e.pluginRoot||"")}isUnuserFilesEnable(e=!1){return e?!1!==this.ignoreUploadUnusedFiles:!0===this.ignoreDevUnusedFiles||!1!==this.ignoreDevUnusedFiles}filter(e,s=!1){return e.filter(e=>this.isNotIgnored(e,s))}isIgnored(e,s=!1){return this.isIgnoredByRules(e)||this.isIgnoreByUnusedFiles(e,s)}isNotIgnored(e,s=!1){return!this.isIgnored(e,s)}isIgnoredByRules(e,s={}){return!this.isFileIncluded(e,s.include)&&this.isFileIgnored(e,s.ignore)}relativePath(e){return path.posix.relative(this.projectRoot,e)}isFileIgnored(e,s){return s||(s=[...this.packOptionIgnores,...this.extOpts.ignore]),filerules_1.default.isFileIgnored(e,s)}isFileIncluded(e,s){return s||(s=[...this.packOptionIncludes,...this.extOpts.include]),filerules_1.default.isFileIncluded(e,s)}isIgnoreByUnusedFiles(e,s=!1){return!!this.isUnuserFilesEnable(s)&&(!this.isFileIncluded(e)&&this.unusedFiles.has((0,tools_1.sourcePathToTargetPath)(e)))}getUnusedFiles(){return this.unusedFiles}isScssOnlyImportedByFile(e){const s=path.extname(e.path);if("Wxss"===e.type&&[".scss",".sass",".less"].includes(s)&&e.parentDeps){let s=!0;return e.parentDeps.forEach(e=>{"file"!==e.from&&(s=!1)}),s}return!1}clearUnusedFiles(){this.unusedFiles.clear()}async initUnusedFiles(e,s,i=!1){if(!(this.unusedFiles.size>0)&&(this.updateState(e),this.isUnuserFilesEnable(i)&&s))try{await s.analyse();const i=s.fileHelper.getFileList("").filter(e=>(0,tools_1.isCodeFile)(path.posix.extname(e))),t=new Set;s.graph.modules.forEach(e=>{const s=i.find(s=>s===e.path);if(s){if(this.isScssOnlyImportedByFile(e))return;t.add((0,tools_1.sourcePathToTargetPath)(s))}});const n=new Set;i.forEach(s=>{const i=(0,tools_1.sourcePathToTargetPath)(s);t.has(i)||n.add(path.posix.join(e.miniprogramRoot,i))}),this.unusedFiles=n}catch(e){console.error("initUnusedFiles error",e)}}}exports.PackOptionsHelper=PackOptionsHelper,exports.default=new PackOptionsHelper;