/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{__decorate as e}from"tslib";import{id as t}from"../kernel.js";import{EventedAccessor as r}from"../core/Evented.js";import{fixJson as s}from"../core/lang.js";import{property as i}from"../core/accessorSupport/decorators.js";import{subclass as o}from"../core/accessorSupport/decorators/subclass.js";let h=class extends r{constructor(e){super(e),this._oAuthCred=null,this.tokenRefreshBuffer=2,e?._oAuthCred&&(this._oAuthCred=e._oAuthCred)}initialize(){this.resources=this.resources||[],null==this.creationTime&&(this.creationTime=Date.now())}refreshToken(){return t.refreshToken(this)}refreshServerTokens(){return t.refreshServerTokens(this)}emitTokenChange(e){clearTimeout(this._refreshTimer);const r=t,s=this.server?r.findServerInfo(this.server):null,i=s?.owningSystemUrl,o=i?r.findServerInfo(i):null;!1===e||i&&"portal"!==this.scope&&(!o?.webTierAuth||r.normalizeWebTierAuth)||null==this.expires&&null==this.validity||this._startRefreshTimer(),this.emit("token-change")}destroy(){this.userId=this.server=this.token=this.expires=this.validity=this.resources=this.creationTime=null,this._oAuthCred&&(this._oAuthCred.destroy(),this._oAuthCred=null);const e=t,r=e.credentials.indexOf(this);r>-1&&e.credentials.splice(r,1),this.emitTokenChange(),this.emit("destroy")}toJSON(){const e=s({userId:this.userId,server:this.server,token:this.token,expires:this.expires,validity:this.validity,ssl:this.ssl,isAdmin:this.isAdmin,creationTime:this.creationTime,scope:this.scope}),t=this.resources;return t&&t.length>0&&(e.resources=t.slice()),e}_startRefreshTimer(){clearTimeout(this._refreshTimer);const e=6e4*this.tokenRefreshBuffer,t=2**31-1;let r=(this.validity?this.creationTime+6e4*this.validity:this.expires)-Date.now();r<0?r=0:r>t&&(r=t),this._refreshTimer=setTimeout(this.refreshToken.bind(this),r>e?r-e:r)}};e([i()],h.prototype,"creationTime",void 0),e([i()],h.prototype,"expires",void 0),e([i()],h.prototype,"isAdmin",void 0),e([i()],h.prototype,"oAuthState",void 0),e([i()],h.prototype,"resources",void 0),e([i()],h.prototype,"scope",void 0),e([i()],h.prototype,"server",void 0),e([i()],h.prototype,"ssl",void 0),e([i()],h.prototype,"token",void 0),e([i()],h.prototype,"tokenRefreshBuffer",void 0),e([i()],h.prototype,"userId",void 0),e([i()],h.prototype,"validity",void 0),h=e([o("esri.identity.Credential")],h);const n=h;export{n as default};
