/* COPYRIGHT Esri - https://js.arcgis.com/5.2/LICENSE.txt */
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,X as s,s as u,P as d,Y as m,Z as c,v as f,_ as l,$ 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 P={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 g(o,w,g){o.domain=function(e,r){return w(e,r,(o,m,c)=>{if(i(c,2,3,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("InvalidParameter",r)})},o.domaincode=function(e,r){return w(e,r,(e,t,o)=>{if(i(o,2,4,r),a(o[0]))return m(o[0],u(o[1]),o[2],void 0===o[3]?void 0:o[3]);throw new n("InvalidParameter",r)})},o.domainname=function(e,r){return w(e,r,(e,t,o)=>{if(i(o,2,4,r),a(o[0]))return c(o[0],u(o[1]),o[2],void 0===o[3]?void 0:o[3]);throw new n("InvalidParameter",r)})},o.expects=function(e,r){return w(e,r,(e,t,o)=>{if(o.length<1)throw new n("WrongNumberOfParameters",r);return f})},o.featureinfilter=function(e,r){return w(e,r,(e,r,t)=>{i(t,2,2,r);const[o,s]=t;if(null==o)return!1;if(""===s||null==s)return!0;if(!a(o)||!T(s))throw new n("InvalidParameter",r);const u=h.create(s,{fieldsIndex:F(o),timeZone:I(o)}),d=u.getVariables();for(const n of d)u.parameters[n]=g(e,{name:n});return u.testFeature(o,P)})},o.gdbversion=function(e,r){return w(e,r,(e,t,o)=>{if(i(o,1,1,r),a(o[0]))return o[0].gdbVersion();throw new n("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("InvalidParameter",r)})},o.subtypecode=function(e,r){return w(e,r,(e,t,o)=>{if(i(o,1,1,r),a(o[0])){const e=p(o[0]);if(!e)return null;if(e.subtypeField&&o[0].hasField(e.subtypeField)){const r=o[0].field(e.subtypeField);for(const t of e.subtypes)if(t.code===r)return t.code;return null}return null}throw new n("InvalidParameter",r)})},o.subtypename=function(e,r){return w(e,r,(e,t,o)=>{if(i(o,1,1,r),a(o[0])){const e=p(o[0]);if(!e)return"";if(e.subtypeField&&o[0].hasField(e.subtypeField)){const r=o[0].field(e.subtypeField);for(const t of e.subtypes)if(t.code===r)return t.name;return""}return""}throw new n("InvalidParameter",r)})},o.subtypes=function(e,r){return w(e,r,(o,s,u)=>{if(i(u,1,1,r),a(u[0])){const r=p(u[0]);return r?t.convertObjectToArcadeDictionary(r,d(e)):null}throw new n("InvalidParameter",r)})},o[r("TimeReceived")]=function(r,t){return w(r,t,(r,t,o)=>{if(i(o,1,1,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("InvalidParameter",t)})}}export{g as registerFunctions};
