{% from "nhsuk/components/card/macro.njk" import card as nhsukCard %}
{% from "nhsuk/macros/content.njk" import nhsukContent %}
{% 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 -%}

  {{- nhsukContent({
    text: item.text,
    html: item.html,
    slots: {
      before: nhsukIcon(icon) | trim,
      start: ((params.prefixText | default("do not")) ~ " ")
        if not params.hidePrefix and (params.prefixText or icon == "cross") else undefined
    }
  }) | trim }}
{%- 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 }}
