{"version":3,"file":"RequireRole.mjs","sources":["../../../../src/lib/auth/authGuards/RequireRole.tsx"],"sourcesContent":["import React, { memo, type ReactNode } from 'react';\nimport { Navigate } from 'react-router-dom';\nimport { useAuth } from '../useAuth';\nimport { routes } from '@/lib/routing/routes';\nimport { AuthGuardLoading } from './AuthGuardLoading';\nimport type { Role } from '../types';\n\ninterface RequireRoleProps {\n  children: ReactNode;\n  /** Single role required */\n  role?: Role;\n  /** Any of these roles required */\n  roles?: Role[];\n  /** Redirect path if role check fails */\n  redirectTo?: string;\n  /** Component to show if access denied (not shown during loading) */\n  fallback?: ReactNode;\n}\n\n/**\n * Guard component that requires a specific role.\n * Redirects if user doesn't have the required role.\n *\n * @example\n * ```tsx\n * // Single role\n * <RequireRole role=\"admin\">\n *   <AdminPanel />\n * </RequireRole>\n *\n * // Multiple roles (any of)\n * <RequireRole roles={['admin', 'manager']}>\n *   <ManagementDashboard />\n * </RequireRole>\n *\n * // With access denied fallback\n * <RequireRole role=\"admin\" fallback={<AccessDenied />}>\n *   <AdminPanel />\n * </RequireRole>\n * ```\n */\nexport const RequireRole = memo(\n  ({ children, role, roles, redirectTo = routes.dashboard, fallback }: RequireRoleProps) => {\n    const { isAuthenticated, isLoading, hasRole, hasAnyRole } = useAuth();\n\n    if (isLoading) {\n      return <AuthGuardLoading ariaLabel=\"Verifying role authorization...\" />;\n    }\n\n    if (!isAuthenticated) {\n      return <Navigate to={routes.login} replace />;\n    }\n\n    // Check single role - cast to any since Role and UserRole have compatible values\n    if (role !== undefined && role !== null && !hasRole(role)) {\n      if (fallback !== undefined && fallback !== null) return <>{fallback}</>;\n      return <Navigate to={redirectTo} replace />;\n    }\n\n    // Check multiple roles\n    if (roles !== undefined && roles !== null && !hasAnyRole(roles)) {\n      if (fallback !== undefined && fallback !== null) return <>{fallback}</>;\n      return <Navigate to={redirectTo} replace />;\n    }\n\n    return <>{children}</>;\n  }\n);\n\n/**\n * HOC version of RequireRole\n */\n// @refresh reset\n// eslint-disable-next-line react-refresh/only-export-components\nexport function withRequireRole<P extends object>(\n  Component: React.ComponentType<P>,\n  roleConfig: { role?: Role; roles?: Role[]; redirectTo?: string }\n): React.ComponentType<P> {\n  return function WithRequireRole(props: P): React.ReactElement {\n    return (\n      <RequireRole {...roleConfig}>\n        <Component {...props} />\n      </RequireRole>\n    );\n  };\n}\n"],"names":["RequireRole","memo","children","role","roles","redirectTo","routes","fallback","isAuthenticated","isLoading","hasRole","hasAnyRole","useAuth","jsx","AuthGuardLoading","Navigate","withRequireRole","Component","roleConfig","props"],"mappings":";;;;;;AAyCO,MAAMA,IAAcC;AAAA,EACzB,CAAC,EAAE,UAAAC,GAAU,MAAAC,GAAM,OAAAC,GAAO,YAAAC,IAAaC,EAAO,WAAW,UAAAC,QAAiC;AACxF,UAAM,EAAE,iBAAAC,GAAiB,WAAAC,GAAW,SAAAC,GAAS,YAAAC,EAAA,IAAeC,EAAA;AAE5D,WAAIH,IACK,gBAAAI,EAACC,GAAA,EAAiB,WAAU,kCAAA,CAAkC,IAGlEN,IAKqBL,KAAS,QAAQ,CAACO,EAAQP,CAAI,IACxBI,KAAa,8BAAgB,UAAAA,GAAS,IAC7D,gBAAAM,EAACE,GAAA,EAAS,IAAIV,GAAY,SAAO,IAAC,IAIhBD,KAAU,QAAQ,CAACO,EAAWP,CAAK,IAC9BG,KAAa,8BAAgB,UAAAA,GAAS,IAC7D,gBAAAM,EAACE,GAAA,EAAS,IAAIV,GAAY,SAAO,IAAC,2BAGjC,UAAAH,GAAS,sBAfTa,GAAA,EAAS,IAAIT,EAAO,OAAO,SAAO,IAAC;AAAA,EAgB/C;AACF;AAOO,SAASU,EACdC,GACAC,GACwB;AACxB,SAAO,SAAyBC,GAA8B;AAC5D,WACE,gBAAAN,EAACb,KAAa,GAAGkB,GACf,4BAACD,GAAA,EAAW,GAAGE,GAAO,EAAA,CACxB;AAAA,EAEJ;AACF;"}