// All material copyright ESRI, All Rights Reserved, unless otherwise specified.
// See http://@sbaseurl@/jsapi/jsapi/esri/copyright.txt and http://www.arcgis.com/apps/webappbuilder/copyright.txt for details.

//>>built
define("dojo/_base/lang dojo/_base/array dojo/Deferred dojo/when dojo/promise/all jimu/portalUtils esri/lang esri/request jimu/utils esri/dijit/PopupTemplate esri/graphic esri/geometry/webMercatorUtils esri/tasks/ProjectParameters".split(" "),function(h,p,n,x,q,y,u,v,w,z,A,B,C){var r={map:null,layerInfosObj:null,appConfig:null,_esriLocatorRegExp:/geocode(.){0,3}\.arcgis.com\/arcgis\/rest\/services\/World\/GeocodeServer/g,setMap:function(a){this.map=a},setLayerInfosObj:function(a){this.layerInfosObj=
a},setAppConfig:function(a){this.appConfig=a},getConfigInfo:function(a){if(a&&a.sources&&0<a.sources.length){var b=null;return this.searchLayer(this.map)&&a.upgradeFromGeocoder?(b=this.map.itemInfo.itemData.applicationProperties.viewing.search,b=p.map(b.layers,h.hitch(this,function(c,d){d.hintText=c;return this._getQueryTypeGeocoder(d)},b.hintText)),q(b).then(h.hitch(this,function(c){a.sources=[].concat(c).concat(a.sources);return a}))):a}return x(this._getSoucesFromPortalAndWebmap()).then(h.hitch(this,
function(c){return{allPlaceholder:"",showInfoWindowOnSelect:!0,sources:c}}))},_getSoucesFromPortalAndWebmap:function(){var a=[],b=null;this.searchLayer(this.map)&&(b=this.map.itemInfo.itemData.applicationProperties.viewing.search,p.forEach(b.layers,h.hitch(this,function(c,d){d.hintText=c;a.push(this._getQueryTypeGeocoder(d))},b.hintText)));return y.getPortalSelfInfo(this.appConfig.portalUrl).then(h.hitch(this,function(c){if((c=c.helperServices&&c.helperServices.geocode)&&0<c.length)for(var d=0,e=
c.length;d<e;d++){var f=c[d];f&&a.push(this._processSingleLine(f))}return q(a).then(h.hitch(this,function(g){for(var m=[],l=0;l<g.length;l++){var k=g[l];k&&(k&&"query"===k.type?m.push(k):(k={name:k.name||this._getGeocodeName(k.url),url:k.url,singleLineFieldName:k.singleLineFieldName,placeholder:"Find address or place",maxResults:6,searchInCurrentMapExtent:!1,type:"locator"},k.enableLocalSearch=this._isEsriLocator(k.url),k.localSearchMinScale=3E5,k.localSearchDistance=5E4,m.push(k)))}return m}))}))},
_getQueryTypeGeocoder:function(a){var b=this.map.getLayer(a.id),c=null,d=null,e=null;e=u.isDefined(a.subLayer)?a.id+"_"+a.subLayer:a.id;c=this.layerInfosObj.traversal(function(f){return f.id===e?(d=f,!0):!1});return b&&c&&d?(c=u.isDefined(a.subLayer)?d.url||b.url+"/"+a.subLayer:d.url||b.url,{name:d.title,layerId:e,url:c,placeholder:a.hintText,searchFields:[a.field.name],displayField:a.field.name,exactMatch:a.field.exactMatch||!1,maxResults:6,searchInCurrentMapExtent:!1,type:"query"}):null},_isEsriLocator:function(a){this._esriLocatorRegExp.lastIndex=
0;return this._esriLocatorRegExp.test(a)},_processSingleLine:function(a){if(a.singleLineFieldName)return a;if(this._isEsriLocator(a.url))return a.singleLineFieldName="SingleLine",a;var b=new n;v({url:a.url,content:{f:"json"},handleAs:"json",callbackParamName:"callback"}).then(h.hitch(this,function(c){c.singleLineAddressField&&c.singleLineAddressField.name?(a.singleLineFieldName=c.singleLineAddressField.name,b.resolve(a)):(console.warn(a.url+"has no singleLineFieldName"),b.resolve(null))}),h.hitch(this,
function(c){console.error(c);b.resolve(null)}));return b.promise},_getGeocodeName:function(a){if("string"!==typeof a)return"geocoder";a=a.split("/");return a[a.length-2]||"geocoder"},getGeocoderName:function(a){return this._getGeocodeName(a)},hasAppSearchInfo:function(a){return a.itemInfo&&a.itemInfo.itemData&&a.itemInfo.itemData.applicationProperties&&a.itemInfo.itemData.applicationProperties.viewing&&a.itemInfo.itemData.applicationProperties.viewing.search},searchLayer:function(a){if(!this.hasAppSearchInfo(a))return!1;
a=a.itemInfo.itemData.applicationProperties.viewing.search;return a.enabled&&0!==a.layers.length?!0:!1},getSingleLineAddressName:function(a){var b=new n;v({url:a,content:{f:"json"},handleAs:"json",callbackParamName:"callback"}).then(h.hitch(this,function(c){c&&c.singleLineAddressField&&c.singleLineAddressField.name?b.resolve(c.singleLineAddressField.name):b.resolve(null)}),h.hitch(this,function(c){console.error(c);b.reject()}));return b},version:{isConfigBefore63:function(a){var b=!1;a&&a.geocoderOptions&&
(b=!0);return b},upgradeConfig:function(a){a.geocoderOptions&&delete a.geocoderOptions},getVersionState:function(){}},config:{onLayerInfosFilterChanged:function(a,b){p.some(a,h.hitch(this,function(c){b.searchOptions&&b.searchOptions.sources&&0<b.searchOptions.sources.length&&p.forEach(b.searchOptions.sources,function(d){d._featureLayerId===c.id&&d.featureLayer.setDefinitionExpression(c.getFilter())})}))},getInfoTemplate:function(a,b,c){var d=new n;c=c.getLayerInfoById(b.layerId);var e;if(c)d=c.loadInfoTemplate();
else{var f=[];p.filter(a.fields,function(g){"shape"!==g.name.toLowerCase()&&f.push(g.name)});(a=w.getDefaultPopupInfo(a,b.name+": {"+b.displayField+"}",f))&&(e=new z(a));d.resolve(e)}return d},getWayPoints:function(a,b,c){var d=new n;b&&d.resolve(a.defaultLocations);b=[];for(var e=0,f=a.defaultLocations.length;e<f;e++){var g=a.defaultLocations[e];if("object"===typeof g&&g.feature&&g.name){var m={};m=g.feature.attributes;g.name&&(m.Name=g.name);g=new A(g.feature,null,m);b.push(g)}else"string"===typeof g?
b.push(g):b.push("")}r.config.projectGeometriesToMapCoords(b,c).then(h.hitch(this,function(l){d.resolve(l)}),h.hitch(this,function(l){d.reject(l)}));return d},projectGeometriesToMapCoords:function(a,b){var c=new n,d=[],e=[];if(4326===parseInt(b.spatialReference.wkid,10))d.push((new n).resolve(a));else if(b.spatialReference.isWebMercator()){for(var f=0,g=a.length;f<g;f++){var m=a[f];if(b=m.geometry)if(b=B.geographicToWebMercator(b))m.geometry=b;e.push(m)}d.push((new n).resolve(e))}else for(e=esriConfig&&
esriConfig.defaults&&esriConfig.defaults.geometryService,e&&"esri.tasks.GeometryService"===e.declaredClass||(geoServiceUrl&&"string"===typeof geoServiceUrl&&h.trim(geoServiceUrl)?(geoServiceUrl=h.trim(geoServiceUrl),e=new GeometryService(geoServiceUrl)):e=w.getArcGISDefaultGeometryService()),f=0,g=a.length;f<g;f++)(m=a[f])?d.push(r.config.projectedToOthersCoords(m,e,b)):d.push((new n).resolve(""));q(d).then(h.hitch(this,function(l){var k=[];if(1<l.length){for(var t=0,D=l.length;t<D;t++)k.push(l[t]);
return c.resolve(k)}return c.resolve(l[0])}),h.hitch(this,function(l){return c.reject(l)}));return c},projectedToOthersCoords:function(a,b,c){var d=new n,e=new C;e.geometries=[a.geometry];e.outSR=c.spatialReference;b.project(e).then(function(f){return(f=f&&f[0])?(a.geometry=f,d.resolve(a)):d.reject("")},function(f){console.error(f);return d.reject("")});return d},_toPointGeometry:function(a){var b=a.feature.geometry;b&&(b.getCentroid?a.feature.geometry=b.getCentroid():b.getExtent&&(b=b.getExtent())&&
(a.feature.geometry=b.getCenter()));return a},_getLocationObject:function(a){return{name:a.name,extent:a.extent,feature:{geometry:a.feature.geometry,attributes:{Score:100,Addr_Type:"PointAddress"}}}}}};return r});