{#
	One setting in a Configure panel: its name, help link and summary from the plugin, its fuller description
	on request, any reason it needs attention, and a control chosen for it in the panel layout. Every control is a plain form field named
	after its option, inside .shield-option-control-col, so unsaved tracking and saving treat it like any
	other option. Settings that only apply when this one is on follow it, indented.
#}
{% macro item( item, strings, hrefs ) %}
	{% import _self as panel %}
	{% set option = item.option %}
	{% set severity = item.issues|map( i => i.severity )|filter( s => s == 'critical' )|length > 0 ? 'critical' : 'warning' %}
	<div class="shield-option-item shield-panel-item shield-panel-item--{{ item.control }} {{ item.stacked ? 'is-stacked' : 'is-inline' }}{{ item.issues is not empty ? ' has-panel-issue has-panel-issue--'~severity : '' }}"
		 data-option-key="{{ option.key }}">
		<div class="shield-panel-item__text">
			<div class="shield-panel-item__name">
				{# A single control is labelled by the setting's name, so clicking the name works it. A group of choices names itself with aria-labelledby. #}
				{% if item.control in [ 'chips', 'roles', 'radios', 'segmented' ] %}
					<span class="option_label_name" id="Label-{{ option.key }}">{{ option.name }}</span>
				{% else %}
					<label class="option_label_name" id="Label-{{ option.key }}" for="Opt-{{ option.key }}">{{ option.name }}</label>
				{% endif %}
				{% if option.disabled and option.unavailable %}
					<span class="shield-option-pill shield-option-pill--pro">{{ strings.opt_pro }}</span>
				{% endif %}
				{{ panel.help( option, strings ) }}
				{% if item.details is not empty %}
					<button type="button"
							class="shield-panel-details-toggle"
							aria-expanded="false"
							aria-controls="Details-{{ option.key }}"
							aria-label="{{ strings.opt_details }}: {{ option.name }}"
							data-bs-toggle="tooltip"
							data-bs-title="{{ strings.opt_details }}"><i class="bi bi-chevron-down" aria-hidden="true"></i></button>
				{% endif %}
				<span class="shield-option-pill shield-option-pill--unsaved" data-option-unsaved="1" hidden>{{ strings.opt_unsaved }}</span>
			</div>
			{% if option.summary is not empty %}
				<div class="options-summary">{{ option.summary }}</div>
			{% endif %}
			{% if item.issues is not empty %}
				<ul class="shield-panel-issues" id="Issues-{{ option.key }}">
					{% for issue in item.issues %}
						<li class="shield-panel-issue {{ issue.severity == 'critical' ? 'shield-panel-issue--critical' : 'shield-panel-issue--warning' }}">
							<i class="bi {{ issue.severity == 'critical' ? 'bi-x-circle-fill' : 'bi-exclamation-triangle-fill' }}" aria-hidden="true"></i>
							<span>{{ issue.text }}</span>
						</li>
					{% endfor %}
				</ul>
			{% endif %}
		</div>
		<div class="shield-option-control-col shield-panel-item__control">
			{{ panel.control( item, strings ) }}
		</div>
		{% if item.details is not empty %}
			{# Spans the whole row beneath the setting, so the control stays level with its name. #}
			<div class="shield-panel-details" id="Details-{{ option.key }}" hidden>
				{% for line in item.details %}<p>{{ line|raw }}</p>{% endfor %}
			</div>
		{% endif %}
	</div>
	{% if item.children is not empty %}
		<div class="shield-panel-children">
			{% for child in item.children %}
				{{ panel.item( child, strings, hrefs ) }}
			{% endfor %}
		</div>
	{% endif %}
{% endmacro %}

{% macro help( option, strings ) %}
	{% if option.beacon_id %}
		<button type="button"
				class="shield-panel-help beacon-article"
				data-beacon_article_format="sidebar"
				data-beacon_article_id="{{ option.beacon_id }}"
				aria-label="{{ strings.opt_help }}: {{ option.name }}"
				data-bs-toggle="tooltip"
				data-bs-title="{{ strings.opt_help }}">?</button>
	{% elseif option.link_info %}
		<a class="shield-panel-help" href="{{ option.link_info }}" target="_blank" rel="noopener"
		   aria-label="{{ strings.opt_help }}: {{ option.name }}"
		   data-bs-toggle="tooltip"
		   data-bs-title="{{ strings.opt_help }}">?</a>
	{% endif %}
{% endmacro %}

{% macro control( item, strings ) %}
	{% set option = item.option %}
	{% set is_disabled = option.disabled %}
	{% set described_by = item.issues is not empty ? 'Issues-'~option.key : '' %}

	{% if item.control == 'switch' %}
		{# A two-choice select shown as a switch: when off, the hidden field sends "disabled". #}
		<div class="form-check form-switch">
			<input type="checkbox"
				   class="form-check-input"
				   id="Opt-{{ option.key }}"
				   name="{{ option.key }}"
				   role="switch"
				   value="{{ item.on_value }}"
				   aria-labelledby="Label-{{ option.key }}"
				   {% if described_by %}aria-describedby="{{ described_by }}"{% endif %}
				   {% if is_disabled %}disabled="disabled"{% endif %}
				   {% if option.value != 'disabled' %}checked="checked"{% endif %} />
			<input type="hidden" name="{{ option.key }}" value="disabled" />
		</div>

	{% elseif item.control == 'slider' %}
		{#
			A native range input, so it drags, responds to a click anywhere on the track and works with the
			keyboard. It moves between the stops; the hidden field carries the chosen stop's value, and the
			chosen stop's name is shown beside the track as it moves.
		#}
		{% set last = item.stops|length - 1 %}
		{% set current = item.stops[ item.current_index ] %}
		<div class="shield-range" data-panel-range="1">
			<div class="shield-range__track">
				<input type="range"
					   class="shield-range__input"
					   id="Opt-{{ option.key }}"
					   min="0" max="{{ last }}" step="1"
					   value="{{ item.current_index }}"
					   data-stops="{{ item.stops|json_encode|e('html_attr') }}"
					   aria-labelledby="Label-{{ option.key }}"
					   aria-valuetext="{{ current.label }}"
					   {% if described_by %}aria-describedby="{{ described_by }}"{% endif %}
					   {% if is_disabled %}disabled="disabled"{% endif %}
					   style="--shield-range-pos: {{ last > 0 ? ( item.current_index / last * 100 )|round(2) : 0 }}%" />
				{# A tick for every step, and the two ends named, so the scale is visible before it's moved. #}
				<div class="shield-range__ticks" aria-hidden="true">
					{% for stop in item.stops %}<span title="{{ stop.label }}"></span>{% endfor %}
				</div>
				<div class="shield-range__ends" aria-hidden="true">
					<span>{{ ( item.stops|first ).label }}</span>
					<span>{{ ( item.stops|last ).label }}</span>
				</div>
			</div>
			<output class="shield-range__value{{ current.value in [ '0', 'disabled' ] ? ' is-off' : '' }}"
					for="Opt-{{ option.key }}" data-range-value="1">{{ current.label }}</output>
			<input type="hidden" name="{{ option.key }}" value="{{ current.value }}" />
		</div>

	{% elseif item.control in [ 'segmented', 'radios' ] %}
		<div class="{{ item.control == 'segmented' ? 'shield-seg' : 'shield-radio-list' }}" role="radiogroup" aria-labelledby="Label-{{ option.key }}"
			 {% if described_by %}aria-describedby="{{ described_by }}"{% endif %}>
			{% for option_value, choice in option.value_options %}
				<label class="{{ item.control == 'segmented' ? 'shield-seg__opt' : 'shield-radio-list__opt' }}">
					<input type="radio"
						   name="{{ option.key }}"
						   value="{{ option_value }}"
						   {% if is_disabled or not choice.is_available %}disabled="disabled"{% endif %}
						   {% if option.value matches "'^"~option_value~"$'" %}checked="checked"{% endif %} />
					<span>{{ choice.name|raw }}</span>
				</label>
			{% endfor %}
		</div>

	{% elseif item.control in [ 'chips', 'roles' ] %}
		{% if item.control == 'roles' %}
			{# The empty entry means "no roles" is still sent when nothing is ticked; a disabled control sends nothing. #}
			<input type="hidden" name="{{ option.key }}[]" value="" {% if is_disabled %}disabled="disabled"{% endif %} />
		{% endif %}
		{% set choices = item.control == 'roles'
			? item.roles
			: option.value_options|map( ( c, v ) => { value: v, label: c.name, is_on: v in option.value, is_available: c.is_available } ) %}
		<div class="shield-option-chipset shield-panel-chips" role="group" aria-labelledby="Label-{{ option.key }}"
			 {% if described_by %}aria-describedby="{{ described_by }}"{% endif %}>
			{% for choice in choices %}
				{% set chip_disabled = is_disabled or not ( choice.is_available ?? true ) %}
				<label class="shield-option-chip{% if chip_disabled %} is-disabled{% endif %}" for="Opt-{{ option.key }}_{{ choice.value }}">
					<input type="checkbox"
						   class="shield-option-chip__input"
						   id="Opt-{{ option.key }}_{{ choice.value }}"
						   name="{{ option.key }}[]"
						   value="{{ choice.value }}"
						   {% if chip_disabled %}disabled="disabled"{% endif %}
						   {% if choice.is_on %}checked="checked"{% endif %} />
					<span class="shield-option-chip__text">{{ choice.label|raw }}</span>
				</label>
			{% endfor %}
		</div>

	{% elseif item.prefix is not empty %}
		<span class="shield-option-prefixed">
			<span class="shield-option-prefixed__prefix" aria-hidden="true">{{ item.prefix }}</span>
			{% include '/components/config/option_control.twig' with { option: option, strings: strings } only %}
		</span>

	{% else %}
		{% include '/components/config/option_control.twig' with { option: option, strings: strings } only %}
		{% if option.unit_label is not empty %}
			<span class="shield-option-unit">{{ option.unit_label }}</span>
		{% endif %}
	{% endif %}
{% endmacro %}
