import { h } from 'vue'
import Icon from '@/components/Module/Icon/index.vue'


export const useMain = () => {
  // 路由过滤，只显示要显示的路由
  const routerFilter = (menus: Array<Record<string, any>>) => {
    return menus.filter((item: Record<string, any>) => {
      if (item.children) {
        item.children = routerFilter(item.children)
      }

      return !item.meta.hidden
    })
  }

  // 路由格式化
  const routerFormat = (arr: Array<Record<string, any>>) => {
    return arr.map((item: Record<string, any>) => {
      if (item.children && item.children.length) {
        item.children = routerFormat(item.children)
      }
      return {
        meta: item.meta,
        icon: () =>
          h(Icon, {
            name: item.meta.icon,
          }),
        fullPath: item.fullPath,
        pid: item.parentId,
        key: item.name,
        label: item.meta.title || '',
        title: item.meta.title || '',
        children: item.children?.length ? item.children : '',
        type: item.type || null,
      }
    })
  }

  return {
    routerFilter,
    routerFormat
  }
}
