// add header contents in multiple sections

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

const docx = new CreateDocx();

// add header contents in the first section
const headersA: OptionsHeader[] = [];

const headerFragmentDefaultA = new WordFragment();
headerFragmentDefaultA.addText("Text header first section.");
const headerContentDefaultA: OptionsHeader = {
    text: headerFragmentDefaultA,
};

const textFirstHeaderA: OptionsText = {
    bold: true,
    text: "Text first header in the first section.",
};
const headerFragmentFirstA = new WordFragment();
headerFragmentFirstA.addText([textFirstHeaderA]);
const headerContentFirstA: OptionsHeader = {
    text: headerFragmentFirstA,
    type: "first",
};

headersA.push(headerContentDefaultA);
headersA.push(headerContentFirstA);

// add headers
docx.addHeader(headersA);

docx.addText("First page in the first section.");
docx.addBreak("page");
docx.addText("Page in the first section.");

// add header contents in the second section excluding headers and footers from the previous section
const headersB: OptionsHeader[] = [];
const optionsSection: OptionsSection = {
    excludeHeadersFooters: true,
};
docx.addSection(optionsSection);

const headerFragmentDefaultB = new WordFragment();
headerFragmentDefaultB.addText("Text header second section.");
const headerContentDefaultB: OptionsHeader = {
    text: headerFragmentDefaultB,
};

const textFirstHeaderB: OptionsText = {
    bold: true,
    text: "Text first header in the second section.",
};
const headerFragmentFirstB = new WordFragment();
headerFragmentFirstB.addText([textFirstHeaderB]);
const headerContentFirstB: OptionsHeader = {
    text: headerFragmentFirstB,
    type: "first",
};

headersB.push(headerContentDefaultB);
headersB.push(headerContentFirstB);

// add headers
docx.addHeader(headersB);

docx.addText("First page in the second section.");
docx.addBreak("page");
docx.addText("Page in the second section.");

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