from __future__ import annotations

from django.forms.widgets import ClearableFileInput


class ImagePreviewWidget(ClearableFileInput):
    template_name = "cms/widgets/image_preview_input.html"

    def __init__(self, attrs=None):
        attrs = {**({} if attrs is None else attrs), "accept": "image/*"}
        super().__init__(attrs=attrs)

    def get_context(self, name, value, attrs):
        # Merge our button-like classes into any provided classes without duplicating the class attribute in template
        base_classes = (
            "file:mr-3 file:px-3 file:py-2 file:rounded file:border-0 "
            "file:bg-blue-600 file:text-white hover:file:bg-blue-700 text-sm text-gray-700"
        )
        attrs = attrs or {}
        existing = (attrs.get("class") or self.attrs.get("class") or "").strip()
        merged = (existing + " " + base_classes).strip() if existing else base_classes
        attrs["class"] = merged
        return super().get_context(name, value, attrs)

    class Media:
        css = {"all": []}
        js = []
