export const EXCALIDRAW_EXTENSION = "excalidraw"

export function fileExtension(name: string): string {
  const base = name.split("/").pop() ?? name
  const lower = base.toLowerCase()
  if (lower.endsWith(".excalidraw.json")) return "excalidraw.json"
  const i = base.lastIndexOf(".")
  if (i <= 0) return ""
  return base.slice(i + 1).toLowerCase()
}

export function isExcalidrawExtension(ext: string): boolean {
  const normalized = ext.toLowerCase()
  return normalized === EXCALIDRAW_EXTENSION || normalized === "excalidraw.json"
}

export function isExcalidrawFile(file: { name: string; mime_type?: string; type?: string }): boolean {
  if (file.type === "directory") return false
  const ext = fileExtension(file.name)
  if (ext && isExcalidrawExtension(ext)) return true
  const mime = (file.mime_type ?? "").toLowerCase()
  return mime === "application/json" && file.name.toLowerCase().endsWith(".excalidraw")
}

export function shouldOpenInUltidrawEditor(file: {
  name: string
  mime_type?: string
  type?: string
}): boolean {
  return isExcalidrawFile(file)
}
