{% apply spaceless %}

{#
  Parameters:
    - "poster" (string) (default: '')
    - "sources" (array) (default: []): format: [
      {
        "src" (string) (default: ''),
        "type" (string) (default: ''),
      },
      ...
    ],
    - "tracks" (array) (default: []): format: [
      {
        "src" (string) (default: ''),
        "kind" (string) (default: ''),
        "src_lang" (string) (default: ''),
        "label" (string) (default: ''),
      },
      ...
    ],
  - "controls" (boolean) (default: true)
  - "autoplay" (boolean) (default: false)
  - "muted" (boolean) (default: false)
  - "loop" (boolean) (default: false)
  - "sr_video_label" (string) (default: ''): additional label for the video item; for screen readers
  - "sr_video_player" (string) (default: ''): additional label for the video player; for screen readers
  - "zoom" (boolean) (default: false): Should the poster picture have a zoom animation?
  - "extra_classes" (string) (default: '') Extra classes on the video element
  - "extra_attributes" (array) (default: []) Extra attributes
    - "name" (string) Attribute name, eg. 'data-test'
    - "value" (string) Attribute value, eg: 'data-test-1'
#}

{# Internal properties #}

{% set _css_class = 'ecl-video' %}
{% set _sources = sources|default([]) %}
{% set _tracks = tracks|default([]) %}
{% set _controls = controls ?? true %}
{% set _poster = poster|default('') %}
{% set _title = title|default('') %}
{% set _autoplay = autoplay|default(false) %}
{% set _muted = muted|default(false) %}
{% set _loop = loop|default(false) %}
{% set _sr_video_player = sr_video_player|default('') %}
{% set _sr_video_label = sr_video_label|default('') %}
{% set _zoom = zoom|default(false) %}
{% set _extra_attributes = 'playsinline' %}

{% if _zoom %}
  {% set _css_class = _css_class ~ ' ecl-video--zoom' %}
{% endif %}

{% if _controls %}
  {% set _extra_attributes = _extra_attributes ~ ' controls' %}
{% endif %}

{% if _autoplay %}
  {% set _extra_attributes = _extra_attributes ~ ' autoplay' %}
{% endif %}

{% if _muted %}
  {% set _extra_attributes = _extra_attributes ~ ' muted' %}
{% endif %}

{% if _loop %}
  {% set _extra_attributes = _extra_attributes ~ ' loop' %}
{% endif %}

{% if _title is not empty and _sr_video_label is not empty %}
  {% set _title = _title ~ ' - ' ~ _sr_video_label %}
{% elseif _title is not empty and _sr_video_player is not empty %}
  {% set _title = _title ~ ' - ' ~ _sr_video_player %}
{% endif %}

{% if extra_classes is defined and extra_classes is not empty %}
  {% set _css_class = _css_class ~ ' ' ~ extra_classes %}
{% endif %}

{% if extra_attributes is defined and extra_attributes is not empty and extra_attributes is iterable %}
  {% for attr in extra_attributes %}
    {% if attr.value is defined %}
      {% set _extra_attributes = _extra_attributes ~ ' ' ~ attr.name|e('html_attr') ~ '="' ~ attr.value|e('html_attr') ~ '"' %}
    {% else %}
      {% set _extra_attributes = _extra_attributes ~ ' ' ~ attr.name|e('html_attr') %}
    {% endif %}
  {% endfor %}
{% endif %}

<video
  class="{{ _css_class }}"
{% if _poster is not empty %}
  poster="{{ _poster }}"
{% endif %}
{% if _title is not empty %}
  aria-label="{{ _title }}"
{% endif %}
  {{- _extra_attributes|raw -}}
>
{% if _sources is not empty and _sources is iterable %}
  {% for _source in _sources %}
  <source
  {% if _source.type is defined and _source.type is not empty %}
    type="{{ _source.type }}"
  {% endif %}
  {% if _source.src is defined and _source.src is not empty %}
    src="{{ _source.src }}"
  {% endif %}
  >
  {% endfor %}
{% endif %}

{%- if _tracks is not empty and _tracks is iterable -%}
  {% for _track in _tracks %}
  <track
  {% if _track.kind is defined and _track.kind is not empty %}
    kind="{{ _track.kind }}"
  {% endif %}
  {% if _track.label is defined and _track.label is not empty %}
    label="{{ _track.label }}"
  {% endif %}
  {% if _track.src is defined and _track.src is not empty %}
    src="{{ _track.src }}"
  {% endif %}
  {% if _track.src_lang is defined and _track.src_lang is not empty %}
    srclang="{{ _track.src_lang }}"
  {% endif %}
  >

  {% endfor %}
{% endif %}
</video>

{% endapply %}
