/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{removeUnordered as e}from"../../../../core/arrayUtils.js";import"../../../../core/has.js";import{sort as t}from"../../../../core/heapSort.js";import{isPromiseLike as r,createAbortError as s}from"../../../../core/promiseUtils.js";import{signal as n}from"../../../../core/signal.js";import{getEmissions as o}from"./emissive/emissions.js";import{DepthRange as i}from"../lib/DepthRange.js";class h{constructor(e){this.context=e,this._renderPlugins=new Array,this._slots=new Array,this._version=n(0);for(let t=0;t<20;++t)this._slots[t]=[]}destroy(){this._renderPlugins.forEach(e=>e.destroy()),this._renderPlugins.length=0}get plugins(){return this._renderPlugins}add(e,t){const n=()=>{if(t?.aborted)throw e.uninitializeRenderContext(),s();this._renderPlugins.push(e),e.produces.forEach((t,r)=>this._slots[r].push(e)),this.context.requestRender(),this._version.value++},o=e.initializeRenderContext(this.context,t);if(r(o))return o.then(n);n()}remove(t){e(this._renderPlugins,t),t.uninitializeRenderContext();for(let e=0;e<this._slots.length;++e)this._slots[e]=this._slots[e].filter(e=>e!==t);this.context.requestRender(),this._version.value++}sortTestsTransparentOrderedSlots(e){if("test"!==process.env.NODE_ENV)return;if(this._renderPlugins.some(e=>0!==e.testsTransparentRenderOrder))for(const r of e){const e=this._slots[r];t(e,0,e.length,(e,t)=>(e.testsTransparentRenderOrder??0)-(t.testsTransparentRenderOrder??0))}}prepareRender(){this._renderPlugins.forEach(e=>e.prepareRender?.(this.context.renderContext))}updateAnimation(e){let t=!1;return this._renderPlugins.forEach(r=>t=r.updateAnimation?.(e)||t),t}precompile(...e){++this.context.techniques.precompiling;const t=this.context.renderContext.bind.slot;for(const r of e)this.context.renderContext.bind.slot=r,this._forEachRender(()=>{});this.context.renderContext.bind.slot=t,--this.context.techniques.precompiling}render(...e){for(const t of e)this.context.renderContext.bind.slot=t,this._forEachRender((e,t)=>e.render(this.context.renderContext,t))}_forEachRender(e){const t=this.context.renderContext.bind.slot,r=this.context.renderContext.output;this._slots[t].forEach(s=>{const n=s.produces.get(t);if(!n?.(r)||s.isDecoration&&!this.context.renderContext.bind.decorations)return;const o=s.acquireTechniques(this.context.renderContext);o&&e(s,o)})}queryDepthRange(e){return this._renderPlugins.reduce((t,r)=>t.union(r.queryDepthRange?.(e)),new i)}get updating(){return this._version.value>=0&&this._renderPlugins.some(e=>e.readyToRun)}produces(e,...t){return t.some(t=>this._slots[t].some(r=>{const s=r.produces.get(t);return!!s&&s(e)}))}hasHighlight(e){return d.some(t=>this._slots[t].some(t=>t.hasHighlight(e)))}get hasDecorations(){return this._renderPlugins.some(e=>e.isDecoration)}get hasOccludees(){return this._renderPlugins.some(e=>e.hasOccludees)}get emissions(){return o(this._renderPlugins)}get renderOccludedFlags(){return this._renderPlugins.reduce((e,t)=>e|(t.renderOccludedFlags??0),0)}get usedMemory(){return this._renderPlugins.reduce((e,t)=>t.material?e:e+(t.usedMemory??0),0)}get test(){}}const d=[2,4,18,12,13,14];export{h as RenderPluginManager};
