import { stampPDFPage } from '../../../utils';
import { Core } from '@pdftron/webviewer';
import { WATERMARK_HORIZONTAL_ALIGNMENTS, WATERMARK_VERTICAL_ALIGNMENTS } from '@red/domain';

export const processPage = async (
    pageNumber: number,
    document1: Core.PDFNet.PDFDoc,
    document2: Core.PDFNet.PDFDoc,
    mergedDocument: Core.PDFNet.PDFDoc,
    pdfNet: typeof Core.PDFNet,
    licenseKey: string,
) => {
    const document1PageCount = await document1.getPageCount();

    if (document1PageCount >= pageNumber) {
        await mergedDocument.insertPages(pageNumber * 2, document1, pageNumber, pageNumber, pdfNet.PDFDoc.InsertFlag.e_none);
        return;
    }

    const pageToCopy = await document2.getPage(pageNumber);
    const blankPage = await mergedDocument.pageCreate(await pageToCopy.getCropBox());
    await blankPage.setRotation(await pageToCopy.getRotation());
    await mergedDocument.pagePushBack(blankPage);
    await stampPDFPage(
        mergedDocument,
        pdfNet,
        '<< Compare Placeholder Page >>',
        20,
        'courier',
        'DIAGONAL',
        WATERMARK_HORIZONTAL_ALIGNMENTS.CENTER,
        WATERMARK_VERTICAL_ALIGNMENTS.CENTER,
        33,
        '#ffb83b',
        [await mergedDocument.getPageCount()],
        licenseKey,
    );
};
