// 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 dojo/_base/lang dojo/_base/array dojo/_base/connect dojo/promise/all dojo/Deferred dojo/json dojo/i18n!../nls/strings ./util esri/lang esri/request esri/arcgis/utils esri/layers/ArcGISDynamicMapServiceLayer esri/layers/ArcGISImageServiceLayer esri/layers/ArcGISTiledMapServiceLayer esri/layers/DynamicLayerInfo esri/layers/FeatureLayer esri/layers/ImageParameters esri/layers/ImageServiceParameters esri/layers/KMLLayer esri/layers/LayerDrawingOptions esri/layers/MosaicRule esri/layers/RasterFunction esri/layers/TileInfo esri/layers/VectorTileLayer esri/layers/WFSLayer esri/layers/WMSLayer esri/layers/WMSLayerInfo esri/layers/WMTSLayer esri/layers/WMTSLayerInfo esri/layers/WebTiledLayer esri/dijit/PopupTemplate esri/InfoTemplate esri/renderers/jsonUtils esri/geometry/Extent esri/SpatialReference jimu/utils".split(" "),
function(K,B,p,L,E,q,y,F,G,k,v,M,N,O,P,Q,H,R,S,T,U,V,W,X,Y,Z,aa,ba,ha,ia,ca,z,da,I,C,ea,fa){return K(null,{item:null,itemUrl:null,map:null,serviceUrl:null,constructor:function(a){B.mixin(this,a)},addItem:function(a,b){var c=new q;this.map=b;this.item=a;this.itemUrl=this._checkMixedContent(a.itemUrl);this.serviceUrl=this._checkMixedContent(a.url);if("Feature Service"===a.type)return this._addFeatureService();if("Image Service"===a.type)return this._addImageService();if("KML"===a.type)return this._addKML();
if("Map Service"===a.type)return this._addMapService();if("Vector Tile Service"===a.type)return this._addVectorTileService();if("WFS"===a.type)return this._addWFS();if("WMS"===a.type)return this._addWMS();if("WMTS"===a.type)return this._addWMTS();console.warn("Unsupported item type: ",a.type);c.resolve(null);return c},_addFeatureService:function(){var a=this,b=new q,c=this.serviceUrl,g=this.item,d={},f=null,h=[],l=[];a._readItemJsonData().then(function(e){(d=e||{},d.layers)&&0<d.layers.length&&p.forEach(d.layers,
function(m){"undefined"!==typeof m.id&&null!==m.id&&(null===f&&(f=[]),f.push(m.id))});return a._readRestInfo(c)}).then(function(e){if(!e||"string"!==typeof e.type||"Feature Layer"!==e.type&&"Table"!==e.type){var m=[];e&&e.layers&&0<e.layers.length&&p.forEach(e.layers,function(r){m.push(r)});e&&e.tables&&0<e.tables.length&&p.forEach(e.tables,function(r){m.push(r)});0<m.length?p.forEach(m,function(r){var u=!0;null!==f&&0<f.length&&(u=p.some(f,function(w){return w===r.id}));u&&(u=new H(c+"/"+r.id,{id:a._generateLayerId(),
outFields:["*"]}),h.push(a._waitForLayer(u)))}):console.warn("No layers or tables...")}else e=new H(c,{id:a._generateLayerId(),outFields:["*"]}),h.push(a._waitForLayer(e));return E(h)}).then(function(e){p.forEach(e,function(m){l.push(m)});l.reverse();return l}).then(function(){p.forEach(l,function(e){var m=a._processFeatureLayer(e,g,d);e.arcgisProps={title:m.title};e._titleForLegend=m.title;k.isDefined(e.title)||(e.title=m.title);a._addLayer(e)})}).then(function(){b.resolve(l)}).otherwise(function(e){b.reject(e)});
return b},_addImageService:function(){var a=this,b=new q;a._readItemJsonData().then(function(c){return a._newImageServiceLayer(c||{})}).then(function(c){a._addLayer(c);b.resolve(c)}).otherwise(function(c){b.reject(c)});return b},_addKML:function(){var a=this,b=new q;a._newKMLLayer().then(function(c){c&&(c.title=a.item.title);a._addLayer(c);b.resolve(c)}).otherwise(function(c){b.reject(c)});return b},_addMapService:function(){var a=this,b=new q;a._readItemJsonData().then(function(c){return a._newMapServiceLayer(c||
{})}).then(function(c){a._addLayer(c);b.resolve(c)}).otherwise(function(c){b.reject(c)});return b},_addVectorTileService:function(){var a=this,b=new q;a._newVectorTileLayer().then(function(c){a._addLayer(c);b.resolve(c)}).otherwise(function(c){b.reject(c)});return b},_addWFS:function(){var a=this,b=new q;a._readItemJsonData().then(function(c){return a._newWFSLayer(c||{})}).then(function(c){c&&(c.title=a.item.title);a._addLayer(c);b.resolve(c)}).otherwise(function(c){console.log("Error adding WFS",
c);b.reject(c)});return b},_addWMS:function(){var a=this,b=new q;a._readItemJsonData().then(function(c){return a._newWMSLayer(c||{})}).then(function(c){c&&(c.title=a.item.title);a._addLayer(c);b.resolve(c)}).otherwise(function(c){b.reject(c)});return b},_addWMTS:function(){var a=this,b=new q;a._readItemJsonData().then(function(c){return a._newWMTSLayer(c||{})}).then(function(c){c&&(c.title=a.item.title);a._addLayer(c);b.resolve(c)}).otherwise(function(c){b.reject(c)});return b},_addLayer:function(a){var b=
this.item;a&&(a.xtnItemId=b.id,a.xtnAddData=!0,!a.arcgisProps&&b&&(a.arcgisProps={title:b.title},a._titleForLegend=b.title),k.isDefined(a.title)||(a.title=b.title),a._wabProperties={itemLayerInfo:{itemId:b.id,itemUrl:this.itemUrl,portalUrl:b.portalUrl}},this.map.addLayer(a))},_checkMixedContent:function(a){return G.checkMixedContent(a)},_checkUrl:function(a){return M._checkUrl(a)},_checkVectorTileUrl:function(a,b){var c=new q;if(function(d,f){return-1!==d.indexOf(f,d.length-f.length)}(a,".json"))return b.styleUrl=
a,c.resolve(a),c;var g={url:null,content:{},handleAs:"json",callbackParamName:"callback"};this.itemUrl?(g.url=this.itemUrl+"/resources/styles/root.json",v(g,{}).then(function(){b.styleUrl=g.url;c.resolve(g.url)}).otherwise(function(){g.url=a+"/resources/styles/root.json";v(g,{}).then(function(){b.styleUrl=g.url;c.resolve(g.url)}).otherwise(function(){b.url=a;c.resolve(a)})})):(g.url=a+"/resources/styles/root.json",v(g,{}).then(function(){b.styleUrl=g.url;c.resolve(g.url)}).otherwise(function(){b.url=
a;c.resolve(a)}));return c},_generateLayerId:function(){return this._generateLayerIds(1)[0]},_generateLayerIds:function(a){var b,c=[];for(b=0;b<a;b++)c.push(this._generateRandomId());return c},_generateRandomId:function(){var a=null;a="function"===typeof Date.now?Date.now():(new Date).getTime();var b=(""+Math.random()).replace("0.","r");return(a+""+b).replace(/-/g,"")},_makeFeatureLayerTitle:function(a,b,c){try{if(b&&c&&b===c)return b;if(b&&c){var g=c.indexOf(b);if(0===g){var d=c.substring(g+b.length+
1);if(13<=d.length){var f=/^\d+$/;if(f.test(d))return b}}}}catch(h){}return a.replace("{serviceName}",b).replace("{layerName}",c)},_newImageServiceLayer:function(a){var b=new q,c=this._generateLayerId(),g=this.serviceUrl,d={mapLayerId:c,bandIds:null,format:null,compressionQuality:null,opacity:1,visibility:!0};k.isDefined(a.visibility)&&!1===a.visibility&&(d.visibility=!1);k.isDefined(a.opacity)&&(d.opacity=a.opacity);k.isDefined(a.minScale)&&!k.isDefined(d.minScale)&&(d.minScale=a.minScale);k.isDefined(a.maxScale)&&
!k.isDefined(d.maxScale)&&(d.maxScale=a.maxScale);k.isDefined(a.refreshInterval)&&!k.isDefined(d.refreshInterval)&&(d.refreshInterval=a.refreshInterval);!a.popupInfo||d.popupInfo||d.disablePopup||(d.popupInfo=a.popupInfo);a.renderingRule&&!d.renderingRule&&(d.renderingRule=a.renderingRule,a.renderingRule.functionName&&(d.renderingRule.rasterFunction=a.renderingRule.functionName));a.bandIds&&!d.bandIds&&(d.bandIds=a.bandIds);a.mosaicRule&&!d.mosaicRule&&(d.mosaicRule=a.mosaicRule);a.format&&!d.format&&
(d.format=a.format);k.isDefined(a.compressionQuality)&&!k.isDefined(d.compressionQuality)&&(d.compressionQuality=a.compressionQuality);!a.layerDefinition||!a.layerDefinition.definitionExpression||k.isDefined(d.layerDefinition)&&k.isDefined(d.layerDefinition.definitionExpression)||(d.layerDefinition=d.layerDefinition||{},d.layerDefinition.definitionExpression=a.layerDefinition.definitionExpression);a=new S;null!==d.bandIds&&(a.bandIds=d.bandIds);null!==d.format&&(a.format=d.format,null!==d.compressionQuality&&
(a.compressionQuality=d.compressionQuality));d.renderingRule&&d.renderingRule.rasterFunction&&(c=new W(d.renderingRule),a.renderingRule=c);d.mosaicRule&&(c=new V(d.mosaicRule),a.mosaicRule=c);k.isDefined(d.noData)&&(a.noData=d.noData);k.isDefined(d.noDataInterpretation)&&(a.noDataInterpretation=d.noDataInterpretation);k.isDefined(d.interpolation)&&(a.interpolation=d.interpolation);a={imageServiceParameters:a,opacity:d.opacity,visible:d.visibility};k.isDefined(d.mapLayerId)&&(a.id=d.mapLayerId);k.isDefined(d.minScale)&&
(a.minScale=d.minScale);k.isDefined(d.maxScale)&&(a.maxScale=d.maxScale);k.isDefined(d.refreshInterval)&&(a.refreshInterval=d.refreshInterval);k.isDefined(d.resourceInfo)&&(a.resourceInfo=d.resourceInfo);g=new O(this._checkUrl(g),a);this._waitForLayer(g).then(function(f){d.layerDefinition&&d.layerDefinition.definitionExpression&&f.setDefinitionExpression(d.layerDefinition.definitionExpression,!0);!d.disablePopup&&d.popupInfo&&f.setInfoTemplate(new z(d.popupInfo));b.resolve(f)},function(f){b.reject(f)});
return b},_newInfoTemplate:function(a,b){if(a)try{return new z({description:a.description,title:a.title,showAttachments:a.showAttachments,fieldInfos:a.fieldInfos,mediaInfos:a.mediaInfos})}catch(c){console.error(c)}a=new da;k.isDefined(b)&&a.setTitle(b);return a},_newKMLLayer:function(){var a={id:this._generateLayerId()};a=new T(this.serviceUrl,a);return this._waitForLayer(a)},_newMapServiceLayer:function(a){var b=this,c=new q,g=this.serviceUrl,d=this._generateLayerId();v({url:g,content:{f:"json"},
handleAs:"json",callbackParamName:"callback"},{}).then(function(f){var h=null;h={id:d};if(f.tileInfo)h=new P(g,h);else if(f&&f.supportedImageFormatTypes&&-1!==f.supportedImageFormatTypes.indexOf("PNG32")&&(h.imageParameters=new R,h.imageParameters.format="png32"),h=new N(g,h),a&&a.layers&&0<a.layers.length){var l=[],e,m=[],r,u=[],w;p.forEach(a.layers,function(n){n.layerDefinition&&n.layerDefinition.definitionExpression&&(l[n.id]=n.layerDefinition.definitionExpression);if(n.layerDefinition&&n.layerDefinition.source){e=
null;w=n.layerDefinition.source;if("mapLayer"===w.type){var t=p.filter(f.layers,function(x){return x.id===w.mapLayerId});t.length&&(e=B.mixin(t[0],n))}else e=B.mixin({},n);e&&(e.source=w,delete e.popupInfo,e=new Q(e),a.visibleLayers&&(t="string"===typeof a.visibleLayers?a.visibleLayers.split(","):a.visibleLayers,-1<p.indexOf(t,n.id)?e.defaultVisibility=!0:e.defaultVisibility=!1),m.push(e))}n.layerDefinition&&n.layerDefinition.source&&n.layerDefinition.drawingInfo&&(r=new U(n.layerDefinition.drawingInfo),
u[n.id]=r)});0<l.length&&h.setLayerDefinitions(l);0<m.length&&(h.setDynamicLayerInfos(m,!0),0<u.length&&h.setLayerDrawingOptions(u,!0))}b._waitForLayer(h).then(function(n){var t=null;p.forEach(n.layerInfos,function(x){var A=null;a&&p.some(a.layers,function(J){if(x.id===J.id)return A=J,!0});var D=null;A&&A.popupInfo&&(D=A.popupInfo);D&&(null===t&&(t={}),t[x.id]={infoTemplate:b._newInfoTemplate(D,x.name)})});null===n.infoTemplates&&(t?n.infoTemplates=t:b._setDynamicLayerInfoTemplates(n));c.resolve(n)},
function(n){c.reject(n)})},function(f){c.reject(f)});return c},_getVisibleFeatureLayers:function(a,b){if(!a||!b||0===b.length)return[];b=","+b+",";var c=[],g,d=",";for(g=0;g<a.length;g++)if(null!==a[g].subLayerIds){if(-1===b.indexOf(","+a[g].id+",")||-1<d.indexOf(","+a[g].id+","))d+=a[g].subLayerIds.toString()+","}else-1<b.indexOf(","+a[g].id+",")&&-1===d.indexOf(","+a[g].id+",")&&c.push(a[g].id);return c},_newPopupInfo:function(a,b){if(a&&a.fields){var c={title:a.name,fieldInfos:[],description:null,
showAttachments:!0,mediaInfos:[]};"string"===typeof b&&0<b.length&&(c.title=b);p.forEach(a.fields,function(g){var d=fa.getDefaultPortalFieldInfo(g);d.visible=!0;d.isEditable=g.editable;c.fieldInfos.push(d)});return c}return null},_newVectorTileLayer:function(){var a=this,b=new q,c={},g=this.serviceUrl,d=this._generateLayerId();"string"===typeof g&&0<g.length?this._checkVectorTileUrl(g,c).then(function(f){"string"===typeof f&&0<f.length?(f=a._checkMixedContent(f),f=new Y(f,{id:d,opacity:1,visible:!0}),
a._waitForLayer(f).then(function(h){b.resolve(h)},function(h){b.reject(h)})):b.resolve(null)},function(f){b.reject(f)}):b.resolve(null);return b},_newWFSLayer:function(a){var b=new q;try{var c=this._generateLayerId();if(a&&a.wfsInfo&&a.layerDefinition)var g={id:c,mode:a.mode,showLabels:!0,title:a.title,url:this._checkMixedContent(a.url),customParameters:a.wfsInfo.customParameters,maxFeatures:a.wfsInfo.maxFeatures,name:a.wfsInfo.name,swapXY:a.wfsInfo.swapXY,version:a.wfsInfo.version,geometryType:a.layerDefinition.geometryType,
labelingInfo:a.layerDefinition.drawingInfo.labelingInfo};else{var d=this.serviceUrl;d?G.loadWFSByUrl(b,map,this,d,c,!1):b.reject(Error("Error adding WFS, no URL"));return b}var f=new Z,h=f.on("error",function(l){h&&h.remove();b.reject(l.error)});f.fromJson(g,function(){try{h&&h.remove();if(a&&a.wfsInfo&&a.layerDefinition){k.isDefined(a.opacity)&&f.setOpacity(a.opacity);k.isDefined(a.visibility)&&f.setVisibility(a.visibility);(a.minScale||a.maxScale)&&f.setScaleRange(a.minScale,a.maxScale);var l=a.layerDefinition.drawingInfo;
l&&l.renderer&&(f.renderer=I.fromJson(l.renderer,{geometryType:g.geometryType}));a.popupInfo&&f.setInfoTemplate(new z(a.popupInfo))}b.resolve(f)}catch(e){console.error("Error adding WFS",e),b.reject(ex)}})}catch(l){console.error("Error adding WFS",l),b.reject(l)}return b},_newWMSLayer:function(a){var b=this,c=this.item,g=!1,d=null,f={id:this._generateLayerId()};if(a){var h=[],l=[];p.forEach(a.layers,function(e){g=!0;l.push(new ba({name:e.name,title:e.title,legendURL:e.legendURL,queryable:e.queryable,
showPopup:e.showPopup}));h.push(e.name)},this);a.visibleLayers&&(h=a.visibleLayers);f={customLayerParameters:a.customLayerParameters,customParameters:a.customParameters,layerInfos:l,version:a.version,maxWidth:a.maxWidth,maxHeight:a.maxHeight,featureInfoFormat:a.featureInfoFormat,getFeatureInfoURL:a.featureInfoUrl,getMapURL:a.mapUrl,spatialReferences:a.spatialReferences,title:a.title,copyright:a.copyright,minScale:a.minScale||0,maxScale:a.maxScale||0,format:a.format};c&&c.extent&&(c=new C(c.extent[0][0],
c.extent[0][1],c.extent[1][0],c.extent[1][1],new ea({wkid:4326})),f.extent=c);a.spatialReferences&&0<a.spatialReferences.length&&(d=a.spatialReferences[0]);f={id:this._generateLayerId(),visibleLayers:h,format:"png",transparent:a.firstLayer?!1:!0,opacity:a.opacity,visible:null!==a.visibility?a.visibility:!0,resourceInfo:f,refreshInterval:a.refreshInterval}}a=new aa(this.serviceUrl,f);a=this._waitForLayer(a);a.then(function(e){g||b._setWMSVisibleLayers(e);d&&e.spatialReference&&(e.spatialReference.wkid=
d)});return a},_newWMTSLayer:function(a){if(a){var b=new ca(a.templateUrl,{id:this._generateLayerId(),visible:null!==a.visibility?a.visibility:!0,opacity:a.opacity,copyright:a.copyright,fullExtent:a.fullExtent&&new C(a.fullExtent),initialExtent:a.fullExtent&&new C(a.fullExtent),subDomains:a.subDomains,tileInfo:a.tileInfo?new X(a.tileInfo):null,refreshInterval:a.refreshInterval,wmtsInfo:a.wmtsInfo});if(k.isDefined(a.minScale)||k.isDefined(a.maxScale))b.loaded?b.setScaleRange(a.minScale,a.maxScale):
L.connect(b,"onLoad",function(){b.setScaleRange(a.minScale,a.maxScale)});return this._waitForLayer(b)}var c=new q;c.resolve();return c},_processFeatureLayer:function(a,b,c){var g=this,d=F.search.featureLayerTitlePattern,f=null;c&&c.layers&&0<c.layers.length?p.some(c.layers,function(h){var l=!1;if(h.id===a.layerId){if(h.popupInfo){var e=h.popupInfo;e=y.parse(y.stringify(e));e=new z(e);a.setInfoTemplate(e);l=!0}k.isDefined(h.showLabels)&&a.setShowLabels(h.showLabels);k.isDefined(h.refreshInterval)&&
a.setRefreshInterval(h.refreshInterval);k.isDefined(h.showLegend)&&console.log("");k.isDefined(h.timeAnimation)&&!1===h.timeAnimation&&console.log("");if(e=h.layerDefinition){e.definitionExpression&&a.setDefinitionExpression(e.definitionExpression);e.displayField&&a.displayField(e.displayField);if(e.drawingInfo){if(e.drawingInfo.renderer){var m=y.parse(y.stringify(e.drawingInfo.renderer));var r=I.fromJson(m);m.type&&"classBreaks"===m.type&&(r.isMaxInclusive=!0);a.setRenderer(r)}k.isDefined(e.drawingInfo.transparency)&&
a.setOpacity(1-e.drawingInfo.transparency/100)}k.isDefined(e.minScale)&&a.setMinScale(e.minScale);k.isDefined(e.maxScale)&&a.setMaxScale(e.maxScale);k.isDefined(e.defaultVisibility)&&!1===e.defaultVisibility&&a.setVisibility(!1)}l||g._setFeatureLayerInfoTemplate(a,h.popupInfo);f={url:a.url,id:a.id,itemId:b.id,title:g._makeFeatureLayerTitle(d,b.title,a.name)};return!0}}):(f={url:a.url,id:a.id,itemId:b.id,title:g._makeFeatureLayerTitle(d,b.title,a.name)},g._setFeatureLayerInfoTemplate(a,null,f.title));
return f},_readItemJsonData:function(){return v({url:this.itemUrl+"/data",content:{f:"json"},handleAs:"json"},{})},_readRestInfo:function(a){return v({url:a,content:{f:"json"},handleAs:"json",callbackParamName:"callback"},{})},_setDynamicLayerInfoTemplates:function(a){var b=this,c=null,g=[],d=function(f){var h=b._readRestInfo(a.url+"/"+f.id);h.then(function(l){try{var e=b._newPopupInfo(l);e&&(c[f.id]={infoTemplate:b._newInfoTemplate(e)})}catch(m){console.warn("Error setting popup."),console.error(m)}});
return h};null===a.infoTemplates&&p.forEach(a.layerInfos,function(f){null===c&&(c={});f.subLayerIds||g.push(d(f))});0<g.length&&E(g).then(function(){c&&(a.infoTemplates=c)}).otherwise(function(f){console.warn("Error reading sublayers.");console.error(f)})},_setFeatureLayerInfoTemplate:function(a,b,c){b||(b=this._newPopupInfo(a,c));b=this._newInfoTemplate(b,c);a.setInfoTemplate(b)},_setWMSVisibleLayers:function(a){var b=[];a&&(p.some(a.layerInfos,function(c){if("string"===typeof c.name&&0<c.name.length)if(10>
b.length)b.push(c.name);else return!0}),10>=b.length&&a.setVisibleLayers(b))},_waitForLayer:function(a){var b=new q,c=[];if(a.loaded)return b.resolve(a),b;if(a.loadError)return b.reject(a.loadError),b;var g=function(){p.forEach(c,function(d){d.remove()})};c.push(a.on("load",function(d){g();b.resolve(d.layer)}));c.push(a.on("error",function(d){g();d=d.error;try{d.message&&-1!==d.message.indexOf("Unable to complete")?(console.warn("layerAccessError",d),b.reject(Error(F.search.layerInaccessible))):b.reject(d)}catch(f){b.reject(d)}}));
return b}})});