import { describe, expect, it } from 'vitest';
import { germanToIso, isoToGerman } from './utils';

describe('isoToGerman', () => {
	it('converts a standard ISO date', () => {
		expect(isoToGerman('2024-03-15')).toBe('15.03.2024');
	});

	it('preserves leading zeros for day and month', () => {
		expect(isoToGerman('2024-01-05')).toBe('05.01.2024');
	});

	it('handles December 31', () => {
		expect(isoToGerman('1945-12-31')).toBe('31.12.1945');
	});

	it('returns empty string for empty input', () => {
		expect(isoToGerman('')).toBe('');
	});

	it('returns empty string for plain text', () => {
		expect(isoToGerman('not-a-date')).toBe('');
	});

	it('returns empty string for partial ISO string', () => {
		expect(isoToGerman('2024-03')).toBe('');
	});

	it('returns empty string for ISO with time component', () => {
		expect(isoToGerman('2024-03-15T12:00:00')).toBe('');
	});
});

describe('germanToIso', () => {
	it('converts a standard German date', () => {
		expect(germanToIso('15.03.2024')).toBe('2024-03-15');
	});

	it('preserves leading zeros for day and month', () => {
		expect(germanToIso('05.01.2024')).toBe('2024-01-05');
	});

	it('handles December 31', () => {
		expect(germanToIso('31.12.1945')).toBe('1945-12-31');
	});

	it('returns empty string for empty input', () => {
		expect(germanToIso('')).toBe('');
	});

	it('returns empty string for plain text', () => {
		expect(germanToIso('not-a-date')).toBe('');
	});

	it('returns empty string for date without leading zeros', () => {
		expect(germanToIso('5.3.2024')).toBe('');
	});

	it('returns empty string for ISO format input', () => {
		expect(germanToIso('2024-03-15')).toBe('');
	});

	it('returns empty string for partial German date', () => {
		expect(germanToIso('15.03')).toBe('');
	});
});

describe('round-trip conversion', () => {
	const dates = ['2024-03-15', '1945-01-01', '2000-12-31', '1899-07-04'];

	for (const date of dates) {
		it(`ISO → German → ISO is identity for ${date}`, () => {
			expect(germanToIso(isoToGerman(date))).toBe(date);
		});
	}

	it('German → ISO → German is identity', () => {
		expect(isoToGerman(germanToIso('20.04.1889'))).toBe('20.04.1889');
	});
});
