import{__decorate as e}from"tslib";import t from"@arcgis/core/rest/support/RelationshipQuery";import{isSublayerExtension as i}from"../../ItemType.js";import{cancelifyWithAbortSignal as r}from"../../support/Cancellable.js";import{serializable as a}from"../../support/Serializable.js";import{isFeatureLayer as s,isMapImageSublayer as o,isSubtypeGroupLayer as n}from"../../utilities/_layers.js";import{isArcGISServiceTableExtension as l,isFeatureLayerExtension as u,isSubtypeGroupLayerExtension as p,isSubtypeSublayerExtension as y}from"../../utilities/extensions.js";import{QueryBasedRelationship as c}from"./QueryBasedRelationship.js";const d=new WeakMap;let m=class extends c{relationshipType="ArcGISRelationship";relationshipTableId;originForeignKey;destinationForeignKey;constructor(e){super(e),this.relationshipTableId=e.relationshipTableId,this.originForeignKey=e.originForeignKey,this.destinationForeignKey=e.destinationForeignKey}get isAttributed(){return"number"==typeof this.relationshipTableId}_getSerializableProperties(){return{...super._getSerializableProperties(),$relationshipType:{serializeModes:["initial"],serialize:()=>"ArcGISRelationship"},destinationForeignKey:"initial",originForeignKey:"initial",relationshipTableId:"initial"}}async _getRelatedFeatures(e,t){return r((async t=>(await f([e],h(this.source),this.relatedSource,{relationshipId:+this.id,signal:t})).get(e)),t?.cancelToken)}async _getAllRelatedFeatures(e,t){return f(e,h(this.source),this.relatedSource,{relationshipId:+this.id})}};m=e([a],m);export{m as ArcGISRelationship};function h(e){let t;if(l(e))t=e.table;else if(u(e))t=e.layer;else if(i(e)&&o(e.sublayer))t=e.sublayer;else if(y(e)&&e.sublayer?.layer)t=e.sublayer.layer;else{if(!p(e))throw new Error(`Cannot query for related records. Unsupported FeatureSource: ${e.title} (${e.itemType}/${e.id})`);t=e.layer}return t}const g={returnGeometry:!0};async function f(e,i,r,a){const o=e.map((e=>+e?.primaryKey)),{signal:l,...u}=a,p=new t({...g,objectIds:o.filter((e=>!isNaN(e)&&"number"==typeof e)),outFields:r?.featureSettings.outFields??["*"],...u});let y;if(s(i)||n(i))y=i;else{const e=i;d.has(e)||d.set(e,await e.createFeatureLayer()),y=d.get(e)}const c=await y.queryRelatedFeatures(p,{signal:l}),m=new Map,[h,f]=await Promise.all([(await import("../../data/FeatureSet.js")).FeatureSet,(await import("../../data/Feature.js")).toFeature]);return o.forEach(((t,i)=>{m.set(e[i],new h({source:r,features:c[t]?.features.map((e=>f(e,{source:r})))??[]}))})),m}