import { Component, ChangeDetectionStrategy, Input, OnChanges, SimpleChanges, HostBinding } from '@angular/core';
import { PackageItemDTO } from '@generated/swagger/wws-api';
import { ReplaySubject } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { PackageDetailsListStore } from './package-details-list.store';

@Component({
  selector: 'page-package-details-list-item',
  templateUrl: 'package-details-list-item.component.html',
  styleUrls: ['package-details-list-item.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
  standalone: false,
})
export class PackageDetailsListItemComponent implements OnChanges {
  private _itemSubject = new ReplaySubject<PackageItemDTO>(1);

  @Input()
  item: PackageItemDTO;

  @Input()
  @HostBinding('class.light')
  light: boolean;

  compartment$ = this._itemSubject.pipe(switchMap((item) => this._store.getCompartmentByEan$(item.product.ean)));

  inStock$ = this._itemSubject.pipe(switchMap((item) => this._store.getInStockByEan$(item.product.ean)));

  sscText$ = this._itemSubject.pipe(switchMap((item) => this._store.getSscTextByEan$(item.product.ean)));

  showStockInfoTooltip$ = this._store.arrivalStatus$.pipe(map((arrivalStatus) => arrivalStatus === 0));

  showStockInfos$ = this._store.arrivalStatus$.pipe(map((arrivalStatus) => arrivalStatus !== 8));

  constructor(private _store: PackageDetailsListStore) {}

  ngOnChanges({ item }: SimpleChanges): void {
    if (item && this.item) {
      this._itemSubject.next(this.item);
    }
  }
}
