import { Component, OnInit, ChangeDetectionStrategy, Input } from '@angular/core';
import { Observable } from 'rxjs';
import { HistoryDTO } from '@cmf/trade-api';
import { HistoryStateFacade } from '@shelf-store/history';

@Component({
  selector: 'app-shelf-history-logs',
  templateUrl: 'shelf-history-logs.component.html',
  styleUrls: ['./shelf-history-logs.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ShelfHistoryLogsComponent implements OnInit {
  @Input() orderId: number;
  @Input() compartmentCode: string;
  @Input() orderItemId: number;
  @Input() orderItemSubsetId: number;

  history$: Observable<HistoryDTO[]>;
  hasError$: Observable<boolean>;

  constructor(private historyStateFacade: HistoryStateFacade) {}

  ngOnInit() {
    this.history$ = this.getHistory$();
    this.hasError$ = this.getErrorStatus$();
  }

  getHistory$(): Observable<HistoryDTO[]> {
    return this.historyStateFacade.getHistory$(this.orderItemSubsetId);
  }

  getErrorStatus$(): Observable<boolean> {
    return this.historyStateFacade.hasErrorStatus$(this.orderItemSubsetId);
  }
}
