// 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 esri/tasks/locator esri/layers/FeatureLayer jimu/utils dojo/dom-construct jimu/LayerInfos/LayerInfos ./searchSourceUtils dojo/when dojo/Deferred dojo/promise/all dojo/on esri/InfoTemplate".split(" "),function(r,t,u,c,k,v,w,x,h,y,z,l,A,B,C,q,D){return r([t,u],{_urlParams:{},config:null,map:null,searchOptions:null,layerInfosObj:null,constructor:function(b){this._urlParams={};this.layerInfosObj=
this.searchOptions=this.map=this.config=null;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(d){return k.filter(d,function(e){return e})})})).then(c.hitch(this,function(a){this.domNode&&this._init(a)}))}))},_convertConfig:function(b){return k.map(b.sources,c.hitch(this,function(a){var d=new B;if(a&&a.url&&"locator"===a.type){var e={locator:new w(a.url||""),outFields:["*"],singleLineFieldName:a.singleLineFieldName||"",name:h.stripHTML(a.name||""),placeholder:h.stripHTML(a.placeholder||
""),countryCode:a.countryCode||"",maxSuggestions:a.maxSuggestions,maxResults:a.maxResults||6,useMapExtent:!!a.searchInCurrentMapExtent};a.enableLocalSearch&&(e.localSearchOptions={minScale:a.localSearchMinScale,distance:a.localSearchDistance});d.resolve(e)}else a&&a.url&&"query"===a.type?(e=new x(a.url||null,{outFields:["*"]}),this.own(q(e,"load",c.hitch(this,function(f){var g=f.layer;var m=this._getInfoTemplate(g,a,a.displayField);var n=null;a.searchFields&&0<a.searchFields.length?n=a.searchFields:
(n=[],k.forEach(g.fields,function(p){"esriFieldTypeOID"!==p.type&&p.name!==g.objectIdField&&"esriFieldTypeGeometry"!==p.type&&n.push(p.name)}));f={featureLayer:g,outFields:["*"],searchFields:n,displayField:a.displayField||"",exactMatch:!!a.exactMatch,name:h.stripHTML(a.name||""),placeholder:h.stripHTML(a.placeholder||""),maxSuggestions:a.maxSuggestions||6,maxResults:a.maxResults||6,infoTemplate:m,useMapExtent:!!a.searchInCurrentMapExtent,_featureLayerId:a.layerId};m||delete f.infoTemplate;f._featureLayerId&&
(m=this.layerInfosObj.getLayerInfoById(f._featureLayerId),g.setDefinitionExpression(m.getFilter()));d.resolve(f)}))),this.own(q(e,"error",function(){d.resolve(null)}))):d.resolve(null);return d}))},_getInfoTemplate:function(b,a,d){var e;var f=(e=this.layerInfosObj.getLayerInfoById(a.layerId))&&e.getInfoTemplate();var g=e&&f;if(e&&!g)return null;g||(f=new D,f.setTitle("\x26nbsp;"),f.setContent(c.hitch(this,"_formatContent",a.name,b,d)));return f},_init:function(b){var a={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:h.stripHTML(this.config.searchSourceSettings.allPlaceholder)};var d=1===b.length?0:"all";c.mixin(a,this.searchOptions);this._urlParams=this._getUrlParams();this.search=new v(a,y.create("div",{"class":"searchControl"},this.domNode));this.search.set("sources",
b);this.search.set("activeSourceIndex",d);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=h.urlToObject(document.location.href);b.query=b.query||{};return b.query},
setSearchText:function(b){this.search.set("value",b)},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){k.some(b,c.hitch(this,function(a){this.search&&this.search.sources&&0<this.search.sources.length&&k.forEach(this.search.sources,function(d){d._featureLayerId===a.id&&d.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){b&&this.search.sources[b.sourceIndex].featureLayer&&b.result&&b.result.name&&this.setSearchText(b.result.name);this.search.blur();this.emit("select-result",b)}})});