"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.ReactiveProject=void 0;const reactivity_1=require("@vue/reactivity"),lodash_1=require("lodash"),tools_1=require("../../utils/tools"),config_1=require("../../config/config"),baseProject_1=require("../baseProject");function isAttrEqual(t,e){return"object"==typeof t&&"object"==typeof e&&(!(!t||!e)&&(t.platform===e.platform&&t.appType===e.appType&&t.gameApp===e.gameApp&&t.isSandbox===e.isSandbox&&t.released===e.released&&(t.setting.MaxCodeSize===e.setting.MaxCodeSize&&t.setting.MaxSubpackageSubCodeSize===e.setting.MaxSubpackageSubCodeSize&&t.setting.MaxSubpackageFullCodeSize===e.setting.MaxSubpackageFullCodeSize&&t.setting.NavigateMiniprogramLimit===e.setting.NavigateMiniprogramLimit&&t.setting.MaxSubPackageLimit===e.setting.MaxSubPackageLimit&&t.setting.MinTabbarCount===e.setting.MinTabbarCount&&t.setting.MaxTabbarCount===e.setting.MaxTabbarCount&&t.setting.MaxCustomTabbarCount===e.setting.MaxCustomTabbarCount&&t.setting.MaxTabbarIconSize===e.setting.MaxTabbarIconSize)))}class ReactiveProject extends baseProject_1.BaseProject{constructor(t){var e;super(),this.fileBoxs=new Map,this.statBoxs=new Map,this.onFileChange=async(t,e)=>{if("change"===t){const t=this.fileBoxs.get(e);if(t){let i;try{i=this.project.getFile("",e)}catch(t){i=t}i instanceof Error||t.value instanceof Error?t.value=i:t.value&&0===i.compare(t.value)||(t.value=i)}}else if("unlink"===t){const t=this.fileBoxs.get(e);t&&(this.fileBoxs.delete(e),t.value=void 0);const i=this.statBoxs.get(e);if(i){const t=await this.project.stat("",e);(0,lodash_1.isEqual)(t,i.value)||(i.value=t)}}else if("unlinkDir"===t){const t=e+"/";let i=Array.from(this.fileBoxs.keys());for(const e of i)if(e.startsWith(t)){const t=this.fileBoxs.get(e);this.fileBoxs.delete(e),t.value=void 0}i=Array.from(this.statBoxs.keys());for(const e of i)if(e.startsWith(t)){const t=this.statBoxs.get(e);void 0!==t.value&&(this.statBoxs.delete(e),t.value=void 0)}}else if("add"===t||"addDir"===t){const t=this.statBoxs.get(e);if(t){const i=await this.project.stat("",e);(0,lodash_1.isEqual)(i,t.value)||(t.value=i)}}},this.project=t,null===(e=this.project.event)||void 0===e||e.on("fileChange",this.onFileChange),this._projectArchitecture=t.projectArchitecture,this.miniprogramRootBox=(0,reactivity_1.ref)(t.miniprogramRoot),this.pluginRootBox=(0,reactivity_1.ref)(t.pluginRoot),this.appidBox=(0,reactivity_1.ref)(t.appid),this.typeBox=(0,reactivity_1.ref)(t.type),this.attrBox=(0,reactivity_1.ref)(config_1.DefaultProjectAttr),this.init()}get appid(){return this.appidBox.value}get type(){return this.typeBox.value}get nameMappingFromDevtools(){return this.project.nameMappingFromDevtools}get projectPath(){return this.project.projectPath}get privateKey(){return this.project.privateKey}get miniprogramRoot(){return this.miniprogramRootBox.value}set miniprogramRoot(t){this.miniprogramRootBox.value=t,this.project.miniprogramRoot=t}get pluginRoot(){return this.pluginRootBox.value}set pluginRoot(t){this.pluginRootBox.value=t,this.project.pluginRoot=t}async init(){}updateFileAndDirs(){throw new Error("Method updateFileAndDirs not implemented.")}async updateProject(){this.appidBox.value!==this.project.appid&&(this.appidBox.value=this.project.appid),this.typeBox.value!==this.project.type&&(this.typeBox.value=this.project.type),this.miniprogramRootBox.value!==this.project.miniprogramRoot&&(this.miniprogramRootBox.value=this.project.miniprogramRoot),this.pluginRootBox.value!==this.project.pluginRoot&&(this.pluginRootBox.value=this.project.pluginRoot);const t=await this.project.attr();return isAttrEqual(t,this.attrBox.value)||(this.attrBox.value=t),new Promise(t=>{setTimeout(t,0)})}release(){(0,tools_1.devtoolsLog)("[reactiveCache] reactiveProject release")}async attr(){return this.attrSync()}getSrcFile(t,e){return this.project.getSrcFile(t,e)}getFileList(t="",e=""){return this.project.getFileList(t,e)}getFilesAndDirs(){return this.project.getFilesAndDirs()}getExtAppid(){return this.project.getExtAppid()}getFile(t,e){const i=this.getTargetPath(t,e),o=this.fileBoxs.get(i);if(o){if(o.value instanceof Error)throw o.value;return o.value}const r=this.project.getFile(t,e),a=(0,reactivity_1.ref)(r);return this.fileBoxs.set(i,a),a.value}stat(t,e){const i=this.getTargetPath(t,e),o=this.statBoxs.get(i);if(o)return o.value;const r=this.project.stat(t,e),a=(0,reactivity_1.ref)(r);return this.statBoxs.set(i,a),a.value}attrSync(){return this.attrBox.value}}exports.ReactiveProject=ReactiveProject;