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

/**
 * @category composables/layout/Menus
 * @class EducationalMenu
 * Classe pour la construction du Menu Suivi pédagogique
 */
class EducationalMenu 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 Suivi pédagogique ou null si aucune page accessible
   * @return {ItemMenu | null}
   */
  getMenu (): ItemMenu | null {
    const children: ItemsMenu = []

    if (this.$ability.can('display', 'criteria_notations_page')) {
      children.push(this.constructMenu('criteria_notations', 'fa-bars', '/criteria_notations/list/', true))
    }

    if (this.$ability.can('display', 'education_notation_config_page')) {
      children.push(this.constructMenu('education_notation_configs', 'fa-bars',  '/education_notation_configs/list/', true))
    }

    if (this.$ability.can('display', 'seizure_period_page')) {
      children.push(this.constructMenu('seizure_period', 'fa-calendar-alt', '/education_teachers/list/', true))
    }

    if (this.$ability.can('display', 'test_seizure_page')) {
      children.push(this.constructMenu('test_seizure', 'fa-pencil-alt', '/education_input/list/', true))
    }

    if (this.$ability.can('display', 'test_validation_page')) {
      children.push(this.constructMenu('test_validation', 'fa-check', '/education_notations/list/', true))
    }

    if (this.$ability.can('display', 'examen_results_page')) {
      children.push(this.constructMenu('examen_results', 'fa-graduation-cap', '/examen_convocations/list/', true))
    }

    if (this.$ability.can('display', 'education_by_student_validation_page')) {
      children.push(this.constructMenu('education_by_student_validation', 'fa-check-square', '/education_by_student/list/', true))
    }

    if (children.length === 1) {
      return children[0]
    } else if (children.length > 0) {
      return this.constructMenu('education_state', 'fa-graduation-cap', undefined, undefined, children)
    } else {
      return null
    }
  }
}

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