import { Component, OnInit, ChangeDetectionStrategy } from '@angular/core';
import { Store, Select } from '@ngxs/store';
import { Observable } from 'rxjs';
import { RemissionSelectors } from 'apps/sales/src/app/core/store/selectors/remission.selectors';
import { CapacityType } from '@isa/remission';
import { map } from 'rxjs/operators';
import { isNullOrUndefined } from 'util';

@Component({
  selector: 'app-remission-uberlauf-capacities',
  templateUrl: './ueberlauf-capacities.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class RemissionUeberlaufCapacitiesComponent implements OnInit {
  @Select(RemissionSelectors.getRequiredCapacities) capacities$: Observable<CapacityType[]>;
  @Select(RemissionSelectors.getRemissionActiveFilters)
  remissionFilters$: Observable<any>;
  @Select(RemissionSelectors.getIsLoading) isLoading$: Observable<boolean>;

  showOverflowInitialMessage$: Observable<boolean>;

  constructor(private store: Store) {}

  ngOnInit() {
    this.initShowOverflowMessage();
  }

  private initShowOverflowMessage() {
    this.showOverflowInitialMessage$ = this.remissionFilters$.pipe(
      map((filters) => isNullOrUndefined(filters) || !Object.values(filters).length || !(filters.abteilungen && filters.abteilungen.length))
    );
  }
}
