{
  "version": 3,
  "sources": ["../../../src/managers/LogoutManager/LogoutManager.ts"],
  "sourcesContent": ["import BigNumber from 'bignumber.js';\nimport { ToastManager } from 'managers/ToastManager';\nimport { getAccountProvider } from 'providers/helpers/accountProvider';\nimport { ProviderTypeEnum } from 'providers/types/providerFactory.types';\nimport { getTokenExpiration } from 'services/nativeAuth/methods/getTokenExpiration';\nimport {\n  addressSelector,\n  isLoggedInSelector\n} from 'store/selectors/accountSelectors';\nimport { nativeAuthConfigSelector } from 'store/selectors/configSelectors';\nimport { tokenLoginSelector } from 'store/selectors/loginInfoSelectors';\nimport { getStore } from 'store/store';\nimport { IconNamesEnum } from 'types/enums.types';\nimport { getHumanReadableTokenExpirationTime } from './helpers/getHumanReadableTokenExpirationTime';\n\nexport class LogoutManager {\n  private static instance: LogoutManager;\n  private store = getStore();\n  private warningLogoutTimeoutRef: ReturnType<typeof setInterval> | null = null;\n  private actualLogoutTimeoutRef: ReturnType<typeof setInterval> | null = null;\n  private plannedLogoutTimeoutRef: ReturnType<typeof setInterval> | null = null;\n  private plannedLogoutKey: string | null = null;\n\n  public static getInstance(): LogoutManager {\n    if (!LogoutManager.instance) {\n      LogoutManager.instance = new LogoutManager();\n    }\n    return LogoutManager.instance;\n  }\n\n  private constructor() {}\n\n  public init = async () => {\n    if (!this.shouldStart()) {\n      return;\n    }\n\n    const state = this.store.getState();\n    const tokenLogin = tokenLoginSelector(state);\n\n    const { isExpired: isNativeAuthTokenExpired } = getTokenExpiration(\n      tokenLogin?.nativeAuthToken\n    );\n\n    if (isNativeAuthTokenExpired) {\n      const provider = getAccountProvider();\n      provider.logout();\n    } else {\n      this.addPlannedLogout();\n    }\n  };\n\n  public stop = () => {\n    clearTimeout(this.warningLogoutTimeoutRef ?? 0);\n    clearTimeout(this.plannedLogoutTimeoutRef ?? 0);\n    clearTimeout(this.actualLogoutTimeoutRef ?? 0);\n\n    this.warningLogoutTimeoutRef = null;\n    this.plannedLogoutTimeoutRef = null;\n    this.actualLogoutTimeoutRef = null;\n    this.plannedLogoutKey = null;\n  };\n\n  private readonly shouldStart = (): boolean => {\n    const state = this.store.getState();\n    const tokenLogin = tokenLoginSelector(state);\n    const isLoggedIn = isLoggedInSelector(state);\n    return Boolean(tokenLogin?.nativeAuthToken && isLoggedIn);\n  };\n\n  private addPlannedLogout = () => {\n    const provider = getAccountProvider();\n    const address = addressSelector(this.store.getState());\n    const isWebviewLogin = provider.getType() === ProviderTypeEnum.webview;\n\n    // prevent unexpected logout if webview login\n    if (!address || isWebviewLogin) {\n      return;\n    }\n\n    this.addLogoutWarning();\n\n    const tokenLogin = tokenLoginSelector(this.store.getState());\n\n    const { secondsUntilExpires, expiresAt } = getTokenExpiration(\n      tokenLogin?.nativeAuthToken\n    );\n\n    // Handle the actual logout functionality.\n    const secondsUntilExpiresBN = new BigNumber(String(secondsUntilExpires));\n    const plannedLogoutKey = `${address}_${expiresAt}`;\n    const plannedLogoutSet = this.plannedLogoutKey === plannedLogoutKey;\n\n    const isValidInterval =\n      secondsUntilExpires && secondsUntilExpiresBN.isGreaterThan(0);\n\n    if (!isValidInterval || plannedLogoutSet) {\n      return;\n    }\n\n    this.plannedLogoutKey = plannedLogoutKey;\n\n    clearTimeout(this.plannedLogoutTimeoutRef ?? 0);\n    const millisecondsUntilLogout = secondsUntilExpiresBN.times(1000);\n\n    this.actualLogoutTimeoutRef = setTimeout(() => {\n      ToastManager.getInstance().createCustomToast({\n        toastId: 'native-auth-logout',\n        iconClassName: 'warning',\n        title: 'Logging out',\n        icon: IconNamesEnum.close,\n        message: 'Your session has expired!'\n      });\n      this.actualLogoutTimeoutRef = null;\n    }, millisecondsUntilLogout.toNumber() - 3000);\n\n    this.plannedLogoutTimeoutRef = setTimeout(() => {\n      provider.logout();\n      this.plannedLogoutTimeoutRef = null;\n      this.plannedLogoutKey = null;\n    }, millisecondsUntilLogout.toNumber());\n  };\n\n  private readonly addLogoutWarning = () => {\n    if (this.warningLogoutTimeoutRef) {\n      return;\n    }\n\n    const nativeAuthConfig = nativeAuthConfigSelector(this.store.getState());\n    const tokenLogin = tokenLoginSelector(this.store.getState());\n\n    const { secondsUntilExpires } = getTokenExpiration(\n      tokenLogin?.nativeAuthToken\n    );\n\n    const secondsUntilExpiresBN = new BigNumber(String(secondsUntilExpires));\n    const millisecondsUntilLogout = secondsUntilExpiresBN.times(1000);\n\n    if (\n      !secondsUntilExpires ||\n      secondsUntilExpiresBN.isLessThanOrEqualTo(0) ||\n      !nativeAuthConfig?.tokenExpirationToastWarningSeconds\n    ) {\n      return;\n    }\n\n    const logoutWarningOffsetSeconds = new BigNumber(\n      nativeAuthConfig?.tokenExpirationToastWarningSeconds ?? 0\n    );\n\n    const logoutWarningOffsetMilliseconds =\n      logoutWarningOffsetSeconds.times(1000);\n\n    const millisecondsUntilLogoutWarning = secondsUntilExpiresBN\n      .times(1000)\n      .minus(logoutWarningOffsetMilliseconds);\n\n    const readableMinutesUntilLogout = getHumanReadableTokenExpirationTime(\n      millisecondsUntilLogout.toNumber()\n    );\n\n    const timeoutUntilLogoutWarning =\n      millisecondsUntilLogoutWarning.isLessThanOrEqualTo(0)\n        ? 0\n        : millisecondsUntilLogoutWarning.toNumber();\n\n    clearTimeout(this.warningLogoutTimeoutRef ?? 0);\n\n    this.warningLogoutTimeoutRef = setTimeout(() => {\n      ToastManager.getInstance().createCustomToast({\n        toastId: 'native-auth-expired',\n        iconClassName: 'warning',\n        title: 'Session Expiration Warning',\n        icon: IconNamesEnum.hourglass,\n        message: `Your session will expire in ${readableMinutesUntilLogout}!`\n      });\n      this.warningLogoutTimeoutRef = null;\n    }, timeoutUntilLogoutWarning);\n  };\n}\n"],
  "mappings": "0jBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,mBAAAE,IAAA,eAAAC,EAAAH,GAAA,IAAAI,EAAsB,2BACtBC,EAA6B,iCAC7BC,EAAmC,6CACnCC,EAAiC,iDACjCC,EAAmC,0DACnCC,EAGO,4CACPC,EAAyC,2CACzCC,EAAmC,8CACnCC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AAE,kBAAkB,CAC3C,QAAS,sBACT,cAAe,UACf,MAAO,6BACP,KAAM,gBAAc,UACpB,QAAS,+BAA+BC,CAA0B,GACpE,CAAC,EACD,KAAK,wBAA0B,IACjC,EAAGC,CAAyB,CAC9B,CApJuB,CAPvB,OAAc,aAA6B,CACzC,OAAK/B,EAAc,WACjBA,EAAc,SAAW,IAAIA,GAExBA,EAAc,QACvB,CAuJF",
  "names": ["LogoutManager_exports", "__export", "LogoutManager", "__toCommonJS", "import_bignumber", "import_ToastManager", "import_accountProvider", "import_providerFactory", "import_getTokenExpiration", "import_accountSelectors", "import_configSelectors", "import_loginInfoSelectors", "import_store", "import_enums", "import_getHumanReadableTokenExpirationTime", "state", "tokenLogin", "isNativeAuthTokenExpired", "isLoggedIn", "provider", "address", "isWebviewLogin", "secondsUntilExpires", "expiresAt", "secondsUntilExpiresBN", "BigNumber", "plannedLogoutKey", "plannedLogoutSet", "millisecondsUntilLogout", "nativeAuthConfig", "logoutWarningOffsetMilliseconds", "millisecondsUntilLogoutWarning", "readableMinutesUntilLogout", "timeoutUntilLogoutWarning"]
}
