// 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 dojo/keys".split(" "),function(t,u,v,c,k,w,x,y,h,r,z,l,A,B,C,m,D,E){return t([u,v],{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(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 x(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,zoomScale:a.zoomScale||5E4,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 y(a.url||null,{outFields:["*"]}),this.own(m(e,"load",c.hitch(this,function(f){var g=f.layer;var n=this._getInfoTemplate(g,a,a.displayField);var p=null;a.searchFields&&0<a.searchFields.length?p=a.searchFields:(p=[],k.forEach(g.fields,function(q){"esriFieldTypeOID"!==q.type&&q.name!==g.objectIdField&&"esriFieldTypeGeometry"!==q.type&&p.push(q.name)}));
f={featureLayer:g,outFields:["*"],searchFields:p,displayField:a.displayField||"",exactMatch:!!a.exactMatch,name:h.stripHTML(a.name||""),placeholder:h.stripHTML(a.placeholder||""),maxSuggestions:a.maxSuggestions||6,maxResults:a.maxResults||6,zoomScale:a.zoomScale||5E4,infoTemplate:n,useMapExtent:!!a.searchInCurrentMapExtent,_featureLayerId:a.layerId};n||delete f.infoTemplate;f._featureLayerId&&(n=this.layerInfosObj.getLayerInfoById(f._featureLayerId),g.setDefinitionExpression(n.getFilter()));d.resolve(f)}))),
this.own(m(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=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:h.stripHTML(this.config.searchSourceSettings.allPlaceholder),sources:b,activeSourceIndex:a};c.mixin(b,this.searchOptions);this._urlParams=this._getUrlParams();this.search=new w(b,r.create("div",{"class":"searchControl"}));r.place(this.search.domNode,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.own(m(this.search.inputNode,"keyup",c.hitch(this,function(d){d.keyCode===E.ENTER&&this.emit("init-attribute-search")})));this.own(m(this.search.submitNode,"click",c.hitch(this,function(){this.emit("init-attribute-search")})));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)},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){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){this.search.blur();this.emit("select-result",b)}})});