// 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 dijit/_WidgetBase dojo/Evented dojo/_base/lang dojo/_base/array esri/dijit/Search dojo/dom-construct esri/tasks/locator esri/layers/FeatureLayer jimu/utils jimu/LayerInfos/LayerInfos ./searchSourceUtils dojo/when dojo/Deferred dojo/promise/all dojo/on esri/InfoTemplate dojo/query dojo/keys dojo/_base/event".split(" "),function(r,t,u,c,h,v,w,x,y,k,z,l,A,B,C,q,D,E,F,G){return r([t,u],{config:null,map:null,searchOptions:null,layerInfosObj:null,_urlParams:{},constructor:function(b){c.mixin(this,
b)},postCreate:function(){this._urlParams={}},startup:function(){this.inherited(arguments);z.getInstance(this.map,this.map.itemInfo).then(c.hitch(this,function(b){this.layerInfosObj=b;this.own(this.layerInfosObj.on("layerInfosFilterChanged",c.hitch(this,this.onLayerInfosFilterChanged)));l.setMap(this.map);l.setLayerInfosObj(this.layerInfosObj);l.setAppConfig(this.appConfig);A(l.getConfigInfo(this.config.searchSourceSettings)).then(c.hitch(this,function(a){this.config.searchSourceSettings||(this.config.searchSourceSettings=
a);return C(this._convertConfig(a)).then(function(e){return h.filter(e,function(f){return f})})})).then(c.hitch(this,function(a){this.domNode&&this._init(a)}))}))},_convertConfig:function(b){return h.map(b.sources,c.hitch(this,function(a){var e=new B;if(a&&a.url&&"locator"===a.type){var f={locator:new x(a.url||""),outFields:["*"],singleLineFieldName:a.singleLineFieldName||"",name:k.stripHTML(a.name||""),placeholder:k.stripHTML(a.placeholder||""),countryCode:a.countryCode||"",maxSuggestions:a.maxSuggestions,
maxResults:a.maxResults||6,zoomScale:a.zoomScale||5E4,useMapExtent:!!a.searchInCurrentMapExtent};a.enableLocalSearch&&(f.localSearchOptions={minScale:a.localSearchMinScale,distance:a.localSearchDistance});e.resolve(f)}else a&&a.url&&"query"===a.type?(f=new y(a.url||null,{outFields:["*"]}),this.own(q(f,"load",c.hitch(this,function(d){var g=d.layer;var m=this._getInfoTemplate(g,a,a.displayField);var n=null;a.searchFields&&0<a.searchFields.length?n=a.searchFields:(n=[],h.forEach(g.fields,function(p){"esriFieldTypeOID"!==
p.type&&p.name!==g.objectIdField&&"esriFieldTypeGeometry"!==p.type&&n.push(p.name)}));d={featureLayer:g,outFields:["*"],searchFields:n,displayField:a.displayField||"",exactMatch:!!a.exactMatch,name:k.stripHTML(a.name||""),placeholder:k.stripHTML(a.placeholder||""),maxSuggestions:a.maxSuggestions||6,maxResults:a.maxResults||6,zoomScale:a.zoomScale||5E4,infoTemplate:m,useMapExtent:!!a.searchInCurrentMapExtent,_featureLayerId:a.layerId};m||delete d.infoTemplate;d._featureLayerId&&(m=this.layerInfosObj.getLayerInfoById(d._featureLayerId),
g.setDefinitionExpression(m.getFilter()));e.resolve(d)}))),this.own(q(f,"error",function(){e.resolve(null)}))):e.resolve(null);return e}))},_getInfoTemplate:function(b,a,e){var f;var d=(f=this.layerInfosObj.getLayerInfoById(a.layerId))&&f.getInfoTemplate();var g=f&&d;if(f&&!g)return null;g||(d=new D,d.setTitle("\x26nbsp;"),d.setContent(c.hitch(this,"_formatContent",a.name,b,e)));return d},_init:function(b){var a=1===b.length?0:"all";b={map:this.map,addLayersFromMap:!1,autoNavigate:!1,autoComplete:!0,
minCharacters:0,searchDelay:100,enableInfoWindow:!0,enableHighlight:this.config.searchSourceSettings.showInfoWindowOnSelect,showInfoWindowOnSelect:this.config.searchSourceSettings.showInfoWindowOnSelect,allPlaceholder:k.stripHTML(this.config.searchSourceSettings.allPlaceholder),sources:b,activeSourceIndex:a};c.mixin(b,this.searchOptions);this._urlParams=this._getUrlParams();this.search=new v(b,w.create("div",{"class":"searchControl"},this.domNode));this.own(this.search.on("load",c.hitch(this,this._load)));
this.own(this.search.on("select-result",c.hitch(this,this._selectResult)));this.own(this.search.on("clear-search",c.hitch(this,this._clear)));this.own(this.search.on("search-results",c.hitch(this,this._results)));this.own(this.search.on("suggest-results",c.hitch(this,this._results)));this.search.startup()},_getUrlParams:function(){var b=k.urlToObject(document.location.href);b.query=b.query||{};return b.query},setSearchText:function(b){this.search.set("value",b)},getSearchText:function(){return this.search.get("value")},
getActiveSource:function(){return this.search.activeSource},clearSearchText:function(){this.search&&this.search.clear()},_setSearchString:function(){this._urlParams.find&&(this.search.set("value",this._urlParams.find),setTimeout(c.hitch(this,function(){this.search.search()}),1E3))},onLayerInfosFilterChanged:function(b){h.some(b,c.hitch(this,function(a){this.search&&this.search.sources&&0<this.search.sources.length&&h.forEach(this.search.sources,function(e){e._featureLayerId===a.id&&e.featureLayer.setDefinitionExpression(a.getFilter())})}))},
_load:function(b){this.emit("search-loaded",b);this._setSearchString()},_results:function(b){this.emit("search-results",b)},_clear:function(b){this.emit("clear-search",b)},_selectResult:function(b){this.search.blur();this.emit("select-result",b)},attachTabEventToSearchMenuItems:function(){if(this.search){var b=E(".sourcesMenu",this.search.domNode);if(b&&0<b.length&&0<b[0].children.length&&(b=b[0].children[0])){var a=b.children;h.forEach(a,c.hitch(this,function(e,f){this.own(q(e,"keydown",c.hitch(this,
function(d){d.keyCode===F.TAB&&f===a.length-1&&(G.stop(d),this.emit("setFirstFocusNode"))})))}))}}}})});