import { Price } from '../models';

/**
 * Ensures that a price object has currency and currencySymbol set.
 * If the price has a value but currency or currencySymbol are missing,
 * defaults to 'EUR' and '€' respectively.
 *
 * This is necessary because the backend API requires these fields to be non-null
 * when a price value is present, but various adapters and API responses may
 * omit these fields.
 *
 * @param price - The price object to normalize
 * @returns The price object with currency defaults applied, or undefined if input was undefined
 *
 * @example
 * const price = {
 *   value: { value: 10.99 } // Missing currency and currencySymbol
 * };
 * const normalized = ensureCurrencyDefaults(price);
 * // Result: { value: { value: 10.99, currency: 'EUR', currencySymbol: '€' } }
 */
export function ensureCurrencyDefaults(
  price: Price | undefined,
): Price | undefined {
  if (!price?.value) {
    return price;
  }

  return {
    ...price,
    value: {
      ...price.value,
      currency: price.value.currency ?? 'EUR',
      currencySymbol: price.value.currencySymbol ?? '€',
    },
  };
}
