import { ProductResponseAttributes } from 'src/application/models';
import { ProductSorting } from './types';

export class ProductSorter {
  static sort(products: ProductResponseAttributes['attributes'][], sorting: ProductSorting) {
    switch (sorting) {
      case ProductSorting.PRICE_ASC:
        return [...products].sort(
          (p1, p2) =>
            (p1.salePrice ?? p1.originalPrice ?? 0) - (p2.salePrice ?? p2.originalPrice ?? 0),
        );

      case ProductSorting.PRICE_DESC:
        return [...products].sort(
          (p1, p2) =>
            (p2.salePrice ?? p2.originalPrice ?? 0) - (p1.salePrice ?? p1.originalPrice ?? 0),
        );

      default:
        return products;
    }
  }
}
