/*
 * 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");

class StructExpressLine {
    
    constructor() {
        this.beginChar = 0;
        this.endChar = 0;
        this.text = null;
        this.keyword = null;
        this.number = null;
        this.isStartOfApp = false;
        this.illegalChars = 0;
    }
    
    toString() {
        let res = new StringBuilder();
        if (this.isStartOfApp) 
            res.append("StartOfApp ");
        if (this.keyword !== null) 
            res.append("[").append(this.keyword).append("] ");
        if (this.number !== null) 
            res.append("N").append(this.number).append(" ");
        if (this.illegalChars > 0) 
            res.append("(illegals=").append(this.illegalChars).append(") ");
        let len = (this.endChar + 1) - this.beginChar;
        if (len > 100) 
            len = 100;
        res.append("\"").append(this.text.substring(this.beginChar, this.beginChar + len)).append("\"");
        return res.toString();
    }
    
    static tryParse(txt, i) {
        for (; i.value < txt.length; i.value++) {
            if (txt[i.value] !== '\n' && txt[i.value] !== '\r') 
                break;
        }
        if (i.value >= txt.length) 
            return null;
        let res = new StructExpressLine();
        res.beginChar = (res.endChar = i.value);
        res.text = txt;
        for (; i.value < txt.length; i.value++) {
            if (txt[i.value] === '\n' || txt[i.value] === '\r') 
                break;
            else 
                res.endChar = i.value;
        }
        res._analyze();
        return res;
    }
    
    _analyze() {
        let i = 0;
        for (i = this.beginChar; i <= this.endChar; i++) {
            if (!Utils.isWhitespace(this.text[i])) 
                break;
        }
        if ((i < this.endChar) && Utils.isUpperCase(this.text[i])) {
            for (const kw of StructExpressLine.m_Keywords) {
                if (StructExpressLine._checkWord(this.text, i, kw)) {
                    this.keyword = kw;
                    i += kw.length;
                    break;
                }
            }
        }
        for (; i <= this.endChar; i++) {
            if (!Utils.isWhitespace(this.text[i])) 
                break;
        }
        if (i <= this.endChar) {
            if (this.text[i] === 'N' || this.text[i] === '№') {
                for (i = i + 1; i <= this.endChar; i++) {
                    if (!Utils.isWhitespace(this.text[i])) 
                        break;
                }
            }
        }
        if (i <= this.endChar) {
            let j = 0;
            for (j = i; j <= this.endChar; j++) {
                let ch = this.text[j];
                if (Utils.isDigit(ch)) 
                    continue;
                if (ch === ')' || ch === '.') 
                    continue;
                if (Utils.isLetter(ch)) {
                    if (j > this.beginChar && Utils.isDigit(this.text[j - 1])) 
                        continue;
                    if ((j + 1) <= this.endChar && this.text[j + 1] === ')') 
                        continue;
                }
                break;
            }
            if (j > i) {
                this.number = this.text.substring(i, i + j - i);
                i = j;
            }
        }
        if (this.keyword === "ПРИЛОЖЕНИЕ" || this.keyword === "ДОДАТОК") {
            if (i > this.endChar) 
                this.isStartOfApp = true;
        }
        this.illegalChars = 0;
        for (; i <= this.endChar; i++) {
            let ch = this.text[i];
            if (Utils.isWhitespace(ch) || Utils.isLetterOrDigit(ch)) 
                continue;
            if (ch === '_' || ch === '|') {
                this.illegalChars++;
                continue;
            }
            if ((ch.charCodeAt(0)) <= 0x80 || ch === '№') 
                continue;
            this.illegalChars++;
        }
    }
    
    static _checkWord(txt, i, sub) {
        let j = 0;
        for (j = 0; (j < sub.length) && ((i + j) < txt.length); j++) {
            if (txt[i + j].toUpperCase() !== sub[j].toUpperCase()) 
                return false;
        }
        if (j >= sub.length) 
            return true;
        return false;
    }
    
    static static_constructor() {
        StructExpressLine.m_Keywords = ["СТАТЬЯ", "СТАТТЯ", "ПРИЛОЖЕНИЕ", "ДОДАТОК"];
    }
}


StructExpressLine.static_constructor();

module.exports = StructExpressLine