import { reactive, ref, computed, watch } from 'vue'
import useMainLayoutStore from '@/store/layout'

const flatMenu = (menus: MainLayoutStore.MenuItem[]): MainLayoutStore.MenuItem[] => {
  const list: MainLayoutStore.MenuItem[] = []
  const stack = [...menus]
  while (stack.length) {
    const node = stack.shift()
    if (node) {
      list.push(node)
      const children = node.children
      if (children) {
        list.push(...children)
      }
    }
  }
  return list
}

const useNavPermissions = () => {
  const mainLayoutStore = useMainLayoutStore()

  const menuList = computed(() => {
    return flatMenu(mainLayoutStore?.renderMenus || [])
  })

  const hasPermissions = (code?: string) => {
    return menuList.value?.some((m) => m.index === code || m.path === code)
  }
  return { hasPermissions }
}

export default useNavPermissions
