"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.ReactiveJSONCompiler=exports.ReactiveProject=void 0,exports.tryToGetReactiveProject=tryToGetReactiveProject,exports.tryToGetReactiveJSONCompiler=tryToGetReactiveJSONCompiler,exports.wrapCompileJSONFunc=wrapCompileJSONFunc,exports.cleanReactiveCache=cleanReactiveCache;const process_1=require("process"),reactivity_1=require("@vue/reactivity"),lodash_1=require("lodash"),tools_1=require("../../../../utils/tools"),reactiveProject_1=require("../../../../project/advance/reactiveProject");function makeReadonly(e){return e&&"object"==typeof e?(0,reactivity_1.readonly)(e):e}Object.defineProperty(exports,"ReactiveProject",{enumerable:!0,get:function(){return reactiveProject_1.ReactiveProject}});const reactiveProjectMap=new Map;function tryToGetReactiveProject(e){let t=reactiveProjectMap.get(e.projectPath);return t||(t=new reactiveProject_1.ReactiveProject(e),reactiveProjectMap.set(e.projectPath,t),t)}let isPending=!1;const pendingRunner=new Set,resolvedPromise=Promise.resolve();function runInNextTick(e){pendingRunner.add(e),isPending||(isPending=!0,resolvedPromise.then(()=>{const e=Date.now();try{const t=Array.from(pendingRunner);pendingRunner.clear(),isPending=!1,t.forEach(e=>{e()})}finally{(0,tools_1.devtoolsInfo)(`[reactiveCache] nextTick update cost ${Date.now()-e} ms`)}}))}class ReactiveJSONCompiler{constructor(e){this.pageComputeds=new Map,this.jsonComputeds=new Map,this.project=e}release(){(0,tools_1.devtoolsLog)("[reactiveCache] reactiveJSONCompiler release")}registerOrGet(e,t,...r){let o=this.jsonComputeds.get(e);if(!o){o=(0,reactivity_1.ref)(void 0),this.jsonComputeds.set(e,o);let i=void 0;i=(0,reactivity_1.effect)(()=>{try{const e=t.call(null,this.project,...r);(0,lodash_1.isEqual)(e,o.value)||(o.value=makeReadonly(e))}catch(t){o.value=t instanceof Error?t:new Error(t.toString()),(0,tools_1.devtoolsLog)(`[reactiveCache] update ${e} failed: `,t)}},{scheduler(){i&&i.active&&runInNextTick(i)}})}const{value:i}=o;if(i instanceof Error)throw i;return i}static setOriginGetPageJSON(e){this.originGetPageJSON=e}static setOriginCheckPageJSON(e){this.originCheckPageJSON=e}getPageJSON(e,t){let r=this.pageComputeds.get(t.pagePath);r||(r={checked:(0,reactivity_1.ref)(void 0),compiled:(0,reactivity_1.ref)(void 0)},this.pageComputeds.set(t.pagePath,r));const o=r[e];if(void 0===o.value){let r=void 0;r=(0,reactivity_1.effect)(()=>{try{(0,tools_1.devtoolsInfo)(`[reactiveCache] start to update ${e} ${t.pagePath}`);const r="compiled"===e?ReactiveJSONCompiler.originGetPageJSON(this.project,t):ReactiveJSONCompiler.originCheckPageJSON(this.project,t);o.value=makeReadonly(r),(0,tools_1.devtoolsInfo)(`[reactiveCache] update finish ${e} ${t.pagePath}`)}catch(r){o.value=r instanceof Error?r:new Error(r.toString()),(0,tools_1.devtoolsLog)(`[reactiveCache] update ${e} ${t.pagePath} failed: `,r)}},{scheduler(){r&&r.active&&(0,process_1.nextTick)(r)}})}const{value:i}=o;if(i instanceof Error)throw i;return i}}exports.ReactiveJSONCompiler=ReactiveJSONCompiler;const reactiveJSONCompilerMap=new Map;function tryToGetReactiveJSONCompiler(e){let t=reactiveJSONCompilerMap.get(e.projectPath);return t||(t=new ReactiveJSONCompiler(e),reactiveJSONCompilerMap.set(e.projectPath,t),t)}function wrapCompileJSONFunc(e,t){var r;return(null===(r=null===process||void 0===process?void 0:process.env)||void 0===r?void 0:r.BUILD_SERVER)?t:function(r,...o){r instanceof reactiveProject_1.ReactiveProject||(r=tryToGetReactiveProject(r));return tryToGetReactiveJSONCompiler(r).registerOrGet(e,t,...o)}}function cleanReactiveCache(){reactiveProjectMap.forEach(e=>{e.release()}),reactiveProjectMap.clear(),reactiveJSONCompilerMap.forEach(e=>{e.release()}),reactiveJSONCompilerMap.clear()}