{% from "nhsuk/components/caption/macro.njk" import caption as nhsukCaption %}
{% from "nhsuk/components/heading/macro.njk" import heading as nhsukHeading %}
{% from "nhsuk/macros/attributes.njk" import nhsukAttributes %}

{#- Set classes for this component #}
{%- set classNames = "nhsuk-fieldset__legend" -%}
{%- set classNamesHeading = "nhsuk-fieldset__heading" -%}

{#- Set heading default (optional) #}
{%- set heading = params.heading -%}

{#- Support heading as string (optional) #}
{%- if heading and (heading is string or heading is escaped) %}
  {% set heading = {
    text: heading if heading is string else undefined,
    html: heading if heading is escaped else undefined
  } %}
{% endif -%}

{%- set hasHeading = true if (params.isPageHeading and heading != false) or
  (params.isPageHeading != false and heading) else false -%}

{#- Set caption from heading or caption options #}
{%- set legendCaption = heading.caption | default(params.caption) if hasHeading else params.caption -%}

{#- Support heading caption as string #}
{%- set headingCaption = legendCaption if legendCaption is mapping and legendCaption is not escaped else {
  text: legendCaption if legendCaption is string else undefined,
  html: legendCaption if legendCaption is escaped else undefined
} -%}

{%- set size = heading.size | default(params.size) if hasHeading else params.size -%}

{#- Limit heading caption to allowed options only #}
{%- set caption = {
  id: headingCaption.id,
  text: headingCaption.text,
  html: headingCaption.html,
  placement: "start" if headingCaption.placement == "before" else headingCaption.placement,
  size: headingCaption.size | default(size),
  classes: headingCaption.classes,
  attributes: headingCaption.attributes,
  element: headingCaption.element
} -%}

{%- set legend = {
  id: params.id,
  text: heading.text | default(params.text) if hasHeading else params.text | default(heading.text),
  html: heading.html | default(params.html) if hasHeading else params.html | default(heading.html),
  visuallyHiddenText: heading.visuallyHiddenText | default(params.visuallyHiddenText) if hasHeading else params.visuallyHiddenText,
  caption: caption if not caption.placement or caption.placement in ["start", "end"] else undefined,
  size: size,
  classes: params.classes,
  attributes: params.attributes
} -%}

{%- if legend.size and
  legend.size in ["s", "m", "l", "xl"] and (
    not legend.classes or (
      not "nhsuk-fieldset__legend--s" in legend.classes and
      not "nhsuk-fieldset__legend--m" in legend.classes and
      not "nhsuk-fieldset__legend--l" in legend.classes and
      not "nhsuk-fieldset__legend--xl" in legend.classes
    )
  )
%}
  {% set classNames = classNames ~ " nhsuk-fieldset__legend--" ~ legend.size %}
{% endif %}

{%- if legend.classes %}
  {% set classNames = classNames ~ " " ~ legend.classes %}
{% endif %}

{%- if caller or legend.html or legend.text %}
{% set innerHtml %}
{% if caller %}
  {{- caller() }}
{% elif legend.html %}
  {{- legend.html | safe }}
{% elif legend.text %}
  {{- legend.text }}
{% endif %}
{% endset -%}

{% if hasHeading %}
<legend class="{{ classNames }}" {%- if legend.id %} id="{{ legend.id }}"{% endif %}
  {{- nhsukAttributes(legend.attributes) }}>
  {{ nhsukHeading({
    id: heading.id,
    html: innerHtml,
    visuallyHiddenText: legend.visuallyHiddenText,
    caption: legend.caption,
    level: heading.level,
    className: classNamesHeading,
    attributes: heading.attributes
  }) | trim | indent(2) }}
</legend>
{% else %}
  {{- nhsukHeading({
    id: legend.id,
    html: innerHtml,
    visuallyHiddenText: legend.visuallyHiddenText,
    caption: legend.caption,
    className: classNames,
    attributes: legend.attributes,
    element: "legend"
  }) | trim }}
{% endif %}
{% if caption.placement == "after" %}
{{ nhsukCaption(caption) | safe | trim }}
{% endif %}
{% endif %}
