/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import e from"../../../../core/Logger.js";import{disposeMaybe as t}from"../../../../core/maybe.js";import{NestedMap as r}from"../../../../core/NestedMap.js";import{GLMaterialParameters as i}from"./GLMaterialParameters.js";import{assert as s}from"./Util.js";class a{constructor(e,t,i,s){this._textures=e,this._techniques=t,this.materialChanged=i,this.requestRender=s,this._id2glMaterialRef=new r}dispose(){this._textures.destroy()}acquire(e,t,r){this._ownMaterial(e);const s=e.produces.get(t);if(!s?.(r))return null;let a=this._id2glMaterialRef.get(r,e.id);if(null==a){const t=e.createGLMaterial(new i(e,r,this._techniques,this._textures));a=new o(t),this._id2glMaterialRef.set(r,e.id,a)}return a.ref(),a.glMaterial}release(e,r){const i=this._id2glMaterialRef.get(r,e.id);null!=i&&(i.unref(),i.referenced||(t(i.glMaterial),this._id2glMaterialRef.delete(r,e.id)))}_ownMaterial(t){t.repository&&t.repository!==this&&e.getLogger("esri.views.3d.webgl-engine.lib.GLMaterialRepository").error("Material is already owned by a different material repository"),t.repository=this}}class o{constructor(e){this.glMaterial=e,this._refCnt=0}ref(){++this._refCnt}unref(){--this._refCnt,s(this._refCnt>=0)}get referenced(){return this._refCnt>0}}export{a as GLMaterialRepository};
