import { useContext } from 'react'
import { PermissionContext } from './PermissionContext'
import { PermissionContextType } from './types'

/**
 * 权限Hook
 * 
 * @description 提供便捷的权限检查功能
 * @version Week 2 Day 7 - 任务17
 * 
 * @example
 * ```tsx
 * const MyComponent = () => {
 *   const { user, checkModulePermission } = usePermission()
 *   
 *   if (!checkModulePermission('advanced')) {
 *     return <UpgradePrompt />
 *   }
 *   
 *   return <div>欢迎 {user?.name}</div>
 * }
 * ```
 */
export const usePermission = (): PermissionContextType => {
  const context = useContext(PermissionContext)
  
  if (context === undefined) {
    throw new Error(
      'usePermission must be used within a PermissionProvider. ' +
      'Please wrap your app with <PermissionProvider>.'
    )
  }
  
  return context
}

/**
 * 导出权限相关类型（方便使用）
 */
export type { UserInfo, UserVersion, PermissionContextType } from './types'




