"use strict";Object.defineProperty(exports,"__esModule",{value:!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 i,s;this.packOptionIgnores=(null===(i=e.packOptions)||void 0===i?void 0:i.ignore)||[],this.packOptionIncludes=(null===(s=e.packOptions)||void 0===s?void 0:s.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,i=!1){return e.filter(e=>this.isNotIgnored(e,i))}isIgnored(e,i=!1){return this.isIgnoredByRules(e)||this.isIgnoreByUnusedFiles(e,i)}isNotIgnored(e,i=!1){return!this.isIgnored(e,i)}isIgnoredByRules(e,i={}){return!this.isFileIncluded(e,i.include)&&this.isFileIgnored(e,i.ignore)}relativePath(e){return path.posix.relative(this.projectRoot,e)}isFileIgnored(e,i){return i||(i=[...this.packOptionIgnores,...this.extOpts.ignore]),filerules_1.default.isFileIgnored(e,i)}isFileIncluded(e,i){return i||(i=[...this.packOptionIncludes,...this.extOpts.include]),filerules_1.default.isFileIncluded(e,i)}isIgnoreByUnusedFiles(e,i=!1){return!!this.isUnuserFilesEnable(i)&&(!this.isFileIncluded(e)&&this.unusedFiles.has((0,tools_1.sourcePathToTargetPath)(e)))}getUnusedFiles(){return this.unusedFiles}async initUnusedFiles(e,i,s=!1){if(this.updateState(e),this.isUnuserFilesEnable(s)&&i)try{await i.analyse();const s=i.fileHelper.getFileList("").filter(e=>(0,tools_1.isCodeFile)(path.posix.extname(e))),t=new Set;i.graph.modules.forEach(e=>{const i=s.find(i=>i===e.path);i&&t.add((0,tools_1.sourcePathToTargetPath)(i))});const o=new Set;s.forEach(i=>{const s=(0,tools_1.sourcePathToTargetPath)(i);t.has(s)||o.add(path.posix.join(e.miniprogramRoot,s))}),this.unusedFiles=o,console.error(this.unusedFiles)}catch(e){console.error("initUnusedFiles error",e)}}}exports.default=new PackOptionsHelper;