// 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/Deferred","dojo/_base/lang","esri/request","jimu/utils"],function(g,f,h,k){return{_cacheDefinition:{},getLayerDefinitionByLayerIdOrUrl:function(c,a){var b=new g,d=this._cacheDefinition[c]||this._cacheDefinition[a];if(d)return b.resolve(d),b;if(!c)return h({url:a,hanleAs:"json",content:{f:"json"},callbackParamName:"callback"}).then(function(e){this._cacheDefinition[a]=e;b.resolve(e)}.bind(this),function(e){b.reject(e)}),b;if(d=this.layerInfosObj.getLayerInfoById(c)){var l=d.getPopupInfo();
this._getServiceDefinitionByLayerInfo(d).then(function(e){this._addAliasForLayerDefinition(e,l);this._cacheDefinition[c]=e;b.resolve(e)}.bind(this))}else b.reject("Invaild layerInfo for layer id: "+c);return b},_getServiceDefinitionByLayerInfo:function(c){return c.getServiceDefinition().then(f.hitch(this,function(a){return a?a:c.getLayerObject().then(f.hitch(this,function(b){return b?k.getFeatureLayerDefinition(b):null}))}))},_addAliasForLayerDefinition:function(c,a){c&&c.fields&&0<c.fields.length&&
c.fields.forEach(f.hitch(this,function(b){var d=this.getFieldAliasByFieldInfo(b,a);d&&(b.alias=d)}))},getFieldAliasByFieldInfo:function(c,a){var b="";if(!c)return b;var d=c.name;b=c.alias||d;a&&(b=this.getAliasFromPopupInfo(d,a));return b},getAliasFromPopupInfo:function(c,a){var b=c;if(!a)return b;(a=a.fieldInfos)&&0<a.length&&a.forEach(function(d){d.fieldName===c&&(b=d.label)});return b},getAliasByFieldName:function(c,a){if(c&&a&&a.fields&&a.fields.length)return(a=a.fields.filter(function(b){return b.name===
c})[0])&&a.alias},updateOptions:function(c,a,b,d){a?(a=f.clone(a),c.removeOption(c.getOptions()),c.addOption(a)):a=[];!b&&0<a.length&&(b=a[0].value);b&&(c.set("value",b),d&&(a=c.getOptions(b))&&"undefined"!==typeof a.label&&c.set("title",a.label))}}});