import { computed } from 'vue'
import {
  getActiveRolePermissions,
  hasActiveRolePermission,
  hasAnyActiveRolePermission,
} from '@/core/utils/activeRolePermission'
import { useAuthStore } from '@/stores/auth'
import type { AuthPermission } from '@/modules/auth/types/auth.types'

const DEVELOPER_ROLE = 'DEVELOPER'

export function usePermissions() {
  const authStore = useAuthStore()

  const activeRolePermissions = computed<AuthPermission[]>(() =>
    getActiveRolePermissions(authStore.user, authStore.activeRole),
  )

  const isDeveloper = computed(
    () => authStore.activeRole?.name === DEVELOPER_ROLE,
  )

  function hasPermission(permission: string): boolean {
    return hasActiveRolePermission(
      authStore.user,
      authStore.activeRole,
      permission,
    )
  }

  function hasAnyPermission(permissions: string[]): boolean {
    return hasAnyActiveRolePermission(
      authStore.user,
      authStore.activeRole,
      permissions,
    )
  }

  return {
    activeRolePermissions,
    hasPermission,
    hasAnyPermission,
    isDeveloper,
  }
}
