/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import e from"../../../../core/Logger.js";import r from"../../../../core/PooledArray.js";import{DepthRange as t}from"../lib/DepthRange.js";class n{constructor(e){this._context=e,this._nodes=new r}destroy(){this._nodes.forEach(e=>e.destroy()),this._nodes.prune()}add(e){this._nodes.push(e)}remove(e){this._nodes.remove(e)}produces(e){return this._nodes.some(({produces:r})=>r===e)}require(e,...r){const t=this._nodes,n=r=>t.reduce((t,{consumes:n,produces:o})=>t+(!n.required.includes(e)||null!=r&&o!==r?0:1),0);return 0===r.length?n():r.reduce((e,r)=>e+n(r),0)}optional(e,...r){const t=this._nodes,n=r=>t.reduce((t,{consumes:n,produces:o})=>t+(!n.optional?.includes(e)||null!=r&&o!==r?0:1),0);return 0===r.length?n():r.reduce((e,r)=>e+n(r),0)}updateAnimation(e){return this._nodes.reduce((r,t)=>t.updateAnimation(e)||r,!1)}precompile(e,...r){++this._context.techniques.precompiling;for(const t of r)this._nodes.forEach(r=>{const n=e||!r.consumes.required.includes("emissive");r.produces===t&&n&&r.precompile()});--this._context.techniques.precompiling}queryDepthRange(r,n){return this._nodes.reduce((t,o)=>{if(o.produces!==r)return t;try{return t.union(o.queryDepthRange(n))}catch(s){return e.getLogger(o).errorOnce(s),t}},new t)}render(e,r,t=()=>{}){return this._render(e,r,t)??e}produce(e,r,t=()=>{}){return this._render(e,r,t)}_render(r,t,n=()=>{}){const o="string"==typeof r?r:r.name,s=this._nodes.filter(({produces:e})=>e===o);if(0===s.length)return;let i="string"==typeof r?null:r;return s.some(r=>{const s=i?[i]:[],u=null==i;for(const e of r.consumes.required){if(e===o){if(u)return!1;continue}const r=t.get(e);if(r)s.push(r);else if("emissive"!==e||!t.get(o)?.hasAttachment(e))return n?.(s),!1}if(r.consumes.optional)for(const e of r.consumes.optional){if(e===o)continue;const r=t.get(e);r&&s.push(r)}try{const n=r.doRender(s);n&&n!==i&&(o!==n.name&&(e.getLogger(r).errorOnce(`RenderNode produced ${n.name}, expected ${o}`),n.setName(o)),i?.release(),i=n,t.set(o,i))}catch(c){e.getLogger(r).errorOnce(c)}return n?.(s),u&&null!=i}),this._context.rctx.enforceState(),i}get requireGeometryDepth(){return this._nodes.some(e=>"disabled"!==e.produces&&e.requireGeometryDepth)}get test(){return{nodes:this._nodes}}}export{n as RenderNodes};
