// 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/lang","dojo/_base/array","dojo/Deferred","jimu/utils","jimu/ServiceBrowserRule"],function(g,l,m,n,p){var q={combineRules:function(f){var c=new p;l.forEach(f,function(b){l.forEach(b.leafTypes,function(a){0>c.leafTypes.indexOf(a)&&c.leafTypes.push(a)});l.forEach(b.serviceTypes,function(a){0>c.serviceTypes.indexOf(a)&&c.serviceTypes.push(a)})});c.getMatchedRule=function(b){var a=null;l.some(f,function(d){return d.isUrlContainsServiceType(b)?(a=d,!0):!1});return a};c.getItem=function(b){var a=
null;return a=(a=c.getMatchedRule(b))?a.getItem(b):c.defaultGetItem(b)};c.getSubItemUrls=function(b){var a=null;return a=(a=c.getMatchedRule(b))?a.getSubItemUrls(b):c.defaultGetSubItemUrls(b)};c.getIconImageName=function(b,a){var d="";if(b.url){var e=c.getMatchedRule(b.url);e&&"function"===typeof e.getIconImageName&&(d=e.getIconImageName(b,a))}return d};return c},getFeaturelayerServiceBrowserRule:function(f,c,b){f=g.clone(f);var a=["point","polyline","polygon"];g.isArrayLike(f)&&0<f.length?(f=l.filter(f,
function(d){return 0<=a.indexOf(d)}),0===f.length&&(f=a)):f=a;return q._getFeaturelayerServiceBrowserRule(f,c,b)},_getFeaturelayerServiceBrowserRule:function(f,c,b){return new p({types:f,leafTypes:["Feature Layer","Table"],serviceTypes:["MapServer","FeatureServer"],_groupLayerType:"Group Layer",_featureLayerType:"Feature Layer",_tableType:"Table",getItem:function(a){var d=new m;this.isUrlEndWithServiceType(a,this.serviceTypes)?d=this.defaultGetItem(a):this.getRestInfo(a).then(g.hitch(this,function(e){e=
this._getItemByLayerDefinition(a,e);d.resolve(e)}),g.hitch(this,function(e){d.reject(e)}));return d},getSubItemUrls:function(a){var d=new m;return d=this.isUrlEndWithServiceType(a)?this._getSubUrlsByServiceUrl(a):this._getSubUrlsByGroupUrl(a)},getIconImageName:function(a,d){var e="";"MapServer"===a.type||"FeatureServer"===a.type?e=d?"mapserver_open.png":"mapserver_close.png":a.type===this._groupLayerType?e=d?"group_layer2.png":"group_layer1.png":a.type===this._featureLayerType?(a=n.getTypeByGeometryType(a.definition&&
a.definition.geometryType),"point"===a?e="point_layer1.png":"polyline"===a?e="line_layer1.png":"polygon"===a&&(e="polygon_layer1.png")):a.type===this._tableType?e="table.png":"root"===a.type&&this._currentUrl&&l.some(this.serviceTypes,g.hitch(this,function(h){return n.isStringEndWith(this._currentUrl,"/"+h)}))&&(e=d?"mapserver_open.png":"mapserver_close.png");return e},_getSubUrlsByServiceUrl:function(a){var d=new m;this.getRestInfo(a).then(g.hitch(this,function(e){var h=[];l.forEach(e.layers,g.hitch(this,
function(k){0<=k.parentLayerId||h.push(a+"/"+k.id)}));b&&l.forEach(e.tables,g.hitch(this,function(k){0<=k.parentLayerId||h.push(a+"/"+k.id)}));d.resolve(h)}),g.hitch(this,function(e){d.reject(e)}));return d},_getSubUrlsByGroupUrl:function(a){var d=new m;this.getRestInfo(a).then(g.hitch(this,function(e){var h=[];if(e.type===this._groupLayerType){var k=this._getServiceUrlByLayerUrl(a);h=l.map(e.subLayers||[],g.hitch(this,function(r){return k+"/"+r.id}))}d.resolve(h)}),g.hitch(this,function(e){d.reject(e)}));
return d},_getItemByLayerDefinition:function(a,d){var e=null,h=d.type;if(h===this._groupLayerType)e={name:d.name,type:h,url:a,definition:d};else if(h===this._featureLayerType||h===this._tableType){var k=!1;h===this._featureLayerType?k=this._validateEsriGeometryType(d.geometryType):h===this._tableType&&(k=!0);k&&(k=!1,(k=c?n.isFeaturelayerUrlSupportQuery(a,d.capabilities):!0)&&(e={name:d.name,type:h,url:a,definition:d}))}return e},_validateEsriGeometryType:function(a){a=n.getTypeByGeometryType(a);
return 0<=this.types.indexOf(a)},_getServiceUrlByLayerUrl:function(a){for(var d="",e=0;e<this.serviceTypes.length;e++){var h=this.serviceTypes[e].toLowerCase(),k=a.toLowerCase().lastIndexOf("/"+h+"/");if(0<=k){d=a.slice(0,k+h.length+1);break}}return d}})},getGeocodeServiceBrowserRule:function(){return new p({leafTypes:["GeocodeServer"],serviceTypes:["GeocodeServer"]})},getGpServiceBrowserRule:function(){return new p({leafTypes:["GPTask"],serviceTypes:["GPServer"],getItem:function(f){var c=new m;this.isUrlEndWithServiceType(f)?
c=this.defaultGetItem(f):this.getRestInfo(f).then(g.hitch(this,function(b){c.resolve({name:b.displayName||b.name,type:"GPTask",url:f,definition:b})}),g.hitch(this,function(b){c.reject(b)}));return c},getSubItemUrls:function(f){var c=new m;this.isUrlEndWithServiceType(f)?this.getRestInfo(f).then(g.hitch(this,function(b){b=l.map(b.tasks||[],g.hitch(this,function(a){return f+"/"+a}));c.resolve(b)}),g.hitch(this,function(b){c.reject(b)})):c.resolve([]);return c},getIconImageName:function(f,c){c="";"GPServer"===
f.type?c="toolbox.png":"GPTask"===f.type&&(c="tool.png");return c}})},getImageServiceBrowserRule:function(f){return new p({leafTypes:["ImageServer"],serviceTypes:["ImageServer"],getItem:function(c){var b=new m;this.isUrlEndWithServiceType(c)?this.defaultGetItem(c).then(g.hitch(this,function(a){f?n.isImageServiceSupportQuery(a.definition.capabilities)?b.resolve(a):b.resolve(null):b.resolve(a)}),g.hitch(this,function(a){b.reject(a)})):b.resolve(null);return b},getIconImageName:function(c,b){b="";"ImageServer"===
c.type&&(b="image_layer.png");return b}})},getQueryableServiceBrowserRule:function(){var f=q.getFeaturelayerServiceBrowserRule(["point","polyline","polygon"],!0,!0),c=q.getImageServiceBrowserRule(!0);return q.combineRules([f,c])}};return q});