import { NuxtConfig } from '@nuxt/types/config'
import { Ability } from '@casl/ability'
import { ItemMenu, ItemsMenu, Menu } from '~/types/interfaces'
import BaseMenu from '~/composables/layout/Menus/baseMenu'

/**
 * @category composables/layout/Menus
 * @class AgendaMenu
 * Classe pour la construction du Menu agenda
 */
class AgendaMenu extends BaseMenu implements Menu {
  private $ability: Ability;

  /**
   * @constructor
   * Initialisation des services issues du context
   */
  constructor ($config: NuxtConfig, $ability: Ability) {
    super($config)
    this.$ability = $ability
  }

  /**
   * Construit le menu Agenda ou null si aucune page accessible
   * @return {ItemMenu | null}
   */
  getMenu (): ItemMenu | null {
    const children: ItemsMenu = []

    if (this.$ability.can('display', 'agenda_page')) {
      children.push(this.constructMenu('schedule', 'fa-calendar-alt', '/calendar', true))
    }

    if (this.$ability.can('display', 'attendance_page')) {
      children.push(this.constructMenu('attendances', 'fa-calendar-check', '/attendances/list/', true))
    }

    if (children.length === 1) {
      return children[0]
    }
    return children.length > 0 ? this.constructMenu('schedule', 'fa-calendar-alt', undefined, undefined, children) : null
  }
}

export const getAgendaMenu = ($config: NuxtConfig, $ability: Ability) => new AgendaMenu($config, $ability).getMenu()
