/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{__decorate as e}from"tslib";import{ClonableMixin as t}from"../core/Clonable.js";import{JSONSupport as s}from"../core/JSONSupport.js";import{addMaybe as o,addMany as r}from"../core/SetUtils.js";import{property as n,cast as i,subclass as l}from"../core/accessorSupport/decorators.js";import{reader as p}from"../core/accessorSupport/decorators/reader.js";import m from"./ExpressionInfo.js";import a from"./elements/GroupElement.js";import{ensureType as f,fromJSON as c,toJSON as d,isTextElement as u,isGroupElement as h,isFieldElement as y,buildTypeMaps as E}from"./support/formUtils.js";import{collectArcadeFieldNames as j,collectFields as x,extractSubstitutionTemplatesFromString as w,collectField as S}from"../layers/support/fieldUtils.js";import{writer as v}from"../core/accessorSupport/decorators/writer.js";const b=E(a);let g=class extends(t(s)){constructor(e){super(e),this.description=null,this.expressionInfos=null,this.preserveFieldValuesWhenHidden=!1,this.supportsAttachmentElements=!1,this.title=null}get elements(){return this._get("elements")??null}set elements(e){this._set("elements",e)}castElements(e){return f(e,b)}readElements(e,t){return c(t.formElements,b)}writeElements(e,t){t.formElements=d(e,b)}async getFieldsUsed(e,t){const s=new Set,{description:o,elements:r,expressionInfos:n,title:i}=this;if(U(s,e,o),U(s,e,i),!r)return[];const l=F(r,n).map(t=>j(s,e,null,t));await Promise.all(l);for(const p of r)I(s,{fieldsIndex:e,relationships:t},p);return Array.from(s).sort()}};function I(e,t,s){const{fieldsIndex:o}=t;if(o?.fields.length!==e.size)switch(U(e,o,s.label),U(e,o,s.description),s.type){case"field":S(e,o,s.fieldName);break;case"group":s.elements.forEach(s=>I(e,t,s));break;case"relationship":if(t.relationships){const r=t.relationships.find(e=>e.id===s.relationshipId);r&&S(e,o,r.keyField)}x(e,o,s.orderByFields?.map(e=>e.field));break;case"text":U(e,o,s.text)}}function F(e,t){if(!t||0===t.length)return[];const s=k(e),o=[];for(const r of t)s.has(r.name)&&o.push(r.expression);return o}function k(e){const t=new Set;for(const s of e)if(o(t,s.visibilityExpression),!u(s))if(h(s))r(t,k(s.elements));else if(o(t,s.editableExpression),y(s)){const{requiredExpression:e,valueExpression:o}=s;r(t,[e,o])}return t}function U(e,t,s){x(e,t,w(s))}e([n({type:String,json:{write:!0}})],g.prototype,"description",void 0),e([n({json:{write:!0}})],g.prototype,"elements",null),e([i("elements")],g.prototype,"castElements",null),e([p("elements",["formElements"])],g.prototype,"readElements",null),e([v("elements")],g.prototype,"writeElements",null),e([n({type:[m],json:{write:!0}})],g.prototype,"expressionInfos",void 0),e([n({type:Boolean,json:{default:!1,write:!0}})],g.prototype,"preserveFieldValuesWhenHidden",void 0),e([n({type:Boolean,json:{default:!1,write:!0}})],g.prototype,"supportsAttachmentElements",void 0),e([n({type:String,json:{write:!0}})],g.prototype,"title",void 0),g=e([l("esri.form.FormTemplate")],g);export{g as default};
