"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FontXform = void 0;
const color_xform_1 = require("./color-xform.js");
const boolean_xform_1 = require("../simple/boolean-xform.js");
const integer_xform_1 = require("../simple/integer-xform.js");
const string_xform_1 = require("../simple/string-xform.js");
const underline_xform_1 = require("./underline-xform.js");
const base_xform_1 = require("../base-xform.js");
// Font encapsulates translation from font model to xlsx
class FontXform extends base_xform_1.BaseXform {
    constructor(options) {
        super();
        this.options = options || FontXform.OPTIONS;
        // Define properties in render order (Excel's expected order)
        const fontProperties = [
            { tag: "b", prop: "bold", xform: new boolean_xform_1.BooleanXform({ tag: "b", attr: "val" }) },
            { tag: "i", prop: "italic", xform: new boolean_xform_1.BooleanXform({ tag: "i", attr: "val" }) },
            { tag: "u", prop: "underline", xform: new underline_xform_1.UnderlineXform() },
            { tag: "strike", prop: "strike", xform: new boolean_xform_1.BooleanXform({ tag: "strike", attr: "val" }) },
            {
                tag: "condense",
                prop: "condense",
                xform: new boolean_xform_1.BooleanXform({ tag: "condense", attr: "val" })
            },
            { tag: "extend", prop: "extend", xform: new boolean_xform_1.BooleanXform({ tag: "extend", attr: "val" }) },
            { tag: "outline", prop: "outline", xform: new boolean_xform_1.BooleanXform({ tag: "outline", attr: "val" }) },
            { tag: "shadow", prop: "shadow", xform: new boolean_xform_1.BooleanXform({ tag: "shadow", attr: "val" }) },
            { tag: "sz", prop: "size", xform: new integer_xform_1.IntegerXform({ tag: "sz", attr: "val" }) },
            { tag: "color", prop: "color", xform: new color_xform_1.ColorXform() },
            {
                tag: this.options.fontNameTag,
                prop: "name",
                xform: new string_xform_1.StringXform({ tag: this.options.fontNameTag, attr: "val" })
            },
            { tag: "family", prop: "family", xform: new integer_xform_1.IntegerXform({ tag: "family", attr: "val" }) },
            { tag: "scheme", prop: "scheme", xform: new string_xform_1.StringXform({ tag: "scheme", attr: "val" }) },
            { tag: "charset", prop: "charset", xform: new integer_xform_1.IntegerXform({ tag: "charset", attr: "val" }) },
            {
                tag: "vertAlign",
                prop: "vertAlign",
                xform: new string_xform_1.StringXform({ tag: "vertAlign", attr: "val" })
            }
        ];
        // Build map and renderOrder from single source of truth
        this.map = Object.fromEntries(fontProperties.map(p => [p.tag, { prop: p.prop, xform: p.xform }]));
        this.renderOrder = fontProperties.map(p => p.tag);
    }
    get tag() {
        return this.options.tagName;
    }
    render(xmlStream, model) {
        const { map, renderOrder } = this;
        xmlStream.openNode(this.options.tagName);
        renderOrder.forEach(tag => {
            map[tag].xform.render(xmlStream, model[map[tag].prop]);
        });
        xmlStream.closeNode();
    }
    parseOpen(node) {
        if (this.parser) {
            this.parser.parseOpen(node);
            return true;
        }
        if (this.map[node.name]) {
            this.parser = this.map[node.name].xform;
            return this.parser.parseOpen(node);
        }
        switch (node.name) {
            case this.options.tagName:
                this.model = {};
                return true;
            default:
                return false;
        }
    }
    parseText(text) {
        if (this.parser) {
            this.parser.parseText(text);
        }
    }
    parseClose(name) {
        if (this.parser && !this.parser.parseClose(name)) {
            const item = this.map[name];
            if (this.parser.model) {
                this.model[item.prop] = this.parser.model;
            }
            this.parser = undefined;
            return true;
        }
        switch (name) {
            case this.options.tagName:
                return false;
            default:
                return true;
        }
    }
}
exports.FontXform = FontXform;
FontXform.OPTIONS = {
    tagName: "font",
    fontNameTag: "name"
};
