type Args = {
  domain?: string,
  exdays: number,
  name: string,
  value: string,
}

export const setCookie = ({
  domain = getDomain(),
  exdays,
  name,
  value,
}: Args) => {
  const date = new Date()
  date.setTime(date.getTime() + (exdays * 24 * 60 * 60 * 1000))
  const expires = `expires=${date.toUTCString()}`
  document.cookie = `${name}=${value};${expires};path=/;domain=${domain}`
}

export const removeCookie = (name: string, domain = getDomain()) => {
  const expires = 'expires=Thu, 01 Jan 1970 00:00:01 GMT'
  document.cookie = `${name}=;${expires};path=/;domain=${domain}`
}

export const checkCookie = (name: string) => {
  const cookies = document.cookie
  const token = cookies?.split('; ')
    ?.filter((cookie: string) => cookie?.includes(name))
  return token[0]
}

const getDomain = () => (
  process.env.NODE_ENV === 'development'
    ? 'localhost'
    : '.insports.tv'
)
