# Copyright (c) 2015, Xhive Global Ltd. and Contributors
# License: MIT. See LICENSE

import xhiveframework
from xhiveframework.utils import strip_html_tags

no_cache = 1


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

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

	if not message_context.title:
		message_context.title = xhiveframework.form_dict.title

	if not message_context.message:
		message_context.message = xhiveframework.form_dict.message

	return message_context
