import AbstractMenuBuilder from '~/services/layout/menuBuilder/abstractMenuBuilder'
import type { MenuGroup, MenuItems } from '~/types/layout'
import { MENU_LINK_TYPE } from '~/types/enum/layout'

/**
 * Menu Distinctions
 */
export default class RewardsMenuBuilder extends AbstractMenuBuilder {
  static readonly menuName = 'AccessRewards'

  /**
   * Construit le menu distinctions, ou null si aucune page accessible
   */
  build(): MenuItems | MenuGroup | null {
    const children: MenuItems = []

    if (this.ability.can('display', 'access_rewards_list_page')) {
      children.push(
        this.createItem(
          'access_rewards_list',
          { name: 'fas fa-trophy' },
          '/access_rewards_not_delivered/list/',
          MENU_LINK_TYPE.V1,
        ),
      )
    }

    if (this.ability.can('display', 'access_rewards_command_page')) {
      children.push(
        this.createItem(
          'access_rewards_command',
          { name: 'fas fa-trophy' },
          '/access_reward_commands',
          MENU_LINK_TYPE.V1,
        ),
      )
    }

    if (this.ability.can('display', 'rewards_list_page')) {
      children.push(
        this.createItem(
          'rewards_list',
          { name: 'fas fa-trophy' },
          '/rewards/list/',
          MENU_LINK_TYPE.V1,
        ),
      )
    }

    if (children.length > 1) {
      // Plusieurs éléments, on retourne un groupe
      return this.createGroup(
        'access_rewards',
        { name: 'fas fa-trophy' },
        children,
      )
    } else if (children.length === 1) {
      // Un seul élément, on retourne cet élément seul
      return children[0]
    }

    return null
  }
}
