// 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(t,u,v,c,k,w,x,y,h,q,z,l,A,B,C,r,D){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(r(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,zoomScale:a.zoomScale||5E4,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(r(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,q.create("div",{"class":"searchControl"}));q.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.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){this.search.blur();this.emit("select-result",b)}})});