import { defineStore } from 'pinia'
import { ref } from 'vue'

function getCookie(name: string): string | null {
  const match = document.cookie.match(new RegExp('(?:^|; )' + name + '=([^;]*)'))
  return match ? decodeURIComponent(match[1]) : null
}

function setCookie(name: string, value: string, days = 365) {
  const expires = new Date(Date.now() + days * 864e5).toUTCString()
  document.cookie = `${name}=${encodeURIComponent(value)};expires=${expires};path=/`
}

export const useUiStore = defineStore('ui', () => {
  const isRetroMode = ref(getCookie('retro-mode') === '1')
  const menuOpen = ref(false)

  function toggleRetroMode() {
    isRetroMode.value = !isRetroMode.value
    setCookie('retro-mode', isRetroMode.value ? '1' : '0')
    window.scrollTo({ top: 0, behavior: 'instant' })
  }

  function toggleMenu() {
    menuOpen.value = !menuOpen.value
  }

  function closeMenu() {
    menuOpen.value = false
  }

  return { isRetroMode, menuOpen, toggleRetroMode, toggleMenu, closeMenu }
})
