/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.25/esri/copyright.txt for details. */ import{_ as e}from"../../chunks/tslib.es6.js";import t from"../../Graphic.js";import{FeatureSetQueryInterceptor as i}from"../../arcade/featureset/support/FeatureSetQueryInterceptor.js";import r from"../../core/Accessor.js";import o from"../../core/Handles.js";import{IdentifiableMixin as s}from"../../core/Identifiable.js";import n from"../../core/Logger.js";import{isSome as a}from"../../core/maybe.js";import{isAbortError as l,eachAlways as p}from"../../core/promiseUtils.js";import{watch as c,on as d,initial as h}from"../../core/reactiveUtils.js";import{throttle as u}from"../../core/throttle.js";import{property as f}from"../../core/accessorSupport/decorators/property.js";import"../../core/arrayUtils.js";import{cast as m}from"../../core/accessorSupport/decorators/cast.js";import{subclass as _}from"../../core/accessorSupport/decorators/subclass.js";import y from"../../popup/content/TextContent.js";import b from"./FeatureAttachments/FeatureAttachmentsViewModel.js";import g from"./FeatureContent/FeatureContentViewModel.js";import I from"./FeatureExpression/FeatureExpressionViewModel.js";import A from"./FeatureFields/FeatureFieldsViewModel.js";import M from"./FeatureMedia/FeatureMediaViewModel.js";import x from"./FeatureRelationship/FeatureRelationshipViewModel.js";import{createCompiledExpressions as w}from"./support/arcadeFeatureUtils.js";import{createfieldInfoMap as F,getAllFieldInfos as v,getSourceLayer as C,substituteFieldsInLinksAndAttributes as E,isExpressionField as T,isRelatedField as V,formatEditInfo as j,graphicCallback as P,queryUpdatedFeature as R,formatAttributes as L,preLayerQueryCallback as O,preRequestCallback as S}from"./support/featureUtils.js";import{queryLayerInfos as k,queryRelatedFeatures as U,setRelatedFeatures as D,getRelatedFieldInfo as N,createRelatedInfo as q,updateRelatedInfo as B}from"./support/relatedFeatureUtils.js";var Q;const G=1,H="content-view-models",z="relationship-view-models",J={attachmentsContent:!0,chartAnimation:!0,customContent:!0,expressionContent:!0,fieldsContent:!0,mediaContent:!0,textContent:!0,relationshipContent:!0};let K=Q=class extends(s(r)){constructor(e){super(e),this._handles=new o,this._error=null,this._featureAbortController=null,this._graphicChangedThrottled=u(this._graphicChanged,G,this),this._expressionAttributes=null,this._graphicExpressionAttributes=null,this.abilities={...J},this.content=null,this.contentViewModels=[],this.description=null,this.defaultPopupTemplateEnabled=!1,this.formattedAttributes=null,this.lastEditInfo=null,this.relatedInfos=new Map,this.title="",this.view=null,this._isAllowedContentType=e=>{const{abilities:t}=this;return"attachments"===e.type&&t.attachmentsContent||"custom"===e.type&&t.customContent||"fields"===e.type&&t.fieldsContent||"media"===e.type&&t.mediaContent||"text"===e.type&&t.textContent||"expression"===e.type&&t.expressionContent||"relationship"===e.type&&t.relationshipContent},this._handles.add(c((()=>[this.graphic,this._effectivePopupTemplate,this.abilities]),(()=>this._graphicChangedThrottled()),h))}destroy(){this._clear(),this._cancelFeatureQuery(),this._error=null,this._handles.destroy(),this._handles=null,this.graphic=null,this._destroyContentViewModels(),this.relatedInfos.clear()}get _effectivePopupTemplate(){return a(this.graphic)?this.graphic.getEffectivePopupTemplate(this.defaultPopupTemplateEnabled):null}get _fieldInfoMap(){return F(v(this._effectivePopupTemplate),this._sourceLayer)}get _sourceLayer(){return C(this.graphic)}castAbilities(e){return{...J,...e}}get state(){return this.graphic?this._error?"error":this.waitingForContent?"loading":"ready":"disabled"}set graphic(e){this._set("graphic",e?e.clone():null)}get spatialReference(){return this.view?.spatialReference??null}set spatialReference(e){this._override("spatialReference",e)}get map(){return this.view?.map||null}set map(e){this._override("map",e)}get waitingForContent(){return!!this._featureAbortController}setActiveMedia(e,t){const i=this.contentViewModels[e];i instanceof M&&i.setActiveMedia(t)}nextMedia(e){const t=this.contentViewModels[e];t instanceof M&&t.next()}previousMedia(e){const t=this.contentViewModels[e];t instanceof M&&t.previous()}_clear(){this._set("title",""),this._set("content",null),this._set("formattedAttributes",null)}async _graphicChanged(){this._cancelFeatureQuery(),this._error=null,this._clear();const{graphic:e}=this;if(!e)return;const t=new AbortController;this._featureAbortController=t;try{await this._queryFeature({signal:t.signal})}catch(i){l(i)||(this._error=i,n.getLogger(this.declaredClass).error("error","The popupTemplate could not be displayed for this feature.",{error:i,graphic:e,popupTemplate:this._effectivePopupTemplate}))}this._featureAbortController===t&&(this._featureAbortController=null)}_cancelFeatureQuery(){const{_featureAbortController:e}=this;e&&e.abort(),this._featureAbortController=null}_compileContentElement(e,t){return"attachments"===e.type?this._compileAttachments(e,t):"custom"===e.type?this._compileCustom(e,t):"fields"===e.type?this._compileFields(e,t):"media"===e.type?this._compileMedia(e,t):"text"===e.type?this._compileText(e,t):"expression"===e.type?this._compileExpression(e,t):"relationship"===e.type?this._compileRelationship(e,t):void 0}_compileContent(e){if(this._destroyContentViewModels(),this.graphic)return Array.isArray(e)?e.filter(this._isAllowedContentType).map(((e,t)=>this._compileContentElement(e,t))):"string"==typeof e?this._compileText(new y({text:e}),0).text:e}_destroyContentViewModels(){this._handles?.remove(z),this._handles?.remove(H),this.contentViewModels.forEach((e=>e&&!e.destroyed&&e.destroy())),this._set("contentViewModels",[])}_matchesFeature(e,t){const i=e?.graphic?.getObjectId(),r=t?.getObjectId();return a(i)&&a(r)&&i===r}_setRelatedFeaturesViewModels({relatedFeatureViewModels:e,relatedFeatures:t,map:i}){t?.filter(Boolean).forEach((t=>{e.find((e=>this._matchesFeature(e,t)))||e.add(new Q({abilities:{relationshipContent:!1},map:i,graphic:t}))})),e.forEach((i=>{const r=t?.find((e=>this._matchesFeature(i,e)));r||e.remove(i)}))}_setExpressionContentVM(e,t){const{formattedAttributes:i}=this,{contentElement:r,contentElementViewModel:o}=e,s=r?.type;o&&s&&("fields"===s&&(this._createFieldsFormattedAttributes({contentElement:r,contentElementIndex:t,formattedAttributes:i}),o.set(this._createFieldsVMParams(r,t))),"media"===s&&(this._createMediaFormattedAttributes({contentElement:r,contentElementIndex:t,formattedAttributes:i}),o.set(this._createMediaVMParams(r,t))),"text"===s&&o.set(this._createTextVMParams(r)))}_compileRelationship(e,t){const{displayCount:i,orderByFields:r,relationshipId:o,title:s,description:n}=e,{_sourceLayer:a,graphic:l,map:p}=this,c=new x({displayCount:i,graphic:l,orderByFields:r,relationshipId:o,layer:a,map:p,...this._compileTitleAndDesc({title:s,description:n})});return this.contentViewModels[t]=c,this._handles.add(d((()=>c.relatedFeatures),"change",(()=>this._setRelatedFeaturesViewModels(c))),z),e}_compileExpression(e,t){const{expressionInfo:i}=e,{graphic:r,map:o,spatialReference:s,view:n}=this,a=new I({expressionInfo:i,graphic:r,interceptor:Q.interceptor,map:o,spatialReference:s,view:n});return this.contentViewModels[t]=a,this._handles.add(c((()=>a.contentElementViewModel),(()=>this._setExpressionContentVM(a,t)),h),H),e}_compileAttachments(e,t){const{graphic:i}=this,{description:r,title:o}=e;return this.contentViewModels[t]=new b({graphic:i,...this._compileTitleAndDesc({title:o,description:r})}),e}_compileCustom(e,t){const{graphic:i}=this,{creator:r,destroyer:o}=e;return this.contentViewModels[t]=new g({graphic:i,creator:r,destroyer:o}),e}_compileTitleAndDesc({title:e,description:t}){const{_fieldInfoMap:i,_sourceLayer:r,graphic:o,formattedAttributes:s,_expressionAttributes:n}=this,{attributes:a}=o,l=s.global;return{title:E({attributes:a,fieldInfoMap:i,globalAttributes:l,expressionAttributes:n,layer:r,text:e}),description:E({attributes:a,fieldInfoMap:i,globalAttributes:l,expressionAttributes:n,layer:r,text:t})}}_createFieldsVMParams(e,t){const{_effectivePopupTemplate:i,formattedAttributes:r}=this,o={...r.global,...r.content[t]},s=(e?.fieldInfos||i?.fieldInfos)?.filter((({fieldName:e})=>T(e)||V(e)||o.hasOwnProperty(e))),n=i?.expressionInfos,{description:a,title:l}=e;return{attributes:o,expressionInfos:n,fieldInfos:s,...this._compileTitleAndDesc({title:l,description:a})}}_compileFields(e,t){const i=e.clone(),r=new A(this._createFieldsVMParams(e,t));return this.contentViewModels[t]=r,i.fieldInfos=r.formattedFieldInfos.slice(0),i}_createMediaVMParams(e,t){const{abilities:i,graphic:r,_fieldInfoMap:o,formattedAttributes:s,_effectivePopupTemplate:n,relatedInfos:a,_sourceLayer:l,_expressionAttributes:p}=this,{attributes:c}=r,{description:d,mediaInfos:h,title:u}=e;return{abilities:{chartAnimation:i.chartAnimation},activeMediaInfoIndex:e.activeMediaInfoIndex||0,attributes:c,layer:l,fieldInfoMap:o,formattedAttributes:{...s.global,...s.content[t]},expressionAttributes:p,mediaInfos:h,popupTemplate:n,relatedInfos:a,...this._compileTitleAndDesc({title:u,description:d})}}_compileMedia(e,t){const i=e.clone(),r=new M(this._createMediaVMParams(e,t));return i.mediaInfos=r.formattedMediaInfos.slice(0),this.contentViewModels[t]=r,i}_createTextVMParams(e){const{graphic:t,_fieldInfoMap:i,_sourceLayer:r,_expressionAttributes:o}=this;if(e&&e.text){const{attributes:s}=t,n=this.formattedAttributes.global;e.text=E({attributes:s,fieldInfoMap:i,globalAttributes:n,expressionAttributes:o,layer:r,text:e.text})}return{graphic:t,creator:e.text}}_compileText(e,t){const i=e.clone();return this.contentViewModels[t]=new g(this._createTextVMParams(i)),i}_compileLastEditInfo(){const{_effectivePopupTemplate:e,_sourceLayer:t,graphic:i}=this;if(!e)return;const{lastEditInfoEnabled:r}=e,o=t?.editFieldsInfo;return r&&o?j(o,i.attributes):void 0}_compileTitle(e){const{_fieldInfoMap:t,_sourceLayer:i,graphic:r,_expressionAttributes:o}=this,{attributes:s}=r,n=this.formattedAttributes.global;return E({attributes:s,fieldInfoMap:t,globalAttributes:n,expressionAttributes:o,layer:i,text:e})}async _getTitle(){const{_effectivePopupTemplate:e,graphic:t}=this,i=e?.title;return P(i,{graphic:t})}async _getContent(){const{_effectivePopupTemplate:e,graphic:t}=this,i=e?.content;return P(i,{graphic:t})}async _queryFeature(e){const{_featureAbortController:t,_sourceLayer:i,graphic:r,_effectivePopupTemplate:o,spatialReference:s,map:n,view:a}=this;if(t!==this._featureAbortController||!r)return;await R({graphic:r,popupTemplate:o,layer:i,spatialReference:s},e);const{content:{value:l},title:{value:c}}=await p({content:this._getContent(),title:this._getTitle()}),{expressionAttributes:{value:d}}=await p({checkForRelatedFeatures:this._checkForRelatedFeatures(e),expressionAttributes:w({expressionInfos:o?.expressionInfos,spatialReference:s,graphic:r,map:n,interceptor:Q.interceptor,view:a})});t===this._featureAbortController&&r&&(this._expressionAttributes=d,this._graphicExpressionAttributes={...r.attributes,...d},this._set("formattedAttributes",this._createFormattedAttributes(l)),this._set("title",this._compileTitle(c)),this._set("lastEditInfo",this._compileLastEditInfo()||null),this._set("content",this._compileContent(l)||null))}_createMediaFormattedAttributes({contentElement:e,contentElementIndex:t,formattedAttributes:i}){const{_effectivePopupTemplate:r,graphic:o,relatedInfos:s,_sourceLayer:n,_fieldInfoMap:a,_graphicExpressionAttributes:l}=this;i.content[t]=L({fieldInfos:r?.fieldInfos,graphic:o,attributes:{...l,...e.attributes},layer:n,fieldInfoMap:a,relatedInfos:s})}_createFieldsFormattedAttributes({contentElement:e,contentElementIndex:t,formattedAttributes:i}){if(e.fieldInfos){const{graphic:r,relatedInfos:o,_sourceLayer:s,_fieldInfoMap:n,_graphicExpressionAttributes:a}=this;i.content[t]=L({fieldInfos:e.fieldInfos,graphic:r,attributes:{...a,...e.attributes},layer:s,fieldInfoMap:n,relatedInfos:o})}}_createFormattedAttributes(e){const{_effectivePopupTemplate:t,graphic:i,relatedInfos:r,_sourceLayer:o,_fieldInfoMap:s,_graphicExpressionAttributes:n}=this,a=t?.fieldInfos,l={global:L({fieldInfos:a,graphic:i,attributes:n,layer:o,fieldInfoMap:s,relatedInfos:r}),content:[]};return Array.isArray(e)&&e.forEach(((e,t)=>{"fields"===e.type&&this._createFieldsFormattedAttributes({contentElement:e,contentElementIndex:t,formattedAttributes:l}),"media"===e.type&&this._createMediaFormattedAttributes({contentElement:e,contentElementIndex:t,formattedAttributes:l})})),l}_checkForRelatedFeatures(e){const{graphic:t,_effectivePopupTemplate:i}=this;return this._queryRelatedInfos(t,v(i),e)}async _queryRelatedInfos(e,t,i){const{relatedInfos:r,_sourceLayer:o}=this;r.clear();const s=a(o.associatedLayer)?await o.associatedLayer.load(i):o;if(!s)return;const n=t.filter((e=>e&&V(e.fieldName)));if(!n||!n.length)return;t.forEach((e=>this._configureRelatedInfo(e,s)));const l=await k({relatedInfos:r,layer:s},i);Object.keys(l).forEach((e=>{const t=r.get(e.toString()),i=l[e]?.value;t&&i&&(t.layerInfo=i.data)}));const p=await U({graphic:e,relatedInfos:r,layer:s},i);Object.keys(p).forEach((e=>{D(p[e]?.value,r.get(e.toString()))}))}_configureRelatedInfo(e,t){const{relatedInfos:i}=this,r=N(e.fieldName);if(!r)return;const{layerId:o,fieldName:s}=r;if(!o)return;const n=i.get(o.toString())||q(o,t);n&&(B({relatedInfo:n,fieldName:s,fieldInfo:e}),this.relatedInfos.set(o,n))}};K.interceptor=new i(O,S),e([f()],K.prototype,"_error",void 0),e([f()],K.prototype,"_featureAbortController",void 0),e([f({readOnly:!0})],K.prototype,"_effectivePopupTemplate",null),e([f({readOnly:!0})],K.prototype,"_fieldInfoMap",null),e([f({readOnly:!0})],K.prototype,"_sourceLayer",null),e([f()],K.prototype,"abilities",void 0),e([m("abilities")],K.prototype,"castAbilities",null),e([f({readOnly:!0})],K.prototype,"content",void 0),e([f({readOnly:!0})],K.prototype,"contentViewModels",void 0),e([f()],K.prototype,"description",void 0),e([f({type:Boolean})],K.prototype,"defaultPopupTemplateEnabled",void 0),e([f({readOnly:!0})],K.prototype,"state",null),e([f({readOnly:!0})],K.prototype,"formattedAttributes",void 0),e([f({type:t,value:null})],K.prototype,"graphic",null),e([f({readOnly:!0})],K.prototype,"lastEditInfo",void 0),e([f({readOnly:!0})],K.prototype,"relatedInfos",void 0),e([f()],K.prototype,"spatialReference",null),e([f({readOnly:!0})],K.prototype,"title",void 0),e([f()],K.prototype,"map",null),e([f({readOnly:!0})],K.prototype,"waitingForContent",null),e([f()],K.prototype,"view",void 0),K=Q=e([_("esri.widgets.FeatureViewModel")],K);const W=K;export{W as default};