{% macro render_errors(field) %}
	{% for e in field.errors %}
		<p class="invalid-feedback" style="display: block;">{{ e }}</p>
	{% endfor %}
{% endmacro %}

{% macro render_label(field, label=None, label_visible=true) %}
	{% if field.type != 'HiddenField' %}
		{% if not label and label != "" %}{% set label=field.label.text %}{% endif %}
		{% if label %}
			<label for="{{ field.id }}" {% if not label_visible %}class="visually-hidden"{% else %}class="form-label"{% endif %}>
				{{ label|safe }}
			</label>
			{% if field.flags.required and label_visible %}
				<span class="ms-1 text-danger">
					<small>
						<i class="fas fa-asterisk"></i>
					</small>
				</span>
				<span class="ms-3 text-muted">
					{{ _("Required") }}
				</span>
			{% endif %}
		{% endif %}
	{% endif %}
{% endmacro %}

{% macro render_field(field, label=None, label_visible=true, right_url=None, right_label=None, fieldclass=None, hint=None, no_class=False) -%}
	<div class="{% if not no_class %}form-group mb-3{% endif %} {% if field.errors %}has-danger{% endif %} {{ kwargs.pop('class_', '') }}">
		{{ render_label(field, label, label_visible) }}
		{{ field(class_=fieldclass or 'form-control', **kwargs) }}
		{% if hint %}
			<small class="form-text text-muted">{{ hint | safe }}</small>
		{% endif %}
		{{ render_errors(field) }}
	</div>
{%- endmacro %}

{% macro render_field_prefix(field, label=None, prefix="@", label_visible=true, right_url=None, right_label=None, fieldclass=None) -%}
	<div class="form-group mb-3 {% if field.errors %}has-danger{% endif %} {{ kwargs.pop('class_', '') }}">
		{{ render_label(field, label, label_visible) }}
		<div class="input-group mb-3">
			<span class="input-group-text" id="basic-addon1">{{ prefix }}</span>
			{{ field(class_=fieldclass or 'form-control', **kwargs) }}
		</div>

		{{ render_errors(field) }}
	</div>
{%- endmacro %}

{% macro render_field_prefix_button(field, label=None, prefix="@", label_visible=true, right_url=None, right_label=None, fieldclass=None, has_view=False, hint=None) -%}
	<div class="form-group mb-3 {% if field.errors %}has-danger{% endif %} {{ kwargs.pop('class_', '') }}">
		{{ render_label(field, label, label_visible) }}
		<div class="input-group mb-1">
			<span class="input-group-text" id="basic-addon1">{{ prefix }}</span>
			{{ field(class_=fieldclass or 'form-control', **kwargs) }}
			{% if has_view %}
				<a class="btn btn-secondary" id="{{ field.name }}-button">
					{{ _("View") }}
				</a>
			{% endif %}
		</div>

		{{ render_errors(field) }}
		{% if hint %}
			<small class="form-text text-muted">{{ hint | safe }}</small>
		{% endif %}
	</div>
{%- endmacro %}

{% macro form_scripts() -%}
	<link href="/static/libs/jquery-ui.min.css?v=2" rel="stylesheet" type="text/css">
	<script src="/static/libs/jquery.min.js?v=2"></script>
	<script src="/static/libs/jquery-ui.min.js?v=2"></script>
	<script src="/static/js/tagselector.js"></script>
{% endmacro %}

{% macro easymde_scripts() %}
	<link rel="stylesheet" type="text/css" href="/static/libs/easymde.min.css?v=2">
	<script src="/static/libs/easymde.min.js?v=2"></script>
	<script src="/static/js/markdowntextarea.js?v=2"></script>
{% endmacro %}

{% macro package_lists() -%}
	<script>
		meta_packages = [
			{% for m in modnames %}
				{# This is safe as name can only contain `[a-z0-9_]` #}
				{
					id: "{{ m.name }}",
					value: "{{ m.name }}",
					toString: function() { return "{{ m.name }}"; },
				},
			{% endfor %}
		]

		function escape(unsafe) {
			return unsafe
				.replace(/&/g, "&amp;")
				.replace(/</g, "&lt;")
				.replace(/>/g, "&gt;")
				.replace(/"/g, "&quot;")
				.replace(/'/g, "&#039;");
		}

		all_packages = meta_packages.slice();

		{% for p in packages %}
			all_packages.push({
				id: "{{ p.author.username }}/{{ p.name }}",
				value: escape({{ p.title | tojson }} + " by " + {{ p.author.display_name | tojson }}),
				toString: function() { return  escape({{ p.title | tojson }} + " by " + {{ p.author.display_name | tojson }} + " only"); },
			});
		{% endfor %}
	</script>
{% endmacro %}

{% macro render_multiselect_field(field, label=None, label_visible=true, right_url=None, right_label=None) -%}
	<div class="form-group mb-3 {% if field.errors %}has-danger{% endif %} {{ kwargs.pop('class_', '') }}">
		{{ render_label(field, label, label_visible) }}
		<div class="multichoice_selector bulletselector form-control">
			<input type="text" placeholder="{{ _('Start typing to see suggestions') }}">
			<div class="clearboth"></div>
		</div>
		<div class="invalid-remaining invalid-feedback"></div>
		{{ field(class_='form-control', **kwargs) }}
		{{ render_errors(field) }}
	</div>
{% endmacro %}

{% macro render_checkbox_field(field, label=None) -%}
	{% if not label %}{% set label=field.label.text %}{% endif %}
	<div class="form-check {{ kwargs.pop('class_', '') }}">
		{{ field(type='checkbox', class_="form-check-input", **kwargs) }}
		<label class="form-check-label" for="{{ field.name }}">
			{{ label }}
		</label>
	</div>
{%- endmacro %}

{% macro render_radio_field(field, hint=None, label=None, label_visible=true) -%}
	<div class="form-group mb-3 {% if field.errors %}has-danger{% endif %} {{ kwargs.pop('class_', '') }}">
		{{ render_label(field, label, label_visible) }}
		{% for value, label, checked, render_kw in field.iter_choices() %}
			<div class="form-check my-1">
				<label class="form-check-label">
					<input class="form-check-input" type="radio" name="{{ field.id }}" id="{{ field.id }}" value="{{ value }}"{% if checked %} checked{% endif %}>
					{{ label }}
				</label>
			</div>
		{% endfor %}
		{% if hint %}
			<small class="form-text text-muted">{{ hint | safe }}</small>
		{% endif %}
		{{ render_errors(field) }}
	</div>
{%- endmacro %}

{% macro render_toggle_field(field, icons=[]) -%}
	<div class="btn-group" role="group" >
		{% for value, label, checked, render_kw in field.iter_choices() %}
			<input type="radio" class="btn-check" name="{{ field.id }}" id="{{ field.id }}-{{ value }}" value="{{ value }}" autocomplete="off"  {% if checked %} checked{% endif %}>
			<label class="btn btn-primary" for="{{ field.id }}-{{ value }}">
				{% set icon = icons[value] %}
				{% if icon %}
					<i class="fas {{ icon }} me-2"></i>
				{% endif %}
				{{ label }}
			</label>
		{% endfor %}
	</div>
{%- endmacro %}

{% macro render_submit_field(field, label=None, tabindex=None, class_=None) -%}
	{% if not label %}{% set label=field.label.text %}{% endif %}
	{#<button type="submit" class="form-control btn btn-default btn-primary">{{label}}</button>#}
	<input type="submit" name="{{ field.name }}" value="{{ label }}" class="{{ class_ or 'btn btn-primary' }}"
			{% if tabindex %}tabindex="{{ tabindex }}"{% endif %}>
{%- endmacro %}
