"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConditionalFormattingsXform = void 0;
const base_xform_1 = require("../../base-xform.js");
const conditional_formatting_xform_1 = require("./conditional-formatting-xform.js");
class ConditionalFormattingsXform extends base_xform_1.BaseXform {
    constructor() {
        super();
        this.cfXform = new conditional_formatting_xform_1.ConditionalFormattingXform();
    }
    get tag() {
        return "conditionalFormatting";
    }
    reset() {
        this.model = [];
    }
    prepare(model, options) {
        // ensure each rule has a priority value
        let nextPriority = model.reduce((p, cf) => Math.max(p, ...cf.rules.map((rule) => rule.priority || 0)), 1);
        model.forEach((cf) => {
            cf.rules.forEach((rule) => {
                if (!rule.priority) {
                    rule.priority = nextPriority++;
                }
                if (rule.style) {
                    rule.dxfId = options.styles.addDxfStyle(rule.style);
                }
                // Ensure dataBar rules have required cfvo and color properties
                if (rule.type === "dataBar") {
                    if (!rule.cfvo || rule.cfvo.length < 2) {
                        rule.cfvo = [{ type: "min" }, { type: "max" }];
                    }
                    if (!rule.color) {
                        // Default blue color for data bars (same as Excel's default)
                        rule.color = { argb: "FF638EC6" };
                    }
                }
            });
        });
    }
    render(xmlStream, model) {
        model.forEach((cf) => {
            this.cfXform.render(xmlStream, cf);
        });
    }
    parseOpen(node) {
        if (this.parser) {
            this.parser.parseOpen(node);
            return true;
        }
        switch (node.name) {
            case "conditionalFormatting":
                this.parser = this.cfXform;
                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)) {
                this.model.push(this.parser.model);
                this.parser = undefined;
                return false;
            }
            return true;
        }
        return false;
    }
    reconcile(model, options) {
        model.forEach((cf) => {
            cf.rules.forEach((rule) => {
                if (rule.dxfId !== undefined) {
                    rule.style = options.styles.getDxfStyle(rule.dxfId);
                    delete rule.dxfId;
                }
            });
        });
    }
}
exports.ConditionalFormattingsXform = ConditionalFormattingsXform;
