/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["require","exports","../../../chunks/_rollupPluginBabelHelpers","../../../core/Logger","../../../core/promiseUtils","../../../layers/FeatureLayer","./featureUtils"],(function(e,r,t,a,i,n,s){"use strict";const o=["$datastore","$map","$layer","$aggregatedfeatures"],c="esri.widgets.Feature.support.arcadeFeatureUtils",l=a.getLogger(c);function p(e){return"string"==typeof e?s.applyTextFormattingHTML(s.htmlEntities(e)):Array.isArray(e)?u(e):"esri.arcade.Dictionary"===(null==e?void 0:e.declaredClass)?y(e):e}function u(e){return`<ul class="esri-widget__list">${e.map((e=>`<li>${"string"==typeof e?s.applyTextFormattingHTML(s.htmlEntities(e)):e}</li>`)).join("")}</ul>`}function y(e){return`<table class="esri-widget__table">${e.keys().map((r=>{const t=e.field(r);return`<tr><th>${r}</th><td>${"string"==typeof t?s.applyTextFormattingHTML(s.htmlEntities(t)):t}</td></tr>`})).join("")}</table>`}function f({aggregatedFeatures:e,arcadeUtils:r,featureSetVars:t,context:a,viewInfo:i,map:s,graphic:o,interceptor:c}){t.forEach((t=>{const l=t.toLowerCase(),p={map:s,spatialReference:i.sr,interceptor:c};if("$map"===l&&(a.vars[l]=r.convertMapToFeatureSetCollection(p)),"$layer"===l&&(a.vars[l]=r.convertFeatureLayerToFeatureSet({layer:o.sourceLayer,spatialReference:i.sr,interceptor:c})),"$datastore"===l&&(a.vars[l]=r.convertServiceUrlToWorkspace({url:o.sourceLayer.url,spatialReference:i.sr,interceptor:c})),"$aggregatedfeatures"===l){const t=o.layer,{fields:s,objectIdField:p,geometryType:u,spatialReference:y,displayField:f}=t,d=new n({fields:s,objectIdField:p,geometryType:u,spatialReference:y,displayField:f,..."feature"===t.type?{templates:t.templates,typeIdField:t.typeIdField,types:t.types}:null,source:e});a.vars[l]=r.convertFeatureLayerToFeatureSet({layer:d,spatialReference:i.sr,interceptor:c})}}))}function d(){return new Promise(((r,t)=>e(["../../../support/arcadeUtils"],r,t)))}function g(e){return m.apply(this,arguments)}function m(){return(m=t._asyncToGenerator((function*({graphic:e,view:r}){const{isAggregate:t,layer:a}=e;if(!t||!a||"2d"!==(null==r?void 0:r.type))return[];const i=yield r.whenLayerView(a);if(!i.createQuery||!i.queryFeatures)return[];const n=i.createQuery();n.aggregateIds=[e.getObjectId()];const{features:s}=yield i.queryFeatures(n);return s}))).apply(this,arguments)}function h(e){return F.apply(this,arguments)}function F(){return(F=t._asyncToGenerator((function*({expressionInfo:e,arcadeUtils:r,interceptor:t,spatialReference:a,map:i,graphic:n,view:s}){if(!e||!e.expression)return null;const c=r.createSyntaxTree(e.expression),p=o.filter((e=>r.hasVariable(c,e))),[u]=yield Promise.all([g({graphic:n,view:s}),r.loadScriptDependencies(c,!0,p)]),y=r.getViewInfo({spatialReference:a}),d=r.createExecContext(n,y);d.interceptor=t,d.useAsync=!0,f({aggregatedFeatures:u,arcadeUtils:r,featureSetVars:p,context:d,viewInfo:y,map:i,graphic:n,interceptor:t});const m=r.createFunction(c,d);return r.executeAsyncFunction(m,d).catch((r=>l.error("arcade-execution-error",{error:r,graphic:n,expressionInfo:e})))}))).apply(this,arguments)}function v(e){return x.apply(this,arguments)}function x(){return(x=t._asyncToGenerator((function*({expressionInfos:e,spatialReference:r,graphic:t,interceptor:a,map:n,view:s}){if(!e||!e.length)return{};const o=yield d(),c={};for(const i of e)c[`expression/${i.name}`]=h({expressionInfo:i,arcadeUtils:o,interceptor:a,spatialReference:r,map:n,graphic:t,view:s});const l=yield i.eachAlways(c),u={};for(const i in l)u[i]=p(l[i].value);return u}))).apply(this,arguments)}r.createCompiledExpression=h,r.createCompiledExpressions=v,r.loadArcadeUtils=d,Object.defineProperties(r,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
