/*
 * SDK Pullenti Unitext, version 4.33, fabruary 2026. Copyright (c) 2013-2026, Pullenti. All rights reserved. 
 * Non-Commercial Freeware and Commercial Software. 
 * This class is generated using the converter UniSharping (www.unisharping.ru) from Pullenti C# project. 
 * The latest version of the code is available on the site www.pullenti.ru
 */

const Utils = require("./../../../unisharp/Utils");
const Hashtable = require("./../../../unisharp/Hashtable");

const RichTextRow = require("./RichTextRow");
const RichTextCell = require("./RichTextCell");
const RichTextItem = require("./RichTextItem");

class RichTextTable extends RichTextItem {
    
    constructor() {
        super();
        this.columnIds = new Array();
    }
    
    toString() {
        return ("Table: " + this.columnIds.length + " Columns, " + this.children.length + " Rows");
    }
    
    get lastRow() {
        if (this.children === null || this.children.length === 0) 
            return null;
        return Utils.as(this.children[this.children.length - 1], RichTextRow);
    }
    
    get lastCell() {
        let r = this.lastRow;
        if (r === null) 
            return null;
        if (r.children === null || r.children.length === 0) 
            return null;
        return Utils.as(r.children[r.children.length - 1], RichTextCell);
    }
    
    correct() {
        if (this.lastRow !== null && !this.lastRow.endOf && ((this.lastCell === null || !this.lastCell.endOf))) 
            this.children.splice(this.children.length - 1, 1);
        for (const r of this.children) {
            let rr = Utils.as(r, RichTextRow);
            if (rr === null) 
                continue;
            if (rr.children !== null && rr.children.length > 1) {
                let la = Utils.as(rr.children[rr.children.length - 1], RichTextCell);
                if (!la.endOf && la.text === null && la.children === null) 
                    rr.children.splice(rr.children.length - 1, 1);
            }
            for (const ci of rr.cellsInfo) {
                if (ci.id > 0 && !this.columnIds.includes(ci.id)) 
                    this.columnIds.push(ci.id);
            }
            if (rr.cellsInfo.length === rr.children.length) {
                for (let ii = 0; ii < rr.cellsInfo.length; ii++) {
                    rr.children[ii].columnId = rr.cellsInfo[ii].id;
                    rr.children[ii].mergeToTop = rr.cellsInfo[ii].vertMergeNext;
                    rr.children[ii].mergeBottoms = rr.cellsInfo[ii].vertMergeFirst;
                }
            }
        }
        this.columnIds.sort((a, b) => a - b);
        let vertMerge = new Hashtable();
        for (const r of this.children) {
            let rr = Utils.as(r, RichTextRow);
            if (rr === null) 
                continue;
            let cells = new Array();
            for (const c of rr.children) {
                if (c instanceof RichTextCell) 
                    cells.push(Utils.as(c, RichTextCell));
            }
            for (const cel of cells) {
                cel.lastGrid = this.columnIds.indexOf(cel.columnId);
            }
            for (let ii = 0; ii < cells.length; ii++) {
                if (ii === 0) 
                    cells[ii].fistGrid = 0;
                else if (cells[ii - 1].lastGrid >= 0) 
                    cells[ii].fistGrid = cells[ii - 1].lastGrid + 1;
                if (ii === (cells.length - 1)) 
                    cells[ii].lastGrid = this.columnIds.length - 1;
            }
            for (let ii = 0; ii < cells.length; ii++) {
                let cel = cells[ii];
                if (cel.lastGrid > cel.fistGrid && cel.fistGrid >= 0) 
                    cel.colsSpan = (cel.lastGrid - cel.fistGrid) + 1;
                if (cel.mergeBottoms && cel.lastGrid >= 0) {
                    if (!vertMerge.containsKey(cel.lastGrid)) 
                        vertMerge.put(cel.lastGrid, cel);
                    else 
                        vertMerge.put(cel.lastGrid, cel);
                }
                else if (cel.mergeToTop && vertMerge.containsKey(cel.lastGrid)) {
                    vertMerge.get(cel.lastGrid).rowsSpan++;
                    Utils.removeItem(rr.children, cel);
                }
            }
        }
    }
}


module.exports = RichTextTable