import type { ResolvedNavItemWithLink, ThemeBadge } from '../../shared/index.js'
import {
  ensureEndingSlash,
  ensureLeadingSlash,
  isLinkAbsolute,
  isLinkWithProtocol,
} from '@vuepress/helper/client'
import { resolveRoute } from 'vuepress/client'

/**
 * Resolve NavLink props from string
 *
 * @example
 * - Input: '/README.md'
 * - Output: { text: 'Home', link: '/' }
 */
export function resolveNavLink(link: string): ResolvedNavItemWithLink {
  const { notFound, meta, path } = resolveRoute<{
    title?: string
    icon?: string
    badge?: string | ThemeBadge
  }>(link)

  return notFound
    ? { text: path, link: path }
    : {
        text: meta.title || normalizeTitleWithPath(path),
        link: path,
        icon: meta.icon,
        badge: meta.badge,
      }
}

function normalizeTitleWithPath(path: string): string {
  path = path.replace(/index\.html?$/i, '').replace(/\.html?$/i, '').replace(/\/$/, '')
  return decodeURIComponent(path.slice(path.lastIndexOf('/') + 1))
}

export function normalizeLink(base = '', link = ''): string {
  return isLinkAbsolute(link) || isLinkWithProtocol(link)
    ? link
    : ensureLeadingSlash(`${base}/${link}`.replace(/\/+/g, '/'))
}

export function normalizePrefix(base: string, link = ''): string {
  return ensureEndingSlash(normalizeLink(base, link))
}
