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

export const useAuthStore = defineStore('auth', () => {
  const jwt = ref<string | undefined>(undefined)
  const user = ref<any | undefined>(undefined)
  const roles = ref<string[]>([])

  const isLoggedIn = computed(() => jwt.value !== undefined)

  function setJwt(token: string | undefined) {
    jwt.value = token
  }

  function setUser(userIn: any | undefined) {
    user.value = userIn
    setRoles(userIn?.roles ?? [])
  }

  function setRoles(rolesIn: string[]) {
    roles.value = rolesIn
  }

  return {
    jwt,
    user,
    roles,
    isLoggedIn,
    setJwt,
    setUser,
    setRoles
  }
})
