/*
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/Error","../../core/Logger","../../intl/date","../../intl/number","./fieldUtils","./labelUtils","../../support/arcadeOnDemand"],(function(e,r,t,n,a,l,i,u,o,s){"use strict";const c=a.getLogger("esri.layers.support.labelFormatUtils"),d={type:"simple",evaluate:()=>null},p={getAttribute:(e,r)=>e.field(r)};function f(e,r,t){return g.apply(this,arguments)}function g(){return(g=t._asyncToGenerator((function*(r,t,a){if(!r||!r.symbol)return d;const l=r.where,i=o.getLabelExpression(r),u=l?yield new Promise(((r,t)=>e(["../../core/sql/WhereClause"],r,t))):null;let f;if("arcade"===i.type){const e=yield s.createLabelExpression(i.expression,a,t);f={type:"arcade",evaluate(r){try{const t=e.evaluate({$feature:"attributes"in r?e.repurposeFeature(r):r});if(null!=t)return t.toString()}catch(t){c.error(new n("arcade-expression-error","Encountered an error when evaluating label expression for feature",{feature:r,expression:i}))}return null},needsHydrationToEvaluate:()=>null==o.getSingleFieldArcadeExpression(i.expression)}}else f={type:"simple",evaluate:e=>i.expression.replace(/{[^}]*}/g,(r=>{const n=r.slice(1,-1),a=t.get(n);if(!a)return r;let l=null;if("attributes"in e){e&&e.attributes&&(l=e.attributes[a.name])}else l=e.field(a.name);return null==l?"":y(l,a)}))};if(l){let e;try{e=u.WhereClause.create(l,t)}catch(g){return c.error(new n("bad-where-clause","Encountered an error when evaluating where clause, ignoring",{where:l,error:g})),d}const r=f.evaluate;f.evaluate=t=>{const a="attributes"in t?void 0:p;try{if(e.testFeature(t,a))return r(t)}catch(g){c.error(new n("bad-where-clause","Encountered an error when evaluating where clause for feature",{where:l,feature:t,error:g}))}return null}}return f}))).apply(this,arguments)}function y(e,r){if(null==e)return"";const t=r.domain;if(t)if("codedValue"===t.type||"coded-value"===t.type){const r=e;for(const e of t.codedValues)if(e.code===r)return e.name}else if("range"===t.type){const r=+e,n="range"in t?t.range[0]:t.minValue,a="range"in t?t.range[1]:t.maxValue;if(n<=r&&r<=a)return t.name}let n=e;return"date"===r.type||"esriFieldTypeDate"===r.type?n=l.formatDate(n,l.convertDateFormatToIntlOptions("short-date")):u.isNumericField(r)&&(n=i.formatNumber(+n)),n||""}r.createLabelFunction=f,r.formatField=y,Object.defineProperties(r,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
