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

const InstrToken1StdTitleType = require("./InstrToken1StdTitleType");
const MetaToken = require("./../../MetaToken");
const BracketParseAttr = require("./../../core/BracketParseAttr");
const EditionHelper = require("./EditionHelper");
const NumberToken = require("./../../NumberToken");
const Referent = require("./../../Referent");
const DecreeReferent = require("./../../decree/DecreeReferent");
const BracketHelper = require("./../../core/BracketHelper");
const InstrumentKind = require("./../InstrumentKind");
const DecreePartReferent = require("./../../decree/DecreePartReferent");
const DecreeKind = require("./../../decree/DecreeKind");
const ContractHelper = require("./ContractHelper");
const MiscHelper = require("./../../core/MiscHelper");
const InstrToken1Types = require("./InstrToken1Types");
const NumberTypes = require("./NumberTypes");
const TableHelper = require("./../../core/TableHelper");
const TextToken = require("./../../TextToken");
const InstrToken1 = require("./InstrToken1");
const NumberingHelper = require("./NumberingHelper");
const FragToken = require("./FragToken");

class ContentAnalyzeWhapper {
    
    constructor() {
        this.docTyp = DecreeKind.UNDEFINED;
        this.topDoc = null;
        this.lines = null;
        this.citKind = InstrumentKind.UNDEFINED;
    }
    
    analyze(root, _topDoc, isCitat, rootKind) {
        const ListHelper = require("./ListHelper");
        this.topDoc = _topDoc;
        this.citKind = rootKind;
        let _lines = new Array();
        let footnotes = new Array();
        let curFootnote = null;
        let directives = 0;
        let parts = 0;
        if (_topDoc !== null && _topDoc.m_Doc !== null) {
            let ty = _topDoc.m_Doc.typ;
            if (ty !== null) {
                if ((ty.includes("КОДЕКС") || ty.includes("ЗАКОН") || ty.includes("КОНСТИТУЦИЯ")) || ty.includes("КОНСТИТУЦІЯ")) 
                    this.docTyp = DecreeKind.KODEX;
                else if (ty.includes("ДОГОВОР") || ty.includes("ДОГОВІР") || ty.includes("КОНТРАКТ")) 
                    this.docTyp = DecreeKind.CONTRACT;
                else if (ty.includes("ЗАДАНИЕ")) 
                    this.docTyp = DecreeKind.TZ;
            }
        }
        for (let t = root.beginToken; t !== null; t = t.next) {
            if (t.beginChar > root.endToken.endChar) 
                break;
            let dpr = Utils.as(t.getReferent(), DecreePartReferent);
            if (dpr !== null && dpr.localTyp !== null && (((dpr.chapter !== null || dpr.clause !== null || dpr.section !== null) || dpr.subSection !== null))) 
                t = t.kit.debedToken(t);
            if (_lines.length === 38) {
            }
            let lt = InstrToken1.parse(t, false, _topDoc, 0, (_lines.length > 0 ? _lines[_lines.length - 1] : null), isCitat && t === root.beginToken, root.endToken.endChar, false, false);
            if (lt !== null && lt.hasManySpecChars && lt.isPureHiphenLine) {
                if (lt.endToken.next !== null && lt.endToken.next.isChar('<')) {
                    lt = InstrToken1.parse(lt.endToken.next, false, _topDoc, 0, (_lines.length > 0 ? _lines[_lines.length - 1] : null), isCitat && t === root.beginToken, root.endToken.endChar, false, false);
                    if (lt !== null && lt.typ === InstrToken1Types.FOOTNOTE) 
                        lt.beginToken = t;
                    else {
                        t = lt.endToken;
                        continue;
                    }
                }
            }
            if ((t.isTableControlChar && this.docTyp !== DecreeKind.CONTRACT && this.docTyp !== DecreeKind.TZ) && ((lt === null || lt.typ !== InstrToken1Types.SUBSECTION))) {
                curFootnote = null;
                let rows = TableHelper.tryParseRows(t, 0, true, false);
                if (rows !== null) 
                    lt = InstrToken1._new1999(t, rows[rows.length - 1].endToken, InstrToken1Types.LINE);
            }
            if (lt === null) 
                continue;
            if (lt.typ === InstrToken1Types.APPENDIX) {
            }
            if (lt.numTyp === NumberTypes.DIGIT && lt.numbers.length === 1 && lt.numbers[0] === "30") {
            }
            if (lt.typ === InstrToken1Types.EDITIONS) {
                if ((!lt.isNewlineAfter && lt.endToken.next !== null && lt.endToken.next.isNewlineAfter) && (lt.endToken.next instanceof TextToken) && !lt.endToken.next.chars.isLetter) 
                    lt.endToken = lt.endToken.next;
            }
            if (lt.numbers.length > 0) {
            }
            if (_lines.length === 0 && rootKind !== InstrumentKind.UNDEFINED) {
                if ((rootKind === InstrumentKind.INDENTION || rootKind === InstrumentKind.ITEM || rootKind === InstrumentKind.SUBITEM) || rootKind === InstrumentKind.CLAUSEPART) 
                    lt.typ = InstrToken1Types.LINE;
                else if (rootKind === InstrumentKind.CHAPTER) 
                    lt.typ = InstrToken1Types.CHAPTER;
                else if (rootKind === InstrumentKind.CLAUSE) 
                    lt.typ = InstrToken1Types.CLAUSE;
                else if (rootKind === InstrumentKind.SECTION) 
                    lt.typ = InstrToken1Types.SECTION;
                else if (rootKind === InstrumentKind.SUBSECTION) 
                    lt.typ = InstrToken1Types.SUBSECTION;
                else if (rootKind === InstrumentKind.DOCPART) 
                    lt.typ = InstrToken1Types.DOCPART;
            }
            if (curFootnote !== null && lt.typ !== InstrToken1Types.EDITIONS) {
                if (lt.typ !== InstrToken1Types.LINE) 
                    curFootnote = null;
                else if (lt.numbers.length > 0) 
                    curFootnote = null;
                else if (lt.newlinesBeforeCount > 1) {
                    if (curFootnote.endToken.isChar(':')) {
                    }
                    else 
                        curFootnote = null;
                }
            }
            if (lt.typ === InstrToken1Types.CLAUSE && lt.firstNumber === 103) {
            }
            if (lt.endChar > root.endChar) 
                lt.endToken = root.endToken;
            if (lt.typ === InstrToken1Types.DIRECTIVE) 
                directives++;
            if (((lt.numTyp === NumberTypes.LETTER && lt.numbers.length === 1 && lt.minNumber === null) && lt.lastNumber > 1 && rootKind !== InstrumentKind.SUBITEM) && rootKind !== InstrumentKind.ITEM) {
                let ok = false;
                for (let i = _lines.length - 1; i >= 0; i--) {
                    if (_lines[i].numTyp === lt.numTyp) {
                        let j = lt.lastNumber - _lines[i].lastNumber;
                        ok = j === 1 || j === 2;
                        break;
                    }
                }
                if (!ok) {
                    lt.numTyp = NumberTypes.UNDEFINED;
                    lt.numbers.splice(0, lt.numbers.length);
                }
            }
            if (lt.typContainerRank > 0 && !lt.isNumDoubt) 
                parts++;
            if (lt.typ !== InstrToken1Types.FOOTNOTE) {
                if (curFootnote === null) 
                    _lines.push(lt);
                else {
                    if (curFootnote.subItems === null) 
                        curFootnote.subItems = new Array();
                    curFootnote.subItems.push(lt);
                }
            }
            else {
                footnotes.push(lt);
                curFootnote = lt;
            }
            t = lt.endToken;
        }
        ListHelper.correctIndex(_lines);
        ListHelper.correctAppList(_lines);
        this._correctForms(_lines);
        if (directives > 0 && directives > parts) 
            this._analizeContentWithDirectives(root, _lines, _topDoc !== null && _topDoc.m_Doc !== null && _topDoc.m_Doc.caseNumber !== null);
        else 
            this._analizeContentWithContainers(root, _lines, 0, _topDoc);
        this._analizePreamble(root);
        root._analizeTables();
        if (this.docTyp === DecreeKind.CONTRACT) {
        }
        else 
            this._correctKodexParts(root);
        this._analizeSections(root);
        this._correctNames(root, null);
        EditionHelper.analizeEditions(root);
        if (this.docTyp === DecreeKind.CONTRACT) 
            ContractHelper.correctDummyNewlines(root);
        ListHelper.analyze(root);
        if (rootKind === InstrumentKind.CLAUSEPART || rootKind === InstrumentKind.ITEM || rootKind === InstrumentKind.SUBITEM) {
            for (const ch of root.children) {
                if (ch.kind === InstrumentKind.ITEM) {
                    if (rootKind === InstrumentKind.CLAUSEPART) {
                        ch.kind = InstrumentKind.CLAUSEPART;
                        for (const chh of ch.children) {
                            if (chh.kind === InstrumentKind.SUBITEM) 
                                chh.kind = InstrumentKind.ITEM;
                        }
                    }
                    else if (rootKind === InstrumentKind.SUBITEM) 
                        ch.kind = InstrumentKind.SUBITEM;
                }
            }
        }
        this._postCorrect(root, _lines);
        for (const fn of footnotes) {
            if (!this._addFootnote(root, fn, true)) {
                let fnn = FragToken._new2000(fn.beginToken, fn.endToken, InstrumentKind.FOOTNOTE);
                NumberingHelper.createNumber(fnn, fn);
                fnn.checkExpired();
                root.children.push(fnn);
            }
        }
        for (let i = 1; i < root.children.length; i++) {
            let ch = root.children[i];
            if (ch.kind === InstrumentKind.CONTENT && ch.children.length === 0 && root.children[i - 1].endChar >= ch.endChar) {
                root.children.splice(i, 1);
                i--;
            }
        }
    }
    
    _postCorrect(root, _lines) {
        for (const ch of root.children) {
            this._postCorrect(ch, _lines);
        }
        if (root.children.length > 0) {
            if (root.endChar < root.children[root.children.length - 1].endChar) 
                root.endToken = root.children[root.children.length - 1].endToken;
            if (root.beginChar > root.children[0].beginChar) 
                root.beginToken = root.children[0].beginToken;
        }
    }
    
    _correctForms(_lines) {
        for (let i = 0; i < _lines.length; i++) {
            let line = _lines[i];
            if (line.typ !== InstrToken1Types.FORM) 
                continue;
            let j = 0;
            for (j = i + 1; j < _lines.length; ) {
                let line1 = _lines[j];
                if ((line1.typ !== InstrToken1Types.LINE && line1.typ !== InstrToken1Types.FIRSTLINE && line1.typ !== InstrToken1Types.SIGNS) && line1.typ !== InstrToken1Types.COMMENT && line1.typ !== InstrToken1Types.EDITIONS) 
                    break;
                if (line.subItems === null) 
                    line.subItems = new Array();
                line.subItems.push(line1);
                _lines.splice(j, 1);
            }
        }
    }
    
    _createFootnote(fn) {
        let fnn = FragToken._new2000(fn.beginToken, fn.endToken, InstrumentKind.FOOTNOTE);
        NumberingHelper.createNumber(fnn, fn);
        fnn.checkExpired();
        if (fn.subItems !== null && fn.subItems.length > 0) {
            if ((fn.endChar - fn.numEndToken.endChar) > 4) 
                fnn.children.push(FragToken._new2000(fn.numEndToken.next, fn.endToken, InstrumentKind.INDENTION));
            this._analizeContentWithoutContainers(fnn, fn.subItems, true, false, false);
            fnn.endToken = fn.subItems[fn.subItems.length - 1].endToken;
        }
        return fnn;
    }
    
    _addFootnote(root, fn, notLast = false) {
        if (fn.beginChar < root.beginChar) 
            return false;
        if ((((root.kind === InstrumentKind.EDITIONS || root.kind === InstrumentKind.COMMENT || root.kind === InstrumentKind.NUMBER) || root.kind === InstrumentKind.HEAD || root.kind === InstrumentKind.TAIL) || root.kind === InstrumentKind.TYP || root.kind === InstrumentKind.KEYWORD) || root.kind === InstrumentKind.FOOTNOTE) 
            return false;
        for (let i = 0; i < (root.children.length - 1); i++) {
            if (root.children[i].beginChar < fn.beginChar) {
                let ch0 = root.children[i];
                if (fn.endChar < root.children[i + 1].beginChar) {
                    if (ch0.kind === InstrumentKind.TABLE || ch0.kind === InstrumentKind.FOOTNOTE || ch0.kind === InstrumentKind.NOTICE) {
                        let fnn = this._createFootnote(fn);
                        root.children.splice(i + 1, 0, fnn);
                        return true;
                    }
                    if (this._addFootnote(ch0, fn, true)) 
                        return true;
                    return false;
                }
                let ch1 = root.children[i + 1];
                if (fn.endChar <= ch1.endChar) {
                    let fnn = this._createFootnote(fn);
                    if (fn.beginChar > ch1.beginChar) {
                        if (this._addFootnote(ch1, fn, true)) 
                            return true;
                        root.children.splice(i + 2, 0, fnn);
                        ch1.endToken = fn.beginToken.previous;
                    }
                    else {
                        root.children.splice(i + 1, 0, fnn);
                        if (fn.endChar === ch1.endChar) 
                            root.children.splice(i + 2, 1);
                        else if (ch1.beginChar < fn.endToken.next.beginChar) 
                            ch1.beginToken = fn.endToken.next;
                    }
                    return true;
                }
            }
        }
        if (root.children.length > 0) {
            let ch1 = root.children[root.children.length - 1];
            if (ch1.kind === InstrumentKind.TABLE || ch1.kind === InstrumentKind.FOOTNOTE || ch1.kind === InstrumentKind.NOTICE) {
                let fnn = this._createFootnote(fn);
                root.children.push(fnn);
                return true;
            }
            if (this._addFootnote(ch1, fn, true)) 
                return true;
        }
        if (root.kind !== InstrumentKind.CONTENT) {
            if (fn.beginChar > root.endChar && notLast) {
                let fnn = this._createFootnote(fn);
                root.children.push(fnn);
                return true;
            }
        }
        return false;
    }
    
    _analizeContentWithContainers(root, _lines, topLevel, _topDoc) {
        let nums = new Array();
        let k = 0;
        let lev = 100;
        let li0 = null;
        let koef = 0;
        if (root.kind === InstrumentKind.CLAUSE) {
            let split = -1;
            let cou = 0;
            let suf = null;
            for (let i = 1; i < (_lines.length - 1); i++) {
                let line = _lines[i];
                if (line.typ !== InstrToken1Types.NOTICE) 
                    continue;
                if ((line.beginToken instanceof TextToken) && line.beginToken.term === "ПРИМЕЧАНИЯ" && line.endToken.isChar(':')) {
                    if (_lines[i + 1].numbers.length === 1 && _lines[i + 1].numbers[0] === "1") {
                        split = i;
                        cou++;
                        suf = _lines[i + 1].numSuffix;
                    }
                }
            }
            if (cou === 1) {
                let lines0 = Array.from(_lines);
                lines0.splice(split, _lines.length - split);
                this._analizeContentWithContainers(root, lines0, topLevel, _topDoc);
                lines0.splice(0, lines0.length);
                for (let i = split + 1; i < _lines.length; i++) {
                    let line = _lines[i];
                    if (line.typ === InstrToken1Types.LINE && line.numbers.length === 1 && line.numSuffix === suf) 
                        line.typ = InstrToken1Types.NOTICE;
                    lines0.push(line);
                }
                this._analizeContentWithContainers(root, lines0, topLevel, _topDoc);
                return;
            }
        }
        if (((root.kind === InstrumentKind.PARAGRAPH && _lines.length > 10 && _lines[0].typ === InstrToken1Types.LINE) && _lines[0].numbers.length > 0 && !_lines[0].hasVerb) && _lines[1].typ === InstrToken1Types.CLAUSE) {
            nums.push(_lines[0]);
            for (let ii = 2; ii < (_lines.length - 1); ii++) {
                let ch = _lines[ii];
                if (ch.typ !== InstrToken1Types.LINE || ch.hasVerb || ch.numbers.length !== nums[0].numbers.length) 
                    continue;
                let la = nums[nums.length - 1];
                if (NumberingHelper.calcDelta(la, ch, false) !== 1) 
                    continue;
                if (la.numTyp !== ch.numTyp || la.numSuffix !== ch.numSuffix) 
                    continue;
                if (ch.endToken.isChar('.')) {
                    if (!la.endToken.isChar('.')) 
                        continue;
                }
                let hasClause = false;
                for (let jj = ii + 1; jj < _lines.length; jj++) {
                    if (_lines[jj].typ === InstrToken1Types.CLAUSE) {
                        hasClause = true;
                        break;
                    }
                    else if (_lines[jj].typ !== InstrToken1Types.COMMENT && _lines[jj].typ !== InstrToken1Types.EDITIONS) 
                        break;
                }
                if (hasClause) 
                    nums.push(ch);
            }
            if (nums.length < 2) 
                nums.splice(0, nums.length);
            else {
                koef = 2;
                for (const nn of nums) {
                    nn.typ = InstrToken1Types.SUBPARAGRAPH;
                    lev = nn.typContainerRank;
                }
            }
        }
        if (nums.length === 0) {
            nums.splice(0, nums.length);
            for (const li of _lines) {
                if (li.typ === InstrToken1Types.COMMENT || li.typ === InstrToken1Types.EDITIONS) 
                    continue;
                if (li0 === null) 
                    li0 = li;
                k++;
                if (li.typ === InstrToken1Types.DOCPART && li.numbers.length === 0) 
                    continue;
                if (li.typContainerRank > topLevel) {
                    if (li.typContainerRank < lev) {
                        if (nums.length > 2 && li.numbers.length === 0) {
                        }
                        else if (k > 20) {
                        }
                        else {
                            lev = li.typContainerRank;
                            nums.splice(0, nums.length);
                        }
                    }
                    if (li.typContainerRank === lev) 
                        nums.push(li);
                }
            }
            for (let i = 0; i < nums.length; i++) {
                let d0 = (i > 0 ? NumberingHelper.calcDelta(nums[i - 1], nums[i], true) : 0);
                let d1 = ((i + 1) < nums.length ? NumberingHelper.calcDelta(nums[i], nums[i + 1], true) : 0);
                let d01 = (i > 0 && ((i + 1) < nums.length) ? NumberingHelper.calcDelta(nums[i - 1], nums[i + 1], true) : 0);
                if (d0 === 1) {
                    if (d1 === 1) 
                        continue;
                    if (d01 === 1 && !nums[i + 1].isNumDoubt && nums[i].isNumDoubt) {
                        nums.splice(i, 1);
                        i--;
                    }
                    continue;
                }
                if (d01 === 1 && nums[i].isNumDoubt) {
                    nums.splice(i, 1);
                    i--;
                    continue;
                }
            }
            for (let i = 1; i < nums.length; i++) {
                let d = NumberingHelper.calcDelta(nums[i - 1], nums[i], true);
                if (d === 1) 
                    koef += 2;
                else if (d === 2) 
                    koef++;
                else if (d <= 0) 
                    koef--;
            }
            if (nums.length > 0) {
                let hasNumBefore = false;
                for (const li of _lines) {
                    if (li === nums[0]) 
                        break;
                    else if (li.numbers.length > 0) 
                        hasNumBefore = true;
                }
                if (!hasNumBefore && ((nums[0].lastNumber === 1 || ((nums[0] === li0 && nums[0].numSuffix !== null))))) 
                    koef += 2;
                else if (nums[0].typ === InstrToken1Types.CLAUSE && nums[0] === li0) 
                    koef += 2;
            }
        }
        let isChapters = false;
        if (nums.length === 0) {
            let chaps = 0;
            let nons = 0;
            let clauses = 0;
            for (let i = 0; i < _lines.length; i++) {
                let li = _lines[i];
                if (li.typ === InstrToken1Types.CHAPTER) {
                    nums.push(li);
                    chaps++;
                    lev = li.typContainerRank;
                }
                else if (li.typ === InstrToken1Types.LINE && li.titleTyp !== InstrToken1StdTitleType.UNDEFINED) {
                    nums.push(li);
                    nons++;
                }
                else if (li.typ === InstrToken1Types.CLAUSE) 
                    clauses++;
            }
            if (chaps === 0) 
                nums.splice(0, nums.length);
            else {
                koef += 2;
                isChapters = true;
            }
        }
        if ((nums.length === 0 && _topDoc !== null && _topDoc.m_Doc !== null) && _topDoc.m_Doc.typ === "ГОСТ") {
            for (let i = 0; i < _lines.length; i++) {
                let li = _lines[i];
                if (li.numTyp !== NumberTypes.DIGIT) 
                    continue;
                if (li.hasVerb) 
                    continue;
                if (nums.length > 0) {
                    let d = NumberingHelper.calcDelta(nums[nums.length - 1], li, true);
                    if (d === 1 || d === 2) 
                        nums.push(li);
                }
                else 
                    nums.push(li);
            }
            if (nums.length > 1) {
                koef = nums.length;
                isChapters = true;
            }
        }
        if (koef < 2) {
            if (topLevel < InstrToken1._calcRank(InstrToken1Types.CHAPTER)) {
                if (this._analizeChapterWithoutKeywords(root, _lines, _topDoc)) 
                    return;
            }
            this._analizeContentWithoutContainers(root, _lines, false, false, false);
            return;
        }
        let n = 0;
        let names = 0;
        let fr = null;
        let blk = new Array();
        for (let i = 0; i <= _lines.length; i++) {
            let li = (i < _lines.length ? _lines[i] : null);
            if (li === null || (((n < nums.length) && li === nums[n]))) {
                if (blk.length > 0) {
                    if (fr === null) {
                        fr = FragToken._new2000(blk[0].beginToken, blk[blk.length - 1].endToken, InstrumentKind.CONTENT);
                        if (blk.length === 1) 
                            fr.itok = blk[0];
                        root.children.push(fr);
                    }
                    fr.endToken = blk[blk.length - 1].endToken;
                    this._analizeContentWithContainers(fr, blk, lev, _topDoc);
                    blk.splice(0, blk.length);
                    fr = null;
                }
            }
            if (li === null) 
                break;
            if ((n < nums.length) && li === nums[n]) {
                n++;
                fr = FragToken._new2004(li.beginToken, li.endToken, li, li.isExpired);
                root.children.push(fr);
                if (li.typ === InstrToken1Types.DOCPART) 
                    fr.kind = InstrumentKind.DOCPART;
                else if (li.typ === InstrToken1Types.CLAUSEPART) 
                    fr.kind = InstrumentKind.CLAUSEPART;
                else if (li.typ === InstrToken1Types.SECTION) 
                    fr.kind = InstrumentKind.SECTION;
                else if (li.typ === InstrToken1Types.SUBSECTION) 
                    fr.kind = InstrumentKind.SUBSECTION;
                else if (li.typ === InstrToken1Types.PARAGRAPH) 
                    fr.kind = InstrumentKind.PARAGRAPH;
                else if (li.typ === InstrToken1Types.SUBPARAGRAPH) 
                    fr.kind = InstrumentKind.SUBPARAGRAPH;
                else if (li.typ === InstrToken1Types.CHAPTER) 
                    fr.kind = InstrumentKind.CHAPTER;
                else if (li.typ === InstrToken1Types.CLAUSE) 
                    fr.kind = InstrumentKind.CLAUSE;
                else if (li.typ === InstrToken1Types.NOTICE) 
                    fr.kind = InstrumentKind.NOTICE;
                else if (li.typ === InstrToken1Types.FORM) 
                    fr.kind = InstrumentKind.FORM;
                else if (isChapters) 
                    fr.kind = InstrumentKind.CHAPTER;
                if (li.beginToken !== li.numBeginToken && li.numBeginToken !== null) 
                    fr.children.push(FragToken._new2005(li.beginToken, li.numBeginToken.previous, InstrumentKind.KEYWORD, true, li));
                NumberingHelper.createNumber(fr, li);
                if (fr.kind === InstrumentKind.CLAUSE && fr.number === 33) {
                }
                if (li.numEndToken !== li.endToken && li.numEndToken !== null && !li.isExpired) {
                    if (!li.allUpper && ((((li.hasVerb && names === 0 && li.endToken.isCharOf(".:,"))) || li.endToken.isChar(':')))) 
                        fr.children.push(FragToken._new2000(li.numEndToken.next, li.endToken, InstrumentKind.CONTENT));
                    else {
                        let frName = FragToken._new2005(li.numEndToken.next, li.endToken, InstrumentKind.NAME, true, li);
                        fr.children.push(frName);
                        fr.name = FragToken.getRestoredNameMT(frName, false);
                        i = ContentAnalyzeWhapper.correctName(fr, frName, _lines, i);
                        names++;
                    }
                }
                else if (li.titleTyp !== InstrToken1StdTitleType.UNDEFINED) {
                    let frName = FragToken._new2005(li.beginToken, li.endToken, InstrumentKind.NAME, true, li);
                    fr.children.push(frName);
                    fr.name = FragToken.getRestoredNameMT(frName, false);
                    i = ContentAnalyzeWhapper.correctName(fr, frName, _lines, i);
                    names++;
                }
                else if (((((i + 1) < _lines.length) && _lines[i + 1].numbers.length === 0 && !_lines[i + 1].hasVerb) && !_lines[i + 1].hasManySpecChars && _lines[i + 1].typ !== InstrToken1Types.EDITIONS) && _lines[i + 1].typ !== InstrToken1Types.COMMENT) {
                    if ((_lines[i + 1].allUpper || ((((i + 2) < _lines.length) && _lines[i + 2].numbers.length > 0)) || (_lines[i + 1].beginToken.isChar('['))) || _lines[i].endToken.isChar('.')) {
                        i++;
                        li = _lines[i];
                        fr.endToken = li.endToken;
                        let frName = FragToken._new2005(li.beginToken, li.endToken, InstrumentKind.NAME, true, li);
                        fr.children.push(frName);
                        fr.name = FragToken.getRestoredNameMT(frName, false);
                        i = ContentAnalyzeWhapper.correctName(fr, frName, _lines, i);
                        names++;
                    }
                }
                continue;
            }
            if (li.typ === InstrToken1Types.EDITIONS && blk.length === 0 && fr !== null) 
                fr.children.push(FragToken._new2000(li.beginToken, li.endToken, InstrumentKind.EDITIONS));
            else 
                blk.push(li);
        }
    }
    
    static correctName(fr, frName, _lines, i) {
        if ((i + 1) >= _lines.length) 
            return i;
        let li = _lines[i];
        if (li.typ === InstrToken1Types.CHAPTER) {
        }
        let endBracket = 0;
        for (let tt = fr.beginToken; tt !== null && (tt.endChar < fr.endChar); tt = tt.next) {
            if (BracketHelper.canBeStartOfSequence(tt, false, false)) {
                let br = BracketHelper.tryParse(tt, BracketParseAttr.CANBEMANYLINES, 100);
                if (br !== null && br.endChar > fr.endChar) 
                    endBracket = br.endChar;
            }
        }
        if (fr.name !== null && (fr.name.length < 300)) {
            for (; (i + 1) < _lines.length; i++) {
                if (fr.name.length > 500) 
                    break;
                let lii = _lines[i + 1];
                if (lii.numbers.length > 0 || lii.typ !== InstrToken1Types.LINE) 
                    break;
                if (lii.endToken.isChar(':')) 
                    break;
                if (li.endToken.isCharOf(";")) 
                    break;
                if (li.beginToken.isTableControlChar) 
                    break;
                if (lii.hasVerb) 
                    break;
                if (li.endToken.isChar('.')) {
                    if (lii.allUpper && li.allUpper) {
                    }
                    else 
                        break;
                }
                if (li.allUpper && !lii.allUpper) 
                    break;
                if (lii.beginToken.previous.isTableControlChar) 
                    break;
                if (endBracket > 0 && (lii.beginChar < endBracket)) {
                }
                else if (lii.beginToken.previous.isCommaAnd) {
                }
                else if (lii.isBoldFont && li.isBoldFont && li.isItalicFont === lii.isItalicFont) {
                }
                else {
                    if ((li.lengthChar < (Utils.intDiv(lii.lengthChar, 2))) && lii.hasVerb) 
                        break;
                    if (li.hasManySpecChars) 
                        break;
                    if (lii.beginToken.whitespacesBeforeCount > 15) 
                        break;
                    if (lii.beginToken.isValue("НЕТ", null) || lii.beginToken.isValue("НЕ", null) || lii.beginToken.isValue("ОТСУТСТВОВАТЬ", null)) 
                        break;
                    if (!(lii.beginToken instanceof TextToken)) 
                        break;
                    if (lii.beginToken.isChar('(')) {
                        let br = BracketHelper.tryParse(lii.beginToken, BracketParseAttr.CANBEMANYLINES, 30);
                        if (br !== null && br.isNewlineAfter) {
                            fr.endToken = frName.endToken = br.endToken;
                            frName.defVal2 = true;
                            fr.name = FragToken.getRestoredNameMT(frName, false);
                            for (; i < _lines.length; i++) {
                                if (_lines[i].beginChar > fr.endChar) 
                                    break;
                            }
                            return i - 1;
                        }
                    }
                    let mc = lii.beginToken.getMorphClassInDictionary();
                    if (mc.isUndefined) 
                        break;
                    let tt = lii.beginToken;
                    while (tt instanceof MetaToken) {
                        tt = tt.beginToken;
                    }
                    if (tt.chars.isCapitalUpper || !tt.chars.isLetter || mc.isPreposition) {
                        if (!li.endToken.isChar(',') && !li.endToken.isHiphen && !li.endToken.morph._class.isConjunction) {
                            if (!mc.isPreposition) 
                                break;
                            if (li.hasVerb) 
                                break;
                            if (!li.allUpper) 
                                break;
                        }
                    }
                }
                li = lii;
                fr.endToken = frName.endToken = li.endToken;
                frName.defVal2 = true;
                fr.name = FragToken.getRestoredNameMT(frName, false);
            }
        }
        return i;
    }
    
    _analizeChapterWithoutKeywords(root, _lines, _topDoc) {
        let nums = NumberingHelper.extractMainSequence(_lines, true, false);
        let isContractStruct = false;
        if (nums === null || nums[0].numbers.length !== 1 || nums[0].numbers[0] !== "1") {
            if (this.docTyp === DecreeKind.CONTRACT) {
                let nums1 = new Array();
                let num0 = "1";
                let ok = true;
                for (let i = 1; i < _lines.length; i++) {
                    let li = _lines[i];
                    let li0 = _lines[i - 1];
                    if ((nums1.length > 0 && nums1[0].titleTyp === InstrToken1StdTitleType.SUBJECT && nums1[0].numbers.length === 0) && nums1[0].allUpper) {
                        if (li0.numbers.length <= 1 && ((li0.allUpper || li0.titleTyp !== InstrToken1StdTitleType.UNDEFINED))) 
                            nums1.push(li0);
                        continue;
                    }
                    if (li.numbers.length === 2 && li.numbers[0] === num0 && li.numbers[1] === "1") {
                        if (li0.numbers.length === 0 && !li0.beginToken.chars.isAllLower) {
                        }
                        else if (li0.numbers.length === 1 && li0.numbers[0] === num0) {
                        }
                        else {
                            ok = false;
                            break;
                        }
                        nums1.push(li0);
                        num0 = (nums1.length + 1).toString();
                        continue;
                    }
                    if (li0.titleTyp !== InstrToken1StdTitleType.UNDEFINED || ((li0.numbers.length === 1 && li0.numbers[0] === num0))) {
                        nums1.push(li0);
                        num0 = (nums1.length + 1).toString();
                    }
                }
                if (ok && nums1.length > 1) {
                    nums = nums1;
                    isContractStruct = true;
                }
            }
        }
        if (nums === null) 
            return false;
        if (nums.length > 500) 
            return false;
        let n = 0;
        let err = 0;
        let fr = null;
        let blk = new Array();
        let childs = new Array();
        for (let i = 0; i <= _lines.length; i++) {
            let li = (i < _lines.length ? _lines[i] : null);
            if (li === null || (((n < nums.length) && li === nums[n])) || ((n >= nums.length && li.titleTyp !== InstrToken1StdTitleType.UNDEFINED))) {
                if (blk.length > 0) {
                    if (fr === null) {
                        fr = FragToken._new2000(blk[0].beginToken, blk[blk.length - 1].endToken, InstrumentKind.CONTENT);
                        if (blk.length === 1) 
                            fr.itok = blk[0];
                        childs.push(fr);
                    }
                    fr.endToken = blk[blk.length - 1].endToken;
                    this._analizeContentWithoutContainers(fr, blk, false, false, false);
                    blk.splice(0, blk.length);
                    fr = null;
                }
            }
            if (li === null) 
                break;
            if ((n < nums.length) && li === nums[n]) {
                n++;
                if (!li.allUpper && li.hasVerb) {
                    if (li.numTyp === NumberTypes.ROMAN) {
                    }
                    else {
                        blk.push(li);
                        continue;
                    }
                }
                fr = FragToken._new2012(li.beginToken, li.endToken, li);
                childs.push(fr);
                fr.kind = InstrumentKind.CHAPTER;
                NumberingHelper.createNumber(fr, li);
                if (li.numEndToken !== li.endToken && li.numEndToken !== null) {
                    if (li.hasManySpecChars) 
                        fr.children.push(FragToken._new2013(li.numEndToken.next, li.endToken, InstrumentKind.CONTENT, li));
                    else {
                        let frName = FragToken._new2005(li.numEndToken.next, li.endToken, InstrumentKind.NAME, true, li);
                        fr.children.push(frName);
                        fr.name = FragToken.getRestoredNameMT(frName, false);
                        i = ContentAnalyzeWhapper.correctName(fr, frName, _lines, i);
                    }
                }
                else if (isContractStruct) {
                    let frName = FragToken._new2005(li.beginToken, li.endToken, InstrumentKind.NAME, true, li);
                    fr.children.push(frName);
                    fr.name = FragToken.getRestoredNameMT(frName, false);
                }
                continue;
            }
            else if (n >= nums.length && li.titleTyp !== InstrToken1StdTitleType.UNDEFINED) {
                fr = FragToken._new2012(li.beginToken, li.endToken, li);
                fr.kind = childs[childs.length - 1].kind;
                childs.push(fr);
                let frName = FragToken._new2005(li.beginToken, li.endToken, InstrumentKind.NAME, true, li);
                fr.children.push(frName);
                fr.name = FragToken.getRestoredNameMT(frName, false);
                i = ContentAnalyzeWhapper.correctName(fr, frName, _lines, i);
                continue;
            }
            if (blk.length === 0 && li.hasManySpecChars) 
                err++;
            blk.push(li);
        }
        let coef = -err;
        for (let i = 0; i < childs.length; i++) {
            let chap = childs[i];
            if (i === 0 && chap.number === 0 && chap.lengthChar > 1000) 
                coef -= 1;
            else {
                let nam = chap.name;
                if (nam === null) {
                    if (i === 0 && chap.kind === InstrumentKind.CONTENT) {
                    }
                    else 
                        coef--;
                }
                else if (nam.length > 300) 
                    coef -= (Utils.intDiv(nam.length, 300));
                else {
                    coef += 1;
                    let len = chap.lengthChar - nam.length;
                    if (len > 200) 
                        coef += 1;
                    else if (chap.children.length < 3) 
                        coef--;
                    if (root.kind === InstrumentKind.SECTION) 
                        coef += 1;
                }
            }
            for (const ch of chap.children) {
                if (ch.kind === InstrumentKind.NAME) {
                    if (ch.endToken.isCharOf(":;")) 
                        coef -= 2;
                    break;
                }
                if (ch.number === 0) 
                    continue;
                if (ch.itok === null) 
                    break;
                break;
            }
        }
        if (coef < 3) {
            if (err > 2) 
                return true;
            return false;
        }
        root.children.splice(root.children.length, 0, ...childs);
        if (_topDoc !== null && _topDoc.m_Doc !== null && _topDoc.m_Doc.typ !== null) {
            let ty = _topDoc.m_Doc.typ;
            if (this.docTyp === DecreeKind.CONTRACT) {
                let ok = true;
                for (const ch of childs) {
                    if (ch.kind === InstrumentKind.CHAPTER) {
                        for (const chh of ch.children) {
                            if (chh.kind === InstrumentKind.CLAUSE) 
                                ok = false;
                        }
                    }
                }
                if (ok) {
                    for (const ch of childs) {
                        if (ch.kind === InstrumentKind.CHAPTER) 
                            ch.kind = InstrumentKind.CLAUSE;
                    }
                }
            }
        }
        return true;
    }
    
    _addForm(root, li) {
        if (li.subItems === null || li.numbers.length === 0) 
            return false;
        let fr = FragToken._new2013(li.beginToken, li.subItems[li.subItems.length - 1].endToken, InstrumentKind.FORM, li);
        NumberingHelper.createNumber(fr, li);
        let i = 0;
        let frName = null;
        if ((li.numEndToken.endChar + 10) < li.endChar) 
            frName = FragToken._new2005(li.numEndToken.next, li.endToken, InstrumentKind.NAME, true, li);
        else if (li.subItems[0].hasManySpecChars) {
        }
        else {
            frName = FragToken._new2005(li.subItems[0].beginToken, li.subItems[0].endToken, InstrumentKind.NAME, true, li.subItems[0]);
            i = 1;
        }
        if (frName !== null) {
            fr.children.push(frName);
            fr.name = FragToken.getRestoredNameMT(frName, false);
            i = ContentAnalyzeWhapper.correctName(fr, frName, li.subItems, 1);
        }
        root.children.push(fr);
        if (i < li.subItems.length) 
            fr.children.push(FragToken._new2000(li.subItems[i].beginToken, fr.endToken, InstrumentKind.CONTENT));
        return true;
    }
    
    _addCommentOrEdition(fr, li) {
        if (li.typ === InstrToken1Types.COMMENT) 
            fr.children.push(FragToken._new2013(li.beginToken, li.endToken, InstrumentKind.COMMENT, li));
        else if (li.typ === InstrToken1Types.EDITIONS) {
            let edt = FragToken._new2013(li.beginToken, li.endToken, InstrumentKind.EDITIONS, li);
            fr.children.push(edt);
            edt.referents = new Array();
            for (let tt = li.beginToken; tt !== null; tt = tt.next) {
                if (tt.endChar > li.endToken.endChar) 
                    break;
                let dr = Utils.as(tt.getReferent(), DecreeReferent);
                if (dr !== null) {
                    if (!edt.referents.includes(dr)) 
                        edt.referents.push(dr);
                }
            }
        }
    }
    
    _analizeContentWithoutContainers(root, _lines, isSubitem, isPreamble = false, isKodex = false) {
        if (root.kind === InstrumentKind.CHAPTER) {
        }
        if (root.kind === InstrumentKind.CLAUSE || ((root.kind === InstrumentKind.CHAPTER && this.docTyp === DecreeKind.CONTRACT))) {
            if (root.number === 8) {
            }
            while (_lines.length > 0) {
                if (_lines[0].typ === InstrToken1Types.COMMENT || _lines[0].typ === InstrToken1Types.EDITIONS) {
                    this._addCommentOrEdition(root, _lines[0]);
                    _lines.splice(0, 1);
                }
                else 
                    break;
            }
            if (_lines.length === 0) 
                return;
            if ((_lines.length > 2 && _lines[0].numbers.length === 0 && _lines[0].endToken.isCharOf(":")) && _lines[1].numbers.length > 0) {
            }
            if (_lines[0].numbers.length === 0 && this.docTyp !== DecreeKind.CONTRACT) {
                let parts = new Array();
                let tmp = new Array();
                let part = null;
                for (let ii = 0; ii < _lines.length; ii++) {
                    let li = _lines[ii];
                    if ((ii > 0 && li.numbers.length === 0 && li.typ !== InstrToken1Types.EDITIONS) && li.typ !== InstrToken1Types.COMMENT && part !== null) {
                        if (MiscHelper.canBeStartOfSentence(li.beginToken)) {
                            let end = true;
                            for (let j = ii - 1; j >= 0; j--) {
                                if (_lines[j].typ !== InstrToken1Types.COMMENT && _lines[j].typ !== InstrToken1Types.EDITIONS) {
                                    let tt = _lines[j].endToken;
                                    if (!tt.isCharOf(".")) {
                                        if (tt.newlinesAfterCount < 2) 
                                            end = false;
                                        else if (tt.isCharOf(":,;")) 
                                            end = false;
                                    }
                                    break;
                                }
                            }
                            if (end) {
                                this._analizeContentWithoutContainers(part, tmp, false, false, isKodex);
                                tmp.splice(0, tmp.length);
                                part = null;
                            }
                        }
                    }
                    if (part === null) {
                        part = FragToken._new2024(li.beginToken, li.endToken, InstrumentKind.CLAUSEPART, parts.length + 1);
                        parts.push(part);
                    }
                    if (li.endChar > part.endChar) 
                        part.endToken = li.endToken;
                    tmp.push(li);
                }
                if (part !== null && tmp.length > 0) 
                    this._analizeContentWithoutContainers(part, tmp, false, false, isKodex);
                let ok = true;
                if (root.kind !== InstrumentKind.CLAUSE) {
                    let num = 0;
                    let tot = 0;
                    for (const p of parts) {
                        for (const ch of p.children) {
                            if (ch.number > 0) 
                                num++;
                            tot++;
                        }
                    }
                    if ((Utils.intDiv(tot, 2)) > num) 
                        ok = false;
                }
                else {
                    let nums1 = NumberingHelper.extractMainSequence(_lines, false, this.docTyp !== DecreeKind.CONTRACT);
                    if (nums1 !== null && nums1.length > 1 && (_lines.indexOf(nums1[0]) < 2)) {
                        let ii = _lines.indexOf(nums1[nums1.length - 1]);
                        if (ii >= (_lines.length - ((Utils.intDiv(_lines.length * 3, nums1.length))))) 
                            ok = false;
                    }
                }
                if (ok) {
                    for (const p of parts) {
                        NumberingHelper.correctChildNumbers(root, p.children);
                    }
                    if (parts.length > 1) {
                        root.children.splice(root.children.length, 0, ...parts);
                        return;
                    }
                    else if (parts.length === 1) {
                        root.children.splice(root.children.length, 0, ...parts[0].children);
                        return;
                    }
                }
            }
        }
        if (root.number === 11 && root.subNumber === 2) {
        }
        let notices = new Array();
        for (let ii = 0; ii < _lines.length; ii++) {
            if (_lines[ii].typ === InstrToken1Types.NOTICE) {
                let li = _lines[ii];
                if (li.numbers.length === 0) {
                    if (((((ii + 1) < _lines.length) && _lines[ii + 1].numbers.length === 1 && _lines[ii + 1].numbers[0] === "1") && (li.beginToken instanceof TextToken) && li.beginToken.term === "ПРИМЕЧАНИЯ") && (li.lengthChar < 13)) {
                        let lines2 = Array.from(_lines);
                        lines2.splice(0, ii + 1);
                        for (let jj = 0; jj < lines2.length; jj++) {
                            if (lines2[jj].typ === InstrToken1Types.TABLE || lines2[jj].beginToken.isTableControlChar) {
                                lines2.splice(jj, lines2.length - jj);
                                break;
                            }
                        }
                        let root0 = new FragToken(li.beginToken, li.endToken);
                        this._analizeContentWithoutContainers(root0, lines2, true, false, false);
                        if (root0.children.length > 0) {
                            for (const ch of root0.children) {
                                notices.push(ch);
                                ch.kind = InstrumentKind.NOTICE;
                                for (const chh of ch.children) {
                                    if (chh.kind === InstrumentKind.SUBITEM) 
                                        chh.kind = InstrumentKind.ITEM;
                                }
                            }
                        }
                        _lines.splice(ii, lines2.length);
                    }
                    else {
                        li = _lines[ii];
                        let not = FragToken._new2013(li.beginToken, li.endToken, InstrumentKind.NOTICE, li);
                        notices.push(not);
                        if (li.numBeginToken !== null && li.beginToken !== li.numBeginToken) 
                            not.children.push(FragToken._new2026(li.beginToken, li.numBeginToken.previous, InstrumentKind.KEYWORD, true));
                        if (li.numbers.length > 0) 
                            NumberingHelper.createNumber(not, li);
                        let li0 = new Array();
                        let num = null;
                        for (let jj = ii + 1; jj < _lines.length; jj++) {
                            let li1 = _lines[jj];
                            if (li1.typ === InstrToken1Types.NOTICE) 
                                break;
                            else if (li1.cnum === null) 
                                li0.push(li1);
                            else {
                                if (num === null) {
                                    if (!li1.cnum.isOne) 
                                        break;
                                }
                                else if (!num.checkNext(li1, false)) 
                                    break;
                                num = li1;
                                li0.push(li1);
                            }
                        }
                        if (not.children.length > 0) 
                            not.children.push(FragToken._new2013((li.numEndToken != null ? li.numEndToken : li.beginToken), li.endToken, InstrumentKind.CONTENT, li));
                        if (li0.length > 0) {
                            this._analizeContentWithoutContainers(not, li0, false, false, false);
                            not.endToken = li0[li0.length - 1].endToken;
                        }
                        _lines.splice(ii, li0.length + 1);
                        ii--;
                    }
                }
            }
        }
        let nums = NumberingHelper.extractMainSequence(_lines, this.docTyp !== DecreeKind.CONTRACT || this.topDoc.kind === InstrumentKind.APPENDIX, this.docTyp !== DecreeKind.CONTRACT);
        if (_lines.length > 5) {
        }
        if (isKodex && nums !== null) {
            let errCou = 0;
            for (const nu of nums) {
                if (nu.numSuffix !== null && nu.numSuffix !== ")" && nu.numSuffix !== ".") 
                    errCou++;
            }
            if (errCou > 0) {
                if (errCou > (Utils.intDiv(nums.length, 2))) 
                    nums = null;
            }
        }
        if (nums === null) {
            let last = (root.children.length > 0 ? root.children[root.children.length - 1] : null);
            for (const li of _lines) {
                if (li.typ === InstrToken1Types.COMMENT || li.typ === InstrToken1Types.EDITIONS) {
                    this._addCommentOrEdition(root, li);
                    last = null;
                    continue;
                }
                if (li.typ === InstrToken1Types.FORM) {
                    if (this._addForm(root, li)) 
                        continue;
                }
                if (li.typ === InstrToken1Types.INDEX) {
                    let ind = FragToken._new2013(li.beginToken, li.endToken, InstrumentKind.INDEX, li);
                    root.children.push(ind);
                    last = null;
                    let tt = li.beginToken;
                    if (!li.indexNoKeyword) {
                        for (; tt !== null && tt.endChar <= li.endChar; tt = tt.next) {
                            if (tt.isNewlineAfter) {
                                ind.children.push(FragToken._new2029(li.beginToken, tt, InstrumentKind.NAME, true));
                                tt = tt.next;
                                break;
                            }
                        }
                    }
                    let isTab = false;
                    for (; tt !== null && tt.endChar <= li.endChar; tt = tt.next) {
                        let it1 = InstrToken1.parse(tt, true, null, 0, null, false, 0, false, true);
                        if (it1 === null) 
                            break;
                        if ((!isTab && it1.endChar === li.endChar && tt.isTableControlChar) && it1.lengthChar > 100) {
                            let it2 = InstrToken1.parse(tt.next, true, null, 0, null, false, 0, false, true);
                            if (it2 === null) 
                                break;
                            it1 = it2;
                            tt = tt.next;
                        }
                        if (it1.value === "СТР") {
                            tt = it1.endToken;
                            continue;
                        }
                        if (tt.getReferent() instanceof DecreePartReferent) {
                            tt = tt.kit.debedToken(tt);
                            it1 = InstrToken1.parse(tt, true, null, 0, null, false, 0, false, false);
                        }
                        if (it1.typ === InstrToken1Types.APPENDIX && !it1.isNewlineAfter) {
                            for (let ttt = it1.endToken; ttt !== null; ttt = ttt.next) {
                                if (ttt.isTableControlChar || ttt.isNewlineBefore) 
                                    break;
                                it1.endToken = ttt;
                            }
                        }
                        let indItem = FragToken._new2000(tt, it1.endToken, InstrumentKind.INDEXITEM);
                        ind.children.push(indItem);
                        let nam = null;
                        if (it1.numEndToken !== null && it1.numEndToken !== it1.endToken) {
                            if (it1.beginToken !== it1.numBeginToken) 
                                indItem.children.push(FragToken._new2026(it1.beginToken, it1.numBeginToken.previous, InstrumentKind.KEYWORD, true));
                            NumberingHelper.createNumber(indItem, it1);
                            indItem.children.push((nam = FragToken._new2026(it1.numEndToken.next, it1.endToken, InstrumentKind.NAME, true)));
                            let it2 = InstrToken1.parse(it1.endToken.next, true, null, 0, null, false, 0, false, true);
                            if (((it2 !== null && it2.typ !== InstrToken1Types.APPENDIX && (it1.endToken.next instanceof TextToken)) && it2.numbers.length === 0 && it2.titleTyp === InstrToken1StdTitleType.UNDEFINED) && !it1.endToken.next.isTableControlChar) {
                                let it3 = InstrToken1.parse(it2.endToken.next, true, null, 0, null, false, 0, false, true);
                                if (it3 !== null && it3.numbers.length > 0) {
                                    nam.endToken = it2.endToken;
                                    nam.defVal2 = true;
                                    indItem.endToken = it1.endToken = it2.endToken;
                                }
                            }
                        }
                        else 
                            indItem.children.push((nam = FragToken._new2026(it1.beginToken, it1.endToken, InstrumentKind.NAME, true)));
                        indItem.name = FragToken.getRestoredNameMT(nam, true);
                        let val = Utils.asString(nam.value);
                        if (val !== null) {
                            while (val.length > 4) {
                                let ch = val[val.length - 1];
                                if ((ch === '.' || ch === '-' || Utils.isDigit(ch)) || Utils.isWhitespace(ch) || ch === (String.fromCharCode(7))) 
                                    val = val.substring(0, 0 + val.length - 1);
                                else 
                                    break;
                            }
                            nam.value = val;
                        }
                        tt = it1.endToken;
                    }
                    continue;
                }
                if (last !== null && last.kind === InstrumentKind.CONTENT) 
                    last.endToken = li.endToken;
                else 
                    root.children.push((last = FragToken._new2013(li.beginToken, li.endToken, (root.kind === InstrumentKind.FOOTNOTE ? InstrumentKind.INDENTION : InstrumentKind.CONTENT), li)));
            }
            if (!isPreamble) {
                if ((root.children.length === 1 && root.children[0].kind === InstrumentKind.CONTENT && root.kind === InstrumentKind.CONTENT) && ((root.children[0].itok === null || !root.children[0].itok.hasChanges))) {
                    if (root.itok === null) 
                        root.itok = root.children[0].itok;
                    root.children.splice(0, root.children.length);
                }
                else if (root.children.length === 1 && root.children[0].kind === InstrumentKind.COMMENT && root.kind === InstrumentKind.CONTENT) {
                    root.children.splice(0, root.children.length);
                    root.kind = InstrumentKind.COMMENT;
                }
            }
            root.children.splice(root.children.length, 0, ...notices);
            return;
        }
        if (isSubitem) {
        }
        let n = 0;
        let fr = null;
        let blk = new Array();
        let i = 0;
        for (i = 0; i < _lines.length; i++) {
            if (_lines[i] === nums[0]) 
                break;
            else 
                blk.push(_lines[i]);
        }
        if (blk.length > 0) 
            this._analizeContentWithoutContainers(root, blk, false, true, isKodex);
        for (; i < _lines.length; i++) {
            let li = _lines[i];
            let j = 0;
            blk.splice(0, blk.length);
            n++;
            for (j = i + 1; j < _lines.length; j++) {
                if ((n < nums.length) && _lines[j] === nums[n]) 
                    break;
                else if (n >= nums.length && _lines[j].titleTyp !== InstrToken1StdTitleType.UNDEFINED && _lines[j].allUpper) 
                    break;
                else 
                    blk.push(_lines[j]);
            }
            fr = FragToken._new2012(li.beginToken, li.endToken, li);
            root.children.push(fr);
            fr.kind = (root.kind === InstrumentKind.NOTICE ? InstrumentKind.ITEM : (li.typ === InstrToken1Types.NOTICE ? InstrumentKind.NOTICE : (isSubitem ? InstrumentKind.SUBITEM : InstrumentKind.ITEM)));
            NumberingHelper.createNumber(fr, li);
            if (li.numEndToken !== li.endToken && li.numEndToken !== null) 
                fr.children.push(FragToken._new2013(li.numEndToken.next, li.endToken, InstrumentKind.CONTENT, li));
            else if (li.titleTyp !== InstrToken1StdTitleType.UNDEFINED && li.allUpper) {
                fr.kind = InstrumentKind.TAIL;
                fr.children.push(FragToken._new2029(li.beginToken, li.endToken, InstrumentKind.NAME, true));
            }
            if (blk.length > 0) {
                fr.endToken = blk[blk.length - 1].endToken;
                this._analizeContentWithoutContainers(fr, blk, true, false, isKodex);
            }
            i = j - 1;
        }
        NumberingHelper.correctChildNumbers(root, root.children);
        if (notices.length > 0 && notices[notices.length - 1].endChar > root.endChar) 
            root.endToken = notices[notices.length - 1].endToken;
        ContentAnalyzeWhapper._insertNotices(root, notices);
        if (notices.length > 0) 
            root.children.splice(root.children.length, 0, ...notices);
    }
    
    static _insertNotices(root, notices) {
        if (root.children.length === 0 || notices.length === 0) 
            return;
        if (root.children.length > 6) {
        }
        for (let ii = notices.length - 1; ii >= 0; ii--) {
            let not = notices[ii];
            for (let jj = 0; jj < (root.children.length - 1); jj++) {
                if ((root.children[jj].endChar < not.beginChar) && (not.endChar < root.children[jj + 1].beginChar)) {
                    root.children.splice(jj + 1, 0, not);
                    notices.splice(ii, 1);
                    break;
                }
            }
        }
        if (notices.length > 0) {
            for (const ch of root.children) {
                ContentAnalyzeWhapper._insertNotices(ch, notices);
            }
        }
    }
    
    static _extractDirectiveSequence(_lines) {
        let res = new Array();
        for (let i = 0; i < _lines.length; i++) {
            if (_lines[i].typ === InstrToken1Types.DIRECTIVE) {
                let j = 0;
                for (j = i - 1; j >= 0; j--) {
                    let li = _lines[j];
                    if (li.typ === InstrToken1Types.FIRSTLINE) {
                        j--;
                        break;
                    }
                    if (li.beginToken.isValue("РУКОВОДСТВУЯСЬ", null) || li.beginToken.isValue("ИССЛЕДОВАВ", null)) {
                        j--;
                        break;
                    }
                    if (li.beginToken.isValue("НА", null) && li.beginToken.next !== null && li.beginToken.next.isValue("ОСНОВАНИЕ", null)) {
                        j--;
                        break;
                    }
                    if (li.numbers.length > 0) 
                        break;
                    if (li.typ === InstrToken1Types.COMMENT) 
                        continue;
                    if (li.typ === InstrToken1Types.LINE) 
                        continue;
                    break;
                }
                res.push(_lines[j + 1]);
            }
        }
        if (res.length === 0) 
            return null;
        if (res[0] !== _lines[0]) 
            res.splice(0, 0, _lines[0]);
        return res;
    }
    
    _analizeContentWithDirectives(root, _lines, isJus) {
        let dirSeq = ContentAnalyzeWhapper._extractDirectiveSequence(_lines);
        if (dirSeq === null) {
            this._analizeContentWithoutContainers(root, _lines, false, false, false);
            return;
        }
        if (dirSeq.length > 1) {
        }
        let parts = new Array();
        let n = 0;
        let j = 0;
        for (let i = 0; i < _lines.length; i++) {
            if (_lines[i] === dirSeq[n]) {
                let blk = new Array();
                for (j = i; j < _lines.length; j++) {
                    if (((n + 1) < dirSeq.length) && dirSeq[n + 1] === _lines[j]) 
                        break;
                    else 
                        blk.push(_lines[j]);
                }
                let fr = this._createDirectivePart(blk);
                if (fr !== null) 
                    parts.push(fr);
                i = j - 1;
                n++;
            }
        }
        if (parts.length === 0) 
            return;
        if (parts.length === 1 && parts[0].children.length > 0) {
            root.children.splice(root.children.length, 0, ...parts[0].children);
            return;
        }
        if (parts.length > 2 || ((parts.length > 1 && isJus))) {
            if (parts[0].name === null && parts[parts.length - 1].name !== null) {
                if (parts[1].name === "МОТИВИРОВОЧНАЯ" && !parts[0].isNewlineAfter) {
                    parts[0].children.splice(parts[0].children.length, 0, ...parts[1].children);
                    parts[0].name = parts[1].name;
                    parts.splice(1, 1);
                    if (parts[0].children.length > 1 && parts[0].children[0].kind === InstrumentKind.CONTENT && parts[0].children[1].kind === InstrumentKind.PREAMBLE) {
                        parts[0].children[1].beginToken = parts[0].children[0].beginToken;
                        parts[0].children.splice(0, 1);
                    }
                }
                else {
                    parts[0].name = "ВВОДНАЯ";
                    parts[0].kind = InstrumentKind.DOCPART;
                    if (parts[0].children.length === 0) 
                        parts[0].children.push(FragToken._new2013(parts[0].beginToken, parts[0].endToken, InstrumentKind.CONTENT, parts[0].itok));
                }
            }
            for (let i = 0; i < (parts.length - 1); i++) {
                if (parts[i].name === "МОТИВИРОВОЧНАЯ" && parts[i + 1].name === null) {
                    parts[i].children.splice(parts[i].children.length, 0, ...parts[i + 1].children);
                    parts.splice(i + 1, 1);
                    i--;
                }
            }
            let hasNull = false;
            for (const p of parts) {
                if (p.name === null) 
                    hasNull = true;
            }
            if (!hasNull) {
                root.children.splice(root.children.length, 0, ...parts);
                return;
            }
        }
        for (const p of parts) {
            if (p.children.length > 0) 
                root.children.splice(root.children.length, 0, ...p.children);
            else 
                root.children.push(p);
        }
    }
    
    _createDirectivePart(_lines) {
        let res = FragToken._new2000(_lines[0].beginToken, _lines[_lines.length - 1].endToken, InstrumentKind.DOCPART);
        let head = new Array();
        let i = 0;
        for (i = 0; i < _lines.length; i++) {
            if (_lines[i].typ === InstrToken1Types.DIRECTIVE) 
                break;
            else 
                head.push(_lines[i]);
        }
        if (i >= _lines.length) {
            this._analizeContentWithoutContainers(res, _lines, false, false, false);
            return res;
        }
        if (head.length > 0) {
            let frHead = FragToken._new2000(head[0].beginToken, head[head.length - 1].endToken, InstrumentKind.CONTENT);
            this._analizeContentWithoutContainers(frHead, head, false, false, false);
            res.children.push(frHead);
        }
        if (res.children.length === 1 && res.children[0].kind === InstrumentKind.CONTENT) 
            res.children[0].kind = InstrumentKind.PREAMBLE;
        res.children.push(FragToken._new2041(_lines[i].beginToken, _lines[i].endToken, InstrumentKind.DIRECTIVE, _lines[i].value, _lines[i]));
        let vvv = _lines[i].value;
        if (vvv === "УСТАНОВЛЕНИЕ" || vvv === "ВСТАНОВЛЕННЯ") 
            res.name = "МОТИВИРОВОЧНАЯ";
        else if (((((vvv === "ПОСТАНОВЛЕНИЕ" || vvv === "ОПРЕДЕЛЕНИЕ" || vvv === "ПРИГОВОР") || vvv === "ПРИКАЗ" || vvv === "РЕШЕНИЕ") || vvv === "ПОСТАНОВА" || vvv === "ВИЗНАЧЕННЯ") || vvv === "ВИРОК" || vvv === "НАКАЗ") || vvv === "РІШЕННЯ") 
            res.name = "РЕЗОЛЮТИВНАЯ";
        _lines.splice(0, i + 1);
        if (_lines.length > 0) 
            this._analizeContentWithoutContainers(res, _lines, false, false, false);
        return res;
    }
    
    _analizeSections(root) {
        for (let k = 0; k < 2; k++) {
            let secs = new Array();
            let items = new Array();
            for (const ch of root.children) {
                if (ch.kind === InstrumentKind.CHAPTER || ch.kind === InstrumentKind.CLAUSE) {
                    if (ch.number === 0 || (ch.children.length < 2)) 
                        return;
                    let newChilds = new Array();
                    let i = 0;
                    for (; i < ch.children.length; i++) {
                        if (ch.children[i].kind !== InstrumentKind.NUMBER && ch.children[i].kind !== InstrumentKind.NAME && ch.children[i].kind !== InstrumentKind.KEYWORD) 
                            break;
                        else 
                            newChilds.push(ch.children[i]);
                    }
                    if (i >= ch.children.length) 
                        return;
                    let sect = null;
                    if (ch.children[i].kind !== InstrumentKind.CONTENT) {
                        if (ch.children[i].kind !== InstrumentKind.ITEM) 
                            return;
                    }
                    else {
                        sect = FragToken._new2026(ch.children[i].beginToken, ch.children[i].endToken, InstrumentKind.SECTION, true);
                        sect.name = Utils.asString(sect.value);
                        sect.value = null;
                        sect.children.push(FragToken._new2000(sect.beginToken, sect.endToken, InstrumentKind.NAME));
                        newChilds.push(sect);
                        if ((ch.children[i].whitespacesBeforeCount < 15) || (ch.children[i].whitespacesAfterCount < 15)) 
                            return;
                        i++;
                        if (((i + 1) < ch.children.length) && ch.children[i].kind === InstrumentKind.COMMENT) 
                            i++;
                    }
                    let j = 0;
                    let its = 0;
                    for (j = i; j < ch.children.length; j++) {
                        if (ch.children[j].kind !== InstrumentKind.ITEM) 
                            return;
                        its++;
                        if (sect !== null) {
                            sect.children.push(ch.children[j]);
                            sect.endToken = ch.children[j].endToken;
                        }
                        else 
                            newChilds.push(ch.children[j]);
                        if ((ch.children[j].whitespacesAfterCount < 15) || j === (ch.children.length - 1)) 
                            continue;
                        let la = ContentAnalyzeWhapper._getLastChild(ch.children[j]);
                        if (la.whitespacesAfterCount < 15) 
                            continue;
                        let nextSect = null;
                        for (let tt = la.endToken; tt !== null && tt.beginChar > la.beginChar; tt = tt.previous) {
                            if (tt.isNewlineBefore) {
                                if (tt.chars.isCyrillicLetter && tt.chars.isAllLower) 
                                    continue;
                                let it = InstrToken1.parse(tt, true, null, 0, null, false, 0, false, false);
                                if (it !== null && it.numbers.length > 0) 
                                    break;
                                if (tt.whitespacesBeforeCount < 15) 
                                    continue;
                                if ((tt.previous.endChar - la.beginChar) < 20) 
                                    break;
                                nextSect = FragToken._new2026(tt, la.endToken, InstrumentKind.SECTION, true);
                                nextSect.name = Utils.asString(nextSect.value);
                                nextSect.value = null;
                                nextSect.children.push(FragToken._new2000(tt, la.endToken, InstrumentKind.NAME));
                                break;
                            }
                        }
                        if (nextSect === null) 
                            continue;
                        if (sect === null) 
                            return;
                        if (k > 0) {
                            sect.endToken = la.endToken = nextSect.beginToken.previous;
                            if (ch.children[j].endChar > la.endChar) 
                                ch.children[j].endToken = la.endToken;
                        }
                        newChilds.push(nextSect);
                        sect = nextSect;
                    }
                    if (k > 0) 
                        ch.children = newChilds;
                    else {
                        items.push(its);
                        secs.push(newChilds.length);
                    }
                }
            }
            if (k > 0) 
                break;
            if (secs.length < 3) 
                break;
            let allsecs = 0;
            let allits = 0;
            let okchapts = 0;
            for (let i = 0; i < items.length; i++) {
                allits += items[i];
                allsecs += secs[i];
                if (secs[i] > 1) 
                    okchapts++;
            }
            let rr = (allits) / (allsecs);
            if (rr < 1.5) 
                break;
            if (okchapts < (Utils.intDiv(items.length, 2))) 
                break;
        }
    }
    
    static _getLastChild(fr) {
        if (fr.children.length === 0) 
            return fr;
        return ContentAnalyzeWhapper._getLastChild(fr.children[fr.children.length - 1]);
    }
    
    _correctNames(root, parent) {
        let i = 0;
        let frNams = null;
        for (i = 0; i < root.children.length; i++) {
            let ch = root.children[i];
            if (ch.kind !== InstrumentKind.CLAUSE && ch.kind !== InstrumentKind.CHAPTER) 
                continue;
            if (ch.name !== null) {
                frNams = null;
                break;
            }
            let j = 0;
            let namHas = false;
            for (j = 0; j < ch.children.length; j++) {
                let chh = ch.children[j];
                if (chh.kind === InstrumentKind.KEYWORD || chh.kind === InstrumentKind.NUMBER || chh.kind === InstrumentKind.EDITIONS) 
                    continue;
                if (chh.kind === InstrumentKind.CONTENT || chh.kind === InstrumentKind.INDENTION || ((chh.kind === InstrumentKind.CLAUSEPART && chh.children.length === 1))) {
                    if (chh.itok === null) 
                        chh.itok = InstrToken1.parse(chh.beginToken, true, null, 0, null, false, 0, false, false);
                    if (chh.itok !== null && !chh.itok.hasVerb) 
                        namHas = true;
                }
                break;
            }
            if (!namHas) {
                frNams = null;
                break;
            }
            if (frNams === null) {
                frNams = new Array();
                frNams.push(ch);
            }
            else {
                if (frNams[frNams.length - 1].kind !== ch.kind) {
                    frNams = null;
                    break;
                }
                frNams.push(ch);
            }
        }
        if (frNams !== null) {
            for (const ch of frNams) {
                let j = 0;
                for (j = 0; j < ch.children.length; j++) {
                    let chh = ch.children[j];
                    if (chh.kind === InstrumentKind.KEYWORD || chh.kind === InstrumentKind.NUMBER || chh.kind === InstrumentKind.EDITIONS) 
                        continue;
                    if (chh.kind === InstrumentKind.CONTENT || chh.kind === InstrumentKind.INDENTION || ((chh.kind === InstrumentKind.CLAUSEPART && chh.children.length === 1))) 
                        break;
                }
                if (j >= ch.children.length) 
                    continue;
                let nam = ch.children[j];
                if (nam.kind === InstrumentKind.INDENTION || ((nam.kind === InstrumentKind.CLAUSEPART && nam.children.length === 1))) {
                    nam.number = 0;
                    let cou = 0;
                    for (let jj = j + 1; jj < ch.children.length; jj++) {
                        if (ch.children[jj].kind === nam.kind) {
                            ch.children[jj].number--;
                            cou++;
                        }
                        else 
                            break;
                    }
                    if (cou === 1) {
                        for (let jj = j + 1; jj < ch.children.length; jj++) {
                            if (ch.children[jj].kind === nam.kind) {
                                let empty = true;
                                for (let k = jj + 1; k < ch.children.length; k++) {
                                    if (ch.children[k].kind !== InstrumentKind.EDITIONS && ch.children[k].kind !== InstrumentKind.COMMENT) {
                                        empty = false;
                                        break;
                                    }
                                }
                                if (empty) {
                                    if (ch.children[jj].kind === InstrumentKind.INDENTION || ch.children.length === 0) {
                                        ch.children[jj].kind = InstrumentKind.CONTENT;
                                        ch.children[jj].number = 0;
                                    }
                                    else {
                                        let ch0 = ch.children[jj];
                                        ch.children.splice(jj, 1);
                                        ch.children.splice(jj, 0, ...ch0.children);
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
                nam.number = 0;
                nam.kind = InstrumentKind.NAME;
                nam.defVal2 = true;
                nam.children.splice(0, nam.children.length);
                ch.name = Utils.asString(nam.value);
            }
        }
        if (parent !== null && parent.isExpired) {
        }
        else 
            root.checkExpired();
        for (const ch of root.children) {
            this._correctNames(ch, root);
        }
    }
    
    _correctKodexParts(root) {
        if (root.number === 2 && root.kind === InstrumentKind.CLAUSE) {
        }
        if (root.number === 11 && root.kind === InstrumentKind.ITEM) {
        }
        let i = 0;
        for (i = 0; i < root.children.length; i++) {
            let ki = root.children[i].kind;
            if ((ki !== InstrumentKind.KEYWORD && ki !== InstrumentKind.NAME && ki !== InstrumentKind.NUMBER) && ki !== InstrumentKind.COMMENT && ki !== InstrumentKind.EDITIONS) 
                break;
        }
        if (i >= root.children.length) 
            return;
        let i0 = i;
        if (root.kind === InstrumentKind.CLAUSE && this.docTyp !== DecreeKind.CONTRACT) {
            for (; i < root.children.length; i++) {
                let ch = root.children[i];
                if (ch.kind === InstrumentKind.ITEM || ch.kind === InstrumentKind.SUBITEM || ((ch.kind === InstrumentKind.CLAUSEPART && ch.number > 0))) {
                    ch.kind = InstrumentKind.CLAUSEPART;
                    for (const chh of ch.children) {
                        if (chh.kind === InstrumentKind.SUBITEM && chh.number > 0) {
                            chh.kind = InstrumentKind.ITEM;
                            for (const chhh of chh.children) {
                                if (chhh.number > 0) 
                                    chhh.kind = InstrumentKind.SUBITEM;
                            }
                        }
                    }
                }
                else if (ch.kind === InstrumentKind.CONTENT) 
                    break;
            }
        }
        if (i === i0 && root.children[i0].kind === InstrumentKind.CONTENT) {
            for (i = i0 + 1; i < root.children.length; i++) {
                if (root.children[i].kind !== InstrumentKind.EDITIONS && root.children[i].kind !== InstrumentKind.COMMENT) 
                    break;
            }
            if ((i < root.children.length) && ((((this.docTyp === DecreeKind.KODEX || root.kind === InstrumentKind.CLAUSE || root.kind === InstrumentKind.ITEM) || root.kind === InstrumentKind.SUBITEM || root.kind === InstrumentKind.CHAPTER) || root.kind === InstrumentKind.CLAUSEPART))) {
                if (root.children[i].kind === InstrumentKind.LISTITEM || root.children[i].kind === InstrumentKind.ITEM || root.children[i].kind === InstrumentKind.SUBITEM) {
                    let num = 1;
                    root.children[i0].kind = InstrumentKind.INDENTION;
                    root.children[i0].number = num;
                    if (root.children[i].kind === InstrumentKind.LISTITEM) {
                        for (; i < root.children.length; i++) {
                            if (root.children[i].kind === InstrumentKind.LISTITEM) {
                                root.children[i].kind = InstrumentKind.INDENTION;
                                root.children[i].number = ++num;
                            }
                            else if (root.children[i].kind !== InstrumentKind.COMMENT && root.children[i].kind !== InstrumentKind.EDITIONS) 
                                break;
                        }
                    }
                }
            }
        }
        let inds = 1;
        for (i = i0; i < root.children.length; i++) {
            let ch = root.children[i];
            if (ch.kind === InstrumentKind.COMMENT) 
                continue;
            let err = -1;
            let wrapinds2046 = new RefOutArgWrapper(inds);
            let wraperr2047 = new RefOutArgWrapper();
            let lii = this._splitContentByIndents(ch, wrapinds2046, wraperr2047);
            inds = wrapinds2046.value;
            err = wraperr2047.value;
            if (lii === null) {
                if (ch.kind === InstrumentKind.EDITIONS) 
                    continue;
                break;
            }
        }
        if (root.numberString === "61") {
        }
        if (inds > 2 && ((i >= root.children.length || root.children[i].kind !== InstrumentKind.DIRECTIVE))) {
            if (root.number === 7 && root.kind === InstrumentKind.CLAUSEPART) {
            }
            let num = 1;
            let errRegime = false;
            for (i = i0; i < root.children.length; i++) {
                let chh = root.children[i];
                if (chh.kind === InstrumentKind.COMMENT) 
                    continue;
                let err = -1;
                let wrapnum2048 = new RefOutArgWrapper(num);
                let wraperr2049 = new RefOutArgWrapper();
                let lii = this._splitContentByIndents(chh, wrapnum2048, wraperr2049);
                num = wrapnum2048.value;
                err = wraperr2049.value;
                if (lii === null) {
                    if (chh.kind === InstrumentKind.EDITIONS) 
                        continue;
                    break;
                }
                if (lii.length === 0) 
                    continue;
                if (err > 0) 
                    errRegime = true;
                else if (err === 0) 
                    errRegime = false;
                if (errRegime) {
                    for (const vv of lii) {
                        vv.isError = true;
                    }
                }
                root.children.splice(i, 1);
                root.children.splice(i, 0, ...lii);
                i += (lii.length - 1);
            }
            num = 1;
            for (i = i0 + 1; i < root.children.length; i++) {
                let ch = root.children[i];
                if (ch.kind === InstrumentKind.COMMENT || ch.kind === InstrumentKind.EDITIONS) 
                    continue;
                if (ch.itok === null || ch.itok.numbers.length !== 1) 
                    break;
                if (ch.itok.firstNumber !== num) 
                    break;
                num++;
            }
            if (num > 1 && i >= root.children.length) {
                for (i = i0 + 1; i < root.children.length; i++) {
                    let ch = root.children[i];
                    if (ch.kind === InstrumentKind.COMMENT || ch.kind === InstrumentKind.EDITIONS) 
                        continue;
                    if (root.kind === InstrumentKind.CLAUSEPART || root.kind === InstrumentKind.CLAUSE) 
                        ch.kind = InstrumentKind.ITEM;
                    else if (root.kind === InstrumentKind.ITEM) 
                        ch.kind = InstrumentKind.SUBITEM;
                    else 
                        break;
                    NumberingHelper.createNumber(ch, ch.itok);
                    if (ch.children.length === 1 && (ch.children[0].endChar < ch.endChar)) 
                        ch.fillByContentChildren();
                }
            }
        }
        for (i = 0; i < root.children.length; i++) {
            let root0 = root.children[i];
            if (root0.number === 0 || (root0.children.length < 2)) 
                continue;
            let ch = root0.children[root0.children.length - 1];
            if (ch.kind !== InstrumentKind.EDITIONS) 
                continue;
            let num = root0.number + 1;
            let err = -1;
            let wrapnum2050 = new RefOutArgWrapper(num);
            let wraperr2051 = new RefOutArgWrapper();
            let lii = this._splitContentByIndents(ch, wrapnum2050, wraperr2051);
            num = wrapnum2050.value;
            err = wraperr2051.value;
            if (lii === null || lii.length === 0 || lii[0].kind !== root0.kind) 
                continue;
            if (err > 0) {
                for (const vv of lii) {
                    vv.isError = true;
                }
            }
            Utils.removeItem(root0.children, ch);
            root.children.splice(i + 1, 0, ...lii);
            i += lii.length;
        }
        for (const ch of root.children) {
            this._correctKodexParts(ch);
        }
    }
    
    _splitContentByIndents(fr, num, err) {
        err.value = -1;
        if (fr.kind !== InstrumentKind.CONTENT && fr.kind !== InstrumentKind.LISTITEM && fr.kind !== InstrumentKind.PREAMBLE) {
            if (fr.kind !== InstrumentKind.EDITIONS) 
                return null;
            if ((fr.beginToken.isValue("АБЗАЦ", null) || fr.beginToken.isValue("ЧАСТЬ", null) || fr.beginToken.isValue("ПУНКТ", null)) || fr.beginToken.isValue("ПОДПУНКТ", null)) {
                let t = fr.beginToken.next;
                let nnn = Utils.as(t, NumberToken);
                if (nnn === null || nnn.intValue === null) 
                    return null;
                else if (nnn.intValue === num.value) 
                    err.value = 0;
                else if (fr.beginToken.isValue("АБЗАЦ", null)) {
                    num.value = nnn.intValue;
                    err.value = 1;
                }
                else if (nnn.intValue !== (num.value - 1)) {
                    err.value = 1;
                    return null;
                }
                let next = nnn.intValue;
                t = t.next;
                if ((t !== null && t.isHiphen && (t.next instanceof NumberToken)) && t.next.intValue !== null) {
                    next = t.next.intValue;
                    t = t.next.next;
                    if (next <= num.value) 
                        return null;
                }
                if ((t === null || !t.isValue("УТРАТИТЬ", "ВТРАТИТИ") || t.next === null) || !t.next.isValue("СИЛА", "ЧИННІСТЬ")) 
                    return null;
                let res0 = new Array();
                let ki = InstrumentKind.INDENTION;
                if (fr.beginToken.isValue("ПУНКТ", null)) 
                    ki = InstrumentKind.ITEM;
                else if (fr.beginToken.isValue("ПОДПУНКТ", null)) 
                    ki = InstrumentKind.SUBITEM;
                else if (fr.beginToken.isValue("ЧАСТЬ", null)) 
                    ki = InstrumentKind.CLAUSEPART;
                for (let i = num.value; i <= next; i++) {
                    res0.push(FragToken._new2052(fr.beginToken, fr.endToken, ki, i, true, fr.referents));
                }
                num.value = next + 1;
                return res0;
            }
            return new Array();
        }
        if (fr.children.length > 0) 
            return null;
        if (fr.itok === null) 
            fr.itok = InstrToken1.parse(fr.beginToken, true, null, 0, null, false, 0, false, false);
        let res = new Array();
        let t0 = fr.beginToken;
        for (let tt = t0; tt !== null && tt.endChar <= fr.endChar; tt = tt.next) {
            if (tt.endChar === fr.endChar) {
            }
            else if (!tt.isNewlineAfter) 
                continue;
            else if (tt.isTableControlChar) 
                continue;
            else if (!MiscHelper.canBeStartOfSentence(tt.next) && !tt.isCharOf(":;.")) {
                if ((tt.next !== null && tt.next.isChar('[') && tt.next.next !== null) && tt.next.next.isValue("КАРТИНКА", null)) {
                }
                else if (tt.next !== null && tt.next.isValue("ГДЕ", null)) {
                }
                else if (tt.isChar(']')) {
                }
                else if (tt.isComma && tt.previous.isChar(']')) {
                }
                else if ((tt.next !== null && tt.next.lengthChar === 1 && tt.next.next !== null) && tt.next.next.isHiphen) {
                }
                else 
                    continue;
            }
            let re = FragToken._new2024(t0, tt, InstrumentKind.INDENTION, num.value);
            num.value++;
            if (t0 === fr.beginToken && tt === fr.endToken) 
                re.itok = fr.itok;
            if (re.itok === null) 
                re.itok = InstrToken1.parse(t0, true, null, 0, null, false, 0, false, false);
            if (res.length > 100) 
                return null;
            if (re.itok.hasManySpecChars && re.itok.isPureHiphenLine) 
                num.value--;
            else if (re.itok.typ === InstrToken1Types.FOOTNOTE) 
                num.value--;
            else 
                res.push(re);
            t0 = tt.next;
        }
        return res;
    }
    
    _analizePreamble(root) {
        let i = 0;
        let cntCou = 0;
        let ch = null;
        let ok = false;
        if ((root.children.length > 1 && root.children[0].kind === InstrumentKind.CONTENT && root.children[1].number > 0) && root.children[0].children.length > 0) {
            for (i = 0; i < root.children[0].children.length; i++) {
                let ch2 = root.children[0].children[i];
                if ((ch2.kind !== InstrumentKind.CONTENT && ch2.kind !== InstrumentKind.INDENTION && ch2.kind !== InstrumentKind.COMMENT) && ch2.kind !== InstrumentKind.EDITIONS) 
                    break;
            }
            if (i >= root.children[0].children.length) {
                let chh = root.children[0];
                root.children.splice(0, 1);
                root.children.splice(0, 0, ...chh.children);
            }
        }
        for (i = 0; i < root.children.length; i++) {
            ch = root.children[i];
            if (ch.kind === InstrumentKind.EDITIONS || ch.kind === InstrumentKind.COMMENT || ch.kind === InstrumentKind.INDEX) 
                continue;
            if (ch.kind === InstrumentKind.DIRECTIVE) {
                ok = true;
                break;
            }
            if (ch.itok !== null && ch.itok.hasChanges) 
                break;
            if (ch.kind === InstrumentKind.CONTENT && ch.children.length === 1 && ch.children[0].kind === InstrumentKind.INDEX) {
                ch.kind = InstrumentKind.INDEX;
                ch.children = ch.children[0].children;
                continue;
            }
            if (ch.kind === InstrumentKind.CONTENT || ch.kind === InstrumentKind.INDENTION) {
                for (let t = ch.beginToken.next; t !== null && (t.endChar < ch.endChar); t = t.next) {
                    if (t.isNewlineBefore) {
                        if (t.previous.isCharOf(".:;") && t.previous.previous !== null && ((t.previous.previous.isValue("НИЖЕСЛЕДУЮЩИЙ", null) || t.previous.previous.isValue("ДОГОВОР", null)))) {
                            let itt1 = InstrToken1.parse(t, true, null, 0, null, false, 0, false, false);
                            if ((itt1 !== null && !itt1.hasVerb && (itt1.endChar < ch.endChar)) && itt1.typ !== InstrToken1Types.APPENDIX) {
                                let clau = FragToken._new2000(t, ch.endToken, InstrumentKind.CHAPTER);
                                if (((i + 1) < root.children.length) && root.children[i + 1].kind === InstrumentKind.CLAUSE) 
                                    clau.kind = InstrumentKind.CLAUSE;
                                let nam = FragToken._new2005(t, itt1.endToken, InstrumentKind.NAME, true, itt1);
                                clau.children.push(nam);
                                clau.name = FragToken.getRestoredNameMT(nam, false);
                                clau.children.push(FragToken._new2000(itt1.endToken.next, ch.endToken, InstrumentKind.CONTENT));
                                ch.endToken = t.previous;
                                root.children.splice(i + 1, 0, clau);
                            }
                            break;
                        }
                    }
                }
                let pream = false;
                if (ch.beginToken.isValue("ПРЕАМБУЛА", null)) 
                    pream = true;
                else if (ch.lengthChar > 1500) 
                    break;
                cntCou++;
                if (ch.endToken.isChar(':') || pream || ((ch.endToken.previous !== null && ch.endToken.previous.isValue("НИЖЕСЛЕДУЮЩИЙ", null)))) {
                    ok = true;
                    i++;
                    break;
                }
                continue;
            }
            break;
        }
        if (cntCou === 0 || cntCou > 3 || i >= root.children.length) 
            return;
        if (ch.number > 0) 
            ok = true;
        if (!ok) 
            return;
        if (cntCou === 1) {
            for (let j = 0; j < i; j++) {
                if (root.children[j].kind === InstrumentKind.CONTENT || root.children[j].kind === InstrumentKind.INDENTION) {
                    root.children[j].kind = InstrumentKind.PREAMBLE;
                    if (root.children[j].children.length === 1 && root.children[j].children[0].kind === InstrumentKind.INDEX) {
                        root.children[j].kind = InstrumentKind.INDEX;
                        root.children[j].children = root.children[j].children[0].children;
                    }
                }
            }
        }
        else {
            let prm = FragToken._new2000(root.children[0].beginToken, root.children[i - 1].endToken, InstrumentKind.PREAMBLE);
            for (let j = 0; j < i; j++) {
                prm.children.push(root.children[j]);
            }
            root.children.splice(0, i);
            root.children.splice(0, 0, prm);
        }
    }
}


module.exports = ContentAnalyzeWhapper