// 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/on dojo/Evented dojo/_base/declare dijit/_WidgetBase dijit/_TemplatedMixin dijit/_WidgetsInTemplateMixin dojo/store/Memory dojo/Deferred dojo/store/Observable dijit/tree/ObjectStoreModel dojo/promise/all dojo/_base/lang dojo/_base/html dojo/_base/array jimu/utils jimu/dijit/_Tree jimu/LayerInfos/LayerInfos jimu/dijit/LoadingIndicator".split(" "),function(x,y,z,A,B,C,D,t,E,F,p,e,u,n,r,G,H,I){var l=z([A,B,C,y],{templateString:'\x3cdiv style\x3d"width:100%;"\x3e\x3cdiv data-dojo-attach-point\x3d"errorTipSection" class\x3d"error-tip-section"\x3e\x3cspan class\x3d"jimu-icon jimu-icon-error"\x3e\x3c/span\x3e\x3cspan class\x3d"jimu-state-error-text" data-dojo-attach-point\x3d"errTip"\x3e${nls.noLayersTip}\x3c/span\x3e\x3c/div\x3e\x3c/div\x3e',
_store:null,_id:0,_treeClass:"layer-chooser-tree",createMapResponse:null,multiple:!1,onlyShowVisible:!1,updateWhenLayerInfosIsShowInMapChanged:!1,onlyShowWebMapLayers:!1,displayTooltipForTreeNode:!1,onlyFilterCurrentLayerWhenAddItem:!1,postMixInProperties:function(){this.nls=window.jimuNls.basicLayerChooserFromMap},postCreate:function(){this.inherited(arguments);u.addClass(this.domNode,"jimu-basic-layer-chooser-from-map");this.multiple=!!this.multiple;this.shelter=new I({hidden:!0});this.shelter.placeAt(this.domNode);
this.shelter.startup();this._createTree();this.basicFilter=e.hitch(this,this.basicFilter);this.filter=l.andCombineFilters([this.basicFilter,this.filter]);this.createMapResponse&&this.setCreateMapResponse(this.createMapResponse)},basicFilter:function(a){var b=new t;this.onlyShowVisible?b.resolve(a.isShowInMap()):b.resolve(!0);return b},filter:function(a){a=new t;a.resolve(!0);return a},getSelectedItems:function(){var a=this.tree.getSelectedItems();return n.map(a,e.hitch(this,function(b){return this.getHandledItem(b)}))},
getAllItems:function(){var a=this.tree.getAllItems(),b=[];n.forEach(a,e.hitch(this,function(c){"root"!==c.id&&(c=this.getHandledItem(c),b.push(c))}));return b},getHandledItem:function(a){return{name:a.name,layerInfo:a.layerInfo}},_isLeafItem:function(a){return a.isLeaf},setCreateMapResponse:function(a){this.createMapResponse=a;H.getInstance(this.createMapResponse.map,this.createMapResponse.itemInfo).then(e.hitch(this,function(b){this.layerInfosObj=b;this.own(x(this.layerInfosObj,"layerInfosChanged",
e.hitch(this,this._onLayerInfosChanged)));this.updateWhenLayerInfosIsShowInMapChanged&&this.own(x(this.layerInfosObj,"layerInfosIsShowInMapChanged",e.hitch(this,this._onLayerInfosIsShowInMapChanged)));this._buildTree(this.layerInfosObj)}))},_onLayerInfosChanged:function(a,b){this._buildTree(this.layerInfosObj);this.emit("update")},_onLayerInfosIsShowInMapChanged:function(a){this._buildTree(this.layerInfosObj);this.emit("update")},_buildTree:function(a){this._clear();u.setStyle(this.errorTipSection,
"display","block");var b=[];this.onlyShowWebMapLayers?(b=a.getLayerInfoArrayOfWebmap(),b=b.concat(a.getTableInfoArrayOfWebmap())):(b=a.getLayerInfoArray(),b=b.concat(a.getTableInfoArray()));0!==b.length&&(u.setStyle(this.errorTipSection,"display","none"),n.forEach(b,e.hitch(this,function(c){this._addDirectLayerInfo(c)})))},_addDirectLayerInfo:function(a){a&&a.getLayerObject().then(e.hitch(this,function(){this._addItem("root",a)}),e.hitch(this,function(b){console.error(b)}))},_clear:function(){var a=
this._store.query({parent:"root"});n.forEach(a,e.hitch(this,function(b){b&&"root"!==b.id&&this._store.remove(b.id)}))},_addItem:function(a,b){var c=null,d=b.getLayerType(),h=this.filter(b);p({layerType:d,valid:h}).then(e.hitch(this,function(f){if(f.valid){var m=e.hitch(this,function(q,w){this._id++;c={name:b.title||"",parent:a,layerInfo:b,type:f.layerType,layerClass:b.layerObject.declaredClass,id:this._id.toString(),isLeaf:q,hasChildren:w};this._store.add(c)}),g=b.getSubLayers(),k=0===g.length,v=
!0;this.onlyFilterCurrentLayerWhenAddItem?(k&&(v=!1),m(k,v)):k?m(k,!1):(g=n.map(g,e.hitch(this,function(q){return this.filter(q)})),p(g).then(e.hitch(this,function(q){(q=n.some(q,function(w){return w}))&&m(k,q)})))}}))},_getRootItem:function(){return{id:"root",name:"Map Root",type:"root",isLeaf:!1,hasChildren:!0}},_createTree:function(){var a=this._getRootItem();a=new D({data:[a],getChildren:function(b){return this.query({parent:b.id})}});this._store=new E(a);a=new F({store:this._store,query:{id:"root"},
mayHaveChildren:e.hitch(this,this._mayHaveChildren)});this.tree=new G({multiple:this.multiple,model:a,showRoot:!1,isLeafItem:e.hitch(this,this._isLeafItem),style:{width:"100%"},onOpen:e.hitch(this,function(b,c){"root"!==b.id&&this._onTreeOpen(b,c)}),onClick:e.hitch(this,function(b,c,d){this._onTreeClick(b,c,d);this.emit("tree-click",b,c,d)}),getIconStyle:e.hitch(this,function(b,c){var d=null;if(!b||"root"===b.id)return null;var h={width:"20px",height:"20px",backgroundRepeat:"no-repeat",backgroundPosition:"center center",
backgroundImage:""},f=window.location.protocol+"//"+window.location.host+require.toUrl("jimu");if(b=this._getIconInfo(b,c).imageName)h.backgroundImage="url("+f+"/css/images/"+b+")",d=h;return d}),getIconClass:e.hitch(this,function(b,c){return this._getIconInfo(b,c).className}),getTooltip:e.hitch(this,function(b){return this.displayTooltipForTreeNode?b.layerInfo.title:""})});u.addClass(this.tree.domNode,this._treeClass);this.tree.placeAt(this.shelter.domNode,"before")},_mayHaveChildren:function(a){return a.hasChildren},
_getIconInfo:function(a,b){var c="",d="";"ArcGISDynamicMapServiceLayer"===a.type||"ArcGISTiledMapServiceLayer"===a.type?b?(c="mapserver_open.png",d="mapservice-layer-icon open"):(c="mapserver_close.png",d="mapservice-layer-icon close"):"GroupLayer"===a.type?b?(c="group_layer2.png",d="group-layer-icon open"):(c="group_layer1.png",d="group-layer-icon close"):"FeatureLayer"===a.type?(a=r.getTypeByGeometryType(a.layerInfo.layerObject.geometryType),"point"===a?(c="point_layer1.png",d="point-layer-icon"):
"polyline"===a?(c="line_layer1.png",d="line-layer-icon"):"polygon"===a&&(c="polygon_layer1.png",d="polygon-layer-icon")):"Table"===a.type?(c="table.png",d="table-icon"):"ArcGISImageServiceLayer"===a.type||"ArcGISImageServiceVectorLayer"===a.type?(c="image_layer.png",d="iamge-layer-icon"):b?(c="mapserver_open.png",d="mapservice-layer-icon open"):(c="mapserver_close.png",d="mapservice-layer-icon close");return{imageName:c,className:d}},_onTreeOpen:function(a,b){if("root"!==a.id){var c=[];b=[];c=a.layerInfo.getSubLayers();
a.checked||(this.shelter.show(),b=n.map(c,e.hitch(this,function(d){return d.getLayerObject()})),p(b).then(e.hitch(this,function(){this.domNode&&(n.forEach(c,e.hitch(this,function(d){this._addItem(a.id,d)})),a.checked=!0,this.shelter.hide())}),e.hitch(this,function(d){console.error(d);this.shelter.hide()})))}},_onTreeClick:function(a,b,c){},destroy:function(){this.shelter&&(this.shelter.destroy(),this.shelter=null);this.tree&&this.tree.destroy();this.inherited(arguments)}});l.createFilterByLayerType=
function(a){e.isArrayLike(a)||(a=[]);return function(b){var c=new t;if(0===a.length)c.resolve(!0);else{var d=[];b.traversal(function(h){d.push(h.getLayerType())});p(d).then(function(h){for(var f=0;f<h.length;f++)for(var m=0;m<a.length;m++)if(h[f]===a[m]){c.resolve(!0);return}c.resolve(!1)},function(h){console.error(h);c.reject(h)})}return c}};l.createFeaturelayerFilter=function(a,b,c,d){var h=["point","polyline","polygon"];a&&0<a.length?(a=n.filter(a,function(f){return 0<=h.indexOf(f)}),0===a.length&&
(a=h)):a=h;return function(f){var m=f.getLayerType();f=f.getLayerObject();return p({layerType:m,layerObject:f}).then(function(g){var k=g.layerType;g=g.layerObject;if("ArcGISDynamicMapServiceLayer"===k||"ArcGISTiledMapServiceLayer"===k||"GroupLayer"===k||"FeatureCollection"===k)return!0;if("FeatureLayer"===k){k=r.getTypeByGeometryType(g.geometryType);k=0<=n.indexOf(a,k);var v=l._shouldPassStatisticsCheck(d,g);return g.url?(g=r.isFeaturelayerUrlSupportQuery(g.url,g.capabilities),k&&g&&v):b&&k}return"Table"===
k?(k=r.isFeaturelayerUrlSupportQuery(g.url,g.capabilities),g=l._shouldPassStatisticsCheck(d,g),c&&k&&g):!1})}};l.createImageServiceLayerFilter=function(a,b){return function(c){var d=c.getLayerType();c=c.getLayerObject();return p({layerType:d,layerObject:c}).then(function(h){var f=h.layerType,m=h.layerObject;return"ArcGISImageServiceLayer"===f||"ArcGISImageServiceVectorLayer"===f?a?r.isImageServiceSupportQuery(h.layerObject.capabilities)?b?l._shouldPassStatisticsCheck(b,m):!0:!1:!0:!1})}};l._shouldPassStatisticsCheck=
function(a,b){return a?(a=!1,a=b.advancedQueryCapabilities?!!b.advancedQueryCapabilities.supportsStatistics:!!b.supportsStatistics):!0};l.createQueryableLayerFilter=function(a){var b=l.createFeaturelayerFilter(["point","polyline","polygon"],!1,!0,a);a=l.createImageServiceLayerFilter(!0,a);return l.orCombineFilters([b,a])};l.andCombineFilters=function(a){return l._combineFilters(a,!0)};l.orCombineFilters=function(a){return l._combineFilters(a,!1)};l._combineFilters=function(a,b){return function(c){var d=
new t,h=n.map(a,function(f){return f(c)});p(h).then(function(f){var m=!1;m=b?n.every(f,function(g){return g}):n.some(f,function(g){return g});d.resolve(m)},function(f){console.error(f);d.reject(f)});return d}};return l});