// 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/aspect dojo/Deferred dojo/cookie dojo/json dojo/topic dojo/request/script esri/kernel esri/config esri/request esri/urlUtils esri/sniff esri/IdentityManager esri/arcgis/OAuthInfo jimu/portalUrlUtils jimu/utils esri/layers/vectorTiles/kernel".split(" "),function(e,l,C,w,p,D,x,y,h,r,E,F,G,z,H,g,t,I){"function"!==typeof p.getAll&&(p.getAll=function(a){var b=[];(a=p(a))&&b.push(a);return b});z={portalUrl:null,cookiePath:"/",_started:!1,webTierPortalUrls:[],
isInBuilderWindow:function(){return!!window.isBuilder},isInConfigOrPreviewWindow:function(){return t.isInConfigOrPreviewWindow()},isStringStartWith:function(a,b){return a.substr(0,b.length)===b},getCookiePath:function(){return this.cookiePath},setPortalUrl:function(a){(a=g.getStandardPortalUrl(a))&&(a+="/");this.portalUrl=a},getPortalUrl:function(){return this.portalUrl},isWebTierPortal:function(a){var b=new w,c=g.getStandardPortalUrl(a);a=c+"/sharing";var d=g.setHttpsProtocol(c+"/sharing/generateToken?f\x3djson"),
f=g.setHttpsProtocol(a);y.get(d,{jsonp:"callback"}).then(e.hitch(this,function(k){k.token?(this.webTierPortalUrls.push(c),this.removeWabAuthInfo(),h.id.getCredential(f).then(e.hitch(this,function(m){function u(n){var A=n.creationTime||(new Date).getTime(),B=n.expires;0<A&&0<B&&B>A&&setTimeout(function(){y.get(d,{jsonp:"callback"}).then(function(q){q.token&&(n.token=q.token,n.expires=q.expires,n.creationTime=(new Date).getTime(),n.refreshServerTokens(),u(n))},function(q){console.error(q)})},.8*(B-
A))}m.token||(m.token=k.token);m.expires||(m.expires=k.expires);var v=this.findServerFromCorsEnabledServers(m.server);-1<v&&r.defaults.io.corsEnabledServers.splice(v,1);this._pushCorsEnabledServerInfo({host:g.getServerByUrl(c),withCredentials:!0});b.resolve(!0);u(m)}),e.hitch(this,function(){b.resolve(!0)}))):b.resolve(!1)}),e.hitch(this,function(k){console.error(k);b.reject(k)}));return b},addAuthorizedCrossOriginDomains:function(a){if(a&&0<a.length)for(var b=0;b<a.length;b++)this.addWithCredentialDomain(a[b])},
addWithCredentialDomain:function(a){if(a&&"string"===typeof a){var b=r.defaults.io.corsEnabledServers,c=g.getServerByUrl(a);(a=g.getServerWithProtocol(a))||(a="http://"+c);a=this.findServerFromCorsEnabledServers(a);-1<a&&b.splice(a,1);this._pushCorsEnabledServerInfo({host:c,withCredentials:!0})}},findServerFromCorsEnabledServers:function(a){var b=r.defaults.io.corsEnabledServers,c,d=-1;G("esri-cors")&&b&&b.length&&l.some(b,function(f,k){f=!f||"object"!==typeof f||f instanceof RegExp?f:f.host;return!(f instanceof
RegExp)&&f&&(c=0!==f.trim().toLowerCase().indexOf("http"),F.hasSameOrigin(a,c?"http://"+f:f)||c&&F.hasSameOrigin(a,"https://"+f))?(d=k,!0):!1});return d},_pushCorsEnabledServerInfo:function(a){if(a){var b=r.defaults.io.corsEnabledServers,c="charAt charCodeAt concat endsWith indexOf lastIndexOf localeCompare match replace search slice split startsWith substr substring toLocaleLowerCase toLocaleUpperCase toLowerCase toString toUpperCase trim trimLeft trimRight valueOf".split(" ");if("object"===typeof a&&
"string"===typeof a.host){for(var d in a.host)a[d]="function"===typeof a.host[d]?function(){return a.host[d].apply(a.host,arguments)}:a.host[d];a.length=a.host.length;l.forEach(c,function(f){"function"===typeof a.host[f]&&(a[f]=function(){return a.host[f].apply(a.host,arguments)})})}b.push(a)}},tryRegisterCredential:function(a){return this.isValidCredential(a)?l.some(h.id.credentials,e.hitch(this,function(b){return a.token===b.token}))?!1:(h.id.credentials.push(a),!0):!1},registerToken:function(a){var b=
g.getSharingUrl(this.portalUrl);h.id.findCredential(b)&&l.some(h.id.credentials,e.hitch(this,function(c,d){if(this.isValidPortalCredentialOfPortalUrl(this.portalUrl,c))return h.id.credentials.splice(d,1),!0}));return this._getTokenInfo(a).then(function(c){c&&h.id.registerToken(c)})},_getTokenInfo:function(a){var b=g.getPortalSelfInfoUrl(this.portalUrl);return y.get(b+("?f\x3djson\x26token\x3d"+a),{jsonp:"callback"}).then(e.hitch(this,function(c){return c.user?{server:g.getSharingUrl(this.portalUrl),
ssl:c.allSSL,token:a,userId:c.user.username}:null}),function(c){console.error(c);throw Error(window.jimuNls.urlParams.validateTokenError);})},_isInvalidPortalUrl:function(a){return a&&"string"===typeof a&&e.trim(a)},signInPortal:function(a){var b=new w;if(this._isInvalidPortalUrl(a)){a=g.getStandardPortalUrl(a);var c=g.getSharingUrl(a),d=this.getPortalCredential(a);d?setTimeout(e.hitch(this,function(){b.resolve(d)}),0):b=h.id.getCredential(c)}else setTimeout(e.hitch(this,function(){b.reject("Invalid portalurl.")}),
0);return b},_loadPortalSelfInfo:function(a){a=g.getPortalSelfInfoUrl(a);return E({url:a,handleAs:"json",content:{f:"json"},callbackParamName:"callback"})},registerOAuthInfo:function(a,b){if(!a||"string"!==typeof a||!b||"string"!==typeof b)return null;var c=h.id.findOAuthInfo(a);c||(c=window.location.protocol+"//"+window.location.host+require.toUrl("jimu")+"/oauth-callback.html",c=new H({appId:b,expiration:20159,portalUrl:a,authNamespace:"/",popup:!0,popupCallbackUrl:c}),h.id.registerOAuthInfos([c]));
c.appId=b;return c},signOutAll:function(a){var b=g.getStandardPortalUrl(this.portalUrl)+"/sharing/rest",c=g.getPortalSignInUrlFromLocation();c=c+"?returnUrl\x3d"+encodeURIComponent(window.location.href);var d=t.getLocationUrlWithoutHashAndQueryParams();d=t.url.addQueryParamToUrl(d,"action","setportalurl");window.appInfo.isRunInPortal?(this.removeEsriAuthCookieStorage(),window.location.href=b+"/oauth2/signout?client_id\x3darcgisonline\x26redirect_uri\x3d"+c):(this.removeWabAuthInfo(),a&&a.supportsOAuth?
(a=b+"/oauth2/signout?client_id\x3d"+a.appId+"\x26redirect_uri\x3d"+d,window.location.href=a):window.location.href=d)},userHaveSignInPortal:function(a){return!!this.getPortalCredential(e.trim(a||""))},isValidCredential:function(a){var b=!1;if(a){b=a.token;var c=a.server,d=a.scope;b=b&&"string"===typeof b&&e.trim(b);c=c&&"string"===typeof c&&e.trim(c);d="portal"===d||"server"===d;var f=!0;a.expires&&(a=parseInt(a.expires,10),f=(new Date).getTime(),f=a>f);b=b&&c&&d&&f}return b},isValidPortalCredentialOfPortalUrl:function(a,
b){var c=!1;this.isValidCredential(b)&&(c="portal"===b.scope,a=g.isSameServer(a,b.server),c=c&&a);return c},getPortalCredential:function(a){var b=null;a=e.trim(a||"");if(!a)return null;a=g.getStandardPortalUrl(a);(b=this._filterPortalCredential(a,h.id.credentials))||this._tryConvertArcGIScomCrendentialToOrgCredential();return b},_tryConvertArcGIScomCrendentialToOrgCredential:function(){var a=this.portalUrl;if(a&&(a=g.getStandardPortalUrl(a),g.isOrgOnline(a)&&!this._filterPortalCredential(a,h.id.credentials))){var b=
this._filterPortalCredential("http://www.arcgis.com",h.id.credentials);b&&h.id.registerToken({token:b.token,scope:"portal",userId:b.userId,server:a+"/sharing/rest",expires:b.expires})}},saveAndRegisterCookieToCredential:function(a){a=e.clone(a);a.referer=window.location.host;a.scope="portal";a.isAdmin=!!a.isAdmin;this.saveWabAuthInfo(a);var b=a.server+"/sharing/rest";a.server=b;h.id.registerToken(a);return h.id.findCredential(b,a.userId)},registerAuth2Hash:function(a){a=e.clone(a);var b=1E3*parseInt(a.expires_in,
10);b=(new Date).getTime()+b;var c=g.getStandardPortalUrl(a.state.portalUrl);return this.saveAndRegisterCookieToCredential({referer:window.location.host,server:c,token:a.access_token,expires:b,userId:a.username,scope:"portal",isAdmin:!!a.isAdmin})},readWabAuthInfo:function(){var a=null,b=localStorage.getItem("wab_token");try{var c=p("wab_auth");c&&(a=D.parse(c),a.token=b)}catch(d){console.error(d)}return a},saveWabAuthInfo:function(a){a=e.clone(a);this.removeWabAuthInfo();localStorage.setItem("wab_token",
a.token);delete a.token;p("wab_auth",D.stringify(a),{expires:new Date(a.expires),path:"/"})},removeWabAuthInfo:function(){this.removeCookie("wab_auth");localStorage.removeItem("wab_token")},removeEsriAuthCookieStorage:function(){window.localStorage&&window.localStorage.removeItem("esriJSAPIOAuth");window.sessionStorage&&window.sessionStorage.removeItem("esriJSAPIOAuth")},_filterPortalCredential:function(a,b){var c=null;a=g.getStandardPortalUrl(a);b&&0<b.length&&(b=l.filter(b,e.hitch(this,function(d){return this.isValidPortalCredentialOfPortalUrl(a,
d)})),0<b.length&&(c=b[b.length-1]));return c},_removePortalCredential:function(a){var b=e.trim(a||"");if(b){b=g.getStandardPortalUrl(b);for(a=l.filter(h.id.credentials,e.hitch(this,function(c){return this.isValidPortalCredentialOfPortalUrl(b,c)}));0<a.length;)a[0].destroy(),a.splice(0,1);h.id.credentials=l.filter(h.id.credentials,e.hitch(this,function(c){return!this.isValidPortalCredentialOfPortalUrl(b,c)}))}},getUserIdByToken:function(a,b){var c=new w;if(a&&"string"===typeof a&&b&&"string"===typeof b){var d=
g.getStandardPortalUrl(b);b=l.filter(h.id.credentials,e.hitch(this,function(k){var m=k.token===a&&k.userId;k=g.isSameServer(d,k.server);return m&&k}));if(0<b.length){var f=b[0];setTimeout(e.hitch(this,function(){c.resolve(f.userId)}),0);return c}b=g.getCommunitySelfUrl(d);E({url:b,handleAs:"json",content:{f:"json"},callbackParamName:"callback"}).then(e.hitch(this,function(k){c.resolve(k&&k.username||"")}),e.hitch(this,function(k){console.error(k);c.reject("fail to get userId by token")}))}else setTimeout(e.hitch(this,
function(){c.reject("invalid parameters")}),0);return c},xtGetCredentialFromCookie:function(a){var b=this.readWabAuthInfo();if(!b||"object"!==typeof b||!g.isSameServer(a,b.server)||window.location.host!==b.referer)return null;b.expires=parseInt(b.expires,10);var c=(new Date).getTime();if(!(b.expires>c))return this.removeWabAuthInfo(),null;a+="/sharing/rest";b.server=a;(c=h.id.findCredential(a))||h.id.registerToken(b);return c=h.id.findCredential(a)},removeCookie:function(a){var b=this.getCookiePath();
t.removeCookie(a,b)},_getDomainsByServerName:function(a){var b=a.split("."),c=b.length;return l.map(b,e.hitch(this,function(d,f){d=b.slice(f,c);var k="",m=d.length-1;l.forEach(d,e.hitch(this,function(u,v){k+=u;v!==m&&(k+=".")}));return k}))},_publishCurrentPortalUserSignIn:function(a){if(this.isValidCredential(a))try{x.publish("userSignIn",a)}catch(b){console.error(b)}},_publishAnyUserSignIn:function(a){if(this.isValidCredential(a))try{x.publish("anyUserSignIn",a)}catch(b){console.error(b)}},_publishCurrentPortalUserSignOut:function(a){try{x.publish("userSignOut",
a)}catch(b){console.error(b)}},_signInSuccess:function(a){try{this.isValidPortalCredentialOfPortalUrl(this.portalUrl,a)&&this._publishCurrentPortalUserSignIn(a),this._publishAnyUserSignIn(a)}catch(b){console.error(b)}},_bindEvents:function(){C.after(h.id,"signIn",e.hitch(this,function(a,b){console.log(b[1]);C.after(a,"callback",e.hitch(this,function(c,d){this._signInSuccess(d[0],!1)}));return a}))},isStart:function(){return this._started},startup:function(){if(!this._started){if(this.isInConfigOrPreviewWindow()){var a=
window.parent;if(a){var b=a.esri&&a.esri.id;b._wab="builder";if(b){h.id=b;var c=window.esriConfig.defaults.io;a=a.esriConfig.defaults.io;c.corsEnabledServers=a.corsEnabledServers;c.webTierAuthServers=a.webTierAuthServers;c._processedCorsServers=a._processedCorsServers;c.corsStatus=a.corsStatus;Object.defineProperty(I,"id",{get:function(){return b},enumerable:!0,configurable:!0})}}}this._bindEvents();this._started=!0}}};z.startup();return z});