import { Item } from '@isa/catalogue/data-access';
import { Price } from '../models';

/**
 * Helper function to extract the retail price from an Item object.
 * The function first checks for store-specific availabilities and falls back to the catalog availability if none are found.
 * @param {Item} item - The item object from which to extract the retail price
 * @returns {Price | undefined} The retail price if available, otherwise undefined
 */
export const getRetailPriceFromItem = (item: Item): Price | undefined => {
  let availability = item?.storeAvailabilities?.find((f) => !!f);

  if (!availability) {
    availability = item?.catalogAvailability;
  }

  if (!availability.price) {
    return {
      value: { value: 0, currency: 'EUR' },
    };
  }

  return availability.price as Price;
};
