{% assign id = include.map-id %}
{% assign data = site.data.maps-vector[id] %}
{% assign color = include.color | default: data.color | default: 'green' %}

{% if data %}
	<div class="ratio ratio-{{ include.ratio | default: '4x3' }}">
		<div>
			<div id="map-{{ id }}" class="w-100 h-100"></div>
		</div>
	</div>

	{% capture script %}
	<script>
		// @formatter:on
		{% if jekyll.environment == 'development' %}window.tabler_map_vector = window.tabler_map_vector || {};{% endif %}

		document.addEventListener("DOMContentLoaded", function() {
			const map = {% if jekyll.environment == 'development' %}window.tabler_map_vector["map-{{ id }}"] = {% endif %}new jsVectorMap({
				selector: '#map-{{ id }}',
				map: '{{ data.map }}',
				backgroundColor: 'transparent',
				regionStyle: {
					initial: {
						{% unless data.filled %}
						fill: '#F8FAFC',
						stroke: 'rgba(98, 105, 118, .16)',
						strokeWidth: 1,
						{% else %}
						fill: '#e9ecef',
						stroke: '#fff',
						strokeWidth: 1,
						{% endunless %}
					}
				},
				zoomOnScroll: {% if data.zoom %}true{% else %}false{% endif %},
				zoomButtons: {% if data.zoom %}true{% else %}false{% endif %},
				{% if data.values %}
				// -------- Series --------
				visualizeData: {
					scale: ['#F8FAFC', '{{ data.color | tabler_color }}'],
					values: {{ data.values }},
				},
				{% endif %}
				{% if data.markers %}
				markers: [
					{% for marker in data.markers %}
					{
						coords: [{{ marker.coords }}],
						name: "{{ marker.name }}",
					},
					{% endfor %}
				],
				markerStyle: {
					initial: {
						r: 4,
						stroke: '#fff',
						opacity: 1,
						strokeWidth: 3,
						stokeOpacity: .5,
						fill: '{{ color | tabler_color }}'
					},
					hover: {
						fill: '{{ color | tabler_color }}',
						stroke: '{{ color | tabler_color }}'
					}
				},
				markerLabelStyle: {
					initial: {
						fontSize: 10
					},
				},
				labels: {
					markers: {
						render: function(marker) {
							return marker.name
						},
					},
				},
				{% endif %}
				{% if data.lines %}
				lines: [
					{% for line in data.lines %}
					{
						from: "{{ line.from }}",
						to: "{{ line.to }}"
					},
					{% endfor %}
				],
				lineStyle: {
					strokeDasharray:"4 4",
					animation: true,
					stroke: "rgba(98, 105, 118, .75)",
					strokeWidth: .5,
				},
				{% endif %}
			});

			window.addEventListener("resize", () => {
				map.updateSize();
			});
		});
		// @formatter:off
	</script>
	{% endcapture %}

	{% if include.show-scripts %}
		{{ script }}
	{% else %}
		{% capture_global scripts %}
			{{ script }}
		{% endcapture_global %}
	{% endif %}
{% endif %}
