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

    if (this.$ability.can('display', 'billing_product_page')) {
      children.push(this.constructMenu('billing_product', 'fa-cube', '/intangibles/list/', true))
    }

    if (this.$ability.can('display', 'billing_products_by_student_page')) {
      children.push(this.constructMenu('billing_products_by_student', 'fa-cubes', '/access_intangibles/list/', true))
    }

    if (this.$ability.can('display', 'billing_edition_page')) {
      children.push(this.constructMenu('billing_edition', 'fa-copy', '/billing_edition', true))
    }

    if (this.$ability.can('display', 'billing_accounting_page')) {
      children.push(this.constructMenu('billing_accounting', 'fa-file-alt', '/bill_accountings/list/', true))
    }

    if (this.$ability.can('display', 'billing_payment_list_page')) {
      children.push(this.constructMenu('billing_payment_list', 'fa-credit-card', '/bill_payments_list/list/', true))
    }

    if (this.$ability.can('display', 'pes_page')) {
      children.push(this.constructMenu('pes_export', 'fa-align-justify', '/pes/list/', true))
    }

    if (this.$ability.can('display', 'berger_levrault_page')) {
      children.push(this.constructMenu('berger_levrault_export', 'fa-align-justify', '/berger_levraults/list/', true))
    }

    if (this.$ability.can('display', 'jvs_page')) {
      children.push(this.constructMenu('jvs_export', 'fa-align-justify', '/jvs/list/', true))
    }

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

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