{% from "nhsuk/components/card/macro.njk" import card as nhsukCard %}
{% from "nhsuk/macros/icon.njk" import nhsukIcon %}

{#- Support heading as string (with deprecated options) #}
{%- set heading = params.heading if params.heading is mapping and params.heading is not escaped else {
  text: params.heading if params.heading is string else params.title,
  html: params.heading if params.heading is escaped else undefined,
  level: params.headingLevel
} -%}

{#- Set icon or (deprecated) type for this component #}
{%- set icon = "cross" if (params.icon or params.type) == "cross" else "tick" %}
{%- set items = params.items | select("mapping") | list if params.items else [] %}

{%- macro _listItem(params, data, icon) %}
  {% set item = {
    text: data.item if data.item is string,
    html: data.item if data.item is escaped
  } if data.item else data -%}

  {{ nhsukIcon(icon) | trim }}
  {% if not params.hidePrefix and (params.prefixText or icon == "cross") %}
    {{- (params.prefixText if params.prefixText else "do not") ~ " " -}}
  {% endif %}
  {% if item.html %}
    {{- item.html | safe | trim }}
  {% elif item.text %}
    {{- item.text | trim }}
  {% endif %}
{% endmacro -%}

{% set listHtml %}
<ul class="nhsuk-list nhsuk-list--{{ icon }}" role="list">
{% for data in items %}
  <li>
    {{ _listItem(params, data, icon) | trim | indent(4) }}
  </li>
{% endfor %}
</ul>
{% endset -%}

{{ nhsukCard({
  id: params.id,
  heading: {
    id: heading.id,
    text: heading.text,
    html: heading.html,
    visuallyHiddenText: heading.visuallyHiddenText,
    level: heading.level | default(3),
    classes: heading.classes,
    attributes: heading.attributes
  },
  description: {
    html: listHtml
  },
  classes: params.classes,
  attributes: params.attributes,
  variant: "feature"
}) | trim }}
