/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import e from"../../../../core/Error.js";import"../../../../geometry/SpatialReference.js";import"../../../../geometry/support/aaBoundingBox.js";import"../../../../core/mathUtils.js";import"../../../../geometry/Extent.js";import"../../../../geometry/support/typeUtils.js";import"../../../../layers/support/Field.js";import"../../../../core/has.js";import{equals as t}from"../../../../layers/graphics/dehydratedFeatureComparison.js";class r{constructor(e,t){this.feature=e,this.resolution=t,this.refCount=1}}class s{get isReferenced(){return 0!==this.versions.length}get isSingle(){return 1===this.versions.length&&1===this.versions[0].refCount}constructor(e,t){this._highestResolutionVersion=null,this.versions=[],this.ref(e,t)}ref(e,s){const o=this.feature;n.oldVersion=o,o&&Object.defineProperty(e,"uid",{value:o.uid,configurable:!0});const i=this._highestResolutionVersion;for(const r of this.versions)if(r.resolution===s){r.refCount++;const s=i===r&&!t(e,r.feature);return(s||i!==r)&&(r.feature=e),n.newVersion=s?e:o,n}const u=new r(e,s);return this.versions.push(u),!i||s<i.resolution?(n.newVersion=e,this._highestResolutionVersion=u):n.newVersion=o,n}unref(t){const{versions:r}=this;for(let e=0;e<r.length;e++){const s=r[e];if(s.resolution===t)return s.refCount--,i.oldVersion=this.feature,0===s.refCount&&(r[e]=r[r.length-1],--r.length,this._highestResolutionVersion===s&&(this._recalculateHighestResolutionVersion(),i.oldVersion=s.feature)),i.newVersion=this.feature,i}const s=`Unref feature with no references: ${this.feature?.uid}`;throw new e(s,s,(new Error).stack?.slice(6))}get feature(){return this._highestResolutionVersion?.feature??null}_recalculateHighestResolutionVersion(){if(0===this.versions.length)return void(this._highestResolutionVersion=null);let e=this.versions[0];for(let t=1;t<this.versions.length;t++){const r=this.versions[t];r.resolution<e.resolution&&(e=r)}this._highestResolutionVersion=e}}class o{get isReferenced(){return 0!==this._refCount}get isSingle(){return 1===this._refCount}constructor(e){this._feature=e,this._refCount=1}ref(e){++this._refCount;const r=this._feature;return n.oldVersion=r,r&&Object.defineProperty(e,"uid",{value:r.uid,configurable:!0}),t(r,e)||(this._feature=e),n.newVersion=this._feature,n}unref(){i.oldVersion=this._feature;const t=this._refCount;if(!(t>0)){const t=`Unref feature with no references: ${this.feature?.uid}`;throw new e(t,t,(new Error).stack?.slice(6))}return this._refCount=t-1,1===t?(i.newVersion=null,i):(i.newVersion=this._feature,i)}get feature(){return this._feature}}const i={oldVersion:null,newVersion:null},n={oldVersion:null,newVersion:null};export{r as FeatureVersion,s as MultiFeatureReference,o as SingleFeatureReference};
