import { IRectangle } from '@red/domain';
import { Core } from '@pdftron/webviewer';
import Quad = Core.Math.Quad;

export const toPosition = (page: number, pageHeight: number, { x1, x2, x3, x4, y2, y4 }: Quad): IRectangle => {
    const height = y2 - y4;
    return {
        page,
        topLeft: {
            x: Math.min(x3, x4, x2, x1),
            y: pageHeight - (y4 + height),
        },
        height,
        width: Math.max(4, Math.abs(x3 - x4), Math.abs(x3 - x1)),
    };
};
