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

@Pipe({
  name: 'showCompartmentCode',
})
export class ShowCompartmentCodePipe implements PipeTransform {
  transform(firstItem: boolean, item: OrderItemListItemDTO, items: OrderItemListItemDTO[]): boolean {
    if (!firstItem) {
      return false;
    }

    const firstCompartmentCodeIndex = items.findIndex((i) => i.compartmentCode === item.compartmentCode);
    const itemIndex = items.findIndex((i) => i.orderItemSubsetId === item.orderItemSubsetId);

    if (isNullOrUndefined(firstCompartmentCodeIndex) || isNullOrUndefined(itemIndex)) {
      return true;
    }

    const previousIdentificalCompartmentCodeExists = firstCompartmentCodeIndex < itemIndex;

    return !previousIdentificalCompartmentCodeExists;
  }
}
