import {Component, OnInit, Input} from '@angular/core';
import {Sort} from '@angular/material/sort';
import {I18nService} from './../../services/i18n.service';

@Component({
  selector: 'app-quotas',
  templateUrl: './quotas.component.html',
  styleUrls: ['./quotas.component.scss']
})
export class QuotasComponent implements OnInit {


  @Input() quotas;
  quotaColumns = ["name", "quota", "quotaUnit"];

  constructor(private i18n: I18nService) {}

  ngOnInit(): void {
  }

  sortData(sort: Sort) {
    const data = this.quotas.slice();
    if(!sort.active || sort.direction === '') {
      this.quotas = data;
      return;
    }

    this.quotas = data.sort((a, b) => {
      const isAsc = sort.direction === 'asc';
      switch(sort.active) {
        case 'name': return this.compare(this.i18n.get('services.' + a.name + '.title', []), this.i18n.get('services.' + b.name + '.title', []), isAsc);
        case 'value': return this.compare(a.value, b.value, isAsc);
        default: return 0;
      }
    });
  }

  compare(a: number | string | String, b: number | string | String, isAsc: boolean) {
    return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
  }

}
