/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{disposeMaybe as e}from"../../../../core/maybe.js";import{vtlBrushes as t}from"../vtlBrushes.js";import l from"./shaders/VTLMaterialManager.js";const s=1e-6;class r{constructor(e,t){this.spriteMosaic=e,this.glyphMosaic=t,this._brushCache={vtlBackground:null,vtlFill:null,vtlLine:null,vtlCircle:null,vtlSymbol:null},this._vtlMaterialManager=new l}dispose(){this._brushCache.vtlBackground?.dispose(),this._brushCache.vtlFill?.dispose(),this._brushCache.vtlLine?.dispose(),this._brushCache.vtlCircle?.dispose(),this._brushCache.vtlSymbol?.dispose(),this._brushCache=null,this._vtlMaterialManager=e(this._vtlMaterialManager),this.spriteMosaic.dispose(),this.glyphMosaic.dispose()}get vectorTilesMaterialManager(){return this._vtlMaterialManager}drawSymbols(e,l,r){const n=r.layers;e.renderPass="translucent";let i=this._brushCache.vtlSymbol;null==i&&(i=new t.vtlSymbol,this._brushCache.vtlSymbol=i),a[0]=l;for(let t=0;t<n.length;t++){const l=n[t];if(3!==l.type)continue;const r=l.getLayoutProperty("visibility");if(r&&1===r.getValue())continue;const o=e.displayLevel;void 0!==l.minzoom&&l.minzoom>o+s||void 0!==l.maxzoom&&l.maxzoom<=o-s||(e.styleLayerUID=l.uid,e.styleLayer=l,i.drawMany(e,a))}a[0]=null}drawBackground(e,l,r){if(0===r.backgroundBucketIds.length)return;const{context:n,displayLevel:i,requiredLevel:o}=e;l.key.level=o,n.setBlendingEnabled(!0),n.setDepthTestEnabled(!1),n.setStencilTestEnabled(!1),e.renderPass="background";let u=this._brushCache.vtlBackground;null==u&&(u=new t.vtlBackground,this._brushCache.vtlBackground=u),a[0]=l,r.backgroundBucketIds.forEach(t=>{const l=r.getLayerById(t);if(0!==l.type)return;const n=l.getLayoutProperty("visibility");n&&1===n.getValue()||void 0!==l.minzoom&&l.minzoom>i+s||void 0!==l.maxzoom&&l.maxzoom<=i-s||(e.styleLayerUID=l.uid,e.styleLayer=l,u.drawMany(e,a))}),a[0]=null}drawTile(e,t,l,s){const{context:r}=e,a=l.layers;r.setBlendingEnabled(!1),r.setDepthTestEnabled(!0),r.setDepthWriteEnabled(!0),r.setDepthFunction(515);const n=a.filter(e=>{if(null!=s&&s!==e.type||!t.layerData.has(e.uid))return!1;const l=e.getLayoutProperty("visibility");return 1!==l?.getValue()});e.renderPass="opaque";for(let i=n.length-1;i>=0;--i)this._renderStyleLayer(n[i],e,t);r.setDepthWriteEnabled(!1),r.setBlendingEnabled(!0),r.setBlendFunctionSeparate(1,771,1,771),e.renderPass="translucent",n.forEach(l=>this._renderStyleLayer(l,e,t)),r.setDepthTestEnabled(!1),r.bindVAO(null)}_renderStyleLayer(e,l,r){const{renderPass:n}=l;let i;switch(e.type){case 0:if("background"!==n)return;i=this._brushCache.vtlBackground,i||(i=new t.vtlBackground,this._brushCache.vtlBackground=i);break;case 1:if("opaque"!==n&&"translucent"!==l.renderPass)return;i=this._brushCache.vtlFill,null==i&&(i=new t.vtlFill,this._brushCache.vtlFill=i);break;case 2:if("translucent"!==n)return;i=this._brushCache.vtlLine,null==i&&(i=new t.vtlLine,this._brushCache.vtlLine=i);break;case 4:if("translucent"!==n)return;i=this._brushCache.vtlCircle,null==i&&(i=new t.vtlCircle,this._brushCache.vtlCircle=i);break;case 3:if("translucent"!==n)return;i=this._brushCache.vtlSymbol,null==i&&(i=new t.vtlSymbol,this._brushCache.vtlSymbol=i)}const{displayLevel:o}=l,{minzoom:u,maxzoom:h}=e;if(void 0!==u&&u>o+s||void 0!==h&&h<=o-s)return;const{context:c}=l;c.setStencilTestEnabled(!1),c.setStencilWriteMask(0),l.styleLayerUID=e.uid,l.styleLayer=e,a[0]=r,i.drawMany(l,a),a[0]=null}}const a=[null];export{r as default};
