from pathlib import Path

from django.template import Library
from django.templatetags.static import static

IMAGE_EXTS = [".jpg", ".jpeg", ".png", ".tif", ".tiff", ".gif"]

register = Library()


@register.filter
def icon_url(file_name):
    ext = Path(file_name).suffix.lower()
    icon = "master"  # default
    if ext in IMAGE_EXTS:
        icon = "image"
    elif ext == ".pdf":
        icon = "pdf"
    elif ext in [".mp3", ".m4a", ".wav"]:
        icon = "mp3"
    elif ext in (".xls", ".xlsx", ".ods", ".csv"):
        icon = "tsv"
    elif ext == ".odt":
        icon = "odt"
    elif ext in (".doc", ".docx"):
        icon = "docx"
    return static(f"ficons/{icon}.svg")
