{% set schema = bolt.data.components['@bolt-components-headline'].schema %}

{% if enable_json_schema_validation %}
  {{ validate_data_schema(schema, _self) | raw }}
{% endif %}

{# DEPRECATED.  Use the property `number_text` instead of `numberText`. #}
{% if numberText %}
  {% set number_text = numberText %}
{% endif %}

{# DEPRECATED.  Use the property `number_color` instead of `numberColor`. #}
{% if numberColor %}
  {% set number_color = numberColor %}
{% endif %}

{% set types = ['headline', 'subheadline', 'eyebrow', 'text'] %} {# Pre-defined types #}
{% set tags = schema.properties.tag.enum %}
{% set weights = schema.properties.weight.enum %}
{% set styles = schema.properties.style.enum %}
{% set transformProps = schema.properties.transform.enum %}
{% set sizes = schema.properties.size.enum %}
{% set alignments = schema.properties.align.enum %}

{% if tags != 'any' %}
  {% set tag = tag in tags ? tag : schema.properties.tag.default %}
{% else %}
  {% set tag = tag ?? schema.properties.tag.default %}
{% endif %}
{% set weight = weight | default(schema.properties.weight.default) %}
{% set style = style | default(schema.properties.style.default) %}
{% set type = type in types ? type : 'text' %}
{% set size = size | default(schema.properties.size.default) %}

{% set numberSize = 'auto' %}
{% set autoshrink = autoshrink is defined ? autoshrink : schema.properties.autoshrink.default %}

{# For backwards compatibility only, setting icon to exactly 'false' is the same as specifying 'none'.  Deprecated. #}
{% if icon is same as(false) %}
  {% set icon = 'none' %}
{% endif %}

{# If this is a link, use a chevron-right icon unless explicitly requested otherwise. #}
{% set fallbackIconName = url ? 'chevron-right' : 'none' %}

{# iconName is a string representing the name of the icon or "none" if no icon should be shown. #}
{% set iconName = icon is iterable ? icon.name : icon | default(fallbackIconName)%}

{# Validate iconName.  If invalid, we'll use the default #}
{#{% set iconSchema = bolt.data.components['@bolt-components-icon'].schema %}#}
{#{% if iconName not in iconSchema.properties.name.enum and iconName != 'none' %}#}
  {#{% set iconName = fallbackIconName %}#}
{#{% endif %}#}

{# If icon was a string, initialize it as an empty object. #}
{% set icon = icon is iterable ? icon : {} %}

{% if iconName == 'none' %}
  {% set icon = false %}
{% else %}
  {% set icon = icon | merge({ name: iconName }) %}
{% endif %}

{# Covers backward compatibility with use of both left and before to describe icon position #}
{% set iconPosition = icon.position == 'left' ? 'before' : icon.position  %}

{% set prefix = 'c-bolt-' %}
{% set baseClass = prefix ~ type %}
{% set attributes = create_attribute(attributes|default({})) %}

{% if (size == 'xxxlarge') and (text|length >= 60) and autoshrink == true %}
  {% set longTitle = true %}
{% endif %}

{% if slot %}
  {% set attributes = attributes.setAttribute('slot', slot) %}
{% endif %}

{% set classes = [
  baseClass,
  number_text ? baseClass ~ '--' ~ 'bulleted' : '',
  quoted ? baseClass ~ '--' ~ 'quoted' : '',
  weight in weights ? baseClass ~ '--' ~ weight : '',
  align in alignments and align != null ? baseClass ~ '--' ~ align : '',
  style in styles and type == 'text' ? baseClass ~ '--' ~ style : '',
  size in sizes and type != 'eyebrow' ? longTitle and type == 'headline' ? baseClass ~ '--' ~ size ~ '-min' : baseClass ~ '--' ~ size : '',
  transform in transformProps and transform != '' ? baseClass ~ '--' ~ transform : '',
  url ? baseClass ~ '--link' : '',
  iconPosition ? baseClass ~ '--icon-position-' ~ iconPosition : ''
] %}

{% set iconBefore %}{% apply spaceless %}
  {% if icon and not url and iconPosition == 'before' %}
    <span class="c-bolt-headline__icon c-bolt-headline__icon--position-before">
      {% include '@bolt-elements-icon/icon.twig' with icon only %}
    </span>
  {% endif %}
{% endapply %}{% endset %}

{% set iconAfter %}{% apply spaceless %}
  {% if icon and not url and iconPosition != 'before' %}
    <span class="c-bolt-headline__icon c-bolt-headline__icon--position-after">
      {% include '@bolt-elements-icon/icon.twig' with icon only %}
    </span>
  {% endif %}
{% endapply %}{% endset %}

{% set hasIcon = iconBefore or iconAfter ? true : false %}

<{{ tag }} {{ attributes.addClass(classes) }}>{% apply spaceless %}
  {% if number_text %}
    <span class="c-bolt-headline__bullet c-bolt-headline__bullet--size-{{ numberSize }}">
      <span class="c-bolt-headline__bullet-inner {{ number_color ? 'c-bolt-headline__bullet-inner--' ~ number_color : ''}}">
        <span class="c-bolt-headline__bullet-text">{{ number_text }}</span>
      </span>
    </span>
  {% endif %}
  {%- if url -%}

    {% set _link_attributes = {
      href: url
    } %}

    {% if target %}
      {% set _link_attributes = _link_attributes|merge({
        target: target
      }) %}
    {% endif %}

    {% set _link_props = {
      content: text,
      reversed_underline: (type != 'text'),
      attributes: _link_attributes,
    } %}

    {% if icon %}
      {% set _icon_rendered %}
        {% include '@bolt-elements-icon/icon.twig' with icon only %}
      {% endset %}

      {% if iconPosition == 'before' %}
        {% set _link_props = _link_props|merge({ icon_before: _icon_rendered }) %}
      {% else %}
        {% set _link_props = _link_props|merge({ icon_after: _icon_rendered }) %}
      {% endif %}
    {% endif %}

    {% include '@bolt-elements-text-link/text-link.twig' with _link_props only %}
  {%- else -%}
    {% if hasIcon or quoted %}<span class="c-bolt-headline__text">{% endif %}
      {{- iconBefore -}}
      {{- text -}}
      {{- iconAfter -}}
    {% if hasIcon or quoted %}</span>{% endif %}
  {% endif %}
{% endapply %}</{{ tag }}>
