import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import type { User } from '@/types'

export const useAuthStore = defineStore('auth', () => {
  // Bootstrapped from the SPA shell injected by SpaController
  const user = ref<User | null>(
    (window as unknown as Record<string, unknown>).__PF_USER__ as User | null ?? null
  )

  const isAuthenticated = computed(() => user.value !== null)

  function setUser(u: User | null) {
    user.value = u
  }

  return { user, isAuthenticated, setUser }
})
