'use strict';

var PDFDocument = require('pdfkit');
var DocComponent = require('../component/DocComponent.js');
var Footer = require('../component/Footer/Footer.js');
var PageLines = require('../component/PageLines/PageLines.js');

let doc;
const Doc = {
    init: ({ params: { metadata: { author, subject, title }, }, }) => {
        doc = new PDFDocument({
            autoFirstPage: false,
            layout: 'portrait',
            lang: 'ko',
            info: {
                Author: author,
                Subject: subject,
                Title: title,
                Creator: author,
                Producer: author,
            },
        });
        return Doc.font('normal');
    },
    end: () => {
        doc.end();
        // @ts-ignore
        doc = undefined;
    },
    addPage: async (pageLinesConfig, footerConfig) => {
        doc.addPage({ size: 'A4', margin: 0 });
        await Doc.renderComponent(PageLines.PageLines({ ...pageLinesConfig }));
        await Doc.renderComponent(Footer.Footer({ ...footerConfig }));
        return Doc;
    },
    margin: 24,
    width: 595,
    height: 842,
    get: () => doc,
    font: (font) => {
        doc.font(`${__dirname}/../../assets/font/Pretendard-${font === 'normal' ? 'Regular' : font === 'bold' ? 'Bold' : 'Medium'}.otf`);
        return Doc;
    },
    renderComponent: async (c) => {
        await DocComponent.renderComponent(doc, c);
        return Doc;
    },
};

exports.Doc = Doc;
