export function getCookie(name) {
  let cookieValue = null
  if (document.cookie && document.cookie !== '') {
    const cookies = document.cookie.split(';')
    for (let i = 0; i < cookies.length; i++) {
      const cookie = cookies[i].trim()
      // Does this cookie string begin with the name we want?
      if (cookie.substring(0, name.length + 1) === (name + '=')) {
        cookieValue = decodeURIComponent(cookie.substring(name.length + 1))
        break
      }
    }
  }
  return cookieValue
}

const twentyYears = 20 * 365 * 24 * 60 * 60 * 1000

export function setCookie(name, value, expiration=twentyYears) {
  const body = [ name, value ].map(encodeURIComponent).join("=")
  const expires = new Date(Date.now() + expiration).toUTCString()
  const cookie = `${body}; domain=; path=/; SameSite=Lax; expires=${expires}`
  document.cookie = cookie
}
