"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HeadlessPDFCaptureProcess = HeadlessPDFCaptureProcess;
const integration_base_1 = require("@things-factory/integration-base");
const integration_headless_1 = require("@things-factory/integration-headless");
const board_service_1 = require("@things-factory/board-service");
const utils_1 = require("@things-factory/utils");
const headless_model_1 = require("./headless-model");
const PAGE_FORMATS = {
    A4: { width: 595.28, height: 841.89 },
    A3: { width: 841.89, height: 1190.55 },
    Letter: { width: 612, height: 792 },
    Legal: { width: 612, height: 1008 }
};
function convertToPixels(value) {
    const dpi = 96; // PDF에서 기본적으로 사용하는 DPI
    if (value.endsWith('px')) {
        return parseFloat(value);
    }
    else if (value.endsWith('in')) {
        const inches = parseFloat(value);
        return inches * dpi;
    }
    else if (value.endsWith('cm')) {
        const cm = parseFloat(value);
        return cm * (dpi / 2.54);
    }
    return 0;
}
async function HeadlessPDFCaptureProcess(step, context) {
    const pdfUtil = new integration_headless_1.PDFCaptureUtil(context);
    await pdfUtil.initBrowser(step.connection);
    try {
        var { process: processObject, accessor, processAccessor, draft, format = 'A4', width, height, landscape, marginLeft = 0, marginRight = 0, marginTop = 0, marginBottom = 0 } = step.params;
        var { domain, data, user, logger } = context;
        const processId = processAccessor ? (0, utils_1.access)(processAccessor, data) : processObject === null || processObject === void 0 ? void 0 : processObject.id;
        if (!processId) {
            throw new Error('The process property must be set');
        }
        const processInput = (0, utils_1.access)(accessor, data);
        const { model, base } = await (0, headless_model_1.headlessModel)({ domain, id: processId }, draft);
        const [fontsToUse, fontStyles] = await board_service_1.BoardFunc.fonts(domain);
        model.fonts = fontsToUse;
        model.fontStyles = fontStyles;
        var widthN = width ? convertToPixels(width) : 0;
        var heightN = height ? convertToPixels(height) : 0;
        if (!widthN && !heightN && PAGE_FORMATS[format]) {
            const pageDimensions = PAGE_FORMATS[format];
            widthN = pageDimensions.width * (96 / 72);
            heightN = pageDimensions.height * (96 / 72);
        }
        if (landscape && widthN && heightN) {
            ;
            [widthN, heightN] = [heightN, widthN];
        }
        marginLeft = convertToPixels(marginLeft);
        marginTop = convertToPixels(marginTop);
        marginBottom = convertToPixels(marginBottom);
        marginRight = convertToPixels(marginRight);
        const contentWidth = widthN - marginLeft - marginRight;
        const contentHeight = heightN - marginTop - marginBottom;
        const page = await pdfUtil.browser.newPage();
        await page.setViewport({ width: Math.round(contentWidth), height: Math.round(contentHeight) });
        await page.setRequestInterception(true);
        await page.setDefaultTimeout(10000);
        page.on('console', async (msg) => {
            console.log(`[browser ${msg.type()}] ${msg.text()}`);
        });
        page.on('requestfailed', request => {
            console.log('Request failed:', request.url());
        });
        const protocol = 'http';
        const host = 'localhost';
        const port = process.env.PORT ? `:${process.env.PORT}` : '';
        const path = '/internal-board-service-view';
        const url = `${protocol}://${host}${port}${path}`;
        const token = await (user === null || user === void 0 ? void 0 : user.sign());
        page.on('request', request => {
            if (request.url() === url) {
                request.continue({
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'x-things-factory-domain': domain === null || domain === void 0 ? void 0 : domain.subdomain,
                        Authorization: 'Bearer ' + token
                    },
                    postData: JSON.stringify({
                        model,
                        base
                    })
                });
            }
            else if (request.url().startsWith(`${protocol}://${host}${port}`)) {
                request.continue({
                    headers: Object.assign(Object.assign({}, request.headers()), { 'x-things-factory-domain': domain === null || domain === void 0 ? void 0 : domain.subdomain, Authorization: 'Bearer ' + token })
                });
            }
            else {
                request.continue();
            }
        });
        await page.goto(url);
        await page.evaluate(data => {
            //@ts-ignore
            s.data = data;
            return new Promise(resolve => {
                requestAnimationFrame(() => resolve(0));
            });
        }, processInput);
        await pdfUtil.processPageAndGeneratePDF(step.params, null, page);
        return {
            data: context.__headless_pdf
        };
    }
    catch (error) {
        throw error;
    }
    finally {
        await pdfUtil.closeBrowser();
    }
}
HeadlessPDFCaptureProcess.parameterSpec = [
    ...(0, integration_headless_1.getCommonParameterSpec)(),
    {
        type: 'string',
        name: 'processAccessor',
        label: 'process-accessor'
    },
    {
        type: 'resource-object',
        name: 'process',
        label: 'process',
        property: {
            queryName: 'processes'
        }
    },
    {
        type: 'boolean',
        name: 'draft',
        label: 'process-draft',
        defaultValue: false,
        description: 'Set whether to get the current working version or the last released version'
    }
];
HeadlessPDFCaptureProcess.help = 'integration/task/headless-pdf-capture-process';
integration_base_1.TaskRegistry.registerTaskHandler('headless-pdf-capture-process', HeadlessPDFCaptureProcess);
//# sourceMappingURL=headless-pdf-capture-process.js.map