/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../../chunks/_rollupPluginBabelHelpers","../../../request","../../../core/Error","../../../core/Logger","../../../core/promiseUtils","../../../layers/support/fieldUtils","../../../core/has","../../../layers/support/source/DataLayerSource","../../../rest/query/executeQueryJSON","../../../core/arrayUtils","../../../core/urlUtils","../../../core/unitUtils","../../../geometry/support/spatialReferenceUtils","../../../layers/graphics/featureConversionUtils","../../../geometry/Extent","../../../geometry/Geometry","../../../geometry/Multipoint","../../../geometry/Point","../../../geometry/Polygon","../../../geometry/Polyline","../../../geometry/support/normalizeUtils","../../../core/pbf","../../../rest/support/FeatureSet","../../../rest/support/Query","../../../kernel","../../../rest/query/support/AttachmentInfo","../../../rest/support/AttachmentQuery","../../../geometry","../../../rest/support/RelationshipQuery","../../../rest/support/TopFeaturesQuery","../../../rest/support/StatisticDefinition","./featureUtils"],(function(e,t,r,s,i,o,n,a,l,u,c,d,f,p,y,h,F,g,I,S,m,w,b,R,T,$,q,v,U,N,j,A,L){"use strict";const Q="esri.widgets.Feature.support.relatedFeatureUtils",x=i.getLogger(Q),O=new Map;function P(e){if(!L.isRelatedField(e))return null;const[t,r]=e.split("/").slice(1);return{layerId:t,fieldName:r}}function k(e,t){if(!t.relationships)return null;let r=null;const{relationships:s}=t;return s.some((t=>t.id===parseInt(e,10)&&(r=t,!0))),r}function C({originRelationship:e,relationships:t,layerId:r}){let s;return t&&t.some((t=>(`${t.relatedTableId}`===r&&t.id===e.id&&(s=t),!!s))),s}function D(e,t){const r=t.toLowerCase();for(const s in e)if(s.toLowerCase()===r)return e[s];return null}function E(e,t){const r=k(e,t);if(!r)return;return{url:`${t.url}/${r.relatedTableId}`,sourceSpatialReference:t.spatialReference,relation:r,relatedFields:[],outStatistics:[]}}function J(e,t){if(!t)return;if(!e)return;const{features:r,statsFeatures:s}=e,i=r&&r.value;t.relatedFeatures=i?i.features:[];const o=s&&s.value;t.relatedStatsFeatures=o?o.features:[]}function M(e,t,r,s){const i=new N;return i.outFields=["*"],i.relationshipId="number"==typeof t.id?t.id:parseInt(t.id,10),i.objectIds=[e.attributes[r.objectIdField]],r.queryRelatedFeatures(i,s)}function _(e,t,r){let s=0;const i=[];for(;s<t.length;)i.push(`${e} IN (${t.slice(s,r+s)})`),s+=r;return i.join(" OR ")}function G(e,t,r,s){return z.apply(this,arguments)}function z(){return(z=t._asyncToGenerator((function*(e,t,r,s){const i=r.layerId.toString(),{layerInfo:a,relation:l,relatedFields:c,outStatistics:d,url:f,sourceSpatialReference:p}=t,y=C({originRelationship:l,relationships:a.relationships,layerId:i});if(y.relationshipTableId&&y.keyFieldInRelationshipTable){const t=(yield M(e,y,r,s))[e.attributes[r.objectIdField]];if(!t)return null;const i=t.features.map((e=>e.attributes[a.objectIdField]));if((null==d?void 0:d.length)>0&&a.supportsStatistics){const e=new T;e.where=_(a.objectIdField,i,1e3),e.outFields=c,e.outStatistics=d,e.sourceSpatialReference=p;const r={features:Promise.resolve(t),statsFeatures:u.executeQueryJSON(f,e)};return o.eachAlways(r)}}const h=null==y?void 0:y.keyField;if(h){const r=n.isNumericField(W(a.fields,h)),i=D(e.attributes,l.keyField),c=r?`${h}=${i}`:`${h}='${i}'`,d=u.executeQueryJSON(f,new T({where:c,outFields:t.relatedFields,sourceSpatialReference:p}),s),y=t.outStatistics&&t.outStatistics.length>0&&a.supportsStatistics?u.executeQueryJSON(f,new T({where:c,outFields:t.relatedFields,outStatistics:t.outStatistics,sourceSpatialReference:p}),s):null,F={features:d};return y&&(F.statsFeatures=y),o.eachAlways(F)}return null}))).apply(this,arguments)}function B(e,t){return r(e,{query:{f:"json"},signal:t&&t.signal})}function H({relatedInfos:e,layer:t},r){const i={};return e.forEach(((e,o)=>{const{relation:n}=e;if(!n){const e=new s("relation-required","A relation is required on a layer to retrieve related records.");throw x.error(e),e}const{relatedTableId:a}=n;if("number"!=typeof a){const e=new s("A related table ID is required on a layer to retrieve related records.");throw x.error(e),e}const l=`${t.url}/${a}`,u=O.get(l),c=u||B(l,r);u||O.set(l,c),i[o]=c})),o.eachAlways(i)}function K({graphic:e,relatedInfos:t,layer:r},s){const i={};return t.forEach(((t,o)=>{t.layerInfo&&(i[o]=G(e,t,r,s))})),o.eachAlways(i)}function V({relatedInfo:e,fieldName:t,fieldInfo:r}){if(e.relatedFields.push(t),r.statisticType){const s=new A({statisticType:r.statisticType,onStatisticField:t,outStatisticFieldName:t});e.outStatistics.push(s)}}function W(e,t){if(null!=e){t=t.toLowerCase();for(const r of e)if(r&&r.name.toLowerCase()===t)return r}return null}e.createRelatedInfo=E,e.getDestinationRelation=C,e.getRelatedFieldInfo=P,e.queryLayerInfos=H,e.queryRelatedFeatures=K,e.setRelatedFeatures=J,e.updateRelatedInfo=V,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
