# -*- coding: utf-8 -*-
from __future__ import annotations

from django.contrib.messages.constants import (  # noqa: F401
    DEBUG,
    DEFAULT_TAGS,
    ERROR,
    INFO,
    SUCCESS,
    WARNING,
)
from django.http import HttpRequest, HttpResponse
from django_htmx.http import trigger_client_event


def add_htmx_message(*,
                     request: HttpRequest,
                     response: HttpResponse,
                     level: str,
                     message: str,
                     extra_tags: str = '',
                     fail_silently: bool = False,
                     ):
    if not request.htmx:
        if fail_silently is False:
            raise RuntimeError(
                "This doesn't look like an HTMX request: request=`%s`",
                request,
            )
    else:
        trigger_client_event(
            response,
            'HotPocket:UI:Messages:addMessage',
            {
                'level': DEFAULT_TAGS.get(level, DEFAULT_TAGS[INFO]),
                'message': message,
                'extra_tags': extra_tags,
            },
            after='swap',
        )
