import{EntityBase as e}from"../Entity.js";import{InvalidOperationError as t}from"../InvalidOperationError.js";import{isFeatureSet as s,ItemType as r}from"../ItemType.js";import{cancelify as a}from"../support/Cancellable.js";import{checkArg as i}from"../utilities/checkArg.js";const o="Cannot get related features without feature source.";export class RelationshipBase extends e{title;get source(){return this._source}_itemType=r.RELATIONSHIP;_source;_getRelatedFeaturesPromises=new WeakMap;async getRelatedFeatures(e,s){if(i("feature",e).isNotMissing(),!this.source)throw new t(o);this._getRelatedFeaturesPromises.has(e)||this._getRelatedFeaturesPromises.set(e,(async()=>{try{return await this._getRelatedFeatures(e,s)}finally{this._getRelatedFeaturesPromises.delete(e)}})());const r=this._getRelatedFeaturesPromises.get(e);return a(r,s?.cancelToken)}async getAllRelatedFeatures(e,r){if(i("feature",e).isNotMissing(),!this.source)throw new t(o);let l;l=Array.isArray(e)?e:s(e)?[...e]:await e.toArray();const n=a(this._getAllRelatedFeatures(l,r),r?.cancelToken);for(const e of l)this._getRelatedFeaturesPromises.has(e)||this._getRelatedFeaturesPromises.set(e,(async()=>{try{return(await n).get(e)}finally{this._getRelatedFeaturesPromises.delete(e)}})());return n}_setSource(e){this._source=e}_getSerializableProperties(){return{...super._getSerializableProperties(),title:"initial"}}}