/*
 * 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 UnitextPlaintext = require("./../../UnitextPlaintext");
const UnitextFootnote = require("./../../UnitextFootnote");
const UnitextPagebreak = require("./../../UnitextPagebreak");
const UnitextNewline = require("./../../UnitextNewline");

class CorrLine {
    
    constructor() {
        this.length = 0;
        this.isPureText = false;
        this.firstInd = 0;
        this.lastInd = 0;
        this.m_Text = null;
        this.newLines = 0;
        this.pageBreakAfter = false;
        this.merge = false;
        this.cantFollowAny = false;
        this.m_CanBeEmptyLineOfTable = -1;
        this.m_CanHasHorLineOfTable = -1;
    }
    
    get text() {
        return this.m_Text;
    }
    set text(value) {
        this.m_Text = Utils.trimEndString(value);
        this.length = value.length;
        this.m_CanBeEmptyLineOfTable = -1;
        this.m_CanHasHorLineOfTable = -1;
        return value;
    }
    
    toString() {
        let res = new StringBuilder();
        if (this.merge) 
            res.append("<-Merge ");
        res.append("[").append(this.length).append((this.isPureText ? " txt" : "")).append("]");
        if (this.pageBreakAfter) 
            res.append(" PageBreak");
        else 
            res.append(" ").append(this.newLines).append("nls");
        if (this.text.length < 100) 
            res.append(" '").append(this.text).append("'");
        else 
            res.append(" '").append(this.text.substring(0, 0 + 100)).append("...");
        return res.toString();
    }
    
    static parseList(cnt) {
        let res = new Array();
        let line = null;
        let tmp = new StringBuilder();
        for (let i = 0; i < cnt.children.length; i++) {
            let ch = cnt.children[i];
            if (ch instanceof UnitextPagebreak) {
                if (line !== null) 
                    line.pageBreakAfter = true;
                continue;
            }
            if (ch instanceof UnitextNewline) {
                if (line !== null) {
                    if (ch.tag !== null) 
                        line.cantFollowAny = true;
                    line.newLines += ch.count;
                    continue;
                }
            }
            if (line === null || line.newLines > 0 || line.pageBreakAfter) {
                if (line !== null) {
                    line.text = tmp.toString();
                    tmp.length = 0;
                }
                line = null;
            }
            if (line === null) {
                line = CorrLine._new336(true);
                tmp.length = 0;
                res.push(line);
                line.firstInd = i;
            }
            line.lastInd = i;
            if (ch instanceof UnitextPlaintext) {
                tmp.append(ch.text);
                continue;
            }
            if (ch instanceof UnitextFootnote) {
            }
            else 
                line.isPureText = false;
        }
        if (line !== null) 
            line.text = tmp.toString();
        return res;
    }
    
    static isHiphen(ch) {
        if ((ch === '-' || ch === '–' || ch === '¬') || ch === '-') 
            return true;
        if (ch === (String.fromCharCode(0x00AD))) 
            return true;
        if ((ch === '-' || ch === '—' || ch === '–') || ch === '−' || ch === '-') 
            return true;
        return false;
    }
    
    static isTableChar(ch) {
        if (CorrLine.isHiphen(ch)) 
            return true;
        if (ch === '|' || ch === '_') 
            return true;
        let cod = ch.charCodeAt(0);
        if (cod >= 0x2500 && cod <= 0x2595) 
            return true;
        return false;
    }
    
    canFollowed(li) {
        if (!li.isPureText || !this.isPureText) 
            return false;
        if (this.cantFollowAny) 
            return false;
        if (this.newLines > 2) 
            return false;
        if (Utils.isNullOrEmpty(this.text) || Utils.isNullOrEmpty(li.text)) 
            return false;
        let lastCh = this.text[this.text.length - 1];
        let firstCh = String.fromCharCode(0);
        let wsBefore = 0;
        for (const ch of li.text) {
            if (!Utils.isWhitespace(ch)) {
                firstCh = ch;
                break;
            }
            else 
                wsBefore++;
        }
        if ((firstCh.charCodeAt(0)) === 0) 
            return false;
        if (wsBefore > 10) {
            let wsb = 0;
            for (const ch of this.text) {
                if (!Utils.isWhitespace(ch)) 
                    break;
                else 
                    wsb++;
            }
            if (wsb < (Math.floor((0.8 * (wsBefore))))) 
                return false;
        }
        if (lastCh === '.' || lastCh === ';' || lastCh === ':') {
            if (this.newLines > 1 || this.pageBreakAfter) 
                return false;
            if (!Utils.isLetter(firstCh)) 
                return false;
            if (!Utils.isLowerCase(firstCh)) 
                return false;
            if (lastCh !== '.') 
                return false;
            return true;
        }
        if (Utils.isLetter(firstCh)) {
            if (Utils.isLowerCase(firstCh)) 
                return true;
        }
        if (this.newLines > 1 || this.pageBreakAfter) 
            return false;
        if (CorrLine.isHiphen(lastCh) || lastCh === ',') {
            if (!Utils.isLetterOrDigit(firstCh)) 
                return false;
            return true;
        }
        if (Utils.isLetter(lastCh) && Utils.isLowerCase(lastCh)) {
            let wsBefore0 = 0;
            for (const ch of this.text) {
                if (!Utils.isWhitespace(ch)) 
                    break;
                else 
                    wsBefore0++;
            }
            if (wsBefore === wsBefore0) {
                if (li.text[li.text.length - 1] === '.') {
                    if (Utils.isLowerCase(firstCh) && Utils.isLowerCase(firstCh)) 
                        return true;
                }
            }
        }
        return false;
    }
    
    get canBeEmptyLineOfTable() {
        if (!this.isPureText) 
            return false;
        if (this.m_CanBeEmptyLineOfTable >= 0) 
            return this.m_CanBeEmptyLineOfTable > 0;
        let cou = 0;
        for (const ch of this.text) {
            if (CorrLine.isTableChar(ch)) 
                cou++;
            else if (!Utils.isWhitespace(ch)) {
                this.m_CanBeEmptyLineOfTable = 0;
                return false;
            }
        }
        this.m_CanBeEmptyLineOfTable = (cou > 1 ? 1 : 0);
        return this.m_CanBeEmptyLineOfTable > 0;
    }
    
    get canHasHorLineOfTable() {
        if (!this.isPureText) 
            return false;
        if (this.m_CanHasHorLineOfTable >= 0) 
            return this.m_CanHasHorLineOfTable > 0;
        for (let i = 0; i < (this.text.length - 2); i++) {
            if (CorrLine.isTableChar(this.text[i]) && this.text[i + 1] === this.text[i] && this.text[i + 2] === this.text[i]) {
                this.m_CanHasHorLineOfTable = 1;
                return true;
            }
        }
        this.m_CanHasHorLineOfTable = 0;
        return false;
    }
    
    static _new336(_arg1) {
        let res = new CorrLine();
        res.isPureText = _arg1;
        return res;
    }
}


module.exports = CorrLine