import {
  createCurrencyFormatter,
  resolveCurrencyAmount,
} from './resolveCurrencyAmount';

function formatMxn2(value: number) {
  return new Intl.NumberFormat('es-MX', {
    style: 'currency',
    currency: 'MXN',
    minimumFractionDigits: 2,
    maximumFractionDigits: 2,
  }).format(value);
}

describe('resolveCurrencyAmount', () => {
  const formatter = createCurrencyFormatter('MXN');

  it('returns null for null, undefined, non-finite numbers, and blank strings', () => {
    expect(resolveCurrencyAmount(null, formatter)).toBeNull();
    expect(resolveCurrencyAmount(undefined, formatter)).toBeNull();
    expect(resolveCurrencyAmount(Number.NaN, formatter)).toBeNull();
    expect(resolveCurrencyAmount('', formatter)).toBeNull();
    expect(resolveCurrencyAmount('   ', formatter)).toBeNull();
  });

  it('exposes an accessible label matching formatted MXN', () => {
    const amount = 1234.5;
    expect(resolveCurrencyAmount(amount, formatter)?.accessibleLabel).toBe(
      formatMxn2(amount),
    );
  });

  it('coerces string amounts at runtime (e.g. JSON)', () => {
    const n = 99.1;
    expect(resolveCurrencyAmount(String(n), formatter)?.accessibleLabel).toBe(
      formatMxn2(n),
    );
  });

  it('drops the decimal separator part so only currency, digits and fraction remain', () => {
    const parts = resolveCurrencyAmount(1234.5, formatter)?.parts ?? [];
    expect(parts.some(part => part.type === 'decimal')).toBe(false);
  });
});
