import e from"@arcgis/core/core/Collection";import{CopyOnWriteCollection as t}from"../utilities/CopyOnWriteCollection.js";import{every as i}from"../utilities/iterable.js";import{objectKeys as s}from"../utilities/object.js";import{handles as r}from"./HandlesMixin.js";import{isDefault as n,SerializableBase as o,shouldExcludeDefaults as a}from"./Serializable.js";import{notifyChange as h,observable as d}from"./observableUtils.js";export class Settings extends(r(d(o))){static _parentHandleKey="settings-parent-handle";_parent;_overrides;get parent(){return this._parent}set parent(e){this._setParent(e)}reset(e){if(this._overrides)if(e)this._overrides.delete(e),this._notifySettingChange(e);else{const e=[...this._overrides.keys()];this._overrides.clear();for(const t of e)this._notifySettingChange(t)}}isModified(e){if(!this._overrides)return!1;if(e){if(!this._overrides.has(e))return!1;const i=this._overrides.get(e);return i instanceof t?i.isModified:!(i instanceof Settings)||i.isModified()}return!i(this._overrides.values(),(e=>e instanceof t&&!e.isModified||e instanceof Settings&&!e.isModified()))}toJSON(e="all"){if(!this._overrides?.size&&"initial-with-defaults"!==e)return a(e)?void 0:{};const t=super.toJSON(e);if(t&&"initial-with-defaults"!==e)for(const e of s(t))this.isModified(e)||delete t[e];return a(e)&&n(t)?void 0:t}getDefaults(){return{}}_hasSettingIncludingAncestor(e){return!!this._overrides?.has(e)||!!this.parent&&this.parent._hasSettingIncludingAncestor(e)}_getSetting(e,t){return this._overrides?.has(e)?this._overrides.get(e):this.parent&&this._hasSettingIncludingAncestor(e)?this.parent[e]:t}_getCollectionSetting(i){if(!this._overrides?.has(i)){const s=this.parent?this.parent[i]:new e;null==this._overrides&&(this._overrides=new Map),this._overrides.set(i,new t(s))}return this._overrides.get(i)}_getNestedSettings(e,t){if(!this._overrides?.has(e)){const i=t();this.parent&&(i.parent=this.parent[e]),null==this._overrides&&(this._overrides=new Map),this._overrides.set(e,i)}return this._overrides.get(e)}_setSetting(e,t){null==this._overrides&&(this._overrides=new Map),this._overrides.set(e,t),this._notifySettingChange(e)}_setParent(i){if(this._parent!==i&&(null!=this._parent||null!=i)){if(this._handles.remove(Settings._parentHandleKey),this._parent=i,null!=this._parent){const e=new WeakRef(this),t=this._parent.on("property-change",(i=>{const s=e.deref();null!=s?s._notifySettingChange(i.property):t.remove()}));this._handles.add(t,Settings._parentHandleKey)}if(this._overrides)for(const[s,r]of this._overrides)r instanceof t?r.originalCollection=i?i[s]:new e:r instanceof Settings&&(r.parent=i?i[s]:void 0);h(this)}}_notifySettingChange(e){this.emit("property-change",{property:e}),h(this,e)}}