// add header contents with images and links

import {
    CreateDocx,
    OptionsHeader,
    OptionsImage,
    OptionsLink,
    OptionsTable,
    OptionsText,
    WordFragment,
} from "../../../build";
import * as fs from "fs";

const docx = new CreateDocx();

// generate header content
const headers: OptionsHeader[] = [];

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

const wordFragmentLink = new WordFragment();
const optionsLink: OptionsLink = {
    link: "https://www.phpdocx.com",
    text: "phpdocx",
};
wordFragmentLink.addLink(optionsLink);

const filePng = fs.readFileSync("../../files/image.png");
const wordFragmentImage = new WordFragment();
const imagePng: OptionsImage = {
    image: filePng,
};
wordFragmentImage.addImage(imagePng);

// add text contents and WordFragments in the table
const items = [[wordFragmentText, wordFragmentLink, wordFragmentImage]];

const wordFragmentTable = new WordFragment();
const borderNone = { style: "nil", width: 0 };
const optionsTable: OptionsTable = {
    border: {
        top: borderNone,
        right: borderNone,
        bottom: borderNone,
        left: borderNone,
        insideH: borderNone,
        insideV: borderNone,
    },
};
wordFragmentTable.addTable(items, [], optionsTable);

const headerContentDefault: OptionsHeader = {
    text: wordFragmentTable,
};
headers.push(headerContentDefault);

// add headers
docx.addHeader(headers);

docx.addText("New content.");

docx.save().then(contents => {
    fs.writeFileSync("addHeader_5.docx", Buffer.from(contents));
});
