import { useCallback } from 'react';
import { usePermissionStore } from '../store/permission/permissionStore';

export function usePermission() {
  const permissions = usePermissionStore((state) => state.permissions);
  const permissionStatus = usePermissionStore((state) => state.status);
  const permissionsReady = permissionStatus === 'ready';
  const hasPermission = useCallback(
    (code: string): boolean => permissionsReady && permissions.includes(code),
    [permissions, permissionsReady],
  );
  const hasAnyPermission = useCallback(
    (...codes: string[]): boolean =>
      permissionsReady && codes.some((code) => permissions.includes(code)),
    [permissions, permissionsReady],
  );
  const hasAllPermissions = useCallback(
    (...codes: string[]): boolean =>
      permissionsReady && codes.every((code) => permissions.includes(code)),
    [permissions, permissionsReady],
  );

  return {
    permissions,
    permissionStatus,
    permissionsReady,
    hasPermission,
    hasAnyPermission,
    hasAllPermissions,
  };
}
