/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../../Graphic","../../../core/maybe","../../../geometry/Polygon","../../../rest/locator/addressToLocations","../../../rest/locator/locationToAddress","../../../rest/locator/suggestLocations","../../../rest/support/AddressToLocationsParameters","../../../rest/support/LocationToAddressParameters","../../../rest/support/SuggestLocationsParameters","./geometryUtils"],(function(e,t,o,r,s,n,c,i,a,l,u){"use strict";const d="Single Line Input",g=3e5;function m(e,t){return e.location?w(e,t):L(e,t)}function f(e,t){if(t.localSearchDisabled)return null;const o=null==e?void 0:e.scale;return"number"==typeof o&&o<=g?e.get("extent.center"):null}function p(e,t){const{source:r,spatialReference:s,view:n,suggestTerm:i,maxSuggestions:a,sourceIndex:u}=e,d=new l,{apiKey:g,url:m}=r,p=I(r,n),y=t&&t.signal;if(!m)return Promise.resolve(null);g&&(d.apiKey=g),r.categories&&(d.categories=r.categories),s&&(d.outSpatialReference=s);const x=f(n,r);x&&(d.location=x);if(!i.trim())return Promise.resolve(null);const{prefix:S="",suffix:v=""}=r,w=`${S}${i}${v}`;return d.text=w,p&&(d.searchExtent=o.unwrap(p)),d.maxSuggestions=a,r.countryCode&&(d.countryCode=r.countryCode),c.suggestLocations(m,d,{signal:y}).then((e=>T(e,u)))}function y(e){return!!e&&/(?:geocode\-api\.arcgis\.com\/arcgis\/rest\/services\/world\/geocodeserver).*/i.test(e)}function x(e){return!!e&&/(?:\/servers\/[\da-z\.-]+\/rest\/services\/world\/geocodeserver).*/i.test(e)}function S(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";function w(e,t){const{source:o,spatialReference:r,location:s,sourceIndex:c,view:i}=e,{apiKey:l,url:u,zoomScale:d,defaultZoomScale:g}=o,m=i&&i.scale,f=t&&t.signal,p=new a;return p.location=s,l&&(p.apiKey=l),r&&(p.outSpatialReference=r),n.locationToAddress(u,p,{signal:f}).then((e=>h([e],{sourceIndex:c,scale:m,view:i,zoomScale:d,defaultZoomScale:g}))).catch((()=>[]))}function I(e,t){const{filter:o,withinViewEnabled:r}=e,s=t&&t.scale,n=t&&t.extent,c=o&&o.geometry;return u.createExtentFromGeometry(c,t,s)||(r&&n?n:void 0)}function L(e,t){const{source:r,suggestResult:n,spatialReference:c,view:a,maxResults:l,sourceIndex:u}=e,g=r&&r.zoomScale,m=r&&r.defaultZoomScale;if(!n.text.trim())return Promise.resolve(null);const p=!n.key&&r.prefix?r.prefix:"",y=!n.key&&r.suffix?r.suffix:"",x=`${p}${n.text}${y}`,S=new i,{apiKey:v,url:w}=r,L=a&&a.scale,G=I(r,a),T=t&&t.signal;if(v&&(S.apiKey=v),!w)return Promise.resolve(null);r.categories&&(S.categories=r.categories),r.locationType&&(S.locationType=r.locationType),c&&(S.outSpatialReference=c);const b=f(a,r);b&&(S.location=b),S.maxLocations=l,G&&(S.searchExtent=o.unwrap(G)),r.countryCode&&(S.countryCode=r.countryCode);const{key:P}=n,R=`${P}`;return P&&(S.magicKey=R),S.address={},S.address[r.singleLineFieldName||d]=x,r.outFields&&(S.outFields=r.outFields),s.addressToLocations(w,S,{signal:T}).then((e=>h(e,{key:R,scale:L,sourceIndex:u,view:a,zoomScale:g,defaultZoomScale:m})))}function G(e,t){return{text:e.text,key:e.magicKey,sourceIndex:t}}function T(e,t){return e.map((e=>G(e,t)))}function b(e,s){const{key:n,scale:c,sourceIndex:i,view:a,zoomScale:l,defaultZoomScale:d}=s,{attributes:g,extent:m,location:f,address:p}=e,y=new t({geometry:f,attributes:g}),x=m||f,S=u.createExtentFromGeometry(x,a,c),v="number"==typeof l?u.scaleExtent(S,a,l):"number"==typeof d&&"point"===x.type?u.scaleExtent(S,a,d):S,w=f?`${f.x},${f.y}`:"",I=p||w,L=y.clone();return o.isSome(v)&&(L.geometry=r.fromExtent(v)),{extent:v,feature:y,target:L,key:n,name:I,sourceIndex:i}}function h(e,t){return e.filter(Boolean).map((e=>b(e,t)))}e.getLocation=f,e.getResults=m,e.getSuggestions=p,e.isArcGISWorldGeocoder=S,e.isMeteredArcGISWorldGeocoder=y,e.isProxiedArcGISWorldGeocoder=x,e.meteredArcGISLocatorUrl=v,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
