{#
    This component outputs a figure element with media, an optional link tag
    around the media part, as well as an optional figcaption. It is designed
    to be used together with a \Contao\CoreBundle\Image\Studio\Figure object,
    but can be fully adapted for any data input by adjusting the respective
    blocks.

        <figure>
            <a href="https://example.com">
                <!-- media, e.g. picture -->
            </a>
            <figcaption>My caption</figcaption>
        </figure>

    See "@Contao/component/_picture.html.twig" for more information about the
    default media part.

    Mandatory variables:
        @var \Contao\CoreBundle\Image\Studio\Figure figure

    Optional variables:
        @var \Contao\CoreBundle\String\HtmlAttributes figure_attributes
        @var \Contao\CoreBundle\String\HtmlAttributes picture_attributes
        @var \Contao\CoreBundle\String\HtmlAttributes source_attributes
        @var \Contao\CoreBundle\String\HtmlAttributes img_attributes
        @var \Contao\CoreBundle\String\HtmlAttributes link_attributes
        @var \Contao\CoreBundle\String\HtmlAttributes caption_attributes
#}

{% use "@Contao/component/_picture.html.twig" %}

{% block figure_component %}
    {% set figure_attributes = attrs()
        .mergeWith(figure.options.attr|default)
        .mergeWith(figure_attributes|default)
    %}
    <figure{{ figure_attributes }}>
        {% if not figure.linkHref|default %}
            {# Media #}
            {% block media %}
                {{ block('picture_component') }}
            {% endblock %}
        {% else %}
            {# Media wrapped with link #}
            {% block media_link %}
                {% set link_attributes = attrs(figure.linkAttributes(true)|default)
                    .setIfExists('title', figure.hasLightbox and figure.hasMetadata ? figure.metadata.title : null)
                    .mergeWith(figure.options.link_attr|default)
                    .mergeWith(link_attributes|default)
                %}
                <a{{ link_attributes }}>{{ block('media') }}</a>
            {% endblock %}
        {% endif %}

        {# Caption #}
        {% block caption %}
            {% if figure.metadata and figure.metadata.caption %}
                {% set caption_attributes = attrs()
                    .mergeWith(figure.options.caption_attr|default)
                    .mergeWith(caption_attributes|default)
                %}
                <figcaption{{ caption_attributes }}>
                    {%- block caption_inner -%}
                        {{- figure.metadata.caption|raw -}}
                    {%- endblock -%}
                </figcaption>
            {% endif %}
        {% endblock %}
    </figure>
{% endblock %}
