import { describe, it, expect, vi } from 'vitest';

vi.mock('$lib/paraglide/messages.js', () => ({
	m: {
		ocr_progress_heading: () => 'OCR-Analyse',
		ocr_status_preparing: () => 'Dokument wird vorbereitet…',
		ocr_status_loading: () => 'Lade Modell und Dokument…',
		ocr_status_analyzing: () => 'OCR-Analyse läuft…',
		ocr_status_creating_blocks: ({ count }: { count: string }) => `${count} Textblöcke erkannt`,
		ocr_status_done_blocks: ({ count }: { count: string }) => `${count} Blöcke erstellt`,
		ocr_status_done_skipped: ({ count, skipped }: { count: string; skipped: string }) =>
			`${count} Blöcke erstellt, ${skipped} Seite(n) übersprungen`,
		ocr_status_analyzing_page: ({ current, total }: { current: string; total: string }) =>
			`Seite ${current} von ${total} wird analysiert…`,
		ocr_status_preprocessing_page: ({ current, total }: { current: string; total: string }) =>
			`Seite ${current} von ${total} wird aufbereitet…`,
		ocr_status_error: () => 'OCR fehlgeschlagen'
	}
}));

import { translateOcrProgress } from './translateOcrProgress';

describe('translateOcrProgress', () => {
	it('returns heading for empty code', () => {
		const result = translateOcrProgress('');
		expect(result.message).toBe('OCR-Analyse');
	});

	it('translates PREPARING', () => {
		const result = translateOcrProgress('PREPARING');
		expect(result.message).toBe('Dokument wird vorbereitet…');
		expect(result.currentPage).toBeUndefined();
		expect(result.totalPages).toBeUndefined();
	});

	it('translates LOADING', () => {
		expect(translateOcrProgress('LOADING').message).toBe('Lade Modell und Dokument…');
	});

	it('translates ANALYZING', () => {
		expect(translateOcrProgress('ANALYZING').message).toBe('OCR-Analyse läuft…');
	});

	it('translates CREATING_BLOCKS with count', () => {
		expect(translateOcrProgress('CREATING_BLOCKS:42').message).toBe('42 Textblöcke erkannt');
	});

	it('translates DONE without skipped pages', () => {
		const result = translateOcrProgress('DONE:15');
		expect(result.message).toBe('15 Blöcke erstellt');
		expect(result.skippedPages).toBeUndefined();
	});

	it('translates DONE with zero skipped', () => {
		const result = translateOcrProgress('DONE:15:0');
		expect(result.message).toBe('15 Blöcke erstellt');
		expect(result.skippedPages).toBeUndefined();
	});

	it('translates DONE with skipped pages', () => {
		const result = translateOcrProgress('DONE:12:2');
		expect(result.message).toBe('12 Blöcke erstellt, 2 Seite(n) übersprungen');
		expect(result.skippedPages).toBe(2);
	});

	it('translates ANALYZING_PAGE with current, total, and blocks', () => {
		const result = translateOcrProgress('ANALYZING_PAGE:2:5:10');
		expect(result.message).toBe('Seite 2 von 5 wird analysiert…');
		expect(result.currentPage).toBe(2);
		expect(result.totalPages).toBe(5);
	});

	it('translates PREPROCESSING_PAGE with current and total', () => {
		const result = translateOcrProgress('PREPROCESSING_PAGE:3:10');
		expect(result.message).toBe('Seite 3 von 10 wird aufbereitet…');
		expect(result.currentPage).toBe(3);
		expect(result.totalPages).toBe(10);
	});

	it('PREPROCESSING_PAGE with no colon parts gracefully falls back to zero', () => {
		const result = translateOcrProgress('PREPROCESSING_PAGE');
		expect(result.currentPage).toBe(0);
		expect(result.totalPages).toBe(0);
	});

	it('translates ERROR', () => {
		expect(translateOcrProgress('ERROR').message).toBe('OCR fehlgeschlagen');
	});

	it('returns raw code for unknown codes', () => {
		expect(translateOcrProgress('UNKNOWN_CODE').message).toBe('UNKNOWN_CODE');
	});
});
