"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DrawingXform = void 0;
const col_cache_1 = require("../../../utils/col-cache.js");
const xml_stream_1 = require("../../../utils/xml-stream.js");
const base_xform_1 = require("../base-xform.js");
const two_cell_anchor_xform_1 = require("./two-cell-anchor-xform.js");
const one_cell_anchor_xform_1 = require("./one-cell-anchor-xform.js");
function getAnchorType(model) {
    const range = typeof model.range === "string" ? col_cache_1.colCache.decode(model.range) : model.range;
    return range.br ? "xdr:twoCellAnchor" : "xdr:oneCellAnchor";
}
class DrawingXform extends base_xform_1.BaseXform {
    constructor() {
        super();
        this.map = {
            "xdr:twoCellAnchor": new two_cell_anchor_xform_1.TwoCellAnchorXform(),
            "xdr:oneCellAnchor": new one_cell_anchor_xform_1.OneCellAnchorXform()
        };
        this.model = { anchors: [] };
    }
    prepare(model) {
        model.anchors.forEach((item, index) => {
            item.anchorType = getAnchorType(item);
            const anchor = this.map[item.anchorType];
            anchor.prepare(item, { index });
        });
    }
    get tag() {
        return "xdr:wsDr";
    }
    render(xmlStream, model) {
        const renderModel = model || this.model;
        xmlStream.openXml(xml_stream_1.XmlStream.StdDocAttributes);
        xmlStream.openNode(this.tag, DrawingXform.DRAWING_ATTRIBUTES);
        renderModel.anchors.forEach(item => {
            const anchor = this.map[item.anchorType];
            anchor.render(xmlStream, item);
        });
        xmlStream.closeNode();
    }
    parseOpen(node) {
        if (this.parser) {
            this.parser.parseOpen(node);
            return true;
        }
        switch (node.name) {
            case this.tag:
                this.reset();
                this.model = {
                    anchors: []
                };
                break;
            default:
                this.parser = this.map[node.name];
                if (this.parser) {
                    this.parser.parseOpen(node);
                }
                break;
        }
        return true;
    }
    parseText(text) {
        if (this.parser) {
            this.parser.parseText(text);
        }
    }
    parseClose(name) {
        if (this.parser) {
            if (!this.parser.parseClose(name)) {
                this.model.anchors.push(this.parser.model);
                this.parser = undefined;
            }
            return true;
        }
        switch (name) {
            case this.tag:
                return false;
            default:
                // could be some unrecognised tags
                return true;
        }
    }
    reconcile(model, options) {
        model.anchors.forEach(anchor => {
            if (anchor.br) {
                this.map["xdr:twoCellAnchor"].reconcile(anchor, options);
            }
            else {
                this.map["xdr:oneCellAnchor"].reconcile(anchor, options);
            }
        });
    }
}
exports.DrawingXform = DrawingXform;
DrawingXform.DRAWING_ATTRIBUTES = {
    "xmlns:xdr": "http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing",
    "xmlns:a": "http://schemas.openxmlformats.org/drawingml/2006/main"
};
