/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{ArcadeDate as e}from"./ArcadeDate.js";import{ArcadeExecutionError as t}from"./executionError.js";import r from"./ImmutableArray.js";import{c as i}from"../chunks/languageUtils.js";import{isDate as s}from"../support/guards.js";const n="Voxel.Position",a="Voxel.LocalTime";function o(e){const i=e.getAttribute(n);if("string"!=typeof i)throw new t("InvalidParameter",null);const s=JSON.parse(i);if(!Array.isArray(s)||"number"!=typeof s[0]||"number"!=typeof s[1]||"number"!=typeof s[2])throw new t("InvalidParameter",null);return new r(s)}function l(r,i){const n=r.getAttribute(a);if(null==n)return null;if(!s(n))throw new t("InvalidParameter",null);return e.dateJSAndZoneToArcadeDate(n,i)}class u{constructor(e,t){this._graphic=e,this._timeZone=t,this.arcadeDeclaredClass="esri.arcade.Voxel",this._layer=e.sourceLayer}static getDeclaredMembers(e){return e.fields.map(e=>{switch(e.name){case n:return{name:e.name,type:"array",elementType:{type:"number"}};case a:return{name:e.name,type:"date"};default:switch(e.type){case"string":return{name:e.name,type:"text"};case"double":return{name:e.name,type:"number"}}return null}}).filter(e=>null!=e)}getPosition(){return void 0!==this._position?this._position:this._position=o(this._graphic)}getLocalTime(){return void 0!==this._localTime?this._localTime:this._localTime=l(this._graphic,this._timeZone??"system")}keys(){return this._layer.fields.map(e=>e.name).sort((e,t)=>e.toLowerCase()<t.toLowerCase()?-1:1)}hasField(e){return this._layer.fieldsIndex.has(e)}field(e){const r=this._layer.fieldsIndex?.get(e)?.name;if(null==r)throw new t("FieldNotFound",null,{key:e});switch(r){case n:return this.getPosition();case a:return this.getLocalTime()}return this._graphic.attributes[r]??null}isEmpty(){return this._layer.fields.length<=0}castToText(e=!1){const t={...this._graphic.attributes};t[n]=this.getPosition(),a in t&&(t[a]=this.getLocalTime());for(const r of Object.keys(t))this._layer.fieldsIndex?.has(r)||delete t[r];return i(t,{useNumbersForDates:e})}}export{u as Voxel};
