/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import t from"../../../../../core/Error.js";import e from"../../../../../core/Logger.js";import{fromRectValues as s}from"../../../../../geometry/support/aaBoundingBox.js";import{displayIdTexelMask as i}from"../../../engine/webgl/DisplayId.js";import{CachedField as r}from"./CachedField.js";import{ComputedExpression as n}from"./ComputedExpression.js";import{ComputedLegacyLabelExpression as o}from"./ComputedLegacyLabelExpression.js";import{ComputedDictionaryTemplate as u}from"./DictionaryTemplate.js";import{NormalizedField as d}from"./NormalizedField.js";import{StaticBitSet as h}from"./StaticBitSet.js";import{createWhereClause as a}from"./whereUtils.js";const c=()=>e.getLogger("esri.views.2d.layers.features.support.ComputedAttributeStorage"),m=4294967295;function p(t,e,s){if(!(t.length>e))for(;t.length<=e;)t.push(s)}class l{constructor(t){this._numerics=[],this._strings=[],this._allocatedSize=256,this._bitsets=[],this._instanceIds=[],this._bounds=[],this._dirtyBitset=this.getBitset(this.createBitset()),this.compilationOptions=t}createBitset(){const t=this._bitsets.length;return this._bitsets.push(h.create(this._allocatedSize,i)),t+1}createDictionaryTemplateField(t,e){return new u(t,e)}async createComputedField(e,s=!1){if(e.expression)try{if(!this.compilationOptions)throw new Error("InternalError: Compilation options not defined");if(s)return await o.create(e.expression,this.compilationOptions);return await n.create(e.expression,this.compilationOptions)}catch(u){const s=new t("featurelayer","Failed to compile arcade expression",{error:u,expression:e.expression});return c().error(s),null}if(e.normalizationType||e.normalizationField)return new d(e.field,e);if(e.field)return new r(e.field);const i=new t("featurelayer","Unable to create computed field. No expression or field found",{info:e});return c().error(i),null}async createWhereClause(t){return t?a(t,this.compilationOptions.fields):null}getBitset(t){return this._bitsets[t-1]}getComputedNumeric(t,e){return this.getComputedNumericAtIndex(t&i,0)}setComputedNumeric(t,e,s){return this.setComputedNumericAtIndex(t&i,s,0)}getComputedString(t,e){return this.getComputedStringAtIndex(t&i,0)}setComputedString(t,e,s){return this.setComputedStringAtIndex(t&i,0,s)}getComputedNumericAtIndex(t,e){const s=t&i;return this._ensureNumeric(e,s),this._numerics[e][s]}setComputedNumericAtIndex(t,e,s){const r=t&i;this._ensureNumeric(e,r),this._numerics[e][r]=s}getPackedChunkId(t){const e=t&i;return this._ensureInstanceId(e),this._instanceIds[e]}setPackedChunkId(t,e){const s=t&i;this._ensureInstanceId(s),this._instanceIds[s]=e}getComputedStringAtIndex(t,e){const s=t&i;return this._ensureString(e,s),this._strings[e][s]}setComputedStringAtIndex(t,e,s){const r=t&i;this._ensureString(e,r),this._strings[e][r]=s}getXMin(t){return this._bounds[4*(t&i)]}getYMin(t){return this._bounds[4*(t&i)+1]}getXMax(t){return this._bounds[4*(t&i)+2]}getYMax(t){return this._bounds[4*(t&i)+3]}setBounds(t,e,s=!1){const r=t&i;if(!s&&!this._dirtyBitset.has(t))return this._bounds[4*r]!==m;this._dirtyBitset.unset(t);const n=e.readGeometryWorldSpace();if(p(this._bounds,4*r+4,0),!n||!n.coords.length)return this._bounds[4*r]=m,this._bounds[4*r+1]=m,this._bounds[4*r+2]=m,this._bounds[4*r+3]=m,!1;let o=1/0,u=1/0,d=-1/0,h=-1/0;return n.forEachVertex((t,e)=>{o=Math.min(o,t),u=Math.min(u,e),d=Math.max(d,t),h=Math.max(h,e)}),this._bounds[4*r]=o,this._bounds[4*r+1]=u,this._bounds[4*r+2]=d,this._bounds[4*r+3]=h,!0}getBounds(t,e){const i=this.getXMin(e),r=this.getYMin(e),n=this.getXMax(e),o=this.getYMax(e);return s(t,i,r,n,o),i!==m}_ensureNumeric(t,e){this._numerics[t]||(this._numerics[t]=[]);p(this._numerics[t],e,0)}_ensureInstanceId(t){p(this._instanceIds,t,0)}_ensureString(t,e){this._strings[t]||(this._strings[t]=[]);p(this._strings[t],e,null)}}export{l as ComputedAttributeStorage};
