import { Ability } from '@casl/ability'
import { NuxtConfig } from '@nuxt/types/config'
import * as _ from 'lodash'
import BaseMenu from '~/composables/layout/Menus/baseMenu'
import { AnyStore, ItemMenu, ItemsMenu, Menu } from '~/types/interfaces'

/**
 * @category composables/layout/Menus
 * @class MyAccessesMenu
 * Classe pour la construction du Menu Mon profile
 */
class MyAccessesMenu extends BaseMenu implements Menu {
  private $ability: Ability;
  private $store: AnyStore;

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

  /**
   * Construit le menu Header Multi compte ou null si aucune page accessible
   * @return {ItemMenu | null}
   */
  getHeaderMenu (): ItemMenu | null {
    const children: ItemsMenu = []

    _.each(this.$store.state.profile.access.multiAccesses, (access) => {
      children.push(this.constructMenu(access.organizationName, undefined, '/switch/' + access.organizationId, true))
    })

    return children.length > 0 ? this.constructMenu('multiAccesses', 'fa-building', undefined, undefined, children) : null
  }
}

export const getMyAccessesMenu = ($config: NuxtConfig, $ability: Ability, $store: AnyStore) => new MyAccessesMenu($config, $ability, $store).getHeaderMenu()
