import { describe, it, expect } from 'vitest';
import { problemDetail, problemFieldErrors } from './api-error';

describe('problemDetail', () => {
  it('extracts the detail from an RFC-7807 ProblemDetails', () => {
    expect(problemDetail({ detail: 'Afgewezen: 0 uren.', status: 422 }, 'fallback')).toBe(
      'Afgewezen: 0 uren.',
    );
  });

  it('falls back when there is no detail', () => {
    expect(problemDetail(new Error('boom'), 'fallback')).toBe('fallback');
    expect(problemDetail({ status: 500 }, 'fallback')).toBe('fallback');
    expect(problemDetail(undefined, 'fallback')).toBe('fallback');
  });
});

describe('problemFieldErrors (G4 seam)', () => {
  it('maps a ValidationProblemDetails errors dict to first-message-per-field', () => {
    expect(
      problemFieldErrors({ errors: { straat: ['Verplicht.'], postcode: ['Ongeldig.', 'x'] } }),
    ).toEqual({ straat: 'Verplicht.', postcode: 'Ongeldig.' });
  });

  it('returns {} when there is no errors envelope (the current backend shape)', () => {
    expect(problemFieldErrors({ detail: 'one banner' })).toEqual({});
    expect(problemFieldErrors(new Error('boom'))).toEqual({});
    expect(problemFieldErrors(undefined)).toEqual({});
  });
});
