// replace text variables with WordFragments using inline type replacements in a DOCX template

import {
    CreateDocx,
    OptionsDateTime,
    OptionsLink,
    OptionsReplaceVariableWordFragment,
    OptionsText,
    WordFragment,
} 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 => {
    // create a link WordFragment
    const wordFragmentLink = new WordFragment();
    const optionsLink: OptionsLink = {
        link: "https://www.phpdocx.com",
        text: "phpdocx",
    };
    wordFragmentLink.addLink(optionsLink);

    // create text WordFragments
    const wordFragmentTextDocument = new WordFragment();
    const textA: OptionsText = {
        bold: true,
        italic: true,
        fontSize: 14,
        text: "new content",
    };
    const textB: OptionsText = {
        color: "0000FF",
        font: "Times New Roman",
        text: " with styles",
    };
    wordFragmentTextDocument.addText([textA, textB]);

    const wordFragmentText = new WordFragment();
    const text: OptionsText = {
        bold: true,
        italic: true,
        fontSize: 14,
        text: "new content",
    };
    wordFragmentText.addText([text]);

    // create a date WordFragment
    const dateTime: OptionsDateTime = {
        format: "M/d/yyyy",
        bold: true,
        color: "333333",
    };
    const wordFragmentDate = new WordFragment();
    wordFragmentDate.addDateTime(dateTime);

    // variables to be replaced in the document
    const variablesDocument = new Map<string, WordFragment>();
    variablesDocument.set("VARNAME", wordFragmentTextDocument);
    variablesDocument.set("OTHERVAR", wordFragmentLink);

    // replace variable options
    const optionsReplaceVariableWordFragment: OptionsReplaceVariableWordFragment =
        {
            type: "inline",
        };

    // replace variables in the document target
    docx.replaceVariableWordFragment(
        variablesDocument,
        "document",
        optionsReplaceVariableWordFragment
    );

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

    // replace variables in the header target
    docx.replaceVariableWordFragment(
        variablesHeader,
        "header",
        optionsReplaceVariableWordFragment
    );

    // variables to be replaced in the footer target
    const variablesFooter = new Map<string, WordFragment>();
    variablesFooter.set("FOOTERVAR", wordFragmentDate);

    // replace variables in the footer target
    docx.replaceVariableWordFragment(
        variablesFooter,
        "footer",
        optionsReplaceVariableWordFragment
    );

    // variables to be replaced in the comment target
    const variablesComment = new Map<string, WordFragment>();
    variablesComment.set("COMMENTVAR_1", wordFragmentText);

    // replace variables in the comment target
    docx.replaceVariableWordFragment(
        variablesComment,
        "comment",
        optionsReplaceVariableWordFragment
    );

    // variables to be replaced in the endnote target
    const variablesEndnote = new Map<string, WordFragment>();
    variablesEndnote.set("ENDNOTEVAR", wordFragmentText);

    // replace variables in the footer target
    docx.replaceVariableWordFragment(
        variablesEndnote,
        "endnote",
        optionsReplaceVariableWordFragment
    );

    // variables to be replaced in the footnote target
    const variablesFootnote = new Map<string, WordFragment>();
    variablesFootnote.set("FOOTNOTEVAR", wordFragmentText);

    // replace variables in the footer target
    docx.replaceVariableWordFragment(
        variablesFootnote,
        "footnote",
        optionsReplaceVariableWordFragment
    );

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