/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{ArcadeDate as e}from"../ArcadeDate.js";import{toSymbolId as r}from"../arcadeEnvironment.js";import t from"../Dictionary.js";import{ArcadeExecutionError as n}from"../executionError.js";import{Feature as o}from"../Feature.js";import{B as i,k as a,T as s,s as u,K as d,U as m,V as c,v as f,W as l,X as p,e as y}from"../../chunks/languageUtils.js";import{layerFieldEsriConstants as v}from"../featureset/support/shared.js";import{SqlTimeStampOffset as w}from"../../core/sql/SqlTimestampOffset.js";import h from"../../core/sql/WhereClause.js";import{internalTimeReceivedField as b}from"../../layers/support/streamLayerUtils.js";import{isString as T,isNumber as j}from"../../support/guards.js";function I(e){const r=e?.fullSchema();return r?.datesInUnknownTimezone?"unknown":r?.dateFieldsTimeZone||"UTC"}function F(e){const r=e.fullSchema()?.fieldsIndex;return null==r&&e instanceof o?e.fieldsIndex:r}const g={getAttributeSQL(e,r){const t=e.field(r);if(null==t)return t;if(y(t)){const n=t.toDateTime(),o=F(e)?.get(r)?.type;return"esriFieldTypeTimestampOffset"===o||"timestamp-offset"===o?w.fromDateTime(n):n}return t}};function P(o,w,P){o.domain=function(e,r){return w(e,r,(o,m,c)=>{if(i(c,2,3,e,r),a(c[0])){const r=s(c[0],u(c[1]),void 0===c[2]?void 0:c[2]);return r&&r.domain?"coded-value"===r.domain.type||"codedValue"===r.domain.type?t.convertObjectToArcadeDictionary({type:"codedValue",name:r.domain.name,dataType:v[r.field.type],codedValues:r.domain.codedValues.map(e=>({name:e.name,code:e.code}))},d(e)):t.convertObjectToArcadeDictionary({type:"range",name:r.domain.name,dataType:v[r.field.type],min:r.domain.minValue,max:r.domain.maxValue},d(e)):null}throw new n(e,"InvalidParameter",r)})},o.domaincode=function(e,r){return w(e,r,(t,o,s)=>{if(i(s,2,4,e,r),a(s[0]))return m(s[0],u(s[1]),s[2],void 0===s[3]?void 0:s[3]);throw new n(e,"InvalidParameter",r)})},o.domainname=function(e,r){return w(e,r,(t,o,s)=>{if(i(s,2,4,e,r),a(s[0]))return c(s[0],u(s[1]),s[2],void 0===s[3]?void 0:s[3]);throw new n(e,"InvalidParameter",r)})},o.expects=function(e,r){return w(e,r,(t,o,i)=>{if(i.length<1)throw new n(e,"WrongNumberOfParameters",r);return f})},o.featureinfilter=function(e,r){return w(e,r,(e,r,t)=>{i(t,2,2,e,r);const[o,s]=t;if(null==o)return!1;if(""===s||null==s)return!0;if(!a(o)||!T(s))throw new n(e,"InvalidParameter",r);const u=h.create(s,{fieldsIndex:F(o),timeZone:I(o)}),d=u.getVariables();for(const n of d)u.parameters[n]=P(e,{name:n});return u.testFeature(o,g)})},o.gdbversion=function(e,r){return w(e,r,(t,o,s)=>{if(i(s,1,1,e,r),a(s[0]))return s[0].gdbVersion();throw new n(e,"InvalidParameter",r)})},o.schema=function(e,r){return w(e,r,(o,i,s)=>{if(a(s[0])){const r=l(s[0]);return r?t.convertObjectToArcadeDictionary(r,d(e)):null}throw new n(e,"InvalidParameter",r)})},o.subtypecode=function(e,r){return w(e,r,(t,o,s)=>{if(i(s,1,1,e,r),a(s[0])){const e=p(s[0]);if(!e)return null;if(e.subtypeField&&s[0].hasField(e.subtypeField)){const r=s[0].field(e.subtypeField);for(const t of e.subtypes)if(t.code===r)return t.code;return null}return null}throw new n(e,"InvalidParameter",r)})},o.subtypename=function(e,r){return w(e,r,(t,o,s)=>{if(i(s,1,1,e,r),a(s[0])){const e=p(s[0]);if(!e)return"";if(e.subtypeField&&s[0].hasField(e.subtypeField)){const r=s[0].field(e.subtypeField);for(const t of e.subtypes)if(t.code===r)return t.name;return""}return""}throw new n(e,"InvalidParameter",r)})},o.subtypes=function(e,r){return w(e,r,(o,s,u)=>{if(i(u,1,1,e,r),a(u[0])){const r=p(u[0]);return r?t.convertObjectToArcadeDictionary(r,d(e)):null}throw new n(e,"InvalidParameter",r)})},o[r("TimeReceived")]=function(r,t){return w(r,t,(r,t,o)=>{if(i(o,1,1,r,t),a(o[0])){if(o[0].hasField(b)){const t=o[0].field(b);return j(t)?e.epochToArcadeDate(t,r.timeZone??"system"):y(t)?t:null}return null}throw new n(r,"InvalidParameter",t)})}}export{P as registerFunctions};
