from django import template
from pydantic import AnyHttpUrl, ValidationError
from pydantic.type_adapter import TypeAdapter

from epdb.template_registry import get_templates

register = template.Library()

url_adapter = TypeAdapter(AnyHttpUrl)


@register.filter
def classname(obj):
    return obj.__class__.__name__


@register.filter
def is_url(value):
    try:
        url_adapter.validate_python(value)
        return True
    except ValidationError:
        return False


@register.simple_tag
def epdb_slot_templates(slot):
    return get_templates(slot)
