/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import e from"../../../../core/Error.js";import t from"../../../../core/Logger.js";import{getEffectiveFieldAlias as r}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 w}from"../GroupElementTemplate.js";import{TextElementTemplate as y}from"../TextElementTemplate.js";import{compileTextElementMarkdownToHTML as b}from"./textElementTemplateUtils.js";const E="expression/dea57012-47ca4b55a000-8df62742ed0c",T=()=>t.getLogger("esri.widgets.BatchAttributeForm.templates.templateUtils");async function h(e,t,r=e.formTemplate){return r?.elements&&r.elements.length>0?I(r,e,t):v(e,t)}async function v(e,{arcadeExecutorProvider:t,formTimeZone:r}){const n=new Set(a(e)),o={arcadeExecutorProvider:t,formTimeZone:r,layer:e},i=[];for(const a of e.fields)U(a,e,n)&&i.push(P(a,o));const s=await Promise.all(i);return new u({elements:s,preserveFieldValuesWhenHidden:!0})}function g(e,t){for(const r of e??[])if("group"!==r.type){if("field"===r.type){if(!1===r.editable&&!r.editableExpression)return!0;if(t.subtypeField&&(m(t)||d(t))&&r.fieldName?.toLowerCase()===t.subtypeField.toLowerCase())return!0}}else if(g(r.elements??[],t))return!0;return!1}async function I(e,t,r){let o=null;g(e.elements??[],t)&&(o=new n({title:"",name:E,expression:"false"}));const i=f(o?[...e.expressionInfos??[],o]:e.expressionInfos),s={description:e.description,elements:new Array,title:e.title,preserveFieldValuesWhenHidden:e.preserveFieldValuesWhenHidden,supportsAttachmentElements:e.supportsAttachmentElements},{elements:a}=e;if(!a)return new u(s);const{arcadeExecutorProvider:l,formTimeZone:m}=r;for(const n of a){const e=await F(n,{arcadeExecutorProvider:l,expressionInfos:i,layer:t,formTimeZone:m});e&&s.elements.push(e)}return new u(s)}async function F(t,r){return o(t)?j(t,r):i(t)?L(t,r):s(t)?Z(t,r):(T().warn(new e("batch-attribute-form:unsupported-element-type",`Form elements of type ${t.type} are not supported`)),null)}async function j(t,{arcadeExecutorProvider:r,expressionInfos:n,layer:o,formTimeZone:i}){const{fieldsIndex:s}=o,{description:a,domain:l,fieldName:p,hint:f,input:u,label:w}=t;if(null==p||!s.has(p))return T().warn(new e("batch-attribute-form:invalid-form-element-field",`Field element with label '${w}' does not refer to a valid field`)),null;!1!==t.editable||t.editableExpression||((t=t.clone()).editableExpression=E),o.subtypeField&&(m(o)||d(o))&&t.fieldName?.toLowerCase()===o.subtypeField.toLowerCase()&&((t=t.clone()).editableExpression=E);const y=new x({description:a,domain:l,field:s.get(p),hint:f,input:u,formTimeZone:i,layerTimeZone:A(o),label:w}),b=await c({element:t,expressionInfos:n,provider:r});return y.addLayer(o,b),y}async function L(e,t){const{arcadeExecutorProvider:r,expressionInfos:n,layer:o}=t,{description:i,label:s,initialState:a}=e,l=[];for(const p of e.elements){const e=await F(p,t);e&&l.push(e)}const m=new w({description:i,elements:l,label:s,initialState:a??"expanded"}),d=await c({element:e,expressionInfos:n,provider:r});return m.addLayer(o,d),m}async function Z(e,{arcadeExecutorProvider:t,expressionInfos:r,layer:n}){const{description:o,label:i,text:s,textFormat:a}=e,{fieldsIndex:l}=n,m=s&&"markdown"===a?await b(s):s,d=new y({compiledText:m,description:o,fieldsIndex:l,label:i}),p=await c({element:e,expressionInfos:r,provider:t}),{expressionParts:f}=d;if(f.length){const e=await Promise.all(f.map(e=>{const n=r.get(e)?.expression??e;return t.getArcadeExecutor(n)}));p.textExpressions=e}return d.addLayer(n,p),d}async function P(e,{arcadeExecutorProvider:t,layer:n,formTimeZone:o}){const i=r(e,n)||e.name,s=new x({field:e,label:i,formTimeZone:o,layerTimeZone:A(n)}),a={},l=(d(n)||m(n))&&n.subtypeField&&n.subtypeField.toLowerCase()===e.name.toLowerCase();return!l&&e.editable&&n.userHasUpdateItemPrivileges&&(a.editableExpression=await t.getArcadeExecutor("true")),l&&(a.editableExpression=await t.getArcadeExecutor("false")),s.addLayer(n,a),s}function U(e,t,r){return!r.has(e.name.toLowerCase())&&l(e,t)}function A(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{h as createBatchFormTemplate,v as createBatchFormTemplateFromFields};
