<!--
~ Copyright (C) 2023 StApps
~ This program is free software: you can redistribute it and/or modify it
~ under the terms of the GNU General Public License as published by the Free
~ Software Foundation, version 3.
~
~ This program is distributed in the hope that it will be useful, but WITHOUT
~ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
~ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
~ more details.
~
~ You should have received a copy of the GNU General Public License along with
~ this program. If not, see <https://www.gnu.org/licenses/>.
-->

<ion-item class="list-header" lines="none">
  <ion-checkbox
    [checked]="selection.checked"
    [indeterminate]="selection.indeterminate"
    (ionChange)="modified.emit(); selection.click()"
  >
    <ion-list-header> {{ 'data.chips.add_events.popover.ALL' | translate }} </ion-list-header>
  </ion-checkbox>
</ion-item>
@for (frequency of selection.children; track frequency) {
  <ion-list inset="true" lines="full">
    <ion-item lines="none" class="list-header">
      <ion-checkbox
        [checked]="frequency.checked"
        [indeterminate]="frequency.indeterminate"
        (ionChange)="modified.emit(); frequency.click()"
      >
        <ion-list-header>
          {{
            frequency.children[0].item.repeatFrequency
              ? (frequency.children[0].item.repeatFrequency | durationLocalized: true | sentencecase)
              : ('data.chips.add_events.popover.SINGLE' | translate | titlecase)
          }}
        </ion-list-header>
      </ion-checkbox>
    </ion-item>
    @for (date of frequency.children; track date) {
      <ion-item>
        <ion-checkbox
          [checked]="date.selected"
          (ionChange)="modified.emit(); date.selected = !date.selected; frequency.notifyChildChanged()"
        >
          @if (date.item.dates.length > 1) {
            <ion-text>
              {{ date.item.dates[0] | amDateFormat: 'dddd, LT' }} -
              {{ date.item.dates[0] | amAdd: date.item.duration | amDateFormat: 'LT' }}
            </ion-text>
            <br />
            <ion-text>
              {{ date.item.dates[0] | amDateFormat: 'LL' }} -
              {{ date.item.dates[date.item.dates.length - 1] | amDateFormat: 'LL' }}
            </ion-text>
          } @else {
            @if (date.item.dates[0]; as time) {
              <ion-text>
                {{ time | amDateFormat: 'LL, LT' }} -
                {{ time | amAdd: date.item.duration | amDateFormat: 'LT' }}
              </ion-text>
            } @else {
              <ion-text color="danger">{{ 'data.chips.add_events.popover.DATA_ERROR' | translate }}</ion-text>
              <br />
              @for (id of date.item.identifiers | keyvalue; track id) {
                <ion-text> {{ id.key }}: {{ id.value }} </ion-text>
              }
            }
          }
          @if (date.item.inPlace) {
            <ng-container class="ion-align-items-center">
              <br />
              <ion-text color="medium" class="place">
                <ion-icon name="pin_drop"></ion-icon>
                <span> {{ 'inPlace.name' | thingTranslate: date.item }}</span>
              </ion-text>
            </ng-container>
          }
        </ion-checkbox>
      </ion-item>
    }
  </ion-list>
}
