import functools

import jingrow


def feature(key: str, site_key: str = "site", raise_error: bool = True):
	def get_site() -> str:
		return (jingrow.request.json or jingrow.request.form).get(site_key)

	def wrapper(fn):
		@functools.wraps(fn)
		def inner(*args, **kwargs):
			[plan, is_free] = jingrow.get_value("Site", get_site(), ["plan", "free"])
			if is_free or jingrow.get_value("Site Plan", plan, key):
				return fn(*args, **kwargs)
			if raise_error:
				message = "Current plan does not support this feature."
				jingrow.throw(message, jingrow.PermissionError)
			return None

		return inner

	return wrapper
