{#
  If a `level` property isn't provided but a `tag_name` is and it is a heading
  element, we can figure out the intended level from that tag.
#}

{% if level is empty and tag_name is not empty and tag_name|first == 'h' %}
  {% set level = tag_name|last|number_format %}
{% endif %}

{#
  If a `tag_name` property isn't provided but a `level` is, we construct the
  `tag_name` from the level.
#}

{% if tag_name is empty and level is not empty %}
  {% set tag_name = 'h' ~ max(1, min(level, 6)) %}
{% endif %}

{#
  If `tag_name` is still empty at this point, it can result in broken HTML,
  so we set a default. We set this to `h6` to coordinate with the default
  visual heading level and to avoid dramatically disrupting the document
  outline.
#}

{% if tag_name is empty %}
  {% set tag_name = 'h6' %}
{% endif %}

{#
  We cache the possible content chunks to variables we can test for content
  when it comes time to output the actual markup.
#}

{%- set _content -%}
  {%- block content -%}
    {{content}}
  {%- endblock -%}
{%- endset -%}

{%- set _subheading -%}
  {%- block subheading -%}
    {{subheading}}
  {%- endblock -%}
{%- endset -%}

{#
  If a `permalink` is desired but no `id` is specified, we try
  creating a `id` from the value of `content`.

  Since Twig lacks a built-in slugify function, this is pretty fragile and
  should be considered a fallback.
#}

{% if permalink and not id and _content is not empty %}
  {% set id = _content|trim|lower|escape('url')|replace({'%20': '-'}) %}
{% endif %}

{#
  Permalink heading markup based on this example from accessibility expert
  Marcy Sutton: http://codepen.io/marcysutton/pen/rLKvgZ
#}

{%- set _permalink -%}
  {%- if permalink and id -%}
    <a class="c-heading__permalink" href="#{{ id }}">
      {% include '@cloudfour/components/icon/icon.twig' with {
        name: 'link',
      } only %}
      <span class="u-hidden-visually">Permalink to {{ _content }}</span>
    </a>
  {%- endif -%}
{%- endset -%}

{#
  Build the class name based on options provided.
#}

{% set _class_prefix = 'c-heading' %}
{% set _class = _class_prefix %}

{% if level is not empty %}
  {% set _level_modifier = ('level-' ~ level)|replace({ '--': '-n' }) %}
  {% set _class = _class ~ ' ' ~ _class_prefix ~ '--' ~ _level_modifier %}
{% endif %}

{% if weight is not empty %}
  {% set _class = _class ~ ' ' ~ _class_prefix ~ '--' ~ weight %}
{% endif %}

{% if _permalink is not empty %}
  {% set _class = _class ~ ' ' ~ _class_prefix ~ '--with-permalink' %}
{% endif %}

{% if _subheading is not empty %}
  {% set _class = _class ~ ' ' ~ _class_prefix ~ '--with-subheading' %}
{% endif %}

{% if class %}
  {% set _class = _class ~ ' ' ~ class %}
{% endif %}

{#
  Output the actual markup depending on the options provided.
#}

{% if _permalink is empty and _subheading is empty %}
  <{{ tag_name }} class="{{ _class }}" {% if id %}id="{{id}}"{% endif %}>
    {{ _content }}
  </{{ tag_name }}>
{% else %}
  <div class="{{_class}}">
    <div class="{{_class_prefix}}__main">
      {{_permalink}}
      <{{ tag_name }} class="{{_class_prefix}}__content" {% if id %}id="{{ id }}"{% endif %}>
        {{ _content }}
      </{{ tag_name }}>
    </div>
    {% if _subheading is not empty %}
      <p class="{{_class_prefix}}__subheading">
        {{ _subheading }}
      </p>
    {% endif %}
  </div>
{% endif %}
