/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["../../../chunks/_rollupPluginBabelHelpers","../../../core/Collection","../../../core/Logger","../../../core/reactiveUtils","../../../chunks/vec3","./RenderContext","../webgl-engine/lib/RenderPass","../webgl-engine/lib/RenderSlot"],(function(e,t,r,n,i,s,d,o){"use strict";const a=r.getLogger("esri.views.3d.externalRenderers.ExternalRendererStore");let c=function(){function e(){this._renderers=new t}var r=e.prototype;return r.add=function(e,t){this._findOrCreateStageRenderer(e).add(t)},r.remove=function(e,t){const r=this._findStageRenderer(e);r&&r.remove(t),0===r.renderers.length&&(r.destroy(),this._renderers.remove(r))},r._findStageRenderer=function(e){return this._renderers.find((t=>t.view===e))},r._findOrCreateStageRenderer=function(e){let t=this._findStageRenderer(e);return t||(t=new h(e),this._renderers.add(t)),t},e}(),h=function(){function r(e){this.view=e,this.canRender=!0,this.renderers=new t,this._readyWatchHandle=n.watch((()=>e.ready),(e=>{e?(this.context=new s(this.view),this.view._stage.addRenderPlugin([o.RenderSlot.OPAQUE_MATERIAL,o.RenderSlot.TRANSPARENT_MATERIAL],this)):(this.renderers.forEach((e=>l(e,"dispose",this.context))),this.context=null)}),n.initial)}var c=r.prototype;return c.destroy=function(){this.renderers.drain((e=>{this.context&&l(e,"dispose",this.context)})),this.view._stage&&this.view._stage.removeRenderPlugin(this),this._readyWatchHandle&&(this._readyWatchHandle.remove(),this._readyWatchHandle=null),this.context=null},c.add=function(e){this.renderers.includes(e)?a.warn("add(): Cannot add external renderer: renderer has already been added"):(this.renderers.add(e),this.context&&(this._webglStateReset(),l(e,"setup",this.context),this.view._stage.renderView.requestRender()))},c.remove=function(e){const t=this.renderers.indexOf(e);-1!==t&&(this.renderers.removeAt(t),this.context&&(l(e,"dispose",this.context),this.view._stage.renderView.requestRender()))},c.initializeRenderContext=function(e){this.context.rctx=e.renderContext.rctx,this.renderers.length>0&&this._webglStateReset(),this.renderers.forEach((e=>l(e,"setup",this.context)))},c.uninitializeRenderContext=function(){},c.render=function(e){0===this.renderers.length||e.pass!==d.RenderPass.MATERIAL||e.bindParameters.slot!==o.RenderSlot.OPAQUE_MATERIAL&&e.bindParameters.slot!==o.RenderSlot.TRANSPARENT_MATERIAL||(this._updateContext(e),this._webglStateReset(),this.renderers.forEach((t=>{switch(e.bindParameters.slot){case o.RenderSlot.OPAQUE_MATERIAL:l(t,"render",this.context);break;case o.RenderSlot.TRANSPARENT_MATERIAL:l(t,"renderTransparent",this.context)}})))},c._updateContext=function(e){this.context.camera.copyFrom(e.bindParameters.camera),i.copy(this.context.sunLight.direction,e.bindParameters.lighting.old.direction),i.copy(this.context.sunLight.diffuse.color,e.bindParameters.lighting.old.diffuse.color),i.copy(this.context.sunLight.ambient.color,e.bindParameters.lighting.old.ambient.color),this.context._renderTargetHelper=e.offscreenRenderingHelper},c._webglStateReset=function(){var e;this.context.rctx.resetState(),null==(e=this.context._renderTargetHelper)||e.bindFramebuffer()},e._createClass(r,[{key:"needsTransparentPass",get:function(){return this.renderers.some((e=>"function"==typeof e.renderTransparent))}}]),r}();function l(e,t,r){"function"==typeof e[t]&&e[t](r)}return c}));
