import { PageViewport, PDFDocumentProxy, PDFPageProxy } from 'pdfjs-dist';
import { IPDFLinkService } from 'pdfjs-dist/types/web/pdf_history';
import { PDFViewer } from 'pdfjs-dist/web/pdf_viewer';
import { MouseEvent } from 'react';

export interface TLTWH {
	left: number;
	top: number;
	width: number;
	height: number;
}

export type TNewHighlight = {
	position: TLTWH;
	pageNumber: number;
};

export type IHighlightType = 'table' | 'cell' | 'page' | 'document' | 'form' | 'word';

export type THighlight = {
	id: string;
	selectedColor: string;
	selectedBorderColor?: string;
	type: IHighlightType;
	unselectedBorderColor?: string;
	unselectedColor?: string;
	label?: string;
	groupId?: string;
} & TNewHighlight;

export interface TPDFJSDocument extends PDFDocumentProxy {
	numPages: number;
	getPage(number: number): Promise<PDFPageProxy>;
}

export interface TPDFJSLinkService extends IPDFLinkService {
	setDocument: (document: any) => void;
	setViewer: (viewer: PDFViewer) => void;
}

export interface TPDFJS {
	TextLayerBuilder: {
		prototype: {
			_bindMouse: () => void;
		};
	};
	PDFViewer: (options: any) => PDFViewer;
	PDFLinkService: () => TPDFJSLinkService;
	getDocument: (url: string) => Promise<TPDFJSDocument>;
	disableWorker: boolean;
}

export type THighlightItem<THT> = Omit<THT, 'position'> & { position: TLTWH };

export type THighLightItemProps = {
	onClick?: (itemId: string) => void;
	onDoubleClick?: (itemId: string) => void;
	onMouseOver?: () => void;
	onMouseOut?: () => void;
};

export interface PdfHighlighterProps<THT> {
	highlights: Array<THT>;
	pdfDocument: TPDFJSDocument;
	pdfScaleValue: string;
	removePageBorders?: boolean;
	onChangeActivePage(page: number): void;
	activePage: number;
	highlightAreaProps?: THighLightItemProps;
	disableRecomputeScale?: boolean;
}

export interface SinglePdfRenderProps {
	pdfDocument: TPDFJSDocument;
	pageNumber: number;
	highlights?: Array<THighlight>;
	highlightAreaProps?: THighLightItemProps;
	activeHighlightsId?: Array<string>;
	focusHighlightId?: string;
	zoomValue?: number;
	rotationValue?: number;
	showLabels?: boolean;
	isCentered?: boolean;
	fullWidth?: boolean;
	useLegacyContainerWidth?: boolean;
	scaleCanvasContainerByZoom?: boolean; // allow to scale canvas container with the zoom value passed, useful if any parent has applied transform css property
	className?: string;
	innerClassName?: string;
	renderTextLayer?: boolean | null;
	overscanCount?: number;
	debounceFullWidthDelay?: number;
	onClick?(evt: MouseEvent<HTMLDivElement>): void;
	onAfterPageRendered?(): void;
}

export interface PdfLoaderProps {
	data?: Blob;
	url?: string;
	beforeLoad: any;
	errorMessage?: any;
	children: (pdfDocument: TPDFJSDocument) => any;
	onError?: (error: Error) => void;
	cMapUrl?: string;
	cMapPacked?: boolean;
	timeoutTaskPromise?: number;
	usePromiseTaskTimeout?: boolean;
}

export interface PDFRenderRef {
	viewport: PageViewport;
	container: HTMLDivElement;
	defaultRotation: number;
	redrawPage(): void;
}

export interface PDFPage {
	div: HTMLDivElement;
	id: number;
}

export interface PDFUtilsType {
	percentageToViewport(ref: PDFRenderRef, data: TLTWH): TLTWH;
	viewportToPercentage(ref: PDFRenderRef, data: TLTWH): TLTWH;
}

export { PageViewport, PDFViewer };
