// 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/Deferred jimu/portalUtils jimu/portalUrlUtils jimu/Role esri/kernel ./PortalAnalysis".split(" "),function(h,d,k,g,l,m,n,p){var e=null,f=h([],{userRole:null,userPortalUrl:null,portalAnalysis:null,portalSelf:null,portalUrl: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 k,a.resolve(!0),a;a=g.getPortal(this.portalUrl);return a.haveSignIn()?this._loadUserInfo(a):this._signIn(a)},_signIn:function(a){return a.loadSelfInfo().then(d.hitch(this,function(c){var b=g.getPortal(c.portalHostname);return null===b?!1:null===b.credential?this._loadUserInfo(b):b.signIn().then(d.hitch(this,function(q){return this._loadUserInfo(b,q)}),function(){return!1})}),function(){return!1})},_registerOrgCredential:function(a,c){c=l.getStandardPortalUrl(c);
a=d.clone(a.toJson());c+="/sharing/rest";a.server=c;a.resources=[c];n.id.registerToken(a)},_loadUserInfo:function(a,c){return a.loadSelfInfo().then(d.hitch(this,function(b){this.userPortalUrl=b.urlKey?b.urlKey+"."+b.customBaseUrl:this.portalUrl;return b&&b.user?(this.userRole=new m({id:b.user.roleId?b.user.roleId:b.user.role,role:b.user.role}),b.user.privileges&&this.userRole.setPrivileges(b.user.privileges),this.portalSelf=b,c&&this._registerOrgCredential(c,this.userPortalUrl),this.portalAnalysis=
new p(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},canPerformAnalysis:function(){return null!==this.portalAnalysis&&
this.portalAnalysis.canPerformAnalysis()},hasPrivileges:function(a){return null!==this.portalAnalysis&&this.portalAnalysis.hasPrivileges(a)}});f.getInstance=function(a){null===e&&(e=new f(a));return e};return f});