/*
 * 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 RefOutArgWrapper = require("./../../../unisharp/RefOutArgWrapper");

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

class RichTextRow extends RichTextItem {
    
    constructor() {
        super();
        this.endOf = false;
        this.lastRow = false;
        this.cellsInfo = new Array();
    }
    
    _addCmd(cmd) {
        if (cmd === null) 
            return false;
        if (cmd === "lastrow") {
            this.lastRow = true;
            return true;
        }
        let last = (this.cellsInfo.length === 0 ? null : this.cellsInfo[this.cellsInfo.length - 1]);
        if (cmd.startsWith("cellx")) {
            let idd = 0;
            let wrapidd248 = new RefOutArgWrapper();
            let inoutres249 = Utils.tryParseInt(cmd.substring(5), wrapidd248);
            idd = wrapidd248.value;
            if (inoutres249) {
                if (idd > 0) {
                    for (const ii of this.cellsInfo) {
                        if (ii.id === idd) {
                            if (last.id === 0) {
                                if (last.horMerge) 
                                    ii.horMerge = true;
                                if (last.vertMergeFirst) 
                                    ii.vertMergeFirst = true;
                                if (last.vertMergeNext) 
                                    ii.vertMergeNext = true;
                                Utils.removeItem(this.cellsInfo, last);
                            }
                            return true;
                        }
                    }
                    if (last !== null && last.id === 0) 
                        last.id = idd;
                    else 
                        this.cellsInfo.push(RichTextRow.CellInfo._new247(idd));
                }
            }
            return true;
        }
        if (cmd.startsWith("clwWidth")) {
            let wi = 0;
            let wrapwi250 = new RefOutArgWrapper();
            let inoutres251 = Utils.tryParseInt(cmd.substring(8), wrapwi250);
            wi = wrapwi250.value;
            if (inoutres251) {
                if (last === null || last.id > 0) 
                    this.cellsInfo.push((last = new RichTextRow.CellInfo()));
                last.width = wi;
                return true;
            }
        }
        if (cmd === "clmgf") {
            if (last === null || last.id > 0) 
                this.cellsInfo.push((last = new RichTextRow.CellInfo()));
            last.horMerge = true;
            return true;
        }
        if (cmd === "clvmgf") {
            if (last === null || last.id > 0) 
                this.cellsInfo.push((last = new RichTextRow.CellInfo()));
            last.vertMergeFirst = true;
            return true;
        }
        if (cmd === "clvmrg") {
            if (last === null || last.id > 0) 
                this.cellsInfo.push((last = new RichTextRow.CellInfo()));
            last.vertMergeNext = true;
            return true;
        }
        return false;
    }
    
    toString() {
        return ((this.lastRow ? "LastRow " : "") + (this.endOf ? "HasEnd " : "") + super.toString());
    }
}


RichTextRow.CellInfo = class  {
    
    constructor() {
        this.id = 0;
        this.horMerge = false;
        this.vertMergeFirst = false;
        this.vertMergeNext = false;
        this.width = 0;
    }
    
    toString() {
        return (String(this.id) + " " + (this.horMerge ? "Merge " : "") + (this.vertMergeFirst ? "VertMergeFirst " : "") + (this.vertMergeNext ? "VertMergeNext " : ""));
    }
    
    static _new247(_arg1) {
        let res = new RichTextRow.CellInfo();
        res.id = _arg1;
        return res;
    }
}


module.exports = RichTextRow