UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 21.4 kB
import{ArcadeDate as e}from"../ArcadeDate.js";import t from"../ArcadePortal.js";import n from"../Dictionary.js";import{ArcadeExecutionError as i,ExecutionErrorCodes as a}from"../executionError.js";import r from"../Feature.js";import s from"../featureSetCollection.js";import{convertToFeatureSet as o,constructFeatureSetFromPortalItem as l,constructFeatureSet as f,constructFeatureSetFromRelationship as c,constructFeatureSetFromUrl as u,constructAssociationMetaDataFeatureSetFromUrl as d}from"../featureSetUtils.js";import m from"../ImmutableArray.js";import{y as p,T as y,c as h,j as w,l as g,C as I,k as F,A as E,h as D,m as b,w as x,x as A,i as N,g as S,X as T,L}from"../../chunks/languageUtils.js";import{getPortal as $}from"../portalUtils.js";import{SqlExpressionAdapted as v,StringToCodeAdapted as P,FieldRename as R,AdaptedFeatureSet as j,OriginalField as C}from"../featureset/actions/Adapted.js";import M from"../featureset/actions/AttributeFilter.js";import O from"../featureset/actions/OrderBy.js";import k from"../featureset/actions/Top.js";import z from"../featureset/sources/Empty.js";import H from"../featureset/sources/FeatureLayerMemory.js";import G from"../featureset/support/OrderbyClause.js";import{cloneField as Z}from"../featureset/support/shared.js";import{isSingleField as W}from"../featureset/support/sqlUtils.js";import{calculateStat as _}from"./fieldStats.js";import{isPromiseLike as U}from"../../core/promiseUtils.js";import{WhereClause as q}from"../../core/sql/WhereClause.js";import B from"../../layers/FeatureLayer.js";import V from"../../layers/support/Field.js";function Q(e,t,n,i){if(1===i.length){if(b(i[0]))return _(e,i[0],-1);if(A(i[0]))return _(e,i[0].toArray(),-1)}return _(e,i,-1)}async function J(e,t,n){const i=e.getVariables();if(i.length>0){const a=[];for(let e=0;e<i.length;e++){const r={name:i[e]};a.push(await t.evaluateIdentifier(n,r))}const r={};for(let e=0;e<i.length;e++)r[i[e]]=a[e];return e.parameters=r,e}return e}function K(e,t,n=null){for(const i in e)if(i.toLowerCase()===t.toLowerCase())return e[i];return n}function X(e){if(null===e)return null;const t={type:K(e,"type",""),name:K(e,"name","")};if("range"===t.type)t.range=K(e,"range",[]);else{t.codedValues=[];for(const n of K(e,"codedValues",[]))t.codedValues.push({name:K(n,"name",""),code:K(n,"code",null)})}return t}function Y(e){if(null===e)return null;const t={},n=K(e,"wkt",null);null!==n&&(t.wkt=n);const i=K(e,"wkid",null);return null!==i&&(t.wkid=i),t}function ee(e){if(null===e)return null;const t={hasZ:K(e,"hasz",!1),hasM:K(e,"hasm",!1)},n=K(e,"spatialreference",null);n&&(t.spatialReference=Y(n));const i=K(e,"x",null);if(null!==i)return t.x=i,t.y=K(e,"y",null),t;const a=K(e,"rings",null);if(null!==a)return t.rings=a,t;const r=K(e,"paths",null);if(null!==r)return t.paths=r,t;const s=K(e,"points",null);if(null!==s)return t.points=s,t;for(const o of["xmin","xmax","ymin","ymax","zmin","zmax","mmin","mmax"]){const n=K(e,o,null);null!==n&&(t[o]=n)}return t}function te(e,t){for(const n of t)if(n===e)return!0;return!1}function ne(e){return!!e.layerDefinition&&(!!e.featureSet&&(!1!==te(e.layerDefinition.geometryType,["",null,"esriGeometryNull","esriGeometryPoint","esriGeometryPolyline","esriGeometryPolygon","esriGeometryMultipoint","esriGeometryEnvelope"])&&(null!==e.layerDefinition.objectIdField&&""!==e.layerDefinition.objectIdField&&(!1!==b(e.layerDefinition.fields)&&!1!==b(e.featureSet.features)))))}function ie(_){"async"===_.mode&&(_.functions.timezone=function(t,r){return _.standardFunctionAsync(t,r,(async(s,o,l)=>{if(p(l,1,2,t,r),y(l[0])){if(await l[0].load(),1===l.length||null===l[1])return l[0].dateTimeReferenceFieldIndex.layerDateFieldsTimeZone;if(!(l[1]instanceof n)||!1===l[1].hasField("type"))throw new i(t,a.InvalidParameter,r);const e=l[1].field("type");if(!1===h(e))throw new i(t,a.InvalidParameter,r);switch(w(e).toLowerCase()){case"preferredtimezone":return l[0].dateTimeReferenceFieldIndex.layerPreferredTimeZone;case"editfieldsinfo":return l[0].dateTimeReferenceFieldIndex.layerEditFieldsTimeZone;case"timeinfo":return l[0].dateTimeReferenceFieldIndex.layerTimeInfoTimeZone;case"field":if(l[1].hasField("fieldname")&&h(l[1].field("fieldname")))return l[0].dateTimeReferenceFieldIndex.fieldTimeZone(w(l[1].field("fieldname")))}throw new i(t,a.InvalidParameter,r)}const f=g(l[0],I(t));if(null===f)return null;const c=f.timeZone;return"system"===c?e.systemTimeZoneCanonicalName:c}))},_.functions.sqltimestamp=function(e,t){return _.standardFunctionAsync(e,t,(async(n,r,s)=>{p(s,1,3,e,t);const o=s[0];if(F(o)){if(1===s.length)return o.toSQLString();if(2===s.length)return o.changeTimeZone(w(s[1])).toSQLString();throw new i(e,a.InvalidParameter,t)}if(y(o)){if(3!==s.length)throw new i(e,a.InvalidParameter,t);await o.load();const n=w(s[1]);if(!1===F(s[2]))throw new i(e,a.InvalidParameter,t);const r=o.fieldTimeZone(n);return null===r?s[2].toSQLString():s[2].changeTimeZone(r).toSQLString()}throw new i(e,a.InvalidParameter,t)}))},_.signatures.push({name:"sqltimestamp",min:2,max:4}),_.functions.featuresetbyid=function(e,t){return _.standardFunctionAsync(e,t,((n,r,o)=>{if(p(o,2,4,e,t),o[0]instanceof s){const n=w(o[1]);let r=E(o[2],null);const s=D(E(o[3],!0));if(null===r&&(r=["*"]),!1===b(r))throw new i(e,a.InvalidParameter,t);return o[0].featureSetById(n,s,r)}throw new i(e,a.InvalidParameter,t)}))},_.signatures.push({name:"featuresetbyid",min:2,max:4}),_.functions.getfeatureset=function(e,t){return _.standardFunctionAsync(e,t,((n,r,s)=>{if(p(s,1,2,e,t),x(s[0])){let t=E(s[1],"datasource");return null===t&&(t="datasource"),t=w(t).toLowerCase(),o(s[0].fullSchema(),t,e.lrucache,e.interceptor,e.spatialReference)}throw new i(e,a.InvalidParameter,t)}))},_.signatures.push({name:"getfeatureset",min:1,max:2}),_.functions.featuresetbyportalitem=function(e,n){return _.standardFunctionAsync(e,n,((r,s,o)=>{if(p(o,2,5,e,n),null===o[0])throw new i(e,a.PortalRequired,n);if(o[0]instanceof t){const t=w(o[1]),r=w(o[2]);let s=E(o[3],null);const f=D(E(o[4],!0));if(null===s&&(s=["*"]),!1===b(s))throw new i(e,a.InvalidParameter,n);let c=null;return e.services&&e.services.portal&&(c=e.services.portal),c=$(o[0],c),l(t,r,e.spatialReference,s,f,c,e.lrucache,e.interceptor)}if(!1===h(o[0]))throw new i(e,a.PortalRequired,n);const f=w(o[0]),c=w(o[1]);let u=E(o[2],null);const d=D(E(o[3],!0));if(null===u&&(u=["*"]),!1===b(u))throw new i(e,a.InvalidParameter,n);if(e.services&&e.services.portal)return l(f,c,e.spatialReference,u,d,e.services.portal,e.lrucache,e.interceptor);throw new i(e,a.PortalRequired,n)}))},_.signatures.push({name:"featuresetbyportalitem",min:2,max:5}),_.functions.featuresetbyname=function(e,t){return _.standardFunctionAsync(e,t,((n,r,o)=>{if(p(o,2,4,e,t),o[0]instanceof s){const n=w(o[1]);let r=E(o[2],null);const s=D(E(o[3],!0));if(null===r&&(r=["*"]),!1===b(r))throw new i(e,a.InvalidParameter,t);return o[0].featureSetByName(n,s,r)}throw new i(e,a.InvalidParameter,t)}))},_.signatures.push({name:"featuresetbyname",min:2,max:4}),_.functions.featureset=function(e,t){return _.standardFunction(e,t,((r,s,o)=>{p(o,1,1,e,t);let l=o[0];const f={layerDefinition:{geometryType:"",objectIdField:"",globalIdField:"",typeIdField:"",fields:[]},featureSet:{geometryType:"",features:[]}};if(h(l))l=JSON.parse(l),void 0!==l.layerDefinition?(f.layerDefinition=l.layerDefinition,f.featureSet=l.featureSet,l.layerDefinition.spatialReference&&(f.layerDefinition.spatialReference=l.layerDefinition.spatialReference)):(f.featureSet.features=l.features,f.featureSet.geometryType=l.geometryType,f.layerDefinition.geometryType=f.featureSet.geometryType,f.layerDefinition.objectIdField=l.objectIdFieldName,f.layerDefinition.typeIdField=l.typeIdFieldName,f.layerDefinition.globalIdField=l.globalIdFieldName,f.layerDefinition.fields=l.fields,l.spatialReference&&(f.layerDefinition.spatialReference=l.spatialReference));else{if(!(o[0]instanceof n))throw new i(e,a.InvalidParameter,t);{l=JSON.parse(o[0].castToText(!0));const e=K(l,"layerdefinition");if(null!==e){f.layerDefinition.geometryType=K(e,"geometrytype",""),f.featureSet.geometryType=f.layerDefinition.geometryType,f.layerDefinition.globalIdField=K(e,"globalidfield",""),f.layerDefinition.objectIdField=K(e,"objectidfield",""),f.layerDefinition.typeIdField=K(e,"typeidfield","");const t=K(e,"spatialreference",null);t&&(f.layerDefinition.spatialReference=Y(t));for(const i of K(e,"fields",[])){const e={name:K(i,"name",""),alias:K(i,"alias",""),type:K(i,"type",""),nullable:K(i,"nullable",!0),editable:K(i,"editable",!0),length:K(i,"length",null),domain:X(K(i,"domain"))};f.layerDefinition.fields.push(e)}const n=K(l,"featureset",null);if(n){const e={};for(const t of f.layerDefinition.fields)e[t.name.toLowerCase()]=t.name;for(const t of K(n,"features",[])){const n={},i=K(t,"attributes",{});for(const t in i)n[e[t.toLowerCase()]]=i[t];f.featureSet.features.push({attributes:n,geometry:ee(K(t,"geometry",null))})}}}else{f.layerDefinition.geometryType=K(l,"geometrytype",""),f.featureSet.geometryType=f.layerDefinition.geometryType,f.layerDefinition.objectIdField=K(l,"objectidfieldname",""),f.layerDefinition.typeIdField=K(l,"typeidfieldname","");const e=K(l,"spatialreference",null);e&&(f.layerDefinition.spatialReference=Y(e));for(const n of K(l,"fields",[])){const e={name:K(n,"name",""),alias:K(n,"alias",""),type:K(n,"type",""),nullable:K(n,"nullable",!0),editable:K(n,"editable",!0),length:K(n,"length",null),domain:X(K(n,"domain"))};f.layerDefinition.fields.push(e)}const t={};for(const n of f.layerDefinition.fields)t[n.name.toLowerCase()]=n.name;for(const n of K(l,"features",[])){const e={},i=K(n,"attributes",{});for(const n in i)e[t[n.toLowerCase()]]=i[n];f.featureSet.features.push({attributes:e,geometry:ee(K(n,"geometry",null))})}}}}if(!1===ne(f))throw new i(e,a.InvalidParameter,t);return""===(f?.layerDefinition?.geometryType||"")&&(f.layerDefinition.geometryType="esriGeometryNull"),H.create(f,e.spatialReference)}))},_.signatures.push({name:"featureset",min:1,max:1}),_.functions.filter=function(e,t){return _.standardFunctionAsync(e,t,(async(n,r,s)=>{if(p(s,2,2,e,t),b(s[0])||A(s[0])){const n=[];let r=s[0];r instanceof m&&(r=r.toArray());let o=null;if(!N(s[1]))throw new i(e,a.InvalidParameter,t);o=s[1].createFunction(e);for(const e of r){const t=o(e);U(t)?!0===await t&&n.push(e):!0===t&&n.push(e)}return n}if(y(s[0])){const t=await s[0].load(),n=q.create(s[1],t.getFieldsIndex()),i=n.getVariables();if(i.length>0){const t=[];for(let n=0;n<i.length;n++){const a={name:i[n]};t.push(await _.evaluateIdentifier(e,a))}const a={};for(let e=0;e<i.length;e++)a[i[e]]=t[e];return n.parameters=a,new M({parentfeatureset:s[0],whereclause:n})}return new M({parentfeatureset:s[0],whereclause:n})}throw new i(e,a.InvalidParameter,t)}))},_.signatures.push({name:"filter",min:2,max:2}),_.functions.orderby=function(e,t){return _.standardFunctionAsync(e,t,(async(n,r,s)=>{if(p(s,2,2,e,t),y(s[0])){const e=new G(s[1]);return new O({parentfeatureset:s[0],orderbyclause:e})}throw new i(e,a.InvalidParameter,t)}))},_.signatures.push({name:"orderby",min:2,max:2}),_.functions.top=function(e,t){return _.standardFunctionAsync(e,t,(async(n,r,s)=>{if(p(s,2,2,e,t),y(s[0]))return new k({parentfeatureset:s[0],topnum:s[1]});if(b(s[0]))return S(s[1])>=s[0].length?s[0].slice(0):s[0].slice(0,S(s[1]));if(A(s[0]))return S(s[1])>=s[0].length()?s[0].slice(0):s[0].slice(0,S(s[1]));throw new i(e,a.InvalidParameter,t)}))},_.signatures.push({name:"top",min:2,max:2}),_.functions.first=function(e,t){return _.standardFunctionAsync(e,t,(async(n,i,a)=>{if(p(a,1,1,e,t),y(a[0])){const t=await a[0].first(n.abortSignal);if(null!==t){const n=r.createFromGraphicLikeObject(t.geometry,t.attributes,a[0],e.timeReference);return n._underlyingGraphic=t,n}return t}return b(a[0])?0===a[0].length?null:a[0][0]:A(a[0])?0===a[0].length()?null:a[0].get(0):null}))},_.signatures.push({name:"first",min:1,max:1}),_.functions.attachments=function(e,t){return _.standardFunctionAsync(e,t,(async(r,s,o)=>{p(o,1,2,e,t);const l={minsize:-1,maxsize:-1,types:null,returnMetadata:!1};if(o.length>1)if(o[1]instanceof n){if(o[1].hasField("minsize")&&(l.minsize=S(o[1].field("minsize"))),o[1].hasField("metadata")&&(l.returnMetadata=D(o[1].field("metadata"))),o[1].hasField("maxsize")&&(l.maxsize=S(o[1].field("maxsize"))),o[1].hasField("types")){const e=T(o[1].field("types"),!1);e.length>0&&(l.types=e)}}else if(null!==o[1])throw new i(e,a.InvalidParameter,t);if(x(o[0])){let t=o[0]._layer;return t instanceof B&&(t=f(t,e.spatialReference,["*"],!0,e.lrucache,e.interceptor)),null===t?[]:!1===y(t)?[]:(await t.load(),t.queryAttachments(o[0].field(t.objectIdField),l.minsize,l.maxsize,l.types,l.returnMetadata))}if(null===o[0])return[];throw new i(e,a.InvalidParameter,t)}))},_.signatures.push({name:"attachments",min:1,max:2}),_.functions.featuresetbyrelationshipname=function(e,t){return _.standardFunctionAsync(e,t,(async(n,r,s)=>{p(s,2,4,e,t);const o=s[0],l=w(s[1]);let d=E(s[2],null);const m=D(E(s[3],!0));if(null===d&&(d=["*"]),!1===b(d))throw new i(e,a.InvalidParameter,t);if(null===s[0])return null;if(!x(s[0]))throw new i(e,a.InvalidParameter,t);let h=o._layer;if(h instanceof B&&(h=f(h,e.spatialReference,["*"],!0,e.lrucache,e.interceptor)),null===h)return null;if(!1===y(h))return null;h=await h.load();const g=h.relationshipMetaData().filter((e=>e.name===l));if(0===g.length)return null;if(void 0!==g[0].relationshipTableId&&null!==g[0].relationshipTableId&&g[0].relationshipTableId>-1)return c(h,g[0],o.field(h.objectIdField),h.spatialReference,d,m,e.lrucache,e.interceptor);let I=h.serviceUrl();if(!I)return null;I="/"===I.charAt(I.length-1)?I+g[0].relatedTableId.toString():I+"/"+g[0].relatedTableId.toString();const F=await u(I,h.spatialReference,d,m,e.lrucache,e.interceptor);await F.load();let A=F.relationshipMetaData();if(A=A.filter((e=>e.id===g[0].id)),!1===o.hasField(g[0].keyField)||null===o.field(g[0].keyField)){const e=await h.getFeatureByObjectId(o.field(h.objectIdField),[g[0].keyField]);if(e){const t=q.create(A[0].keyField+"= @id",F.getFieldsIndex());return t.parameters={id:e.attributes[g[0].keyField]},F.filter(t)}return new z({parentfeatureset:F})}const N=q.create(A[0].keyField+"= @id",F.getFieldsIndex());return N.parameters={id:o.field(g[0].keyField)},F.filter(N)}))},_.signatures.push({name:"featuresetbyrelationshipname",min:2,max:4}),_.functions.featuresetbyassociation=function(e,t){return _.standardFunctionAsync(e,t,(async(n,r,s)=>{p(s,2,3,e,t);const o=s[0],l=w(E(s[1],"")).toLowerCase(),c=h(s[2])?w(s[2]):null;if(null===s[0])return null;if(!x(s[0]))throw new i(e,a.InvalidParameter,t);let u=o._layer;if(u instanceof B&&(u=f(u,e.spatialReference,["*"],!0,e.lrucache,e.interceptor)),null===u)return null;if(!1===y(u))return null;await u.load();const m=u.serviceUrl(),g=await d(m,e.spatialReference);let I=null,F=null,D=!1;if(null!==c&&""!==c&&void 0!==c){for(const e of g.terminals)e.terminalName===c&&(F=e.terminalId);null===F&&(D=!0)}const b=g.associations.getFieldsIndex(),A=b.get("TOGLOBALID").name,N=b.get("FROMGLOBALID").name,S=b.get("TOTERMINALID").name,T=b.get("FROMTERMINALID").name,$=b.get("FROMNETWORKSOURCEID").name,M=b.get("TONETWORKSOURCEID").name,O=b.get("ASSOCIATIONTYPE").name,k=b.get("ISCONTENTVISIBLE").name,z=b.get("OBJECTID").name;for(const e of u.fields)if("global-id"===e.type){I=o.field(e.name);break}let H=null,G=new v(new V({name:"percentalong",alias:"percentalong",type:"double"}),q.create("0",g.associations.getFieldsIndex())),W=new v(new V({name:"side",alias:"side",type:"string"}),q.create("''",g.associations.getFieldsIndex()));const _="globalid",U="globalId",Q={};for(const e in g.lkp)Q[e]=g.lkp[e].sourceId;const J=new P(new V({name:"classname",alias:"classname",type:"string"}),null,Q);let K="";switch(l){case"midspan":{K=`((${A}='${I}') OR ( ${N}='${I}')) AND (${O} IN (5))`,J.codefield=q.create(`CASE WHEN (${A}='${I}') THEN ${$} ELSE ${M} END`,g.associations.getFieldsIndex());const e=Z(j.findField(g.associations.fields,N));e.name=_,e.alias=_,H=new v(e,q.create(`CASE WHEN (${N}='${I}') THEN ${A} ELSE ${N} END`,g.associations.getFieldsIndex())),G=g.unVersion>=4?new C(j.findField(g.associations.fields,b.get("PERCENTALONG").name)):new v(new V({name:"percentalong",alias:"percentalong",type:"double"}),q.create("0",g.associations.getFieldsIndex()));break}case"junctionedge":{K=`((${A}='${I}') OR ( ${N}='${I}')) AND (${O} IN (4,6))`,J.codefield=q.create(`CASE WHEN (${A}='${I}') THEN ${$} ELSE ${M} END`,g.associations.getFieldsIndex());const e=Z(j.findField(g.associations.fields,N));e.name=_,e.alias=_,H=new v(e,q.create(`CASE WHEN (${N}='${I}') THEN ${A} ELSE ${N} END`,g.associations.getFieldsIndex())),W=new v(new V({name:"side",alias:"side",type:"string"}),q.create(`CASE WHEN (${O}=4) THEN 'from' ELSE 'to' END`,g.associations.getFieldsIndex()));break}case"connected":{let e=`${A}='@T'`,t=`${N}='@T'`;null!==F&&(e+=` AND ${S}=@A`,t+=` AND ${T}=@A`),K="(("+e+") OR ("+t+"))",K=L(K,"@T",I??""),e=L(e,"@T",I??""),null!==F&&(e=L(e,"@A",F.toString()),K=L(K,"@A",F.toString())),J.codefield=q.create("CASE WHEN "+e+` THEN ${$} ELSE ${M} END`,g.associations.getFieldsIndex());const n=Z(j.findField(g.associations.fields,N));n.name=_,n.alias=_,H=new v(n,q.create("CASE WHEN "+e+` THEN ${N} ELSE ${A} END`,g.associations.getFieldsIndex()));break}case"container":K=`${A}='${I}' AND ${O} = 2`,null!==F&&(K+=` AND ${S} = `+F.toString()),J.codefield=$,K="( "+K+" )",H=new R(j.findField(g.associations.fields,N),_,_);break;case"content":K=`(${N}='${I}' AND ${O} = 2)`,null!==F&&(K+=` AND ${T} = `+F.toString()),J.codefield=M,K="( "+K+" )",H=new R(j.findField(g.associations.fields,A),_,_);break;case"structure":K=`(${A}='${I}' AND ${O} = 3)`,null!==F&&(K+=` AND ${S} = `+F.toString()),J.codefield=$,K="( "+K+" )",H=new R(j.findField(g.associations.fields,N),_,U);break;case"attached":K=`(${N}='${I}' AND ${O} = 3)`,null!==F&&(K+=` AND ${T} = `+F.toString()),J.codefield=M,K="( "+K+" )",H=new R(j.findField(g.associations.fields,A),_,U);break;default:throw new i(e,a.InvalidParameter,t)}D&&(K="1 <> 1");return new j({parentfeatureset:g.associations,adaptedFields:[new C(j.findField(g.associations.fields,z)),new C(j.findField(g.associations.fields,k)),H,W,J,G],extraFilter:K?q.create(K,g.associations.getFieldsIndex()):null})}))},_.signatures.push({name:"featuresetbyassociation",min:2,max:6}),_.functions.groupby=function(e,t){return _.standardFunctionAsync(e,t,(async(r,s,o)=>{if(p(o,3,3,e,t),!y(o[0]))throw new i(e,a.InvalidParameter,t);const l=await o[0].load(),f=[],c=[];let u=!1,d=[];if(h(o[1]))d.push(o[1]);else if(o[1]instanceof n)d.push(o[1]);else if(b(o[1]))d=o[1];else{if(!A(o[1]))throw new i(e,a.InvalidParameter,t);d=o[1].toArray()}for(const m of d)if(h(m)){const e=q.create(w(m),l.getFieldsIndex()),t=!0===W(e)?w(m):"%%%%FIELDNAME";f.push({name:t,expression:e}),"%%%%FIELDNAME"===t&&(u=!0)}else{if(!(m instanceof n))throw new i(e,a.InvalidParameter,t);{const n=m.hasField("name")?m.field("name"):"%%%%FIELDNAME",r=m.hasField("expression")?m.field("expression"):"";if("%%%%FIELDNAME"===n&&(u=!0),!n)throw new i(e,a.InvalidParameter,t);f.push({name:n,expression:q.create(r||n,l.getFieldsIndex())})}}if(d=[],h(o[2]))d.push(o[2]);else if(b(o[2]))d=o[2];else if(A(o[2]))d=o[2].toArray();else{if(!(o[2]instanceof n))throw new i(e,a.InvalidParameter,t);d.push(o[2])}for(const m of d){if(!(m instanceof n))throw new i(e,a.InvalidParameter,t);{const n=m.hasField("name")?m.field("name"):"",r=m.hasField("statistic")?m.field("statistic"):"",s=m.hasField("expression")?m.field("expression"):"";if(!n||!r||!s)throw new i(e,a.InvalidParameter,t);c.push({name:n,statistic:r.toLowerCase(),expression:q.create(s,l.getFieldsIndex())})}}if(u){const e={};for(const n of l.fields)e[n.name.toLowerCase()]=1;for(const n of f)"%%%%FIELDNAME"!==n.name&&(e[n.name.toLowerCase()]=1);for(const n of c)"%%%%FIELDNAME"!==n.name&&(e[n.name.toLowerCase()]=1);let t=0;for(const n of f)if("%%%%FIELDNAME"===n.name){for(;1===e["field_"+t.toString()];)t++;e["field_"+t.toString()]=1,n.name="FIELD_"+t.toString()}}for(const t of f)await J(t.expression,_,e);for(const t of c)await J(t.expression,_,e);return o[0].groupby(f,c)}))},_.signatures.push({name:"groupby",min:3,max:3}),_.functions.distinct=function(e,t){return _.standardFunctionAsync(e,t,(async(r,s,o)=>{if(y(o[0])){p(o,2,2,e,t);const r=await o[0].load(),s=[];let l=[];if(h(o[1]))l.push(o[1]);else if(o[1]instanceof n)l.push(o[1]);else if(b(o[1]))l=o[1];else{if(!A(o[1]))throw new i(e,a.InvalidParameter,t);l=o[1].toArray()}let f=!1;for(const o of l)if(h(o)){const e=q.create(w(o),r.getFieldsIndex()),t=!0===W(e)?w(o):"%%%%FIELDNAME";s.push({name:t,expression:e}),"%%%%FIELDNAME"===t&&(f=!0)}else{if(!(o instanceof n))throw new i(e,a.InvalidParameter,t);{const n=o.hasField("name")?o.field("name"):"%%%%FIELDNAME",l=o.hasField("expression")?o.field("expression"):"";if("%%%%FIELDNAME"===n&&(f=!0),!n)throw new i(e,a.InvalidParameter,t);s.push({name:n,expression:q.create(l||n,r.getFieldsIndex())})}}if(f){const e={};for(const n of r.fields)e[n.name.toLowerCase()]=1;for(const n of s)"%%%%FIELDNAME"!==n.name&&(e[n.name.toLowerCase()]=1);let t=0;for(const n of s)if("%%%%FIELDNAME"===n.name){for(;1===e["field_"+t.toString()];)t++;e["field_"+t.toString()]=1,n.name="FIELD_"+t.toString()}}for(const t of s)await J(t.expression,_,e);return o[0].groupby(s,[])}return Q("distinct",r,s,o)}))})}export{ie as registerFunctions};