/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{clone as n}from"../../core/lang.js";import e from"./FieldConfiguration.js";import{fieldFormatFromFieldInfo as o,isFormattableField as t,defaultFieldFormat as s,fieldInfoFormatFromFieldFormat as f}from"./fieldFormatUtils.js";import{isFieldEditable as i}from"./fieldUtils.js";import l from"../../popup/FieldInfo.js";function r(n){const{popupTemplate:t,fieldsIndex:s}=n,f=t?.fieldInfos;if(!f?.length||!s)return;const i=[];for(const l of f){const{fieldName:n,label:t}=l,f=s.get(n);if(!f)continue;const r=t&&t!==f.alias?t:null,a=o(l,f);(r||a)&&i.push(new e({name:f.name,alias:r,fieldFormat:a}))}return i.length?i:null}function a(n){const{fields:o}=n;if(!o?.length)return;const f=[];for(const i of o){const n=t(i)?s(i):null;n&&f.push(new e({name:i.name,fieldFormat:n}))}return f.length?f:null}function u(n,e){const{popupTemplate:o,fieldsIndex:t}=n;if(!o||!t)return;const s=c(n,e,o.fieldInfos);if(!s)return;const f=o.clone();return f.fieldInfos=s,f}function c(e,o,s){o??=[],s=n(s)??[];let i=!1;const l=new Map;for(const n of o){const o=e.fieldsIndex.get(n.name);o&&l.set(o.name,n)}for(const n of s){const o=e.fieldsIndex.get(n.fieldName);if(!o)continue;const s=l.get(o.name);n.label=s?.alias||o.alias,n.format=t(o)&&s?.fieldFormat?f(s.fieldFormat,o):null,i=!0,l.delete(o.name)}for(const n of l.values()){const o=m(e,n);o&&(s.push(o),i=!0)}return i?s:null}function m(n,e){const{name:o,alias:s,fieldFormat:r}=e,a=n.fieldsIndex.get(o);if(!a||!s&&!r)return;const u=s||a.alias,c=t(a)&&r?f(r,a):null;return new l({fieldName:a.name,label:u,format:c,visible:!1,isEditable:i(a,n)})}function d(n){const e=new Map;for(const o of n)e.set(o.name.toLowerCase(),o);return e}function p(e,o){if(!o.length)return;const{fieldConfigurations:t}=e,s=t?d(t):null;let f;if(s?.size){f=[];for(const n of o)s.has(n.name.toLowerCase())||f.push(n);if(!f.length)return}else f=o;const i=n(t)??[];i.push(...f),e.fieldConfigurations=i}function g(n,e){const{fieldConfigurations:o}=n;if(!e.length||!o?.length)return;const t=d(e),s=[];let f=!1;for(const i of o){const n=t.get(i.name.toLowerCase());n?(s.push(n),f=!0):s.push(i.clone())}f&&(n.fieldConfigurations=s)}function h(n,e){const{fieldConfigurations:o}=n;if(!e.length||!o?.length)return;const t=new Set(e.map(n=>n.toLowerCase())),s=[];let f=!1;for(const i of o)t.has(i.name.toLowerCase())?f=!0:s.push(i.clone());f&&(n.fieldConfigurations=s)}export{p as addFieldConfigurations,a as fieldConfigurationsFromFields,r as fieldConfigurationsFromPopupTemplate,u as popupTemplateWithFieldConfigurations,h as removeFieldConfigurations,g as replaceFieldConfigurations};
