var t;import{__decorate as e}from"tslib";import i from"@arcgis/core/core/Collection";import n from"@arcgis/core/popup/content/Content.js";import{EntityBase as r}from"../Entity.js";import{InvalidOperationError as s}from"../InvalidOperationError.js";import{ItemType as o}from"../ItemType.js";import{jsonToContent as p}from"../json/PopupInfoJson.js";import{serializable as a}from"../support/Serializable.js";import{notifyChange as f}from"../support/observableUtils.js";import{compare as d}from"../utilities/array.js";import{isCustomContent as u,isExpressionContent as h,isPopupContent as l,isTextContent as _}from"../utilities/esri.js";import{deepClone as m}from"../utilities/object.js";export const _popupContentExtensionMap=new WeakMap;let c=t=class extends r{get content(){return this._content}get featureSettings(){return this._featureSettings}_deferredContentProperties;visibleInSummary;summaryHiddenFields=new i;_itemType=o.POPUP_CONTENT_EXTENSION;_content;_featureSettings;_originalRef;get _index(){return Array.isArray(this.featureSettings?.popupTemplate?.content)?this.featureSettings.popupTemplate.content.indexOf(this.content):-1}constructor(t){super(t);const{content:e,visibleInSummary:i}=t??{};l(e)?this._setContent(e):"string"==typeof e?this._deferredContentProperties={$ref:e}:e?e.$ref?this._deferredContentProperties=e:this._setContent(p(e)):this._setContent(new n),this.visibleInSummary=i??this._getDefaultSummaryVisibility()}static matchesRef(e,i,n){const r=e.map(((e,i)=>y(e,e instanceof t&&e._index>=0?e._index:i))),s=r[i],{type:o,index:p,title:a,text:f}=n.$ref;if(o!==s.type)return!1;if("text"===o&&f===s.text)return!0;if(a&&a===s.title)return!0;if("number"==typeof p&&p===s.index)return!0;return 1===r.map(((t,e)=>t.type===o?t.index:void 0)).filter((t=>"number"==typeof t)).length}_switchContent(t){t&&this._content!==t&&(this._content=t,this._originalRef=y(t,this._index),_popupContentExtensionMap.set(t,this),f(this,"content"))}_setFeatureSettings(t){this._featureSettings=t,this._resolveContent(),f(this,"featureSettings")}_resolveContent(){if(this._content||!this._deferredContentProperties||!this._featureSettings)return;const e=this._featureSettings._popupContent.find(((e,i)=>t.matchesRef(this.featureSettings._popupContent.toArray(),i,this._deferredContentProperties)));e&&(this._setContent(e),this.assignProperties({content:this._deferredContentProperties}),this._originalRef={...this._deferredContentProperties.$ref},delete this._deferredContentProperties,_popupContentExtensionMap.set(e,this))}_getSerializableProperties(){return{...super._getSerializableProperties(),content:{serializeModes:["initial"],serialize:()=>this.content?this._originalRef?{$ref:this._originalRef}:this._featureSettings?._popupContent.includes(this._content)?{$ref:y(this.content,this._index)}:this.content?.toJSON():this._deferredContentProperties?m(this._deferredContentProperties):void 0,deserialize:t=>{if(l(t)&&t!==this.content)throw new Error("Property 'popupContent' is read-only.")}},visibleInSummary:{serializeModes:["initial"],isDefault:(t,e)=>t===this._getDefaultSummaryVisibility()},summaryHiddenFields:{serializeModes:["initial"],serialize:()=>this.summaryHiddenFields.toArray(),deserialize:t=>{d(this.summaryHiddenFields,t,{onMatch:(t,e,i,n)=>{this.summaryHiddenFields.reorder(t,n)},onMissing:t=>{this.summaryHiddenFields.remove(t)},onNew:t=>{this.summaryHiddenFields.add(t)}})}}}}async _onInitialize(){if(this._resolveContent(),!this._content){const t=this._deferredContentProperties.$ref;throw this._featureSettings?new s(`PopupContent not found with reference: ${JSON.stringify(t)}. PopupContent does not exist.`):new s(`PopupContent not found with reference: ${JSON.stringify(t)}. PopupContent does not have a parent featureSettings.`)}}_setContent(t){this._content=t,f(this,"content")}_getDefaultSummaryVisibility(){return"text"===(this.content?.type??this._deferredContentProperties?.type??this._deferredContentProperties?.$ref?.type)}};c=t=e([a],c);export{c as PopupContentExtension};function y(t,e){const i=t instanceof c?t.content:t;if(!i)return t._deferredContentProperties?.$ref;const n={type:i.type};return e>=0&&(n.index=e),h(i)?n.title=i.expressionInfo.title:_(i)?n.text=i.text:u(i)||(n.title=i.title),n}