import { describe, it, expect, afterEach } from 'vitest';
import { cleanup, render } from 'vitest-browser-svelte';
import { page } from 'vitest/browser';
import SinglePersonHintBar from './SinglePersonHintBar.svelte';

afterEach(cleanup);

describe('SinglePersonHintBar', () => {
	it('renders the no-filter prompt when neither fromDate nor toDate is supplied', async () => {
		render(SinglePersonHintBar, { props: { senderName: 'Anna Schmidt' } });

		await expect.element(page.getByText('Anna Schmidt')).toBeVisible();
		await expect.element(page.getByText(/wähle einen korrespondenten/i)).toBeVisible();
	});

	it('renders the year range and sort label when fromDate is supplied', async () => {
		render(SinglePersonHintBar, {
			props: {
				senderName: 'Anna Schmidt',
				fromDate: '1923-01-01',
				toDate: '1925-12-31',
				sortDir: 'DESC'
			}
		});

		await expect.element(page.getByText('1923–1925')).toBeVisible();
		await expect.element(page.getByText('Neueste')).toBeVisible();
	});

	it('uses the "Älteste" label when sortDir is ASC', async () => {
		render(SinglePersonHintBar, {
			props: { senderName: 'Anna Schmidt', fromDate: '1923-01-01', sortDir: 'ASC' }
		});

		await expect.element(page.getByText('Älteste')).toBeVisible();
	});

	it('hides the no-filter prompt when fromDate alone is set', async () => {
		render(SinglePersonHintBar, {
			props: { senderName: 'Anna Schmidt', fromDate: '1923-01-01' }
		});

		await expect.element(page.getByText(/wähle einen korrespondenten/i)).not.toBeInTheDocument();
	});

	it('shows year range using only fromYear when toDate is empty', async () => {
		render(SinglePersonHintBar, {
			props: { senderName: 'Anna Schmidt', fromDate: '1923-01-01' }
		});

		await expect.element(page.getByText('1923–')).toBeVisible();
	});
});
