<!--
Menu principal de l'application
Prend en paramètre une liste de ItemMenu et les met en forme
-->

<template>
  <v-navigation-drawer
    v-model="displayMenu"
    :rail="isRail"
    :disable-resize-watcher="true"
    style="z-index: 1006"
    class="theme-secondary main-menu"
  >
    <!-- Forcing z-index to avoid this : https://github.com/vuetifyjs/nuxt-module/issues/205 -->

    <template #prepend>
      <slot name="prepend" :is-rail="isRail" />
    </template>

    <v-list open-strategy="single" active-class="active" class="left-menu">
      <v-list-item
        v-if="smAndDown"
        prepend-icon="fas fa-home"
        :title="$t('homepage')"
        class="theme-secondary menu-item"
        height="48px"
      />

      <!-- TODO: que se passe-t-il si le menu ne comprend qu'un seul MenuItem? -->
      <div v-for="(item, i) in items" :key="i">
        <!-- Cas 1 : l'item n'a pas d'enfants, c'est un lien (ou le menu est en mode réduit) -->
        <v-list-item
          v-if="!item.children || isRail"
          :id="'main-menu-item' + item.label"
          :title="$t(item.label)"
          :prepend-icon="item.icon.name"
          :href="!isInternalLink(item) ? item.to : undefined"
          :to="isInternalLink(item) ? item.to : undefined"
          :target="item.target"
          exact
          height="48px"
          class="menu-item"
        />

        <!-- Cas 2 : l'item a des enfants, c'est un groupe -->
        <v-list-group
          v-else
          expand-icon="fas fa-angle-right"
          collapse-icon="fas fa-angle-down"
        >
          <template #activator="{ props }">
            <v-list-item
              v-bind="props"
              :id="'main-menu-item' + item.label"
              :prepend-icon="item.icon.name"
              :title="$t(item.label)"
              class="theme-secondary menu-item"
              height="48px"
            />
          </template>

          <v-list-item
            v-for="child in item.children"
            :key="child.label"
            :title="$t(child.label)"
            :id="'main-menu-item' + item.label + '-' + child.label"
            :prepend-icon="child.icon.name"
            :href="!isInternalLink(child) ? child.to : undefined"
            :to="isInternalLink(child) ? child.to : undefined"
            exact
            height="38px"
            class="theme-secondary-alt"
          />
        </v-list-group>
      </div>
    </v-list>

    <template #append>
      <slot name="foot" :is-rail="isRail" />
    </template>
  </v-navigation-drawer>
</template>

<script setup lang="ts">
import { useMenu } from '~/composables/layout/useMenu'
import { computed } from '@vue/reactivity'
import { useDisplay } from 'vuetify'
import type { MenuGroup, MenuItem } from '~/types/layout'
import { useApiLegacyRequestService } from '~/composables/data/useApiLegacyRequestService'

const i18n = useI18n()
const organizationProfile = useOrganizationProfileStore()
const { getMenu, hasMenu, isInternalLink, setMenuState, isMenuOpened } =
  useMenu()

const { smAndDown, mdAndUp, lgAndUp } = useDisplay()

const menu: MenuGroup | null = getMenu('Main')

// En vue lg+, on affiche toujours le menu
const displayMenu = computed(() => {
  return menu !== null && hasMenu('Main') && (lgAndUp.value || isOpened.value)
})

const isOpened = computed(() => isMenuOpened('Main'))

const items: Array<MenuGroup | MenuItem> = getItems(menu)

// En vue md+, fermer le menu le passe simplement en mode rail
// Sinon, le fermer le masque complètement
const isRail = computed(() => {
  return (
    menu !== null &&
    mdAndUp.value &&
    !isOpened.value &&
    !items.some((item) => item.expanded)
  )
})

const unwatch = watch(lgAndUp, (newValue, oldValue) => {
  // Par défaut si l'écran est trop petit au chargement de la page, le menu doit rester fermé.
  if (process.client && menu !== null) {
    setMenuState('Main', lgAndUp.value)
  }
})

onUnmounted(() => {
  unwatch()
})

/**
 * Récupère les menuItem disponibles
 * @param menu
 */
function getItems(
  menu: MenuGroup | MenuItem | null,
): Array<MenuGroup | MenuItem> {
  let items: Array<MenuGroup | MenuItem>

  if (menu === null) {
    items = []
  } else if (menu.hasOwnProperty('children')) {
    items = (menu as MenuGroup).children ?? []
  } else {
    items = [menu]
  }

  return items
}
</script>

<style scoped lang="scss">
.v-list-item {
  min-height: 10px !important;
}

:deep(.v-list-item-title),
:deep(.v-icon) {
  font-size: 14px;
  color: rgb(var(--v-theme-on-secondary));
}

:deep(.v-list-item__prepend > .v-icon ~ .v-list-item__spacer) {
  width: 12px;
}

.v-application--is-ltr .v-list-group--no-action > .v-list-group__header {
  margin-left: 0;
  padding-left: 0;
}
.v-application--is-ltr
  .v-list-group--no-action
  > .v-list-group__items
  > .v-list-item {
  padding-left: 30px;
}

.v-list-item__content {
  padding: 8px 0;
}

.v-list-group__items .v-list-item {
  padding-inline-start: 30px !important;
}

.v-list-group--no-action > .v-list-group__header,
.v-list-item {
  border-left: 3px solid rgb(var(--v-theme-secondary));
  height: 48px;
}

.v-list-item:hover,
.v-list-item.active,
:deep(.v-list-group__items .v-list-item) {
  border-left: 3px solid rgb(var(--v-theme-primary));
  background-color: rgb(var(--v-theme-secondary-alt)) !important;
  color: rgb(var(--v-theme-on-secondary-alt)) !important;
}

:deep(.v-list-group__items .v-list-item-title) {
  color: rgb(var(--v-theme-on-secondary-alt));
}

:deep(.v-list-group__items .v-icon) {
  color: rgb(var(--v-theme-on-secondary-alt));
}

:deep(.v-list-item .v-icon) {
  margin-right: 10px;
}

:deep(.menu-item .fa) {
  text-align: center;
}
</style>
