import { Typography } from '@tidbcloud/uikit'

import { log } from 'common/log'
import { ErrorPanel } from 'dbaas/components/Error'

import { ErrorType } from './Callback'

export default () => {
  const params = new URLSearchParams(window.location.search)
  const error = params.get('error')
  const errDesc = params.get('error_description')
  const message = error ? `${error}:${errDesc}` : errDesc || ''
  if (message) {
    // report only if error exists
    log.auth.warning('Auth Callback Error:', message)
  }

  if (error === ErrorType.Denied && /bookmark/.test(message)) {
    return (
      <ErrorPanel message="Access Denied" withRetryLogin>
        <Typography variant="body-lg" ta="center">
          invalid URL - login could have been initiated from the wrong place (e.g., a bookmark).
        </Typography>
        <Typography variant="body-lg" ta="center">
          Please double check and try again.
        </Typography>
      </ErrorPanel>
    )
  }
  if (/access expired/.test(message.toLowerCase())) {
    return (
      <ErrorPanel message="Access Expired" withRefresh>
        <Typography variant="body-lg" ta="center">
          Your link has expired or has already been used. Please check your email and try again.
        </Typography>
      </ErrorPanel>
    )
  }

  return <ErrorPanel message={message} withRetryLogin />
}
