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

    if (this.$ability.can('display', 'inbox_page')) {
      children.push(this.constructMenu('inbox', 'fa-inbox', '/messages/list/', true))
    }

    if (this.$ability.can('display', 'message_send_page')) {
      children.push(this.constructMenu('message_send', 'fa-paper-plane', '/messagessends/list/', true))
    }

    if (this.$ability.can('display', 'message_templates_page')) {
      children.push(this.constructMenu('message_templates', 'fa-edit', '/templates/list/', true))
    }

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

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