import{__decorate as e}from"tslib";import{RelationshipBase as t}from"../../data/RelationshipBase.js";import{serializable as i}from"../../support/Serializable.js";import{checkArg as r}from"../../utilities/checkArg.js";import{QueryService as a}from"./QueryService.js";let s=class extends t{relatedSource;cardinality;keyField;relatedKeyField;relationshipType="QueryBasedRelationship";queryService;constructor(e){super(e),r("properties",e).isNotMissing(),r("properties.relatedSource",e.relatedSource).isNotMissing(),r("properties.keyField",e.keyField).isNotMissing().isNotEmpty(),r("properties.relatedKeyField",e.relatedKeyField).isNotMissing().isNotEmpty(),this.relatedSource=e.relatedSource,this.keyField=e.keyField,this.relatedKeyField=e.relatedKeyField,this.queryService=e.queryService||new a,this.cardinality=e.cardinality||"many-to-many"}_getSerializableProperties(){return{...super._getSerializableProperties(),keyField:"initial",relatedKeyField:"initial",$relationshipType:{serializeModes:["initial"],serialize:()=>"QueryBasedRelationship"},cardinality:"initial",relatedSource:"initial",queryService:{serialize:!1}}}async _getRelatedFeatures(e,t){const{FeatureSet:i}=await import("../../data/FeatureSet.js"),r=e.attributes.get(this.keyField);if(null==r)return new i({source:this.relatedSource});const a="string"==typeof r?`${this.relatedKeyField}='${l(r)}'`:`${this.relatedKeyField}=${r}`;return i.fromFeatureStream(this.queryService.query(this.relatedSource,a,t))}async _getAllRelatedFeatures(e,t){const i=new Map,r=[],a=new Set,{FeatureSet:s}=await import("../../data/FeatureSet.js");e.forEach((e=>{const t=e.attributes.get(this.keyField);null==t?i.set(e,new s({source:this.relatedSource})):(r.push(e),a.add("string"==typeof t?`'${l(t)}'`:`${t}`))}));const o=`${this.relatedKeyField} IN (${[...a].join(", ")})`,d=await s.fromFeatureStream(this.queryService.query(this.relatedSource,o,t));return r.forEach((e=>{const t=e.attributes.get(this.keyField),r=new s({source:this.relatedSource});d.forEach((e=>{t===e.attributes.get(this.relatedKeyField)&&r.add(e)})),i.set(e,r)})),i}};s=e([i],s);export{s as QueryBasedRelationship};function l(e){return e.replace(/([%'])/g,"$1$1")}