<template>
  <div v-if="!item.hidden">
    <template
        v-if="hasOneShowingChild(item.children, item) && (!onlyOneChild.children || onlyOneChild.noShowingChildren) && !item.alwaysShow"
    >
      <app-link
          v-if="onlyOneChild.meta && onlyOneChild.meta.title !== 'ログアウト'"
          :to="resolvePath(onlyOneChild.path, onlyOneChild.query)"
      >
        <el-menu-item
            :class="{ 'submenu-title-noDropdown': !isNest }"
            :index="resolvePath(onlyOneChild.path)"
        >
          <svg-icon :icon-class="onlyOneChild.meta.icon || (item.meta && item.meta.icon)"/>
          <template #title>
            <span :title="hasTitle(onlyOneChild.meta.title)" class="menu-title">
              {{ onlyOneChild.meta.title }}
            </span>
          </template>
        </el-menu-item>
      </app-link>

      <el-menu-item
          v-else
          :class="{ 'submenu-title-noDropdown': !isNest }"
          :index="resolvePath(onlyOneChild.path)"
          @click="logout"
      >
        <svg-icon :icon-class="onlyOneChild.meta.icon || (item.meta && item.meta.icon)"/>
        <template #title>
          <span :title="hasTitle(onlyOneChild.meta.title)" class="menu-title">
            {{ onlyOneChild.meta.title }}
          </span>
        </template>
      </el-menu-item>
    </template>


    <el-sub-menu v-else ref="subMenu" :index="resolvePath(item.path)" teleported>
      <template v-if="item.meta" #title>
        <svg-icon :icon-class="item.meta && item.meta.icon"/>
        <span :title="hasTitle(item.meta.title)" class="menu-title">{{ item.meta.title }}</span>
      </template>

      <sidebar-item
          v-for="(child, index) in item.children"
          :key="child.path + index"
          :base-path="resolvePath(child.path)"
          :is-nest="true"
          :item="child"
          class="nest-menu"
      />
    </el-sub-menu>
  </div>
</template>

<script setup>
import {isExternal} from '@/utils/validate'
import AppLink from './Link'
import {getNormalPath} from '@/utils/yamada.js'
import { ElMessageBox } from 'element-plus'
import useUserStore from '@/store/modules/user'
import { formatMsg } from "@/utils/yamada.js"

const props = defineProps({
  // route object
  item: {
    type: Object,
    required: true
  },
  isNest: {
    type: Boolean,
    default: false
  },
  basePath: {
    type: String,
    default: ''
  }
})

const onlyOneChild = ref({});
const userStore = useUserStore();

function hasOneShowingChild(children = [], parent) {
  if (!children) {
    children = [];
  }
  const showingChildren = children.filter(item => {
    if (item.hidden) {
      return false
    }
    onlyOneChild.value = item
    return true
  })

  // When there is only one child router, the child router is displayed by default
  if (showingChildren.length === 1) {
    return true
  }

  // Show parent if there are no child router to display
  if (showingChildren.length === 0) {
    onlyOneChild.value = {...parent, path: '', noShowingChildren: true}
    return true
  }

  return false
};

function resolvePath(routePath, routeQuery) {
  if (isExternal(routePath)) {
    return routePath
  }
  if (isExternal(props.basePath)) {
    return props.basePath
  }
  if (routeQuery) {
    let query = JSON.parse(routeQuery);
    return {path: getNormalPath(props.basePath + '/' + routePath), query: query}
  }
  return getNormalPath(props.basePath + '/' + routePath)
}

function hasTitle(title) {
  if (title.length > 5) {
    return title;
  } else {
    return "";
  }
}

function logout() {
  ElMessageBox.confirm(formatMsg('Q0003'), '確認', {
    confirmButtonText: 'はい',
    cancelButtonText: 'いいえ',
    type: 'warning'
  }).then(() => {
    userStore.logOut().then(() => {
      location.href = '/index';
    })
  }).catch(() => {
  });
}
</script>
