{#
/**
 * Video
 *
 * Video components are low-level video elements with some helper functionality.
 *
 * @var string $src A string with the video URL. Or:
 * @var array $sources An array of objects with two keys: `src` and `type`. Ignored if `src` is set.
 * @var string $poster A string with an image URL. Default ''.
 * @var bool $controls A boolean to enable or disable video player controls. Default TRUE.
 * @var bool $autoplay A boolean to enable or disable immediate playback. Default FALSE.
 * @var bool $loop A boolean to enable or disable video replay. Default FALSE.
 * @var string $fallback A string with text or markup to display if the browser doesn't support the `video` element.
 * @var Drupal\Core\Template\Attribute $attributes Attributes (class, data, etc.) for the entire card.
 */
#}
{{ attach_library('union_organizer/video') }}
{% set attributes = union_attributes(attributes|default([])) %}
{% set attributes = attributes.addClass(['cu-video']) %}

{% if src is defined %}
  {% set attributes = attributes.setAttribute('src', src) %}
{% elseif sources is defined and sources is iterable %}
  {% set source_markup %}
    {% for source in sources %}
      <source src="{{source.src}}" type="{{source.type}}">
    {% endfor %}
  {% endset %}
{% endif %}

{% if poster is not empty %}
{% set attributes = attributes.setAttribute('poster', poster) %}
{% endif %}

{% if controls is defined %}
{# The 'not not' syntax effectively casts the variable to a boolean,
   which Drupal setAttributes requires. For example, `control: 0` will
   now work as expected. #}
{% set attributes = attributes.setAttribute('controls', not not controls) %}
{% endif %}

{% if autoplay is defined %}
{% set attributes = attributes.setAttribute('autoplay', not not autoplay) %}
{% endif %}

{% if loop is defined %}
{% set attributes = attributes.setAttribute('loop', not not loop) %}
{% endif %}

{% if muted is defined %}
  {% set attributes = attributes.setAttribute('muted', not not muted) %}
{% endif %}

<video{{ attributes }}>{{ source_markup }}{{ fallback }}</video>
