import { ALLOWED_EXTENSIONS } from './constants'

export function getFileName(filePath: string): string {
  // E2: 先剔除尾部分隔符再取最后段，避免 /a/b/ 返回 /a/b/
  const clean = filePath.replace(/[/\\]+$/, '')
  return clean.split(/[/\\]/).pop() || clean || filePath
}

export function isAllowedFile(filePath: string): boolean {
  const ext = getFileExtension(filePath)
  return (ALLOWED_EXTENSIONS as readonly string[]).includes(ext)
}

// L-07: 无扩展名文件正确返回空字符串
export function getFileExtension(filePath: string): string {
  const name = filePath.split(/[/\\]/).pop() || filePath
  const dotIndex = name.lastIndexOf('.')
  if (dotIndex <= 0) return '' // 无扩展名或以 . 开头的隐藏文件
  return name.substring(dotIndex).toLowerCase()
}
