/**
 * KRA - Access Hook
 * Permission checking utilities
 */
import { useCallback } from 'react';
import { useAccess as useUmiAccess } from '@umijs/max';

export function useAccess() {
  const access = useUmiAccess();

  // Check if user has specific route access
  const canAccess = useCallback(
    (path: string): boolean => {
      // Check route permission
      return access?.canAccessRoute?.(path) ?? true;
    },
    [access]
  );

  // Check if user has specific button permission
  const hasButton = useCallback(
    (buttonKey: string): boolean => {
      // Check button permission
      return access?.buttons?.includes(buttonKey) ?? false;
    },
    [access]
  );

  // Check if user has specific authority
  const hasAuthority = useCallback(
    (authorityId: number): boolean => {
      return access?.authorityIds?.includes(authorityId) ?? false;
    },
    [access]
  );

  // Check if user is admin
  const isAdmin = access?.isAdmin ?? false;

  return {
    access,
    canAccess,
    hasButton,
    hasAuthority,
    isAdmin,
  };
}

export default useAccess;
