"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StyleXform = void 0;
const base_xform_1 = require("../base-xform.js");
const alignment_xform_1 = require("./alignment-xform.js");
const protection_xform_1 = require("./protection-xform.js");
// Style assists translation from style model to/from xlsx
class StyleXform extends base_xform_1.BaseXform {
    constructor(options) {
        super();
        this.xfId = !!(options && options.xfId);
        this.map = {
            alignment: new alignment_xform_1.AlignmentXform(),
            protection: new protection_xform_1.ProtectionXform()
        };
    }
    get tag() {
        return "xf";
    }
    render(xmlStream, model) {
        xmlStream.openNode("xf", {
            numFmtId: model.numFmtId || 0,
            fontId: model.fontId || 0,
            fillId: model.fillId || 0,
            borderId: model.borderId || 0
        });
        if (this.xfId) {
            xmlStream.addAttribute("xfId", model.xfId || 0);
        }
        if (model.applyNumberFormat || model.numFmtId) {
            xmlStream.addAttribute("applyNumberFormat", "1");
        }
        if (model.applyFont || model.fontId) {
            xmlStream.addAttribute("applyFont", "1");
        }
        if (model.applyFill || model.fillId) {
            xmlStream.addAttribute("applyFill", "1");
        }
        if (model.applyBorder || model.borderId) {
            xmlStream.addAttribute("applyBorder", "1");
        }
        if (model.applyAlignment || model.alignment) {
            xmlStream.addAttribute("applyAlignment", "1");
        }
        if (model.applyProtection || model.protection) {
            xmlStream.addAttribute("applyProtection", "1");
        }
        if (model.pivotButton) {
            xmlStream.addAttribute("pivotButton", "1");
        }
        /**
         * Rendering tags causes close of XML stream.
         * Therefore adding attributes must be done before rendering tags.
         */
        if (model.alignment) {
            this.map.alignment.render(xmlStream, model.alignment);
        }
        if (model.protection) {
            this.map.protection.render(xmlStream, model.protection);
        }
        // Add checkbox extLst if needed
        if (model.checkbox && model.xfComplementIndex !== undefined) {
            xmlStream.openNode("extLst");
            xmlStream.openNode("ext", {
                "xmlns:xfpb": "http://schemas.microsoft.com/office/spreadsheetml/2022/featurepropertybag",
                uri: "{C7286773-470A-42A8-94C5-96B5CB345126}"
            });
            xmlStream.leafNode("xfpb:xfComplement", { i: model.xfComplementIndex });
            xmlStream.closeNode();
            xmlStream.closeNode();
        }
        xmlStream.closeNode();
    }
    parseOpen(node) {
        if (this.parser) {
            this.parser.parseOpen(node);
            return true;
        }
        // used during sax parsing of xml to build font object
        switch (node.name) {
            case "xf":
                this.model = {
                    numFmtId: parseInt(node.attributes.numFmtId, 10),
                    fontId: parseInt(node.attributes.fontId, 10),
                    fillId: parseInt(node.attributes.fillId, 10),
                    borderId: parseInt(node.attributes.borderId, 10)
                };
                if (this.xfId) {
                    this.model.xfId = parseInt(node.attributes.xfId, 10);
                }
                if (node.attributes.pivotButton === "1") {
                    this.model.pivotButton = true;
                }
                // Preserve apply* flags from original file
                const applyFlags = [
                    "applyNumberFormat",
                    "applyFont",
                    "applyFill",
                    "applyBorder",
                    "applyAlignment",
                    "applyProtection"
                ];
                for (const flag of applyFlags) {
                    if (node.attributes[flag] === "1") {
                        this.model[flag] = true;
                    }
                }
                return true;
            case "alignment":
                this.parser = this.map.alignment;
                this.parser.parseOpen(node);
                return true;
            case "protection":
                this.parser = this.map.protection;
                this.parser.parseOpen(node);
                return true;
            default:
                return false;
        }
    }
    parseText(text) {
        if (this.parser) {
            this.parser.parseText(text);
        }
    }
    parseClose(name) {
        if (this.parser) {
            if (!this.parser.parseClose(name)) {
                if (this.map.protection === this.parser) {
                    this.model.protection = this.parser.model;
                }
                else {
                    this.model.alignment = this.parser.model;
                }
                this.parser = undefined;
            }
            return true;
        }
        return name !== "xf";
    }
}
exports.StyleXform = StyleXform;
