/*
 * SDK Pullenti Lingvo, 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#.NET project. 
 * The latest version of the code is available on the site www.pullenti.ru
 */


const BlkTyps = require("./../../core/internal/BlkTyps");
const NounPhraseParseAttr = require("./../../core/NounPhraseParseAttr");
const TextToken = require("./../../TextToken");
const BlockTitleToken = require("./../../core/internal/BlockTitleToken");
const NounPhraseHelper = require("./../../core/NounPhraseHelper");
const MetaToken = require("./../../MetaToken");
const MiscHelper = require("./../../core/MiscHelper");
const PersonReferent = require("./../../person/PersonReferent");
const TitleItemTokenTypes = require("./TitleItemTokenTypes");
const TitleItemToken = require("./TitleItemToken");

class Line extends MetaToken {
    
    constructor(begin, end) {
        super(begin, end, null);
    }
    
    get charsCount() {
        let cou = 0;
        for (let t = this.beginToken; t !== null; t = t.next) {
            cou += t.lengthChar;
            if (t === this.endToken) 
                break;
        }
        return cou;
    }
    
    get isPureEn() {
        let en = 0;
        let ru = 0;
        for (let t = this.beginToken; t !== null && t.endChar <= this.endChar; t = t.next) {
            if ((t instanceof TextToken) && t.chars.isLetter) {
                if (t.chars.isCyrillicLetter) 
                    ru++;
                else if (t.chars.isLatinLetter) 
                    en++;
            }
        }
        if (en > 0 && ru === 0) 
            return true;
        return false;
    }
    
    get isPureRu() {
        let en = 0;
        let ru = 0;
        for (let t = this.beginToken; t !== null && t.endChar <= this.endChar; t = t.next) {
            if ((t instanceof TextToken) && t.chars.isLetter) {
                if (t.chars.isCyrillicLetter) 
                    ru++;
                else if (t.chars.isLatinLetter) 
                    en++;
            }
        }
        if (ru > 0 && en === 0) 
            return true;
        return false;
    }
    
    static parse(t0, maxLines, maxChars, maxEndChar) {
        let res = new Array();
        let totalChars = 0;
        for (let t = t0; t !== null; t = t.next) {
            if (maxEndChar > 0) {
                if (t.beginChar > maxEndChar) 
                    break;
            }
            if (Line.isStartOfIndex(t)) 
                break;
            let t1 = null;
            for (t1 = t; t1 !== null && t1.next !== null; t1 = t1.next) {
                if (t1.isNewlineAfter) {
                    if (t1.next === null || MiscHelper.canBeStartOfSentence(t1.next)) 
                        break;
                }
                if (t1 === t && t.isNewlineBefore && (t.getReferent() instanceof PersonReferent)) {
                    if (t1.next === null) 
                        continue;
                    if ((t1.next instanceof TextToken) && t1.next.chars.isLetter && !t1.next.chars.isAllLower) 
                        break;
                }
            }
            if (t1 === null) 
                t1 = t;
            let tit = TitleItemToken.tryAttach(t);
            if (tit !== null) {
                if (tit.typ === TitleItemTokenTypes.KEYWORDS) 
                    break;
            }
            let bl = BlockTitleToken.tryAttach(t, false, null);
            if (bl !== null) {
                if (bl.typ !== BlkTyps.UNDEFINED) 
                    break;
            }
            let l = new Line(t, t1);
            res.push(l);
            totalChars += l.charsCount;
            if (res.length >= maxLines || totalChars >= maxChars) 
                break;
            t = t1;
        }
        return res;
    }
    
    static isStartOfIndex(t) {
        if (t === null || !t.isNewlineBefore) 
            return false;
        let t1 = null;
        if (t.isValue("СОДЕРЖИМОЕ", "ВМІСТ") || t.isValue("СОДЕРЖАНИЕ", "ЗМІСТ") || t.isValue("ОГЛАВЛЕНИЕ", "ЗМІСТ")) 
            t1 = t;
        else if (t.isValue("СПИСОК", null) && t.next !== null && t.next.isValue("РАЗДЕЛ", null)) 
            t1 = t1.next;
        if (t1 === null) 
            return false;
        if (!t1.isNewlineAfter) {
            let npt = NounPhraseHelper.tryParse(t1, NounPhraseParseAttr.NO, 0, null);
            if (npt !== null && npt.morph._case.isGenitive) 
                t1 = npt.endToken.next;
        }
        if (t1 !== null && t1.isCharOf(":.;")) 
            t1 = t1.next;
        if (t1 === null || !t1.isNewlineBefore) 
            return false;
        return true;
    }
}


module.exports = Line