/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import e from"../../../../core/Error.js";import r from"../../../../core/Logger.js";import{getEffectiveFieldAlias as t}from"../../../../editing/fieldUtils.js";import n from"../../../../form/ExpressionInfo.js";import{isFieldElement as o,isGroupElement as i,isTextElement as s}from"../../../../form/support/formUtils.js";import{getLowerCaseEditTrackingFields as a,isFieldVisibleByDefault as l}from"../../../../layers/support/fieldUtils.js";import{isSubtypeSublayer as m,isSubtypeGroupLayer as d}from"../../../../layers/support/layerUtils.js";import{unknown as p}from"../../../../time/constants.js";import{makeExpressionInfosMap as f,makeExecutorSetForFormElement as c}from"../../expressions/expressionUtils.js";import u from"../BatchFormTemplate.js";import{FieldElementTemplate as x}from"../FieldElementTemplate.js";import{GroupElementTemplate as y}from"../GroupElementTemplate.js";import{TextElementTemplate as w}from"../TextElementTemplate.js";const b="expression/dea57012-47ca4b55a000-8df62742ed0c",E=()=>r.getLogger("esri.widgets.BatchAttributeForm.templates.templateUtils");async function T(e,r,t=e.formTemplate){return t?.elements&&t.elements.length>0?F(t,e,r):v(e,r)}async function v(e,{arcadeExecutorProvider:r,formTimeZone:t}){const n=new Set(a(e)),o={arcadeExecutorProvider:r,formTimeZone:t,layer:e},i=[];for(const a of e.fields)P(a,e,n)&&i.push(Z(a,o));const s=await Promise.all(i);return new u({elements:s,preserveFieldValuesWhenHidden:!0})}function h(e,r){for(const t of e??[])if("group"!==t.type){if("field"===t.type){if(!1===t.editable&&!t.editableExpression)return!0;if(r.subtypeField&&(m(r)||d(r))&&t.fieldName?.toLowerCase()===r.subtypeField.toLowerCase())return!0}}else if(h(t.elements??[],r))return!0;return!1}async function F(e,r,t){let o=null;h(e.elements??[],r)&&(o=new n({title:"",name:b,expression:"false"}));const i=f(o?[...e.expressionInfos??[],o]:e.expressionInfos),s={description:e.description,elements:new Array,title:e.title,preserveFieldValuesWhenHidden:e.preserveFieldValuesWhenHidden},{elements:a}=e;if(!a)return new u(s);const{arcadeExecutorProvider:l,formTimeZone:m}=t;for(const n of a){const e=await I(n,{arcadeExecutorProvider:l,expressionInfos:i,layer:r,formTimeZone:m});e&&s.elements.push(e)}return new u(s)}async function I(r,t){return o(r)?g(r,t):i(r)?j(r,t):s(r)?L(r,t):(E().warn(new e("batch-attribute-form:unsupported-element-type",`Form elements of type ${r.type} are not supported`)),null)}async function g(r,{arcadeExecutorProvider:t,expressionInfos:n,layer:o,formTimeZone:i}){const{fieldsIndex:s}=o,{description:a,domain:l,fieldName:p,hint:f,input:u,label:y}=r;if(null==p||!s.has(p))return E().warn(new e("batch-attribute-form:invalid-form-element-field",`Field element with label '${y}' does not refer to a valid field`)),null;const w={description:a,domain:l,field:s.get(p),hint:f,input:u,formTimeZone:i,layerTimeZone:U(o),label:y};!1!==r.editable||r.editableExpression||((r=r.clone()).editableExpression=b),o.subtypeField&&(m(o)||d(o))&&r.fieldName?.toLowerCase()===o.subtypeField.toLowerCase()&&((r=r.clone()).editableExpression=b);const T=new x(w),v=await c({element:r,expressionInfos:n,provider:t});return T.addLayer(o,v),T}async function j(e,r){const{arcadeExecutorProvider:t,expressionInfos:n,layer:o}=r,{description:i,label:s,initialState:a}=e,l={description:i,elements:[],label:s,initialState:a??"expanded"};for(const p of e.elements){const e=await I(p,r);e&&l.elements.push(e)}const m=new y(l),d=await c({element:e,expressionInfos:n,provider:t});return m.addLayer(o,d),m}async function L(e,{arcadeExecutorProvider:r,expressionInfos:t,layer:n}){const{description:o,label:i,text:s}=e,a=new w({description:o,label:i,text:s}),l=await c({element:e,expressionInfos:t,provider:r});return a.addLayer(n,l),a}async function Z(e,{arcadeExecutorProvider:r,layer:n,formTimeZone:o}){const i=t(e,n)||e.name,s=new x({field:e,label:i,formTimeZone:o,layerTimeZone:U(n)}),a={},l=(d(n)||m(n))&&n.subtypeField&&n.subtypeField.toLowerCase()===e.name.toLowerCase();return!l&&e.editable&&n.userHasUpdateItemPrivileges&&(a.editableExpression=await r.getArcadeExecutor("true")),l&&(a.editableExpression=await r.getArcadeExecutor("false")),s.addLayer(n,a),s}function P(e,r,t){return!t.has(e.name.toLowerCase())&&l(e,r)}function U(e){return m(e)&&e.parent&&(e=e.parent),e&&"datesInUnknownTimezone"in e&&e.datesInUnknownTimezone?p:e&&"preferredTimeZone"in e&&e.preferredTimeZone?e.preferredTimeZone:null}export{T as createBatchFormTemplate,v as createBatchFormTemplateFromFields};
