{#
* listDays macro is used to display all the days withing that particular calendar month
* we use a recursive strategy to create a loop from the startDate to the endDate, displaying as a day in the iteration

Params:
  week_start:   The day number of the week. eg. Sun = 1, Wed = 4.
  startDate:    The begining date of that particular month. eg. '2021-06-01'
  endDate:      The end date of that particular month. eg. '2021-06-30'
  track:        This is used as a counter as we iterate within the FIRST row of the calendar.
                This is to avoid the calendar cells from going over the predefined 7 days of the week
                if the week_start is greater than Sunday.
  alterations:  A list of all the alterations
#}

{% macro listDays(week_start, startDate, endDate, track, alterations) %}
{# We need to keep a collection of alteration if the current date is within the alteration period #}
{% set alt_array = [] %}
{% set row_count = 0 %}
{% set day_count = 7 %}
{% set week_start = week_start == 7 ? 0: week_start %}

{% if startDate <= endDate %}
  {% if track==0 and week_start == 0 %}

<div class="cells-row">
  {% elseif week_start > 0 %}
<div class="cells-row">

  {# Make leading days #}
  {% for i in range(1, week_start) %}
    {% include "@organisms/tm-sa/sa-calendar/calendar-cell.twig" with {
      "class": "disabled",
      "alterations": []
    } %}
    {% set track = track + 1 %}
    {% set row_count = row_count + 1 %}
    {% set week_start = week_start - 1 %}
  {% endfor %}

{% elseif track % day_count == 0 %}

</div>

  <div class="cells-row">
  {% endif %}

    {# Loop through alterations, format the date periods to yyyy/mm/dd then do a comparison to the current date iteration #}
    {% for alt in alterations %}
      {% set f_sdate = alt.start_date|slice(5) %}
      {% set f_sdate = f_sdate|slice(6) ~ '-' ~ f_sdate|slice(3,2) ~ '-' ~ f_sdate|slice(0,2) %}

      {% set f_edate = alt.end_date|slice(5) %}
      {% set f_edate = f_edate|slice(6) ~ '-' ~ f_edate|slice(3,2) ~ '-' ~ f_edate|slice(0,2) %}

      {% if startDate >= f_sdate and startDate <= f_edate %}
        {% set alt_array = alt_array|merge([alt]) %}
      {% endif %}
    {% endfor %}

    {% set class = 'no-alterations disabled' %}
    {% if alt_array|length %}
      {% set class = 'has-alterations' %}
      {% if alt_array|length == 1 %}
        {% set class = class ~ '  alterations-1' %}
      {% endif %}
    {% endif %}

    {% include "@organisms/tm-sa/sa-calendar/calendar-cell.twig" with {
      "class": "#{class}",
      "date_text": startDate,
      "alterations": alt_array
    } %}
    {% set row_count = row_count + 1 %}

    {# Make trailing days #}
    {% if startDate == endDate %}

      {% for i in range(1, track % day_count) %}
        {% set row_count = row_count + 1 %}
      {% endfor %}

      {% if track % day_count == 0 %}
        {% set row_count = 1 %}
      {% endif %}

      {% if track % day_count == 6 %}
        {% set row_count = 7 %}
      {% endif %}

      {% if row_count <  day_count %}
        {% for i in range(1, day_count - row_count) %}
          {% include "@organisms/tm-sa/sa-calendar/calendar-cell.twig" with {
            "class": "disabled",
            "alterations": []
          } %}
        {% endfor %}
      {% endif %}
      </div>{# This div was missing! #}
    {% endif %}

    {# Call the macro again with the new parameters to start a new iteration #}
    {{ _self.listDays(
      week_start,
      startDate|date_modify("+1 day")|date('Y-m-d'),
      endDate, track + 1,
      alterations)
    }}

{% endif %}

{% endmacro %}

</div>{# This div was missing! not 100% it goes here, but seems to work #}
