import { inject } from '@angular/core';
import { CanActivateFn, Router } from '@angular/router';
import { findMenu } from '../services/menu.utils';
import { TenantService } from '../services/tenant.service';

export const hasMenuGuard = (menuCode: string): CanActivateFn => {
  return () => {
    const tenantService = inject(TenantService);
    const router = inject(Router);

    const tenant = tenantService.tenant();

    if (!tenant) {
      return router.createUrlTree(['/']);
    }

    const hasMenu = findMenu(tenant.menues ?? [], menuCode) !== undefined;

    if (hasMenu) {
      return true;
    }

    return router.createUrlTree(['/']);
  };
};
