import { useMemo } from 'react'
import { useLocation } from 'react-router-dom'
import type { BreadcrumbProps } from 'antd'

const breadcrumbNameMap: Record<string, string> = {
  '/dashboard': '概览',
  '/users': '用户管理',
  '/roles': '角色管理',
  '/permissions': '权限管理',
  '/logs': '审计日志',
  '/logs/login': '登录日志',
  '/logs/operation': '操作日志',
  '/webhooks': 'Webhooks',
  '/import-export': '导入导出',
  '/profile': '个人资料',
  '/profile/security': '安全设置',
}

export function useBreadcrumbs(): BreadcrumbProps['items'] {
  const location = useLocation()

  return useMemo(() => {
    const pathSnippets = location.pathname.split('/').filter(Boolean)

    if (pathSnippets.length === 0) {
      return []
    }

    const items: BreadcrumbProps['items'] = []
    let currentPath = ''

    pathSnippets.forEach((snippet, index) => {
      currentPath += `/${snippet}`

      const name = breadcrumbNameMap[currentPath]
      if (!name) {
        return
      }

      items.push({
        title: name,
        path: index === pathSnippets.length - 1 ? undefined : currentPath,
      })
    })

    return items
  }, [location.pathname])
}
