import type { ComponentProps } from "react"
import {
  IconBrandCss3,
  IconBrandHtml5,
  IconBrandJavascript,
  IconFile,
  IconFileCode,
  IconFileMusic,
  IconFileText,
  IconFileZip,
  IconJson,
  IconPhoto,
  IconTypography,
  IconVideo,
} from "@tabler/icons-react"

type SvgIconProps = ComponentProps<typeof IconFile>

const baseProps: SvgIconProps = {
  size: 16,
  stroke: 1.5,
  "aria-hidden": true,
}

function extensionOf(relativePath: string): string {
  const base = relativePath.split("/").pop() ?? relativePath
  const i = base.lastIndexOf(".")
  if (i <= 0) return ""
  return base.slice(i + 1).toLowerCase()
}

/** Иконка по расширению файла (путь относительный, как в API `files`). */
export function LandingFileIcon({ path }: { path: string }) {
  const ext = extensionOf(path)

  switch (ext) {
    case "html":
    case "htm":
      return <IconBrandHtml5 {...baseProps} />
    case "css":
    case "scss":
    case "sass":
    case "less":
      return <IconBrandCss3 {...baseProps} />
    case "js":
    case "jsx":
    case "mjs":
    case "cjs":
    case "ts":
    case "tsx":
      return <IconBrandJavascript {...baseProps} />
    case "json":
      return <IconJson {...baseProps} />
    case "png":
    case "jpg":
    case "jpeg":
    case "gif":
    case "webp":
    case "svg":
    case "ico":
    case "bmp":
    case "avif":
      return <IconPhoto {...baseProps} />
    case "mp4":
    case "webm":
    case "mov":
    case "mkv":
      return <IconVideo {...baseProps} />
    case "mp3":
    case "wav":
    case "ogg":
    case "flac":
    case "m4a":
      return <IconFileMusic {...baseProps} />
    case "woff":
    case "woff2":
    case "ttf":
    case "otf":
    case "eot":
      return <IconTypography {...baseProps} />
    case "zip":
    case "rar":
    case "7z":
    case "gz":
      return <IconFileZip {...baseProps} />
    case "txt":
    case "md":
    case "csv":
      return <IconFileText {...baseProps} />
    case "xml":
    case "wasm":
    case "map":
    case "php":
    case "py":
    case "rb":
    case "sh":
      return <IconFileCode {...baseProps} />
    default:
      return <IconFile {...baseProps} />
  }
}
