import type { Match } from 'requests'

import type { User } from 'oidc-client'
import { isFuture } from 'date-fns'
import { isSubscribePopup } from '../../../../helpers'

export enum MatchAccess {
  CanBuyMatch = 'CanBuyMatch',
  NoAccess = 'NoAccess',
  NoCountryAccess = 'NoCountryAccess',
  RedirectToProfile = 'RedirectToProfile',
  ViewMatchPopup = 'ViewMatchPopup',
  ViewMatchPopupWithoutUser = 'ViewMatchPopupWithoutUser',
}

export const getMatchAccess = (match: Match, user: User | undefined) => {
  const {
    access,
    date,
    live,
    sub,
  } = match
  const dateToMs = Date.parse(date?.replace(/ /, 'T')) // без замены не будет работать в сафари
  const dateNowMin10 = dateToMs - 10 * 60 * 1000

  const matchDate = new Date(date)
  const isFutureDate = isFuture(matchDate)

  switch (true) {
    case !user && isFutureDate:
      return MatchAccess.NoAccess
    case !user && !isSubscribePopup():
      return MatchAccess.ViewMatchPopupWithoutUser
    case !sub:
      return MatchAccess.CanBuyMatch
    case !access:
      return MatchAccess.NoCountryAccess
      // проверка времени матча - 10минут
    case ((new Date(dateNowMin10) < new Date()) && !live):
      return MatchAccess.RedirectToProfile
    case live:
      return MatchAccess.ViewMatchPopup
    default:
      return MatchAccess.NoAccess
  }
}
