import { Pipe, PipeTransform } from '@angular/core';
import { KeyValueDTOOfStringAndString, OrderItemListItemDTO } from '@swagger/oms';

@Pipe({
  name: 'goodsInReservationSelectable',
})
export class GoodsInReservationSelectablePipe implements PipeTransform {
  transform(
    item: OrderItemListItemDTO,
    selectionRules: (action: KeyValueDTOOfStringAndString) => boolean,
    selectedItems: OrderItemListItemDTO[]
  ): boolean {
    if (selectedItems.length > 0) {
      const firstSelectedItem = selectedItems[0];
      const selectedLabel = firstSelectedItem.actions.find((action) => selectionRules(action)).label;
      return item?.actions?.some((action) => selectionRules(action) && action.label === selectedLabel);
    } else {
      return item?.actions?.some((action) => selectionRules(action));
    }
  }
}
