import AbstractMenuBuilder from '~/services/layout/menuBuilder/abstractMenuBuilder'
import {MenuGroup, MenuItems} from "~/types/layout";
import {MENU_LINK_TYPE} from "~/types/enum/layout";
import _ from "lodash"

/**
 * Menu Famille
 */
export default class MyFamilyMenuBuilder extends AbstractMenuBuilder {
  static readonly menuName = "MyFamily"

  /**
   * Construit le menu Header Changement d'utilisateur ou null si aucune page accessible
   */
  build(): MenuGroup | null {
    const children: MenuItems = []

    // Si Access des membres de la familles (enfants)
    _.each(this.accessProfile.familyAccesses, (access) => {
      const url = `/switch_user/${this.organizationProfile.id}/${this.accessProfile.id}/${access.id}`
      const icon = {
        avatarId: access.avatarId,
        avatarByDefault: access.gender == 'MISTER' ? '/images/default/men-1.png' : '/images/default/women-1.png'
      }

      children.push(this.createItem(`${access.givenName} ${access.name}`, icon, url, MENU_LINK_TYPE.V1))
    })

    // Si on est en compte switch, on doit pouvoir retourner au compte d'origine
    const originalAccess = this.accessProfile.originalAccess
    if (originalAccess && !originalAccess.isSuperAdminAccess) {
      const url = `/switch_user/${originalAccess.organization.id}/${originalAccess.id}/exit`
      children.push(this.createItem(`${originalAccess.givenName} ${originalAccess.name}`, undefined, url, MENU_LINK_TYPE.V1))
    }

    if (children.length > 0) {
      return this.createGroup('familyAccesses', {name: 'fas fa-users'}, children)
    }

    return null
  }
}
