/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import e from"../../../../core/Logger.js";import{disposeMaybe as t}from"../../../../core/maybe.js";import{GLMaterialParameters as r}from"./GLMaterialParameters.js";import{assert as i}from"./Util.js";class s{constructor(e,t,r,i){this._textures=e,this._techniques=t,this.materialChanged=r,this.requestRender=i,this._id2glMaterialRef=new Map}dispose(){this._textures.destroy()}acquire(e,t,i){this._ownMaterial(e);const s=e.produces.get(t);if(!s?.(i))return null;let l=this._id2glMaterialRef.get(e.id);if(null==l){const t=e.createGLMaterial(new r(e,this._techniques,this._textures));l=new a(t),this._id2glMaterialRef.set(e.id,l)}return l.ref(),l.glMaterial}release(e){const r=this._id2glMaterialRef.get(e.id);null!=r&&(r.unref(),r.referenced||(t(r.glMaterial),this._id2glMaterialRef.delete(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 a{constructor(e){this.glMaterial=e,this._refCnt=0}ref(){++this._refCnt}unref(){--this._refCnt,i(this._refCnt>=0)}get referenced(){return this._refCnt>0}}export{s as GLMaterialRepository};
