# Copyright (c) 2015, JINGROW and Contributors
# License: MIT. See LICENSE

import jingrow
from jingrow.utils import strip_html_tags
from jingrow.utils.html_utils import clean_html

no_cache = 1


def get_context(context):
	message_context = jingrow._dict()
	if hasattr(jingrow.local, "message"):
		message_context["header"] = jingrow.local.message_title
		message_context["title"] = strip_html_tags(jingrow.local.message_title)
		message_context["message"] = jingrow.local.message
		if hasattr(jingrow.local, "message_success"):
			message_context["success"] = jingrow.local.message_success

	elif jingrow.local.form_dict.id:
		message_id = jingrow.local.form_dict.id
		key = f"message_id:{message_id}"
		message = jingrow.cache.get_value(key, expires=True)
		if message:
			message_context.update(message.get("context", {}))
			if message.get("http_status_code"):
				jingrow.local.response["http_status_code"] = message["http_status_code"]

	if not message_context.title:
		message_context.title = clean_html(jingrow.form_dict.title)

	if not message_context.message:
		message_context.message = clean_html(jingrow.form_dict.message)

	return message_context
