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 StatsMenu
 * Classe pour la construction du Menu Statistiques
 */
class StatsMenu 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 Statistique et Dons ou null si aucune page accessible
   * @return {ItemMenu | null}
   */
  getMenu (): ItemMenu | null {
    const children: ItemsMenu = []

    if (this.$ability.can('display', 'report_activity_page')) {
      children.push(this.constructMenu('report_activity', 'fa-chart-bar', '/report_activity', true))
    }

    if (this.$ability.can('display', 'education_quotas_page')) {
      children.push(this.constructMenu('educations_quotas_by_education', 'fa-user-circle',  '/educations_quotas_by_education_year/list/', true))
    }

    if (this.$ability.can('display', 'fede_stats_page')) {
      children.push(this.constructMenu('fede_stats', 'fa-chart-bar', '/statistic/membersfedeonly', true))
    }

    if (this.$ability.can('display', 'structure_stats_page')) {
      children.push(this.constructMenu('structure_stats', 'fa-chart-bar', '/statistic/membersfedeassos', true))
    }

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

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