/*
 * 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 TextToken = require("./../../TextToken");
const NumberToken = require("./../../NumberToken");
const BracketParseAttr = require("./../../core/BracketParseAttr");
const InstrumentKind = require("./../InstrumentKind");
const BracketHelper = require("./../../core/BracketHelper");

class ContractHelper {
    
    static correctDummyNewlines(fr) {
        let i = 0;
        for (i = 0; i < fr.children.length; i++) {
            let ch = fr.children[i];
            if ((ch.kind === InstrumentKind.KEYWORD || ch.kind === InstrumentKind.NUMBER || ch.kind === InstrumentKind.NAME) || ch.kind === InstrumentKind.EDITIONS || ch.kind === InstrumentKind.COMMENT) {
            }
            else 
                break;
        }
        if ((i < fr.children.length) && fr.children[i].kind === InstrumentKind.INDENTION) {
            let j = 0;
            for (j = i + 1; j < fr.children.length; j++) {
                if (fr.children[j].kind !== InstrumentKind.INDENTION) 
                    break;
                else if (ContractHelper._calcNewlineBetweenCoef(fr.children[j - 1], fr.children[j]) > 0) 
                    break;
            }
            if (j >= fr.children.length) {
                j--;
                fr.children[i].kind = InstrumentKind.CONTENT;
                fr.children[i].number = 0;
                fr.children[i].endToken = fr.children[j].endToken;
                if ((i + 1) < fr.children.length) 
                    fr.children.splice(i + 1, fr.children.length - i - 1);
                if (fr.kind === InstrumentKind.PREAMBLE && fr.children.length === 1) 
                    fr.children.splice(0, fr.children.length);
            }
            else {
                let ch = false;
                for (j = i + 1; j < fr.children.length; j++) {
                    if (fr.children[j - 1].kind === InstrumentKind.INDENTION && fr.children[j].kind === InstrumentKind.INDENTION && (ContractHelper._calcNewlineBetweenCoef(fr.children[j - 1], fr.children[j]) < 0)) {
                        fr.children[j - 1].endToken = fr.children[j].endToken;
                        fr.children.splice(j, 1);
                        j--;
                        ch = true;
                    }
                }
                if (ch) {
                    let num = 1;
                    for (j = i; j < fr.children.length; j++) {
                        if (fr.children[j].kind === InstrumentKind.INDENTION) 
                            fr.children[j].number = num++;
                    }
                }
            }
        }
        for (const ch of fr.children) {
            ContractHelper.correctDummyNewlines(ch);
        }
    }
    
    static _calcNewlineBetweenCoef(fr1, fr2) {
        if (fr1.newlinesAfterCount > 1) 
            return 1;
        for (let tt = fr1.beginToken; tt !== null && tt.endChar <= fr1.endChar; tt = tt.next) {
            if (BracketHelper.canBeStartOfSequence(tt, false, false)) {
                let br = BracketHelper.tryParse(tt, BracketParseAttr.CANBEMANYLINES, 100);
                if (br !== null && br.endChar >= fr2.beginChar) 
                    return -1;
            }
        }
        let t = fr1.endToken;
        if (t.isCharOf(":;.")) 
            return 1;
        if ((t instanceof TextToken) && ((t.morph._class.isPreposition || t.morph._class.isConjunction))) 
            return -1;
        let t1 = fr2.beginToken;
        if (t1 instanceof TextToken) {
            if (t1.chars.isAllLower) 
                return -1;
            if (BracketHelper.canBeStartOfSequence(t1, false, false)) {
                if (t.chars.isAllLower) 
                    return -1;
            }
        }
        else if (t1 instanceof NumberToken) {
            if (t.chars.isAllLower) 
                return -1;
        }
        if (t.chars.isAllLower) {
            if (fr2.endToken.isChar(';')) 
                return -1;
        }
        return 0;
    }
}


module.exports = ContractHelper