import type { Organization } from '~~/types/keycloak'

export const useUserStore = defineStore('Organization', () => {
  const { user } = useUserSession()
  const _selectedOrganization = ref<Organization | null>(null)
  
  const selectedOrganization = computed<Organization | null>({
    get: () => {
      if (_selectedOrganization.value) {
        return _selectedOrganization.value
      }
      if (!user.value?.organizations || user.value.organizations.length === 0) {
        return null
      }
      return user.value.organizations[0]
    },
    set: (value: Organization | null) => {
      _selectedOrganization.value = value
    }
  })

  return {
    user: user.value,
    organizations: user.value?.organizations,
    selectedOrganization
  }
})
