"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.filterUnusedFile=filterUnusedFile;const tslib_1=require("tslib"),projectconfig_1=require("../../modules/corecompiler/original/json/projectconfig"),path=tslib_1.__importStar(require("path")),code_analyse_1=require("../../common/code-analyse"),code_analyse_2=require("../code-analyse"),babel_helper_1=require("../../utils/babel_helper"),tools_1=require("../../utils/tools");class CodeDataFileHelper{constructor(e,t){this.project=e,this.codes=t,this._dirSet=new Set,this._fileSet=new Set,this._cacheFileSize=new Map;for(const e of Object.keys(this.codes))this._fileSet.add(e.startsWith("/")?e.replace(/^\//,""):e),this.cacheDirName(path.posix.dirname(e))}cacheDirName(e){this._dirSet.has(e)||(this._dirSet.add(e),this.cacheDirName(path.posix.dirname(e)))}cacheFileSize(e){if(this._cacheFileSize.has(e))return this._cacheFileSize.get(e);const t=this.codes[e];let i=0;return i="string"==typeof t?Buffer.from(t,"utf-8").length:t.length,this._cacheFileSize.set(e,i),i}stat(e){const t=this._fileSet.has(e),i=this._dirSet.has(e);if(!t&&!i)return;let r=0;return r=i?Object.keys(this.codes).filter(t=>t.startsWith(e)).reduce((e,t)=>e+this.cacheFileSize(t),0):this.cacheFileSize(e),{isFile:t,isDirectory:i,size:r,mtime:0}}mtime(e){return 0}exist(e){return this.existFile(e)||this.existDir(e)}existDir(e){return e.endsWith("/")&&(e=e.replace(/\/$/,"")),this._dirSet.has(e)}existFile(e){return this._fileSet.has(e)}getFileList(e="",t=""){return Array.from(this._fileSet).filter(i=>(!t||path.extname(i)===t)&&!(e&&!i.startsWith(e)))}async getString(e){const t=this.codes[e];if("string"==typeof t)return t;if(Buffer.isBuffer(t))return t.toString("utf-8");throw new Error(e+" is not in codes")}async getLocalFileString(e){return this.project.getFile(this.project.miniprogramRoot,e).toString("utf-8")}async getJSON(e){const t=await this.getString(e);try{return JSON.parse(t)}catch(e){return null}}async readdir(e){e.endsWith("/")||(e+="/");const t=Array.from(this._fileSet).filter(t=>t.startsWith(e));return Array.from(this._dirSet).filter(t=>t.startsWith(e)).concat(t).filter(t=>t.slice(e.length).indexOf("/")<0).map(t=>t.slice(e.length))}watchFileChange(e){e()}destroy(){}}async function filterUnusedFile(e,t,i){var r,s;if("miniProgram"!==t.type)return i;const n=await(0,projectconfig_1.getProjectConfigJSON)(t);let o=!1;return o=e?!1!==(null===(r=n.setting)||void 0===r?void 0:r.ignoreDevUnusedFiles):!1!==(null===(s=n.setting)||void 0===s?void 0:s.ignoreUploadUnusedFiles),o?doFilterUnusedFile(t,i):i}function checkUnusedCodeFiles(e,t){var i;const r=t.fileHelper.getFileList("").filter(e=>{return t=path.posix.extname(e),/\.(json|wxml|wxss|js|wxs|ts|less|sass|scss)/.test(t);var t}),s=new Set;t.graph.modules.forEach(e=>{const t=r.find(t=>t===e.path);t&&s.add(t)});const n=(0,babel_helper_1.getBabelOutputPath)((null===(i=(0,projectconfig_1.getProjectConfigJSON)(e))||void 0===i?void 0:i.setting)||{});return r.forEach(e=>{(function(e){return e.indexOf(n)>=0})(e)&&s.add(e)}),r.filter(e=>!s.has(e))}async function doFilterUnusedFile(e,t){var i;const r=(0,projectconfig_1.getProjectConfigJSON)(e),s=(0,code_analyse_2.transCompileType)(e);if("gamePlugin"===s)throw new Error("gamePlugin is not support yet!");let n="plugin"===s?r.pluginRoot:r.miniprogramRoot;n=n?path.posix.join(e.projectPath,n):e.projectPath;const o=new CodeDataFileHelper(e,t),a=new code_analyse_1.Analyzer({root:n,type:s,fileHelper:o,plugins:[]});await a.analyse();const l=checkUnusedCodeFiles(e,a),c=(null===(i=r.packOptions)||void 0===i?void 0:i.include)||[];return((e,t)=>{const i=Object.keys(e);for(const r of i)t(r)&&delete e[r];return e})(t,e=>(e=>{if((0,tools_1.isFileIncluded)(e,c))return!1;const t=e.startsWith("/")?e:"/"+e;for(const e of l){if(t===(e.startsWith("/")?e:"/"+e))return!0}return!1})(e))}