/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import e from"../../../../../core/Error.js";import"../../../../../core/has.js";import{NestedMap as t}from"../../../../../core/NestedMap.js";import{ShaderTechniqueConfiguration as i}from"./ShaderTechniqueConfiguration.js";class r{constructor(e){this._context=e,this._debug=null,this._precompiling=this._debug?0:1,this._cache=new t,this._frame=0,this._debug&&"stackTraceLimit"in Error&&(Error.stackTraceLimit=50)}destroy(){this._cache.forAll(([e])=>e.destroy()),this._cache.clear(),this._context=null}get context(){return this._context}get precompiling(){return this._precompiling}set precompiling(e){this._precompiling=e,0===e&&this.context.rctx.gl.flush()}precompile(e,t=c){++this.precompiling,this.get(e,t),--this.precompiling}get(t,i=c,r){const n=i.key.code;let s=this._cache.get(t,n)?.[0];if(null==s){if(s=new t(this.context,i,r),this._debug){if(0===this._precompiling){const r=`Uncached ${s.declaredClass} compile in ${(new Error).stack}\n  for configuration\n${i.decode()}`+this._debugCachedConfigurations(i,t);throw console.log(r),new e("shader:uncached-compile",r)}this._debug.set(s,new o(i,(new Error).stack))}this._cache.set(t,n,[s,this._frame])}return s}getCompiled(e,t=c,i){++this.precompiling;const r=this.get(e,t,i);return--this.precompiling,r.compiled?r:null}frameUpdate(){this._debug&&(this._cache.forAll(([t],i)=>{if(!t.unused||t.ignoreUnused)return;const r=this._debug?.get(t),o=`Unused ${t.declaredClass} for\n${r?.configuration.decode(t.key)}\n at frame ${this._frame} from\n${r?.stack}`+this._debugCachedConfigurations(r?.configuration,i);throw console.log(o,t.compiled),new e("shader:unused-precompile",o)}),++this._frame)}async reloadAll(){const e=new Array;this._cache.forEach(t=>e.push(n(t))),await Promise.all(e)}_debugCachedConfigurations(e,t){if(!e)return"";const i=this._cache.getInner(t);if(i?.size){let t="\n\n  cached configurations:\n";return t+=Array.from(i.values()).map(([t,i])=>`at frame ${i}: ${e.decode(t.key)}`).sort().join("\n\n"),t}return""}}class o{constructor(e,t){this.configuration=e,this.stack=t}}async function n(e){let t=!0;e.forEach(async([e])=>{await e.reload(t),t=!1})}const c=new i;export{c as NoConfiguration,r as ShaderTechniqueRepository};
