/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{unique as e}from"../../../core/arrayUtils.js";import"../../../core/has.js";import{fetchMessageBundle as i}from"../../../intl/messages.js";import{substitute as n}from"../../../intl/substitute.js";import{numericTypes as t}from"../../../layers/support/fieldUtils.js";import{FieldsContent as r,TextContent as s}from"../../../popup/content.js";import o from"../../../popup/ExpressionInfo.js";import a from"../../../popup/FieldInfo.js";import l from"../../../popup/support/FieldInfoFormat.js";import{getAttributes as p}from"../../../renderers/support/utils.js";import{viewScaleRE as f}from"../../../renderers/visualVariables/support/visualVariableUtils.js";import{getFeatureReductionFields as u}from"../../support/adapters/support/utils.js";let m=0;function d(e=0){}const c="expression/";function b(e){return"hasVisualVariables"in e&&e.hasVisualVariables()?e.visualVariables.filter(e=>!f.test(e.valueExpression)&&(!("target"in e)||"outline"!==e.target)):[]}function g(e,i){if(!i)return null;const n=u(e);return n?.find(e=>e.name.toLowerCase()===i.toLowerCase())}function x(e,i,n){let r=null;if(n&&"featureReduction"in e){const i=e.featureReduction;i&&"popupTemplate"in i&&i.popupTemplate&&(r=i.popupTemplate.fieldInfos)}else"popupTemplate"in e&&e.popupTemplate&&(r=e.popupTemplate.fieldInfos);const s=n?g(e,i):e.getField(i);let o=null;if(r&&r.some(e=>!(!e||e.fieldName?.toLowerCase()!==s?.name.toLowerCase())&&(o=e.clone(),!0)),!o){const e=t.includes(s.type),i="integer"===s.type||"small-integer"===s.type;o=new a({fieldName:s.name,isEditable:s.editable,visible:!0,format:e?new l({places:i?0:2,digitSeparator:!0}):void 0})}return o.label||(o.label=s.alias),o}function F(e){const{expression:i,title:n,returnType:t}=e;return new o({name:"expr"+m++,expression:i,title:n,returnType:t})}function $(e){const i="number"===e.returnType?new l({places:2,digitSeparator:!0}):void 0;return new a({fieldName:`${c}${e.name}`,visible:!0,format:i})}async function y(t){const r=await i("esri/smartMapping/t9n/smartMapping"),{renderer:s,layer:o,normFieldExpressionTemplate:a,isFeatureReduction:l}=t,f=[],u=[],m=p(s,b);for(const e of m)if("field"===e.type)f.push(x(o,e.field,l));else if("normalized-field"===e.type){const i=l?g(o,e.field):o.getField(e.field),t=l?g(o,e.normalizationField):o.getField(e.normalizationField),s=F({expression:`\n      $feature["${i.name}"];\n      $feature["${t.name}"];\n      ${"percentage"===a?`($feature["${i.name}"] / $feature["${t.name}"]) * 100;`:`$feature["${i.name}"] / $feature["${t.name}"];`}\n      `,title:n("percentage"===a?r.normFieldLabelAsPercent:r.normFieldLabel,{expression1:i.alias,expression2:t.alias}),returnType:"number"});f.push($(s),x(o,e.field,l),x(o,e.normalizationField,l)),u.push(s)}else if("expression"===e.type){const i=F(e);f.push($(i)),u.push(i)}return{fieldInfos:e(f,(e,i)=>e.fieldName===i.fieldName),expressionInfos:e(u,(e,i)=>e.expression===i.expression)}}async function w(e,t,o){const{fieldInfos:a,expressionInfos:l}=t,p=await i("esri/smartMapping/t9n/smartMapping");if(a.length>2)return[new r({fieldInfos:a})];const f=[];for(const i of a){const t=i.fieldName||"";let r=i.label;if(!r){const i=o?g(e,t):e.getField(t);if(i)r=i.alias||t;else if(l){const e=t.split(c)[1],i=l[l.findIndex(i=>i.name===e)];i&&(r=i.title||i.name)}}f.push(new s({text:n(p.fieldInfo,{fieldLabel:r,fieldValue:`{${t}}`})}))}return f}function V(e){return!(!("normalizationField"in e)||!e.normalizationField)||"hasVisualVariables"in e&&e.hasVisualVariables()&&e.visualVariables.some(e=>!(!("normalizationField"in e)||!e.normalizationField))}export{c as expressionFieldPrefix,w as getContentFromFieldInfos,F as getExpressionInfo,y as getFieldAndExpressionInfos,x as getFieldInfo,$ as getFieldInfoFromExpressionInfo,b as getPrimaryVisualVariables,V as hasNormalizedField,d as resetSuffixForTests};
