// replace text variables in a DOCX template

import { CreateDocx } from "../../../build";
import * as fs from "fs";

const docxTemplate = fs.readFileSync("../../files/TemplateVariables.docx");

const docx = new CreateDocx();
// load the template
docx.openTemplate(docxTemplate).then(docx => {
    // variables to be replaced in the document target
    const variablesDocument = new Map<string, string>();
    variablesDocument.set("VARNAME", "new value");
    variablesDocument.set("OTHERVAR", "a new text\nwith a line break");

    // replace variables in the document target
    docx.replaceVariableText(variablesDocument);

    // variables to be replaced in the header target
    const variablesHeader = new Map<string, string>();
    variablesHeader.set("HEADERVAR", "New header content");
    // replace template variables in the header target
    docx.replaceVariableText(variablesHeader, "header");

    // variables to be replaced in the footer target
    const variablesFooter = new Map<string, string>();
    variablesFooter.set("FOOTERVAR", "New footer content.");
    // replace template variables in the footer target
    docx.replaceVariableText(variablesFooter, "footer");

    // variables to be replaced in the comment target
    const variablesComment = new Map<string, string>();
    variablesComment.set("COMMENTVAR_1", "text content");
    // replace template variables in the comment target
    docx.replaceVariableText(variablesComment, "comment");

    // variables to be replaced in the endnote target
    const variablesEndnote = new Map<string, string>();
    variablesEndnote.set("ENDNOTEVAR", "endnote content");
    // replace template variables in the endnote target
    docx.replaceVariableText(variablesEndnote, "endnote");

    // variables to be replaced in the footnote target
    const variablesFootnote = new Map<string, string>();
    variablesFootnote.set("FOOTNOTEVAR", "foonote content");
    // replace template variables in the footnote target
    docx.replaceVariableText(variablesFootnote, "footnote");

    // save the DOCX
    docx.save().then(contents => {
        fs.writeFileSync("replaceVariableText_1.docx", Buffer.from(contents));
    });
});
