// replace image variables in a DOCX template

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

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

const docx = new CreateDocx();
// load the template
docx.openTemplate(docxTemplate).then(docx => {
    const fileJpgA = fs.readFileSync("../../files/imageP2.jpg");
    const fileJpgB = fs.readFileSync("../../files/imageP3.jpg");
    const fileJpgC = fs.readFileSync("../../files/image.jpg");

    // variables to be replaced in the document target
    const variablesDocument = new Map<string, ArrayBuffer>();
    variablesDocument.set("IMAGE_1", fileJpgA);
    variablesDocument.set("IMAGE_2", fileJpgB);

    // replace template image variables in the document target
    docx.replaceVariableImage(variablesDocument, "document");

    // variables to be replaced in the header target
    const variablesHeader = new Map<string, ArrayBuffer>();
    variablesHeader.set("HEADERIMG", fileJpgC);

    // replace template image variables in the header target
    docx.replaceVariableImage(variablesHeader, "header");

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