/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../../../chunks/_rollupPluginBabelHelpers","../../../../core/maybe","../../../../core/reactiveUtils","../../webgl-engine/lib/basicInterfaces","../../webgl-engine/lib/ContentObjectType","../../webgl-engine/lib/Object3D","../../webgl-engine/lib/Texture","../../webgl-engine/lib/WebGLLayer"],(function(e,s,t,r,i,c,o,n,a){"use strict";let u=function(){function e(e){this.resourceFactory=e,this._resources=null,this._visible=!0,this._attached=!1}var u=e.prototype;return u.destroy=function(){this._destroyResources()},u.recreate=function(){this.attached&&this._createResources()},u.recreateGeometry=function(){if(!this.resourceFactory.recreateGeometry)return void this.recreate();const e=this.resourceFactory.view._stage;if(t.isNone(this._resources)||!e)return;const s=this._resources.object;this._resources.external.forEach((s=>{s.type===c.ContentObjectType.Geometry&&e.remove(s)})),s.removeAllGeometries();const r=this.resourceFactory.recreateGeometry(this._resources.external,s,this._resources.layer);e.addMany(r)},u._createOrDestroyResources=function(){this._attached?this._resources||this._createResources():this._destroyResources()},u._createResources=function(){this._destroyResources();const e=this.resourceFactory,s=e.view,t=s._stage;if(!t)return;const c=new a.WebGLLayer({isPickable:!1,updatePolicy:i.UpdatePolicy.SYNC});t.add(c);const u=new o.Object3D({castShadow:!1}),l=e.createResources(u,c);l.forEach((e=>{t.add(e),e instanceof n.Texture&&t.loadImmediate(e)})),t.add(u),c.add(u);const h=e.cameraChanged?r.watch((()=>s.state.camera),(s=>e.cameraChanged(s)),r.initial):null;this._resources={layer:c,object:u,external:l,cameraHandle:h},this._syncVisible()},u._destroyResources=function(){if(t.isNone(this._resources))return;const e=this.resourceFactory.view._stage;null==e||e.remove(this._resources.object),null==e||e.remove(this._resources.layer),this._resources.external.forEach((s=>{null==e||e.remove(s),"dispose"in s&&s.dispose()})),this._resources.object.dispose(),this._resources.cameraHandle&&this._resources.cameraHandle.remove(),this._resources=null},u._syncVisible=function(){t.isNone(this._resources)||this._resources.object.setVisible(this._visible)},s._createClass(e,[{key:"object",get:function(){return t.isSome(this._resources)?this._resources.object:null}},{key:"resources",get:function(){return t.isSome(this._resources)?this._resources.external:null}},{key:"visible",get:function(){return this._visible},set:function(e){e!==this._visible&&(this._visible=e,this._syncVisible())}},{key:"attached",get:function(){return this._attached},set:function(e){e!==this._attached&&(this._attached=e,this._createOrDestroyResources())}}]),e}();e.VisualElementResources=u,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
