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