export default defineNuxtPlugin(() => {
  // Initialize the player store on client side
  const store = usePlayerStore()

  // Restore volume from localStorage
  if (typeof localStorage !== 'undefined') {
    const savedVolume = localStorage.getItem('librodrome-volume')
    if (savedVolume) {
      store.setVolume(parseFloat(savedVolume))
    }

    const savedMode = localStorage.getItem('librodrome-mode') as 'guided' | 'free' | null
    if (savedMode) {
      store.setMode(savedMode)
    }

    // Watch for changes and persist
    watch(() => store.volume, (vol) => {
      localStorage.setItem('librodrome-volume', String(vol))
    })

    watch(() => store.mode, (mode) => {
      localStorage.setItem('librodrome-mode', mode)
    })
  }
})
