// All material copyright ESRI, All Rights Reserved, unless otherwise specified.
// See http://js.arcgis.com/3.23/esri/copyright.txt for details.
//>>built
define("esri/layers/clustering/ClusterManager","dojo/_base/declare dojo/_base/lang dojo/_base/array dojo/has ../../kernel ../../Evented ../../graphic ../../lang ../../Color ../../graphicsUtils ../../dijit/PopupTemplate ../../dijit/Legend/utils ../../renderers/ClassBreaksRenderer ../../symbols/SimpleMarkerSymbol ../GraphicsLayer ../Field ./GeohashAggregation ./statUtils dojo/i18n!../../nls/jsapi".split(" "),function(l,f,e,z,A,B,q,n,H,C,r,t,D,E,F,u,G,h,v){var k=v.layers.clusters,w=v.widgets.popup,x=
k.numFeatures,y=Math.pow(2,53)-1;l=l(B,{declaredClass:"esri.layers.clustering.ClusterManager",layer:null,aggregationInfo:null,container:null,defaults:{clusterRadius:80,markerSymbol:{color:[77,77,77,255],size:6,angle:0,xoffset:0,yoffset:0,type:"esriSMS",style:"esriSMSCircle",outline:{color:[255,255,255,255],width:.75,type:"esriSLS",style:"esriSLSSolid"}}},_initialized:!1,_map:null,_eventHandles:null,_clusterRadius:null,_renderer:null,_statisticInfos:null,_fields:null,_infoTemplate:null,_clusterGenerator:null,
_singleGraphics:null,constructor:function(a){this._eventHandles=[];this.layer=a.layer;this.setAggregationInfo(a.aggregationInfo)},initialize:function(a){this._initialized=!0;this._map=a;this.container=this._createContainer();this._clusterGenerator=this._createClusterGenerator();this._initClusterGenerator();this._createLayerEventListeners();this._createMapEventListeners();this._createPopupEventListeners()},destroy:function(a){this._clearSingleGraphics();this._removeClusterBoundary();this._destroyEventListeners();
this._removeFromPopup(this.container);this._removeFromPopup(this.layer);this._resetClusterNav();this._destroyClusterGenerator(a);this._destroyContainer();this.layer=this.aggregationInfo=this._map=null},setAggregationInfo:function(a){this.aggregationInfo=a?f.mixin({},a):null;this._applyAggregationInfo()},getClusterRenderer:function(){return this._renderer},getClusterFields:function(){return this._fields||[]},getFeaturesInCluster:function(a){a=a&&a.getAggregationInfo();return this._clusterGenerator?
this._clusterGenerator.getFeaturesInCluster(a):[]},getAggregateGraphics:function(){return this.container?this.container.graphics.slice(0):[]},getSingleGraphics:function(){return this._singleGraphics?this._singleGraphics.slice(0):[]},redraw:function(){this.container&&this.container.redraw()},isClusteringEnabled:function(){return!(!this._clusterGenerator||!this._clusterGenerator.clustersEnabled)},isClusteringActive:function(){return!!(this._clusterGenerator&&0<this._clusterGenerator.getNumFeatures())},
_createContainer:function(){var a=this.layer,b=new F._GraphicsLayer({_child:!0,id:a.id+"_clusters",visible:a.visible,minScale:a.minScale,maxScale:a.maxScale,infoTemplate:this._infoTemplate});b.fields=this._fields;b.setRenderer(this._renderer);b.loaded=!0;b.onLoad(b);b._setMap(this._map,a._div);return a._childLayer=b},_destroyContainer:function(){var a=this.container;a&&(a.clear(),a._unsetMap(this._map,this.layer._div),this.layer._childLayer=null);this.container=null},_createClusterGenerator:function(){return new G({map:this._map,
layer:this.layer,clusterRadius:this._clusterRadius,statisticInfos:this._statisticInfos})},_initClusterGenerator:function(){var a=this._clusterGenerator;a.loaded?(this._applyRenderer(),this._initClusterUpdates()):this._eventHandles.push(a.on("load",f.hitch(this,function(){this._applyRenderer();this._initClusterUpdates()})),a.on("load-error",f.hitch(this,function(a){console.log(a.error)})))},_destroyClusterGenerator:function(a){this._clusterGenerator&&this._clusterGenerator.destroy(a);this._clusterGenerator=
null},_applyAggregationInfo:function(){this._applyClusterRadius();this._applyRenderer();this._clusterGenerator&&!this._clusterGenerator.isUpdateScheduled()&&this.redraw()},_applyClusterRadius:function(){var a=this.aggregationInfo;this._clusterRadius=a&&a.clusterRadius||this.defaults.clusterRadius;this._clusterGenerator&&this._clusterGenerator.setClusterRadius(this._clusterRadius);this._computeClusterRadius()},_computeClusterRadius:function(){var a=this.aggregationInfo||{};a.clusterRadius=this._clusterRadius;
this.aggregationInfo=a},_applyRenderer:function(){var a=this.container,b=this._getClusterRendererInfo(),c=this._compareStatInfos(this._statisticInfos,b.statisticInfos);c&&(this._statisticInfos=b.statisticInfos,this._clusterGenerator&&(this._clusterGenerator.setStatisticInfos(e.map(this._statisticInfos,function(a){return{attributeInfo:a.attributeInfo,statisticType:a.statisticType}})),a&&e.forEach(a.graphics,function(a){this._applyClusterAttributes(a,a.getAggregationInfo())},this)));this._renderer=
b.renderer;this._applyFields();this._applyInfoTemplate(!!c&&this._initialized);a&&(a.setRenderer(this._renderer),this.emit("renderer-change"))},_applyFields:function(){this._fields=this._getFields(this._statisticInfos);this.container&&(this.container.fields=this._fields)},_applyInfoTemplate:function(a){if(this.layer.infoTemplate){var b=this.aggregationInfo,b=b&&b.infoTemplate||this._infoTemplate;a=a||!b?this._createInfoTemplate():this._updateInfoTemplate(b)}else a=null;this._infoTemplate=a;this.container&&
this.container.setInfoTemplate(this._infoTemplate);this._computeInfoTemplate()},_computeInfoTemplate:function(){var a=this.aggregationInfo||{};a.infoTemplate=this._infoTemplate;this.aggregationInfo=a},_getClusterRendererInfo:function(){var a=this.layer&&this.layer.renderer;return a?this._createClusterRenderer(a):this._getDefaultClusterRenderer()},_getDefaultClusterRenderer:function(){var a=this._createContinuousCountRenderer();this._addSizeByCountVariable(a);return{renderer:a,statisticInfos:[]}},
_createContinuousCountRenderer:function(a){var b=new D(null,"cluster_count");b.addBreak({minValue:-y,maxValue:y,symbol:a?new a.constructor(a.toJson()):new E(f.clone(this.defaults.markerSymbol))});return b},_createClusterRenderer:function(a){if(!this._isSupportedRenderer(a))return this._getDefaultClusterRenderer();var b,c=[];if(this._isSimpleRenderer(a))b=this._createContinuousCountRenderer(a.symbol);else{b=new a.constructor(a.toJson());var d=this._getRendererAttributeInfo(a),g;this._isCBRenderer(b)?
(g=h.getClusterField(d,"avg"),c.push(this._getStatInfo(d,"avg")),b.normalizationType=null,b.normalizationField=null,b.normalizationTotal=null):this._isUVRenderer(b)&&(g=h.getClusterField(d,"type"),c.push(this._getStatInfo(d,"type")));b.attributeField=g;b.setValueExpression(null);b.valueExpressionTitle=null;b.setVisualVariables(null);this._setRendererTitle(b,a)}a=this._getSupportedVariables(a);d=this._createClusterVariables(a.allVars,c);b.setVisualVariables(d);a.sizeVars.length||this._isCBSizeRenderer(b)||
this._addSizeByCountVariable(b);return{renderer:b,statisticInfos:c}},_isSupportedRenderer:function(a){if(this._isSimpleRenderer(a))return!0;if(this._isCBRenderer(a)){var b=a.normalizationType;return"function"===typeof a.attributeField||b&&"field"!==b?!1:!0}return this._isUVRenderer(a)?"function"===typeof a.attributeField||a.attributeField2?!1:!0:!1},_isSimpleRenderer:function(a){return-1<a.declaredClass.toLowerCase().indexOf("simplerenderer")},_isCBRenderer:function(a){return-1<a.declaredClass.toLowerCase().indexOf("classbreaksrenderer")},
_isUVRenderer:function(a){return-1<a.declaredClass.toLowerCase().indexOf("uniquevaluerenderer")},_isCBSizeRenderer:function(a){var b=a.infos;if(!this._isCBRenderer(a)||!b||2>b.length)return!1;var c=Infinity,d=-Infinity;e.forEach(b,function(a){if(a=a.symbol){var b=0;switch(a.type){case "simplemarkersymbol":b=a.size;break;case "picturemarkersymbol":b=(a.width+a.height)/2}c=Math.min(c,b);d=Math.max(d,b)}});return Infinity!==c&&-Infinity!==d&&c!==d},_getRendererAttributeInfo:function(a){var b=a.attributeField,
c=b?this.layer.getField(b):null;return{field:b,attributeType:c&&"esriFieldTypeDate"===c.type?"date":null,rotationType:null,valueExpression:a.valueExpression,valueExpressionTitle:a.valueExpressionTitle,normalizationField:a.normalizationField}},_setRendererTitle:function(a,b){a.legendOptions=f.clone(b.legendOptions);a.legendOptions&&a.legendOptions.title||(a.legendOptions=a.legendOptions||{},a.legendOptions.title=t.getRendererTitle(b,this.layer))},_getSupportedVariables:function(a){var b=a.getVisualVariablesForType("colorInfo",
!1)||[],c=a.getVisualVariablesForType("sizeInfo",!1)||[],d=a.getVisualVariablesForType("opacityInfo",!1)||[];a=a.getVisualVariablesForType("rotationInfo",!1)||[];b=e.filter(b,this._variableFilter);c=e.filter(c,this._variableFilter);d=e.filter(d,this._variableFilter);a=e.filter(a,this._variableFilter);return{sizeVars:c,allVars:b.concat(c).concat(d).concat(a)}},_variableFilter:function(a){return"function"!==typeof a.field},_createClusterVariables:function(a,b){return e.map(a,function(a){return this._createVarForAvg(a,
b)},this)},_createVarForAvg:function(a,b){var c=f.clone(a),d=this._getVariableAttributeInfo(a);c.field=h.getClusterField(d,"avg");c.normalizationField=null;c.valueExpression=null;c.valueExpressionTitle=null;d=this._getStatInfo(d,"avg");this._addStatInfo(b,d);this._setVariableTitle(c,a);return c},_getVariableAttributeInfo:function(a){var b="rotationInfo"===a.type,c=b?"angle":null,b=b?a.rotationType:null,d=a.legendOptions&&a.legendOptions.title,g=a.field,e=g?this.layer.getField(g):null;e&&"esriFieldTypeDate"===
e.type&&(c="date");return{field:g,attributeType:c,rotationType:b,valueExpression:a.valueExpression,valueExpressionTitle:a.valueExpressionTitle||a.valueExpression&&d,normalizationField:a.normalizationField}},_setVariableTitle:function(a,b){a.legendOptions&&a.legendOptions.title||(a.legendOptions=a.legendOptions||{},a.legendOptions.title=t.getVisualVariableTitle(b,this.layer))},_getStatInfo:function(a,b){return{statisticHash:h.getStatisticHash(a,b),attributeInfo:a,statisticType:b}},_addStatInfo:function(a,
b){var c=this._findStatInfo(a,b);c?c.attributeInfo.valueExpressionTitle||(c.attributeInfo.valueExpressionTitle=b.attributeInfo.valueExpressionTitle):a.push(b)},_findStatInfo:function(a,b){var c;e.some(a,function(a){a.statisticHash===b.statisticHash&&(c=a);return!!c});return c},_compareStatInfos:function(a,b){var c=e.filter(a,function(a){return!this._findStatInfo(b,a)},this),d=e.filter(b,function(b){return!this._findStatInfo(a,b)},this);return d.length||c.length?{added:d,removed:c}:null},_addSizeByCountVariable:function(a){var b=
this._createSizeByCountVariable();b&&this._addVariable(a,b)},_getSizeByCountVariable:function(a){a=a.getVisualVariablesForType("sizeInfo",!1)||[];return e.filter(a,function(a){return"cluster_count"===a.field})[0]},_updateSizeByCountVariable:function(){var a=this._renderer,b=this._getSizeByCountVariable(a);if(b){var c=this._createSizeByCountVariable();c&&this._replaceVariable(a,b,c)}},_createSizeByCountVariable:function(){var a=this._clusterGenerator,b;if(a){b=a.getCurrentLodStats();var c=a.getNumFeatures(),
a=a.clusters,d=b.min,g=b.max;1===b.count&&d===c||1===a.length&&a[0].count===c?(d=1,g=c):d&&d===g&&(g=2*d);b={type:"sizeInfo",field:"cluster_count",valueUnit:"unknown",minSize:12,maxSize:50,minDataValue:d,maxDataValue:g,legendOptions:{title:x}}}return b},_addVariable:function(a,b){var c=a.visualVariables||[];c.push(b);a.setVisualVariables(c)},_replaceVariable:function(a,b,c){var d=a.visualVariables;b=e.indexOf(d,b);-1<b&&d.splice(b,1);d.push(c);a.setVisualVariables(d)},_getFields:function(a){var b=
[new u({name:"cluster_count",type:"esriFieldTypeInteger"})];e.forEach(a,function(a){var c=a.statisticType;a=a.attributeInfo;var g;"avg"===c?g="date"===a.attributeType?"esriFieldTypeDate":"esriFieldTypeDouble":"type"===c&&(g=a.field?this.layer.getField(a.field).type:"esriFieldTypeString");b.push(new u({name:h.getClusterField(a,c),type:g}))},this);return b},_createInfoTemplate:function(){var a=this._statisticInfos,b=this._renderer,c=[{fieldName:"cluster_count",label:x,visible:!0,format:{digitSeparator:!0,
places:0}}],d=[],g=[n.substitute({count:"{cluster_count}"},k.countSummary)],f=this._isUVRenderer(b)?b.infos:[];e.forEach(a,function(a){var b,e,p=a.statisticType,k=a.attributeInfo,m=h.getClusterField(k,p),l=this._getFieldLabel(a);"avg"===p?(b={fieldName:m,label:l,visible:!0,format:"date"===k.attributeType?{dateFormat:"shortDateShortTime"}:{digitSeparator:!0,places:1}},e=this._getFieldSummary(a,m)):"type"===p&&(e="expression/"+m,b={fieldName:e,visible:!0},d.push({name:m,title:l,returnType:"string",
expression:this._getExpression(f,m)}),e=this._getFieldSummary(a,e));b&&c.push(b);e&&g.push(e)},this);return new r({fieldInfos:c,expressionInfos:d,description:g.join("\x3cbr/\x3e\x3cbr/\x3e")})},_updateInfoTemplate:function(a){var b=a.toJson(),c=this._isUVRenderer(this._renderer)?this._renderer.infos:[];e.forEach(this._statisticInfos,function(a){var d=a.statisticType;a=h.getClusterField(a.attributeInfo,d);"type"===d&&(d=this._findExpressionInfo(a,b))&&(d.expression=this._getExpression(c,a))},this);
return new r(b)},_findExpressionInfo:function(a,b){var c;e.some(b.expressionInfos,function(b){b.name===a&&(c=b);return!!c});return c},_escapeDoubleQuotes:function(a){return a?a.replace(/"/g,'\\"'):""},_getExpression:function(a,b){return["var uvInfos \x3d ["+this._getObjects(a).join(", ")+"];",'var predominantType \x3d Text($feature["'+b+'"]);','var label \x3d "'+this._escapeDoubleQuotes(k.predominantNoneValue)+'";',"for (var i \x3d 0; i \x3c Count(uvInfos); i++) {\nif (uvInfos[i].value \x3d\x3d predominantType) {\nlabel \x3d uvInfos[i].label;\nbreak;\n}\n}\nreturn label;"].join("\n")},
_getObjects:function(a){return e.map(a,function(a){return'{"value": "'+String(a.value)+'","label": "'+this._escapeDoubleQuotes(String(a.label))+'"}'},this)},_getFieldLabel:function(a){var b=a.statisticType,c=a.attributeInfo,d=c.field;a=c.normalizationField;var e="",f;"avg"===b?f=a?k.avgNormFieldLabel:k.avgFieldLabel:"type"===b&&(f=k.predominantFieldLabel);f&&(b=c.valueExpression?c.valueExpressionTitle:this.layer.getFieldLabel(d),a=a&&this.layer.getFieldLabel(a),e=n.substitute({fieldLabel:b||"",normFieldLabel:a||
""},f));return e},_getFieldSummary:function(a,b){var c=a.statisticType,d=a.attributeInfo,e=d.field,f=d.normalizationField,l="",h;"avg"===c?h=f?k.avgNormFieldSummary:k.avgFieldSummary:"type"===c&&(h=k.predominantFieldSummary);h&&(c=d.valueExpression?d.valueExpressionTitle:this.layer.getFieldLabel(e),f=f&&this.layer.getFieldLabel(f),l=n.substitute({fieldLabel:c||"",normFieldLabel:f||"",fieldValue:"{"+b+"}"},h));return l},_removeFromPopup:function(a){var b=this._map.infoWindow,c=b.features;if(c&&c.length){var d=
e.filter(c,function(b){return b.getLayer()!==a});d.length<c.length&&(d.length?b.setFeatures(d):(b.clearFeatures(),b.hide()))}},_resetClusterNav:function(){this._resetClusterNavPartial();this._popupClusterGraphic=null},_resetClusterNavPartial:function(){this._map.infoWindow.removeActions(this._popupActions);this._hideSingleGraphic(this._popupSingleGraphic);this._popupSingleGraphic=null},_addClusterBoundary:function(a,b){var c=b.clusterFillSymbol;c&&(c=new c.constructor(c.toJson()));c=new q(a,c);this._map.graphics.add(c);
this._clusterBoundary=c},_removeClusterBoundary:function(){this._map.graphics.remove(this._clusterBoundary);this._clusterBoundary=null},_createBrowseFeaturesAction:function(){return this._map.infoWindow.addActions([{title:w.NLS_browseFeatures,className:"browseFeatures",callback:f.hitch(this,function(a){a.preventDefault();a=this._map.infoWindow;var b=a.getSelectedFeature(),c=b.getChildGraphics(),d=a.getCurrentAnchor();a.setFeatures(c,{anchor:d});if(c=C.graphicsExtent(c))this._addClusterBoundary(c,
a),a.show(this._getPopupLocation(c,d));this._popupClusterGraphic=b})}])[0]},_getPopupLocation:function(a,b){var c=a.getCenter();b=b.toLowerCase();-1<b.indexOf("top")?c.update(c.x,a.ymax):-1<b.indexOf("bottom")?c.update(c.x,a.ymin):-1<b.indexOf("left")?c.update(a.xmin,c.y):c.update(a.xmax,c.y);return c},_createViewSummaryAction:function(){return this._map.infoWindow.addActions([{title:w.NLS_viewSummary,className:"viewSummary",callback:f.hitch(this,function(a){a.preventDefault();a=this._popupClusterGraphic;
var b=this._map.infoWindow;b.setFeatures([a]);b.show(a.geometry);this._popupClusterGraphic=null})}])[0]},_initClusterUpdates:function(){this._updateSizeVariable();this._updateClusterGraphics();this._eventHandles.push(this._clusterGenerator.on("update-end",f.hitch(this,function(a){a.mapLevelChange&&this._updateSizeVariable();this._updateClusterGraphics()})),this._clusterGenerator.on("index-complete",f.hitch(this,function(){this._updateSizeVariable()})))},_updateClusterGraphics:function(){this._hideSingleGraphics();
var a=[],b=[];e.forEach(this._clusterGenerator.clusters,function(c){var d=new q(c.centroid);d.setAggregationSourceLayer(this.layer);d.setAggregationInfo(c);this._applyClusterAttributes(d,c);1===c.count?(c=this._getSingleGraphic(d),b.push(c)):a.push(d)},this);this._showSingleGraphics(b);this._addGraphics(a)},_addGraphics:function(a){var b=this.container;b.clear();e.forEach(a,function(a){b.add(a)})},_getSingleGraphic:function(a){var b=this._clusterGenerator.getCell(a.getAggregationInfo().primary).features[0],
c=this.layer.renderer,c=c&&c.getSymbol(b),d=this._getSizeByCountVariable(this._renderer);c&&d&&(a=d?this._renderer.getSize(a,{sizeInfo:d,shape:c.style,resolution:this._map.getResolutionInMeters(),scale:this._map.getScale()}):null,b.setSize(a));return b},_showSingleGraphics:function(a){e.forEach(a,this._showSingleGraphic);this._singleGraphics=a},_hideSingleGraphics:function(){e.forEach(this._singleGraphics,function(a){a.setSize(null);this._hideSingleGraphic(a)},this);this._singleGraphics=null},_clearSingleGraphics:function(){e.forEach(this._singleGraphics,
function(a){a.setSize(null)});this._singleGraphics=null},_showSingleGraphic:function(a){a&&(a._suspended=!1,a.draw())},_hideSingleGraphic:function(a){a&&(a._suspended=!0,a.show(),a.draw())},_updateSizeVariable:function(){this.isClusteringEnabled()&&(this._updateSizeByCountVariable(),this.emit("renderer-change"))},_applyClusterAttributes:function(a,b){a.setAttributes(b.attributes)},_createLayerEventListeners:function(){var a=this.layer;this._eventHandles.push(a.on("visibility-change",f.hitch(this,
function(a){this.container.setVisibility(a.visible)})),a.on("scale-range-change",f.hitch(this,function(){this.container.setScaleRange(this.layer.minScale,this.layer.maxScale)})),a.on("renderer-change",f.hitch(this,function(){this._applyRenderer()})),a.on("info-template-change",f.hitch(this,function(){this._applyInfoTemplate()})))},_createMapEventListeners:function(){this._eventHandles.push(this._map.on("zoom-start",f.hitch(this,function(){this._removeClusterBoundary();this._removeFromPopup(this.container);
this._removeFromPopup(this.layer);this._resetClusterNav();this.container.clear();this._hideSingleGraphics()})))},_createPopupEventListeners:function(){var a=this._map.infoWindow;this._eventHandles.push(a.on("selection-change",f.hitch(this,function(){this._resetClusterNavPartial();var a=this._map.infoWindow.getSelectedFeature(),c=a&&a.getLayer();if(c){var d;c===this.container?(this._removeClusterBoundary(),d=this._createBrowseFeaturesAction()):c===this.layer&&a._suspended?(d=this._createViewSummaryAction(),
this._popupSingleGraphic=a,this._showSingleGraphic(a)):this._removeClusterBoundary();this._popupActions=d?[d]:null}})),a.on("clear-features",f.hitch(this,function(a){this._resetClusterNav();a.isIntermediate||this._removeClusterBoundary()})),a.on("hide",f.hitch(this,function(){this._popupSingleGraphic&&this._popupSingleGraphic.hide();this._clusterBoundary&&this._clusterBoundary.hide()})),a.on("show",f.hitch(this,function(){this._popupSingleGraphic&&this._popupSingleGraphic.show();this._clusterBoundary&&
this._clusterBoundary.show()})))},_destroyEventListeners:function(){e.forEach(this._eventHandles,function(a){a.remove()})}});z("extend-esri")&&f.setObject("layers.clustering.ClusterManager",l,A);return l});