/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{isArrayLike as e}from"../../../../../core/arrayUtils.js";class t{constructor(e,t){this.brushes=e,this.name=t.name,this.drawPhase=t.drawPhase||1,this._targetFn=t.target,this.effects=t.effects||[],this.enableDefaultDraw=t.enableDefaultDraw??(()=>!0),this.forceDrawByDisplayOrder=!!t.forceDrawByDisplayOrder}render(e){const{context:t,profiler:r}=e,s=this._targetFn(),a=this.drawPhase&e.drawPhase;if(r.recordPassStart(this.name),a){this.enableDefaultDraw()&&this._doRender(e,s),r.recordPassEnd();for(const r of this.effects){if(!r.enable())continue;const a=r.apply,n=r.args?.(),i=t.getViewport(),d=t.getBoundFramebufferObject(),o=e.passOptions;this._bindEffect(e,a,n),this._doRender(e,s,a.defines),this._drawAndUnbindEffect(e,a,i,d,o,n)}}}_doRender(e,t,r){if(null==t)return;const{profiler:s,context:a}=e;for(const n of this.brushes){if(s.recordBrushStart(n.name),null!=n.brushEffect){const s=a.getViewport(),i=a.getBoundFramebufferObject(),d=e.passOptions;this._bindEffect(e,n.brushEffect),this._drawWithBrush(n,e,t,r),this._drawAndUnbindEffect(e,n.brushEffect,s,i,d)}else this._drawWithBrush(n,e,t,r);s.recordBrushEnd()}}_drawWithBrush(t,r,s,a){e(s)?(t.prepareState(r,a),t.drawMany(r,s,a)):s.visible&&(t.prepareState(r,a),t.draw(r,s,a))}_bindEffect(e,t,r){const{profiler:s}=e;s.recordPassStart(this.name+"."+t.name),t.bind(e,r);const a=t.createOptions(e,r);e.passOptions=a}_drawAndUnbindEffect(e,t,r,s,a,n){const{profiler:i,context:d}=e;e.passOptions=a,i.recordBrushStart(t.name),t.draw(e,n),t.unbind(e,n),d.bindFramebuffer(s);const{x:o,y:f,width:h,height:c}=r;d.setViewport(o,f,h,c),i.recordBrushEnd(),i.recordPassEnd()}}export{t as default};
