// replace table variables in a DOCX template

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

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

const docx = new CreateDocx();
// load the template
docx.openTemplate(docxTemplate).then(docx => {
    // variables to be replaced in the document
    const rowA = new Map<string, string>();
    rowA.set("ITEM", "Product A");
    rowA.set("REFERENCE", "107AW3");
    const rowB = new Map<string, string>();
    rowB.set("ITEM", "Product B");
    rowB.set("REFERENCE", "204RS67O");
    const rowC = new Map<string, string>();
    rowC.set("ITEM", "Product C");
    rowC.set("REFERENCE", "25GT&R56");

    const variablesDocument: Map<string, string | WordFragment>[] = [
        rowA,
        rowB,
        rowC,
    ];

    // replace variables in the document target
    docx.replaceVariableTable([variablesDocument]);

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