import {
  Database,
  FileArchive,
  Files,
  FolderPlus,
  FolderOpen,
  Settings,
  UserRound
} from 'lucide-react'

import type { MenuItem } from '@renderer/types/app'

export const menuTree: MenuItem[] = [
  {
    id: 'student',
    title: '学生管理',
    icon: Database,
    children: [
      { id: 'classes', title: '班级管理', path: '/student/classes', icon: FolderPlus },
      { id: 'students', title: '学生数据', path: '/student/list', icon: UserRound },
      { id: 'template', title: '模板管理', path: '/student/templates', icon: FolderOpen },
      { id: 'reports', title: '报告管理', path: '/student/reports', icon: Files },
      { id: 'backup', title: '备份恢复', path: '/student/backup', icon: FileArchive }
    ]
  },
  {
    id: 'settings',
    title: '设置',
    path: '/settings',
    icon: Settings
  }
]

export function findMenuItemByPath(
  pathname: string,
  menuItems: MenuItem[] = menuTree
): MenuItem | undefined {
  for (const item of menuItems) {
    if (item.path === pathname) {
      return item
    }

    const childMatch = item.children ? findMenuItemByPath(pathname, item.children) : undefined

    if (childMatch) {
      return childMatch
    }
  }

  return undefined
}

export function getOpenMenuIds(
  pathname: string,
  menuItems: MenuItem[] = menuTree,
  parents: string[] = []
): string[] {
  for (const item of menuItems) {
    const nextParents = [...parents, item.id]

    if (item.path === pathname) {
      return parents
    }

    if (item.children) {
      const childOpenIds = getOpenMenuIds(pathname, item.children, nextParents)

      if (childOpenIds.length > 0) {
        return childOpenIds
      }
    }
  }

  return []
}
