import { m } from '$lib/paraglide/messages.js';

export interface OcrProgressResult {
	message: string;
	currentPage?: number;
	totalPages?: number;
	skippedPages?: number;
}

export function translateOcrProgress(code: string): OcrProgressResult {
	if (!code) return { message: m.ocr_progress_heading() };

	const parts = code.split(':');
	const key = parts[0];

	switch (key) {
		case 'PREPARING':
			return { message: m.ocr_status_preparing() };
		case 'LOADING':
			return { message: m.ocr_status_loading() };
		case 'ANALYZING':
			return { message: m.ocr_status_analyzing() };
		case 'CREATING_BLOCKS':
			return { message: m.ocr_status_creating_blocks({ count: parts[1] ?? '0' }) };
		case 'DONE': {
			const count = parts[1] ?? '0';
			const skipped = parts[2] ? parseInt(parts[2], 10) : 0;
			if (skipped > 0) {
				return {
					message: m.ocr_status_done_skipped({
						count,
						skipped: String(skipped)
					}),
					skippedPages: skipped
				};
			}
			return { message: m.ocr_status_done_blocks({ count }) };
		}
		case 'ANALYZING_PAGE': {
			const current = parseInt(parts[1] ?? '0', 10);
			const total = parseInt(parts[2] ?? '0', 10);
			return {
				message: m.ocr_status_analyzing_page({
					current: String(current),
					total: String(total)
				}),
				currentPage: current,
				totalPages: total
			};
		}
		case 'PREPROCESSING_PAGE': {
			const current = parseInt(parts[1] ?? '0', 10);
			const total = parseInt(parts[2] ?? '0', 10);
			return {
				message: m.ocr_status_preprocessing_page({
					current: String(current),
					total: String(total)
				}),
				currentPage: current,
				totalPages: total
			};
		}
		case 'ERROR':
			return { message: m.ocr_status_error() };
		default:
			return { message: code };
	}
}
