from django import forms
from django.forms.widgets import Media
from django.templatetags.static import static
from js_asset import JS


class LexicalEditorWidget(forms.TextInput):
    """
    Widget providing LexicalEditor for Rich Text Editing.
    """

    def __init__(self, template_name="lexical_editor/widget.html", *args, **kwargs):
        self.template_name = template_name
        super().__init__(*args, **kwargs)

    @property
    def media(self):
        return Media(
            js=(
                JS(
                    static("lexical_editor/assets/lexical.js"),
                    {"type": "module", "defer": "defer"},
                ),
            ),
            css={
                "all": [
                    "lexical_editor/assets/lexical.css",
                ]
            },
        )

    def get_context(self, name, value, attrs):
        context = super().get_context(name, value, attrs)
        return context
