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