import { vi } from 'vitest';
import type { LibLoader } from './usePdfRenderer.svelte';

export function makeFakePdfjsLib() {
	class TextLayerMock {
		render() {
			return Promise.resolve();
		}
		cancel() {}
	}

	return {
		GlobalWorkerOptions: { workerSrc: '' },
		getDocument: vi.fn().mockReturnValue({
			promise: Promise.resolve({
				numPages: 2,
				getPage: vi.fn().mockResolvedValue({
					getViewport: vi.fn().mockReturnValue({ width: 595, height: 842 }),
					render: vi.fn().mockReturnValue({ promise: Promise.resolve() }),
					streamTextContent: vi.fn().mockReturnValue(new ReadableStream())
				})
			})
		}),
		TextLayer: TextLayerMock
	} as unknown as typeof import('pdfjs-dist');
}

export function makeFakeLibLoader(): LibLoader {
	const fakePdfjs = makeFakePdfjsLib();
	return vi.fn().mockResolvedValue([fakePdfjs, { default: '' }] as const);
}
