import { useAuthStore } from 'src/stores/auth';

export type Link = {
  name: string;
  to: string;
  icon: string;
  front_links?: boolean;
  enabled?: boolean;
  color?: string;
  sublinks?: Link[];
  requiredRoles?: string[];
};

export const links = <Link[]>[
  {
    name: 'Home',
    to: '/',
    icon: 'home',
    front_links: false,
    enabled: true,
  },
  {
    name: 'Profile',
    to: '/profile',
    icon: 'account_circle',
    front_links: false,
    enabled: true,
  },
  {
    name: 'Boats',
    to: '/boat',
    icon: 'sailing',
    front_links: true,
    enabled: true,
  },
  {
    name: 'Schedule',
    to: '/schedule',
    icon: 'calendar_month',
    front_links: true,
    enabled: true,
    sublinks: [
      {
        name: 'My View',
        to: '/schedule/list',
        icon: 'list',
        front_links: false,
        enabled: true,
      },
      {
        name: 'Book',
        to: '/schedule/book',
        icon: 'more_time',
        front_links: false,
        enabled: true,
      },
      {
        name: 'Calendar',
        to: '/schedule/view',
        icon: 'calendar_month',
        front_links: false,
        enabled: true,
      },
    ],
  },
  {
    name: 'Certifications',
    to: '/certification',
    icon: 'verified',
    front_links: true,
    enabled: false,
  },
  {
    name: 'Checklists',
    to: '/checklist',
    icon: 'checklist',
    front_links: true,
    enabled: false,
  },
  {
    name: 'Reference',
    to: '/reference',
    icon: 'info_outline',
    front_links: true,
    enabled: false,
  },
  {
    name: 'Tasks',
    to: '/task',
    icon: 'build',
    front_links: true,
    enabled: false,
  },
  {
    name: 'Manage',
    icon: 'tune',
    enabled: true,
    requiredRoles: ['Schedule Admins'],
    color: 'negative',
    sublinks: [
      {
        name: 'Schedule',
        to: '/schedule/manage',
        icon: 'edit_calendar',
        front_links: false,
        enabled: true,
        color: 'accent',
        requiredRoles: ['Schedule Admins'],
      },
    ],
  },
];

const authStore = useAuthStore();

function hasRole(roles: string[] | undefined) {
  if (roles === undefined) return true;
  const hasRole = authStore.hasRequiredRole(roles);
  return hasRole;
}
export const enabledLinks = links
  .filter((link) => link.enabled)
  .map((link) => {
    if (link.sublinks) {
      link.sublinks = link.sublinks.filter(
        (sublink) => sublink.enabled && hasRole(sublink.requiredRoles)
      );
    }
    return link;
  });
