"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.standaloneProcessServiceRouter = void 0;
const tslib_1 = require("tslib");
const content_disposition_1 = tslib_1.__importDefault(require("content-disposition"));
const koa_router_1 = tslib_1.__importDefault(require("koa-router"));
const auth_base_1 = require("@things-factory/auth-base");
const shell_1 = require("@things-factory/shell");
const headless_model_1 = require("../controllers/headless-model");
const screenshot_1 = require("../controllers/screenshot");
const process_1 = require("../service/process/process");
const pdf_1 = require("../controllers/pdf");
const fonts_1 = require("../controllers/fonts");
exports.standaloneProcessServiceRouter = new koa_router_1.default();
// for process headless
exports.standaloneProcessServiceRouter.get('/process-headless-full/:id', async (context, next) => {
    const { domain, user } = context.state;
    const { id } = context.params;
    const { model, base } = await (0, headless_model_1.headlessModel)({ domain, id });
    model.fonts = await (0, fonts_1.fonts)();
    (0, auth_base_1.setAccessTokenCookie)(context, await user.sign({ domain }));
    await context.render('internal-process-full-feature-view', { model, base });
});
// for process headless
exports.standaloneProcessServiceRouter.get('/process-headless/:id', async (context, next) => {
    const { domain, user } = context.state;
    const { id } = context.params;
    const { model, base } = await (0, headless_model_1.headlessModel)({ domain, id });
    model.fonts = await (0, fonts_1.fonts)();
    (0, auth_base_1.setAccessTokenCookie)(context, await user.sign({ domain }));
    await context.render('internal-process-service-view', { model, base });
});
// for process thumbnail
exports.standaloneProcessServiceRouter.get('/process-thumbnail/:id', async (context, next) => {
    const { domain } = context.state;
    const { id } = context.params;
    const { name, thumbnail } = await (0, shell_1.getRepository)(process_1.Process).findOneBy({ domain: { id: domain.id }, id });
    if (thumbnail) {
        const index = thumbnail.indexOf(';base64,');
        const base64 = thumbnail.substring(index + 8);
        const buffer = Buffer.from(base64, 'base64');
        context.type = 'image/png';
        context.set('Content-Disposition', (0, content_disposition_1.default)(`thumbnail-${name}.png`));
        context.body = buffer;
    }
    else {
        context.throw(404, 'thumbnail not found');
    }
});
// for webpage scrap
exports.standaloneProcessServiceRouter.get('/process-screenshot/:id', async (context, next) => {
    const { domain } = context.state;
    const { id } = context.params;
    const { model, process } = await (0, headless_model_1.headlessModel)({ domain, id });
    const data = Object.keys(context.query).length ? context.query : null;
    context.type = 'image/png';
    context.set('Content-Disposition', (0, content_disposition_1.default)(`${process.name}.png`));
    context.body = await (0, screenshot_1.screenshot)({ id, model, data, options: { encoding: 'binary', type: 'png' }, context });
});
// for webpage scrap
exports.standaloneProcessServiceRouter.get('/process/pdf/:id', async (context, next) => {
    const { domain } = context.state;
    const { id } = context.params;
    const { model, process } = await (0, headless_model_1.headlessModel)({ domain, id });
    const data = Object.keys(context.query).length ? context.query : null;
    const result = await (0, pdf_1.pdf)({
        id,
        model,
        data,
        options: {
            format: 'A4'
        },
        context
    });
    context.type = 'application/pdf';
    context.set('Content-Disposition', (0, content_disposition_1.default)(`${process.name}.pdf`));
    context.body = result;
});
//# sourceMappingURL=standalone-process-service-router.js.map