import { ChangeDetectionStrategy, Component } from '@angular/core';
import { ReviewDTO } from '@swagger/cat';
import { UiModalRef } from '@ui/modal';

@Component({
  selector: 'modal-reviews',
  templateUrl: 'reviews.component.html',
  styleUrls: ['reviews.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ModalReviewsComponent {
  reviews = this.modalRef.data;
  rating = Math.floor((this.reviews.map((r) => r.rating).reduce((total, num) => total + num) / this.reviews.length) * 2) / 2;
  expandIds: number[] = [];

  constructor(private modalRef: UiModalRef<void, ReviewDTO[]>) {}

  expand(id: number) {
    let index = this.expandIds.indexOf(id);

    if (index > -1) {
      this.expandIds.splice(index, 1);
    } else {
      this.expandIds.push(id);
    }
  }

  close() {
    this.modalRef.close();
  }
}
