"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FilterColumnXform = void 0;
const base_xform_1 = require("../base-xform.js");
const list_xform_1 = require("../list-xform.js");
const custom_filter_xform_1 = require("./custom-filter-xform.js");
const filter_xform_1 = require("./filter-xform.js");
class FilterColumnXform extends base_xform_1.BaseXform {
    constructor() {
        super();
        this.map = {
            customFilters: new list_xform_1.ListXform({
                tag: "customFilters",
                count: false,
                empty: true,
                childXform: new custom_filter_xform_1.CustomFilterXform()
            }),
            filters: new list_xform_1.ListXform({
                tag: "filters",
                count: false,
                empty: true,
                childXform: new filter_xform_1.FilterXform()
            })
        };
        this.model = { filterButton: false };
    }
    get tag() {
        return "filterColumn";
    }
    prepare(model, options) {
        model.colId = options.index.toString();
    }
    render(xmlStream, model) {
        if (model.customFilters) {
            xmlStream.openNode(this.tag, {
                colId: model.colId,
                hiddenButton: model.filterButton ? "0" : "1"
            });
            this.map.customFilters.render(xmlStream, model.customFilters);
            xmlStream.closeNode();
            return;
        }
        xmlStream.leafNode(this.tag, {
            colId: model.colId,
            hiddenButton: model.filterButton ? "0" : "1"
        });
    }
    parseOpen(node) {
        if (this.parser) {
            this.parser.parseOpen(node);
            return true;
        }
        const { attributes } = node;
        switch (node.name) {
            case this.tag:
                this.model = {
                    filterButton: attributes.hiddenButton === "0"
                };
                return true;
            case "dynamicFilter":
                // Ignore dynamicFilter nodes - we don't need to preserve them for reading
                return true;
            default:
                this.parser = this.map[node.name];
                if (this.parser) {
                    this.parseOpen(node);
                    return true;
                }
                throw new Error(`Unexpected xml node in parseOpen: ${JSON.stringify(node)}`);
        }
    }
    parseText() { }
    parseClose(name) {
        if (this.parser) {
            if (!this.parser.parseClose(name)) {
                this.parser = undefined;
            }
            return true;
        }
        switch (name) {
            case this.tag:
                this.model.customFilters = this.map.customFilters.model;
                return false;
            default:
                // could be some unrecognised tags
                return true;
        }
    }
}
exports.FilterColumnXform = FilterColumnXform;
