// 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/declare dojo/_base/lang dojo/_base/array dojo/Deferred dojo/on dojo/when dojo/promise/all jimu/BaseWidget jimu/portalUtils jimu/utils esri/lang esri/request esri/dijit/Search esri/tasks/locator esri/layers/FeatureLayer esri/InfoTemplate".split(" "),function(y,h,m,q,v,w,r,z,A,p,x,B,C,D,E,F){return y([z],{baseClass:"jimu-search-layers",declaredClass:"jimu.dijit.SearchLayers",_searchDijitReady:null,_searchDijit:null,_layerInfosObj:null,_esriLocatorRegExp:/geocode(.){0,3}\.arcgis.com\/arcgis\/rest\/services\/World\/GeocodeServer/g,
_mainWidgetNode:null,constructor:function(b,a,c,e,d){this._mainWidgetNode=d;this._searchDijitReady=new q;this._layerInfosObj=c;this.own(c.on("layerInfosFilterChanged",h.hitch(this,this._onLayerInfosFilterChanged)));w(this._getConfigInfo(b,a)).then(h.hitch(this,function(f){return r(this._convertConfig(f)).then(function(l){return m.filter(l,function(k){return k})})})).then(h.hitch(this,function(f){b.sources=f;this._searchDijit=new C(b,e);this._searchDijit.startup();this.setFocus();this._searchDijitReady.resolve(this._searchDijit)}))},
searchDijit:function(){return this._searchDijitReady},setFocus:function(){this._searchDijit&&(this._mainWidgetNode.openAtStartAysn=!0,p.isAutoFocusFirstNodeWidget(this._mainWidgetNode)&&this._searchDijit.inputNode.focus())},destroy:function(){this._searchDijit&&(this._searchDijit.destroy(),this._searchDijit=null);this.inherited(arguments)},_onLayerInfosFilterChanged:function(b){m.some(b,h.hitch(this,function(a){this._searchDijit&&this._searchDijit.sources&&0<this._searchDijit.sources.length&&m.forEach(this._searchDijit.sources,
function(c){c._featureLayerId===a.id&&c.featureLayer.setDefinitionExpression(a.getFilter())})}))},_getConfigInfo:function(b,a){return b&&b.sources&&0<b.sources.length?(a=null,this._searchLayer(b.map)&&b.upgradeFromGeocoder?(a=b.map.itemInfo.itemData.applicationProperties.viewing.search,a=m.map(a.layers,h.hitch(this,function(c,e){e.hintText=c;return this._getQueryTypeGeocoder(b.map,e)},a.hintText)),r(a).then(h.hitch(this,function(c){b.sources=[].concat(c).concat(b.sources);return b}))):b):w(this._getSourcesFromPortalAndWebmap(b.map,
a)).then(h.hitch(this,function(c){return{allPlaceholder:"",showInfoWindowOnSelect:!1,sources:c}}))},_convertConfig:function(b){return m.map(b.sources,h.hitch(this,function(a){var c=new q;if(a&&a.url&&"locator"===a.type){var e={locator:new D(a.url||""),outFields:["*"],singleLineFieldName:a.singleLineFieldName||"",name:p.stripHTML(a.name||""),placeholder:p.stripHTML(a.placeholder||""),countryCode:a.countryCode||"",maxSuggestions:a.maxSuggestions,maxResults:a.maxResults||6,zoomScale:a.zoomScale||5E4,
useMapExtent:!!a.searchInCurrentMapExtent};a.enableLocalSearch&&(e.localSearchOptions={minScale:a.localSearchMinScale,distance:a.localSearchDistance});c.resolve(e)}else a&&a.url&&"query"===a.type?(e=new E(a.url||null,{outFields:["*"]}),this.own(v(e,"load",h.hitch(this,function(d){var f=d.layer,l=this._getInfoTemplate(f,a,a.displayField),k=null;a.searchFields&&0<a.searchFields.length?k=a.searchFields:(k=[],m.forEach(f.fields,function(n){"esriFieldTypeOID"!==n.type&&n.name!==f.objectIdField&&"esriFieldTypeGeometry"!==
n.type&&k.push(n.name)}));d={featureLayer:f,outFields:["*"],searchFields:k,displayField:a.displayField||"",exactMatch:!!a.exactMatch,name:p.stripHTML(a.name||""),placeholder:p.stripHTML(a.placeholder||""),maxSuggestions:a.maxSuggestions||6,maxResults:a.maxResults||6,zoomScale:a.zoomScale||5E4,infoTemplate:l,useMapExtent:!!a.searchInCurrentMapExtent,_featureLayerId:a.layerId,enableHighlight:!0,showInfoWindowOnSelect:b.showInfoWindowOnSelect,enableInfoWindow:b.showInfoWindowOnSelect};l||delete d.infoTemplate;
d._featureLayerId&&(l=this._layerInfosObj.getLayerInfoById(d._featureLayerId),f.setDefinitionExpression(l.getFilter()));c.resolve(d)}))),this.own(v(e,"error",function(){c.resolve(null)}))):c.resolve(null);return c}))},_getInfoTemplate:function(b,a,c){var e=this._layerInfosObj.getLayerInfoById(a.layerId),d=e&&e.getInfoTemplate(),f=e&&d;if(e&&!f)return null;f||(d=new F,d.setTitle("\x26nbsp;"),d.setContent(h.hitch(this,"_formatContent",a.name,b,c)));return d},_getSourcesFromPortalAndWebmap:function(b,
a){var c=[],e=null;this._searchLayer(b)&&(e=b.itemInfo.itemData.applicationProperties.viewing.search,m.forEach(e.layers,h.hitch(this,function(d,f){f.hintText=d;c.push(this._getQueryTypeGeocoder(b,f))},e.hintText)));return A.getPortalSelfInfo(a).then(h.hitch(this,function(d){if((d=d.helperServices&&d.helperServices.geocode)&&0<d.length)for(var f=0,l=d.length;f<l;f++){var k=d[f];k&&c.push(this._processSingleLine(k))}return r(c).then(h.hitch(this,function(n){for(var t=[],u=0;u<n.length;u++){var g=n[u];
g&&(g&&"query"===g.type?t.push(g):(g={name:g.name||this._getGeocodeName(g.url),url:g.url,singleLineFieldName:g.singleLineFieldName,placeholder:g.placeholder||g.name||this._getGeocodeName(g.url),maxResults:6,searchInCurrentMapExtent:!1,type:"locator"},g.enableLocalSearch=this._isEsriLocator(g.url),g.localSearchMinScale=3E5,g.localSearchDistance=5E4,t.push(g)))}return t}))}))},_getQueryTypeGeocoder:function(b,a){b=b.getLayer(a.id);var c=null,e=null,d=null;d=x.isDefined(a.subLayer)?a.id+"_"+a.subLayer:
a.id;c=this._layerInfosObj.traversal(function(f){return f.id===d?(e=f,!0):!1});return b&&c&&e?(c=x.isDefined(a.subLayer)?e.url||b.url+"/"+a.subLayer:e.url||b.url,{name:e.title,layerId:d,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(b){this._esriLocatorRegExp.lastIndex=0;return this._esriLocatorRegExp.test(b)},_processSingleLine:function(b){if(b.singleLineFieldName)return b;
if(this._isEsriLocator(b.url))return b.singleLineFieldName="SingleLine",b;var a=new q;B({url:b.url,content:{f:"json"},handleAs:"json",callbackParamName:"callback"}).then(h.hitch(this,function(c){c.singleLineAddressField&&c.singleLineAddressField.name?(b.singleLineFieldName=c.singleLineAddressField.name,a.resolve(b)):(console.warn(b.url+"has no singleLineFieldName"),a.resolve(null))}),h.hitch(this,function(c){console.error(c);a.resolve(null)}));return a.promise},_getGeocodeName:function(b){if("string"!==
typeof b)return"geocoder";b=b.split("/");return b[b.length-2]||"geocoder"},_getGeocoderName:function(b){return this._getGeocodeName(b)},_hasAppSearchInfo:function(b){return b.itemInfo&&b.itemInfo.itemData&&b.itemInfo.itemData.applicationProperties&&b.itemInfo.itemData.applicationProperties.viewing&&b.itemInfo.itemData.applicationProperties.viewing.search},_searchLayer:function(b){if(!this._hasAppSearchInfo(b))return!1;b=b.itemInfo.itemData.applicationProperties.viewing.search;return b.enabled&&0!==
b.layers.length?!0:!1}})});