// 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/Deferred dojo/promise/all jimu/portalUtils jimu/portalUrlUtils jimu/Role esri/request esri/kernel esri/lang ./PortalAnalysis".split(" "),function(t,f,m,g,u,n,h,v,p,w,q,x){var k=null,l=t([],{userRole:null,userPortalUrl:null,portalAnalysis:null,portalSelf:null,portalUrl:null,licenseDef:null,licenseLevel:null,federatedServers:null,constructor:function(a){this.portalUrl=a},_clearLoadedInfo:function(){this.portalUrl=this.portalSelf=this.portalAnalysis=
this.userPortalUrl=this.userRole=null},loadPrivileges:function(a){a&&this.portalUrl!==a&&(this._clearLoadedInfo(),this.portalUrl=a);if(this._privilegeLoaded())return a=new g,a.resolve(!0),a;a=n.getPortal(this.portalUrl);var b=new g;(a.haveSignIn()?this._loadUserInfo(a):this._signIn(a)).then(f.hitch(this,function(d){d&&this.isPortal()?this.checkAnalysisLicense().then(function(){b.resolve(d)}):b.resolve(d)}));return b},_signIn:function(a){return a.loadSelfInfo().then(f.hitch(this,function(b){var d=
n.getPortal(b.portalHostname);return null===d?!1:d.signIn().then(f.hitch(this,function(c){return this._loadUserInfo(d,c)}),function(){return!1})}),function(){return!1})},_registerOrgCredential:function(a,b){b=h.getStandardPortalUrl(b);a=f.clone(a.toJson());b+="/sharing/rest";a.server=b;a.resources=[b];w.id.registerToken(a)},_loadUserInfo:function(a,b){var d=a.loadSelfInfo();a=a.getHelpMap();return u([d,a]).then(f.hitch(this,function(c){var e=c[0];c=c[1]&&c[1].helpMap;var r={};c&&(r.helpMap=c.v&&c.m?
c:{v:"1.0",m:c});this.userPortalUrl=e.urlKey?e.urlKey+"."+e.customBaseUrl:this.portalUrl;return e&&e.user?(this.userRole=new v({id:e.user.roleId?e.user.roleId:e.user.role,role:e.user.role}),e.user.privileges&&this.userRole.setPrivileges(e.user.privileges),this.portalSelf=f.mixin(e,r),b&&this._registerOrgCredential(b,this.userPortalUrl),this.portalAnalysis=new x(this.userRole,this.portalSelf),!0):!1}),function(){return!1})},_privilegeLoaded:function(){return null!==this.portalSelf},getUser:function(){return this._privilegeLoaded()?
this.portalSelf.user:null},isAdmin:function(){return this._privilegeLoaded()?this.userRole.isAdmin():!1},getUserPortal:function(){return this._privilegeLoaded()?this.userPortalUrl:null},isPortal:function(){return null!==this.portalSelf&&!0===this.portalSelf.isPortal},livingAtlasConfigEnabled:function(){return this.isPortal()?this.portalSelf&&this.portalSelf.livingAtlasGroupQuery&&0<this.portalSelf.livingAtlasGroupQuery.length:!0},canPerformAnalysis:function(){return null!==this.portalAnalysis&&this.portalAnalysis.canPerformAnalysis()},
canPerformSpatialAnalytics:function(){return null!==this.portalAnalysis&&this.portalAnalysis.canPerformSpatialAnalytics()},canPerformGeoAnalytics:function(){return null!==this.portalAnalysis&&this.portalAnalysis.canPerformGeoAnalytics()},canPerformRasterAnalysis:function(){return null!==this.portalAnalysis&&this.portalAnalysis.canPerformRasterAnalysis()},canPerformGeoEnrichment:function(){return null!==this.portalAnalysis&&this.portalAnalysis._canPerformGeoEnrichment()},hasPrivileges:function(a){var b=
a,d=!1;f.isArray(a)&&0<=a.indexOf("svradvanced")&&(d=!0,b=m.filter(a,function(c){return"svradvanced"!==c}));return d&&this.isPortal()?this.isAdvanceLicense()&&null!==this.portalAnalysis&&this.portalAnalysis.hasPrivileges(b):null!==this.portalAnalysis&&this.portalAnalysis.hasPrivileges(b)},isAdvanceLicense:function(){return"svradvanced"===this.licenseLevel},getFederatedServers:function(){var a=new g;if(this.isPortal()&&this.userRole&&!this.federatedServers){var b=h.getSharingUrl(this.portalUrl)+"/portals/"+
this.portalSelf.id+"/servers";p({url:b,content:{f:"json"}}).then(f.hitch(this,function(d){this.federatedServers=d.servers;a.resolve(this.federatedServers)}),f.hitch(this,function(d){console.log("cannot load federated servers",d);this.federatedServers=[];a.resolve([])}))}else a.resolve(this.federatedServers);return a},getLicense:function(a){var b=new g,d;if(0===a.length)return b.resolve(null),b;a=m.filter(a,function(c){return"HOSTING_SERVER"===c.serverRole},this);0<a.length&&(d=a[0]);a=h.getSharingUrl(this.portalUrl);
p({url:a+"/portals/"+this.portalSelf.id+"/servers/"+d.id,content:{f:"json"}}).then(f.hitch(this,function(c){var e=f.mixin({},c);c=q.isDefined(c.edition)||q.isDefined(c.level)?null:"svradvanced";this.licenseLevel=e.edition?e.edition.name:c;b.resolve({licenseInfo:e,licenseLevel:this.licenseLevel})}),f.hitch(this,function(c){console.log("cannot load hostingServer info",c);b.resolve({})}));return b},checkAnalysisLicense:function(){var a=new g;this.licenseDef?this.licenseDef.isResolved()?a.resolve("svradvanced"===
this.licenseLevel):this.licenseDef.then(f.hitch(this,function(){a.resolve("svradvanced"===this.licenseLevel)})):(this.licenseDef=this.getFederatedServers().then(f.hitch(this,function(b){return this.getLicense(b)})),this.licenseDef.then(f.hitch(this,function(){return a.resolve("svradvanced"===this.licenseLevel)})));return a}});l.getInstance=function(a){null===k&&(k=new l(a));return k};return l});