/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import e from"../../../Graphic.js";import t from"../../../geometry/Polygon.js";import{getExtentFromGeometry as o,scaleExtent as r}from"../../../geometry/support/geometryUtils.js";import{addressToLocations as s}from"../../../rest/locator/addressToLocations.js";import{locationToAddress as n}from"../../../rest/locator/locationToAddress.js";import{suggestLocations as a}from"../../../rest/locator/suggestLocations.js";import i from"../../../rest/support/AddressToLocationsParameters.js";import c from"../../../rest/support/LocationToAddressParameters.js";import l from"../../../rest/support/SuggestLocationsParameters.js";const u="Single Line Input",m=3e5;function p(e,t){return e.location?S(e,t):j(e,t)}function f(e,t){if(t.localSearchDisabled)return null;const o=e?.scale;return"number"==typeof o&&o<=m?e?.extent?.center:null}async function g(e,t){const{source:o,spatialReference:r,view:s,suggestTerm:n,maxSuggestions:i,sourceIndex:c}=e,u=new l,{apiKey:m,url:p}=o,g=v(o,s,await import("../../../geometry/operators/labelPointOperator.js")),d=t?.signal;if(!p)return null;m&&(u.apiKey=m),o.categories&&(u.categories=o.categories),r&&(u.outSpatialReference=r);const y=f(s,o);y&&(u.location=y);if(!n.trim())return null;const{prefix:x="",suffix:w=""}=o,S=`${x}${n}${w}`;return u.text=S,g&&(u.searchExtent=g),u.maxSuggestions=i,o.countryCode&&(u.countryCode=o.countryCode),a(p,u,{signal:d}).then(e=>h(e,c))}function d(e){return!!e&&/(?:geocode-api\.arcgis\.com\/arcgis\/rest\/services\/world\/geocodeserver).*/i.test(e)}function y(e){return!!e&&/(?:\/servers\/[\da-z.-]+\/rest\/services\/world\/geocodeserver).*/i.test(e)}function x(e){return!!e&&/(?:arcgis\.com\/arcgis\/rest\/services\/world\/geocodeserver).*/i.test(e)}const w="https://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer";async function S(e,t){const{location:o,source:r,sourceIndex:s,spatialReference:a,view:i}=e,{apiKey:l,defaultZoomScale:u,language:m,url:p,zoomScale:f}=r;if(null==p)return[];const g=i?.scale,d=t?.signal,y=new c({language:m,location:o,apiKey:l,outSpatialReference:a});try{const e=await n(p,y,{signal:d});return $([e],{defaultZoomScale:u,scale:g,sourceIndex:s,view:i,zoomScale:f},await import("../../../geometry/operators/labelPointOperator.js"))}catch{return[]}}function v(e,t,r){const{filter:s,withinViewEnabled:n}=e,a=t?.scale,i=t?.extent,c=s?.geometry;return o(c,t,a,r)||(n&&i?i:void 0)}async function j(e,t){const{source:o,suggestResult:r,spatialReference:n,view:a,maxResults:c,sourceIndex:l}=e,m=o?.zoomScale,p=o?.defaultZoomScale,g=r.text?.trim();if(!g)return null;const d=!r.key&&o.prefix?o.prefix:"",y=!r.key&&o.suffix?o.suffix:"",x=`${d}${r.text}${y}`,w=new i,{apiKey:S,url:j}=o,b=a?.scale,h=await import("../../../geometry/operators/labelPointOperator.js"),I=v(o,a,h),K=t?.signal;if(S&&(w.apiKey=S),!j)return null;o.categories&&(w.categories=o.categories),o.locationType&&(w.locationType=o.locationType),n&&(w.outSpatialReference=n);const L=f(a,o);L&&(w.location=L),w.maxLocations=c,I&&(w.searchExtent=I),o.countryCode&&(w.countryCode=o.countryCode);const{key:R}=r,T=`${R}`;return R&&(w.magicKey=T),w.address={},w.address[o.singleLineFieldName||u]=x,o.outFields&&(w.outFields=o.outFields),s(j,w,{signal:K}).then(e=>$(e,{key:T,scale:b,sourceIndex:l,view:a,zoomScale:m,defaultZoomScale:p},h))}function b(e,t){return{text:e.text,key:e.magicKey,sourceIndex:t}}function h(e,t){return e.map(e=>b(e,t))}function I(s,n,a){const{key:i,scale:c,sourceIndex:l,view:u,zoomScale:m,defaultZoomScale:p}=n,{attributes:f,extent:g,location:d,address:y}=s,x=new e({geometry:d,attributes:f}),w=g||d,S=o(w,u,c,a),v=S?"number"==typeof m?r(S,u??void 0,m):"number"==typeof p&&"point"===w?.type?r(S,u??void 0,p):S:null,j=d?`${d.x},${d.y}`:"",b=y||j,h=x.clone();return null!=v&&(h.geometry=t.fromExtent(v)),{extent:v,feature:x,target:h,key:i,name:b,sourceIndex:l}}function $(e,t,o){return e.filter(Boolean).map(e=>I(e,t,o))}export{f as getLocation,p as getResults,g as getSuggestions,x as isArcGISWorldGeocoder,d as isMeteredArcGISWorldGeocoder,y as isProxiedArcGISWorldGeocoder,w as meteredArcGISLocatorUrl};
