/*
 * 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 UnitextList = require("./UnitextList");
const UnitextStyledFragmentType = require("./UnitextStyledFragmentType");
const UnitextItem = require("./UnitextItem");
const UnitextHelper = require("./internal/uni/UnitextHelper");

/**
 * Ячейка таблицы
 */
class UnitextTablecell extends UnitextItem {
    
    constructor() {
        super();
        this._colbegin = 0;
        this._colend = 0;
        this._rowbegin = 0;
        this._rowend = 0;
        this.content = null;
        this.isPsevdo = false;
        this.isHeader = false;
    }
    
    get colBegin() {
        return this._colbegin;
    }
    set colBegin(value) {
        this._colbegin = value;
        return this._colbegin;
    }
    
    get colEnd() {
        return this._colend;
    }
    set colEnd(value) {
        this._colend = value;
        return this._colend;
    }
    
    get rowBegin() {
        return this._rowbegin;
    }
    set rowBegin(value) {
        this._rowbegin = value;
        return this._rowbegin;
    }
    
    get rowEnd() {
        return this._rowend;
    }
    set rowEnd(value) {
        this._rowend = value;
        return this._rowend;
    }
    
    clone() {
        let res = new UnitextTablecell();
        res._cloneFrom(this);
        res.colBegin = this.colBegin;
        res.colEnd = this.colEnd;
        res.rowBegin = this.rowBegin;
        res.rowEnd = this.rowEnd;
        if (this.content !== null) 
            res.content = this.content.clone();
        res.isPsevdo = this.isPsevdo;
        res.isHeader = this.isHeader;
        return res;
    }
    
    get isInline() {
        return false;
    }
    
    get innerTag() {
        return "tcell";
    }
    
    findById(_id) {
        if (this.id === _id) 
            return this;
        let res = null;
        if (this.content !== null) {
            if ((((res = this.content.findById(_id)))) !== null) 
                return res;
        }
        return null;
    }
    
    getStyledFragment(textPos = -1) {
        let res = super.getStyledFragment(textPos);
        if (res === null) 
            return null;
        if (res.typ === UnitextStyledFragmentType.TABLECELL && res.children.length === 1 && res.children[0].typ === UnitextStyledFragmentType.PARAGRAPH) 
            return res.children[0];
        return res;
    }
    
    optimize(isContent, pars) {
        if (this.content !== null) 
            this.content = this.content.optimize(true, pars);
        if (this.content !== null && this.content.isWhitespaces) 
            this.content = null;
        let li = Utils.as(this.content, UnitextList);
        if ((li !== null && li.items.length === 1 && li.items[0].content === null) && li.items[0].prefix !== null) 
            this.content = li.items[0].prefix;
        return this;
    }
    
    toString() {
        return ("Cell" + (this.isPsevdo ? "?" : "") + " " + this.colBegin + (this.colBegin < this.colEnd ? ("-" + this.colEnd) : "") + " " + (this.rowBegin < this.rowEnd ? (" (rows " + this.rowBegin + "-" + this.rowEnd + ")") : "") + " " + (this.content === null ? "" : this.content.toString()));
    }
    
    getPlaintext(res, pars) {
        let nullPars = pars === null;
        let pos0 = res.length;
        if (pars !== null && pars.setPositions) 
            this.beginChar = res.length;
        if (pars === null) 
            pars = UnitextItem.m_DefParams;
        if (this.content !== null) {
            let i0 = res.length;
            this.content.getPlaintext(res, pars);
            for (let i = res.length - 1; i >= i0; i--) {
                if ((Utils.isWhitespace(res.charAt(i)) || res.charAt(i) === (String.fromCharCode(7)) || res.charAt(i) === '\t') || res.charAt(i) === '\f') 
                    res.length = i;
                else 
                    break;
            }
        }
        if (Utils.isNullOrEmpty(pars.tableCellEnd)) 
            res.append((pars.newLine != null ? pars.newLine : ""));
        else {
            if (this.colBegin < this.colEnd) {
                for (let i = 0; i < ((this.colEnd - this.colBegin)); i++) {
                    res.append('\t');
                }
            }
            if (this.rowBegin < this.rowEnd) {
                for (let i = 0; i < ((this.rowEnd - this.rowBegin)); i++) {
                    res.append('\f');
                }
            }
            if (pars === null) 
                res.append(String.fromCharCode(7));
            else 
                res.append((pars.tableCellEnd != null ? pars.tableCellEnd : ""));
        }
        if (pars !== null && pars.setPositions) 
            this.endChar = res.length - 1;
        if (this.ignorable && ((nullPars || !pars.keepIgnored))) {
            for (let i = pos0; i < res.length; i++) {
                res.setCharAt(i, ' ');
            }
        }
    }
    
    getHtml(res, par) {
        if (!par.callBefore(this, res)) 
            return;
        res.append("<").append((this.isHeader ? "th" : "td"));
        if (this.rowEnd > this.rowBegin) 
            res.append(" rowspan=\"").append(((this.rowEnd - this.rowBegin) + 1)).append("\"");
        if (this.colEnd > this.colBegin) 
            res.append(" colspan=\"").append(((this.colEnd - this.colBegin) + 1)).append("\"");
        let hasValign = false;
        if (this.id !== null) {
            res.append(" id=\"").append(this.id).append("\"");
            if (par !== null && par.styles.containsKey(this.id)) {
                let sty = par.styles.get(this.id);
                if (sty.includes("vartical-align")) 
                    hasValign = true;
                res.append(" style=\"").append(sty).append("\"");
            }
            else if (par !== null && par.outStyles && this.getStyledFragment(-1) !== null) {
                let fr = this.getStyledFragment(-1);
                if ((fr !== null && ((fr.typ === UnitextStyledFragmentType.TABLECELL || fr.typ === UnitextStyledFragmentType.PARAGRAPH)) && fr.styleId > 0) && fr.style !== null) {
                    if (fr.style.attrs.containsKey("vertical-align")) 
                        hasValign = true;
                    res.append(" style=\"");
                    fr.style.getHtml(res);
                    if (fr.parent !== null && fr.parent.typ === UnitextStyledFragmentType.TABLECELL && fr.parent.style !== null) 
                        fr.parent.style.getHtml(res);
                    res.append("\"");
                }
            }
        }
        if (!hasValign) 
            res.append(" valign=\"top\"");
        res.append(">");
        if (this.content === null) 
            res.append("&nbsp;");
        else 
            this.content.getHtml(res, par);
        res.append((this.isHeader ? "</th>" : "</td>"));
        par.callAfter(this, res);
    }
    
    getXml(xml) {
        xml.writeStartElement("cell");
        this._writeXmlAttrs(xml);
        if (this.isPsevdo) 
            xml.writeAttributeString("psevdo", "true");
        if (this.isHeader) 
            xml.writeAttributeString("header", "true");
        xml.writeAttributeString("row", this.rowBegin.toString());
        if (this.rowEnd > this.rowBegin) 
            xml.writeAttributeString("rowspan", ((this.rowEnd - this.rowBegin) + 1).toString());
        xml.writeAttributeString("col", this.colBegin.toString());
        if (this.colEnd > this.colBegin) 
            xml.writeAttributeString("colspan", ((this.colEnd - this.colBegin) + 1).toString());
        if (this.content !== null) 
            this.content.getXml(xml);
        xml.writeEndElement();
    }
    
    fromXml(xml) {
        super.fromXml(xml);
        if (xml.attributes !== null) {
            for (const a of xml.attributes) {
                if (a.local_name === "row") 
                    this.rowBegin = this.rowEnd = Utils.parseInt(a.value);
                else if (a.local_name === "col") 
                    this.colBegin = this.colEnd = Utils.parseInt(a.value);
                else if (a.local_name === "rowspan") 
                    this.rowEnd = (this.rowBegin + Utils.parseInt(a.value)) - 1;
                else if (a.local_name === "colspan") 
                    this.colEnd = (this.colBegin + Utils.parseInt(a.value)) - 1;
                else if (a.local_name === "psevdo") 
                    this.isPsevdo = true;
                else if (a.local_name === "header") 
                    this.isHeader = true;
            }
        }
        for (const x of xml.child_nodes) {
            this.content = UnitextHelper.createItem(x, this);
            if (this.content !== null) 
                break;
        }
    }
    
    getAllItems(res, lev) {
        if (res !== null) 
            res.push(this);
        if (this.content !== null) {
            this.content.parent = this;
            this.content.getAllItems(res, lev + 1);
        }
    }
    
    addPlainTextPos(d) {
        super.addPlainTextPos(d);
        if (this.content !== null) 
            this.content.addPlainTextPos(d);
    }
    
    correct(typ, data) {
        if (this.content !== null) 
            this.content.correct(typ, data);
    }
    
    replaceChild(old, ne) {
        if (this.content !== old) 
            return false;
        this.content = ne;
        ne.parent = this;
        return true;
    }
    
    setDefaultTextPos(cp, res) {
        this.beginChar = cp.value;
        if (this.content !== null) {
            this.content.setDefaultTextPos(cp, res);
            this.content.parent = this;
        }
        this.endChar = cp.value;
        cp.value++;
        if (res !== null) 
            res.append((String.fromCharCode(7)));
    }
    
    static _new83(_arg1, _arg2, _arg3, _arg4, _arg5) {
        let res = new UnitextTablecell();
        res.colBegin = _arg1;
        res.colEnd = _arg2;
        res.rowBegin = _arg3;
        res.rowEnd = _arg4;
        res.tag = _arg5;
        return res;
    }
    
    static _new636(_arg1, _arg2, _arg3, _arg4, _arg5) {
        let res = new UnitextTablecell();
        res.isPsevdo = _arg1;
        res.colBegin = _arg2;
        res.colEnd = _arg3;
        res.rowBegin = _arg4;
        res.rowEnd = _arg5;
        return res;
    }
    
    static _new640(_arg1, _arg2, _arg3, _arg4, _arg5) {
        let res = new UnitextTablecell();
        res.content = _arg1;
        res.colBegin = _arg2;
        res.colEnd = _arg3;
        res.rowBegin = _arg4;
        res.rowEnd = _arg5;
        return res;
    }
}


module.exports = UnitextTablecell