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

const UnitextItem = require("./../../UnitextItem");
const UnitextPlaintext = require("./../../UnitextPlaintext");
const UnitextTable = require("./../../UnitextTable");
const CorrLine = require("./CorrLine");

class CorrTable {
    
    constructor() {
        this.beginInd = 0;
        this.endInd = 0;
        this.result = null;
    }
    
    static tryParse(lines, i, nexti) {
        nexti.value = i;
        if (i >= lines.length) 
            return null;
        if (!lines[i].canBeEmptyLineOfTable) 
            return null;
        let width = lines[i].text.length;
        let j = 0;
        for (j = i + 1; j < lines.length; j++) {
            if (!lines[j].isPureText) 
                break;
            else if (lines[j].length !== lines[i].length || lines[j].text.length !== width) 
                break;
        }
        let height = j - i;
        nexti.value = j - 1;
        if ((width < 30) || (height < 4)) 
            return null;
        let verts = new Array();
        for (j = 0; j < width; j++) {
            let r = 0;
            for (r = 0; r < height; r++) {
                if (lines[i + r].canHasHorLineOfTable) 
                    continue;
                if (j >= lines[i + r].text.length) 
                    continue;
                let ch = lines[i + r].text[j];
                if (CorrLine.isTableChar(ch) && !CorrLine.isHiphen(ch)) {
                    verts.push(j);
                    break;
                }
            }
        }
        if (verts.length < 3) 
            return null;
        let colBegs = new Array();
        let colEnds = new Array();
        for (j = 0; j < verts.length; j++) {
            let b = 0;
            if (j > 0) 
                b = verts[j - 1] + 1;
            if ((verts[j] - 1) >= b) {
                colBegs.push(b);
                colEnds.push(verts[j] - 1);
            }
        }
        if (colBegs.length < 2) 
            return null;
        let rnums = new Array();
        let rnum = 0;
        for (j = 0; j < height; j++) {
            rnums.push(rnum);
            let w = 0;
            for (w = 0; w < width; w++) {
                if (!verts.includes(w)) {
                    if (w >= lines[i + j].text.length) 
                        continue;
                    let ch = lines[i + j].text[w];
                    if (CorrLine.isTableChar(ch)) {
                        if (CorrLine.isHiphen(ch)) {
                            if (w > 0 && lines[i + j].text[w - 1] === ch) {
                            }
                            else if (((w + 1) < width) && lines[i + j].text[w + 1] === ch) {
                            }
                            else 
                                continue;
                        }
                        rnum++;
                        break;
                    }
                }
            }
        }
        if (rnum < 3) 
            return null;
        let tab = CorrTable._new345(i, (i + height) - 1);
        tab.result = new UnitextTable();
        let tmp = new StringBuilder();
        for (let h = 0; h < rnums.length; h++) {
            for (let c = 0; c < colBegs.length; c++) {
                let w0 = colBegs[c];
                let w1 = colEnds[c];
                let h0 = h;
                let ww = 0;
                for (; h0 < rnums.length; h0++) {
                    for (ww = w0; ww <= w1; ww++) {
                        let ch = lines[i + h0].text[ww];
                        if (!CorrLine.isTableChar(ch)) 
                            break;
                    }
                    if (ww < w1) 
                        break;
                }
                if (h0 >= rnums.length) 
                    continue;
                let cel0 = tab.result.getCell(rnums[h0], c);
                if (cel0 !== null) 
                    continue;
                let cc = 0;
                for (cc = c + 1; cc < colBegs.length; cc++) {
                    for (ww = colEnds[cc - 1]; ww <= colEnds[cc]; ww++) {
                        let ch = lines[i + h0].text[ww];
                        if (CorrLine.isTableChar(ch) && !CorrLine.isHiphen(ch)) 
                            break;
                    }
                    if (ww < colEnds[cc]) 
                        break;
                    w1 = colEnds[cc];
                }
                cc--;
                let h1 = 0;
                for (h1 = h0 + 1; h1 < rnums.length; h1++) {
                    for (ww = w0; ww <= w1; ww++) {
                        let ch = lines[i + h1].text[ww];
                        if (CorrLine.isTableChar(ch)) {
                            if (CorrLine.isHiphen(ch)) {
                                if (ww > 0 && lines[i + h1].text[ww - 1] === ch) 
                                    break;
                                if (((ww + 1) < width) && lines[i + h1].text[ww + 1] === ch) 
                                    break;
                            }
                            else 
                                break;
                        }
                    }
                    if (ww < w1) 
                        break;
                }
                h1--;
                tmp.length = 0;
                for (let hh = h0; hh <= h1; hh++) {
                    for (ww = w0 - 1; ww <= w1; ww++) {
                        if (ww < 0) 
                            continue;
                        let ch = lines[i + hh].text[ww];
                        if (CorrLine.isTableChar(ch)) {
                            if (!CorrLine.isHiphen(ch)) 
                                continue;
                        }
                        if (Utils.isWhitespace(ch)) {
                            if (tmp.length > 0 && tmp.charAt(tmp.length - 1) === ' ') 
                                continue;
                            ch = ' ';
                        }
                        tmp.append(ch);
                    }
                    if ((hh < h1) && tmp.length > 0) 
                        tmp.append(' ');
                    for (let jj = tmp.length - 1; jj >= 0; jj--) {
                        if (tmp.charAt(jj) !== ' ') {
                            if (CorrLine.isHiphen(tmp.charAt(jj))) 
                                tmp.length = jj;
                            break;
                        }
                    }
                }
                let cel = tab.result.addCell(rnums[h0], rnums[h1], c, cc, UnitextPlaintext._new57(tmp.toString().trim()), false);
                c = cc;
            }
        }
        tab.result.optimize(false, null);
        return tab;
    }
    
    static _new345(_arg1, _arg2) {
        let res = new CorrTable();
        res.beginInd = _arg1;
        res.endInd = _arg2;
        return res;
    }
}


module.exports = CorrTable