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 Admin2iosMenu
 * Classe pour la construction du Menu Admin 2IOS
 */
class Admin2iosMenu extends BaseMenu implements Menu {
  private $ability: Ability;

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

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

    if (this.$ability.can('display', 'all_accesses_page')) {
      children.push(this.constructMenu('all_accesses', 'fa-users', '/all_accesses/list/', true))
    }

    if (this.$ability.can('display', 'all_organizations_page')) {
      children.push(this.constructMenu('all_organizations', 'fa-building', '/organization_params/list/', true))
    }

    if (this.$ability.can('display', 'tips_page')) {
      children.push(this.constructMenu('tips', 'fa-info-circle', '/tips/list/', true))
    }

    if (this.$ability.can('display', 'dgv_page')) {
      children.push(this.constructMenu('dgv', 'fa-house-damage', '/admin2ios/dgv', true))
    }

    if (this.$ability.can('display', 'cmf_cotisation_page')) {
      children.push(this.constructMenu('cmf_cotisation', 'fa-info-circle', '/admin2ios/cotisationcmf', true))
    }

    if (this.$ability.can('display', 'right_page')) {
      children.push(this.constructMenu('right_menu', 'fa-balance-scale-right', '/admin2ios/right', true))
    }

    if (this.$ability.can('display', 'tree_page')) {
      children.push(this.constructMenu('tree_menu', 'fa-sitemap', '/admin2ios/tree', true))
    }

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

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