{#-
 # content array
 # defaultLocale string
 # seo array
 # shadowBaseLocale string
 # localizations array
-#}

{% if localizations is not defined %}
    {% deprecated 'The "urls" variable is deprecated, set "localizations" instead in the "seo.html.twig".' %}
    {% set localizations = {} %}
    {% for locale, url in urls %}
        {% set localizations = localizations|merge({
            (locale): {
                locale: locale,
                url: sulu_content_path(url, null, locale),
            }
        }) %}
    {% endfor %}
{% endif %}

{#- merge seo data with _seo from attributes for custom urls -#}
{% set seo = seo|default([])|merge(app.request.attributes.get('_seo', [])|default([])) %}

{#- fallback to content title when no seo title is set -#}
{% set seoTitle = seo.title|default(content.title|default()) %}
{% set seoDescription = seo.description|default() %}
{% set seoKeywords = seo.keywords|default() %}

{% set seoRobots = '' %}
{%- if seo.noIndex|default(false) -%}
    {% set seoRobots = seoRobots ~ 'noIndex' -%}
{%- else -%}
    {% set seoRobots = seoRobots ~ 'index' -%}
{%- endif -%}
{%- if seo.noFollow|default(false) -%}
    {% set seoRobots = seoRobots ~ ',noFollow' -%}
{%- else -%}
    {% set seoRobots = seoRobots ~ ',follow' -%}
{%- endif -%}

{% set seoCanonical = seo.canonicalUrl|default() %}

{%- if not seoCanonical and shadowBaseLocale and localizations[shadowBaseLocale]|default() %}
    {% set seoCanonical = localizations[shadowBaseLocale].url %}
{%- endif -%}

{#- render blocks -#}

{%- block title -%}
    {%- if seoTitle -%}
        <title>{{ seoTitle }}</title>
    {%- endif -%}
{%- endblock -%}

{%- block description -%}
    {%- if seoDescription -%}
        <meta name="description" content="{{ seoDescription }}"/>
    {%- endif -%}
{%- endblock -%}

{%- block keywords -%}
    {%- if seoKeywords -%}
        <meta name="keywords" content="{{ seoKeywords }}"/>
    {%- endif -%}
{%- endblock -%}

{%- block robots -%}
    {%- if seoRobots -%}
        <meta name="robots" content="{{ seoRobots }}"/>
    {%- endif -%}
{%- endblock -%}

{%- block urls -%}
    {#- when only one language do not show alternative -#}
    {%- if localizations|length > 1 -%}
        {%- for localization in localizations -%}
            {%- if defaultLocale == localization.locale -%}
                <link rel="alternate" href="{{ localization.url }}" hreflang="x-default"/>
            {%- endif -%}
            <link rel="alternate" href="{{ localization.url }}" hreflang="{{ localization.locale|replace({'_': '-'}) }}"/>
        {%- endfor -%}
    {%- endif -%}
{%- endblock -%}

{%- block canonical -%}
    {#- Set canonical to itself if a bot clone the page -#}
    <link rel="canonical" href="{{ seoCanonical|default(app.request.uri) }}"/>
{%- endblock -%}
