// utils/nav.uts
import type { MenuItem, MenuGroup, MenuChild } from '../types.uts'

export function findActiveByCurrentPage(
  menuList: MenuItem[],
  currentPage: string
): { activeMenuId: string, activeSubId: string } {

  const page = currentPage || ''

  // 1) currentPage 直接是一级 menu id
  const mById = menuList.find(m => m.id === page)
  if (mById) {
    const leaf = firstLeafOfMenu(mById)
    return { activeMenuId: mById.id, activeSubId: leaf?.id ?? '' }
  }

  // 2) currentPage 是 path（/pages/xxx 或 pages/xxx）
  const pageNorm = normalize(page)

  for (const m of menuList) {
    const groups = m.groups ?? []

    // group / child / 四级 全部扫描
    for (const g of groups) {
      // group 叶子（可选）
      if (g.path && normalize(g.path) === pageNorm) {
        return { activeMenuId: m.id, activeSubId: '' }
      }

      const cs = g.children ?? []
      for (const c of cs) {
        // 用 id 命中
        if (c.id === page) return { activeMenuId: m.id, activeSubId: c.id }
        // 用 path 命中
        if (c.path && normalize(c.path) === pageNorm) return { activeMenuId: m.id, activeSubId: c.id }

        // 四级
        const ds = c.children ?? []
        const hit = findInChildren(ds, page, pageNorm)
        if (hit) return { activeMenuId: m.id, activeSubId: hit.id }
      }
    }
  }

  // 3) 找不到：兜底 home
  return { activeMenuId: 'home', activeSubId: '' }
}

function findInChildren(list: MenuChild[], targetId: string, targetPathNorm: string): MenuChild | null {
  for (const n of list) {
    if (n.id === targetId) return n
    if (n.path && normalize(n.path) === targetPathNorm) return n
    const deep = n.children ?? []
    if (deep.length > 0) {
      const hit = findInChildren(deep, targetId, targetPathNorm)
      if (hit) return hit
    }
  }
  return null
}

function normalize(p: string): string {
  if (!p) return ''
  const s = p.startsWith('/') ? p.slice(1) : p
  const q = s.indexOf('?')
  return q >= 0 ? s.slice(0, q) : s
}

// 你可以把 index.uvue 里的 firstLeafOfMenu 移到这里复用（保持逻辑一致）
function firstLeafOfMenu(m: MenuItem): MenuChild | null {
  const gs = m.groups ?? []
  if (gs.length === 0) return null
  const g0 = gs[0]
  const cs = g0.children ?? []
  if (cs.length === 0) return null
  let n = cs[0]
  while (n.children && n.children.length > 0) n = n.children[0]
  return n
}


export function getCurrentRoutePath(): string {
  // 使用页面栈获取当前路由（uni-app标准能力）
  // getCurrentPages 用于获取当前页面栈实例 :contentReference[oaicite:2]{index=2}
  const pages = getCurrentPages()
  const last: any = pages[pages.length - 1]
  // #ifdef H5
  return last?.route ? `/${last.route}` : ''
  // #endif
  // #ifndef H5
  // 小程序/App 可能是 route / $page?.fullPath 形式，按你项目实际字段微调
  return last?.route ? `/${last.route}` : (last?.$page?.fullPath || '')
  // #endif
}
