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

const SemFraglink = require("./SemFraglink");
const ISemContainer = require("./ISemContainer");
const SemGraph = require("./SemGraph");
const SemDocument = require("./SemDocument");

/**
 * Блок документа (абзац)
 * 
 */
class SemBlock extends ISemContainer {
    
    constructor(blk) {
        super();
        this.m_Graph = new SemGraph();
        this.m_Higher = null;
        this.fragments = new Array();
        this.links = new Array();
        this.m_Higher = blk;
    }
    
    get graph() {
        return this.m_Graph;
    }
    
    get higher() {
        return this.m_Higher;
    }
    
    get document() {
        return Utils.as(this.m_Higher, SemDocument);
    }
    
    get beginChar() {
        return (this.fragments.length === 0 ? 0 : this.fragments[0].beginChar);
    }
    
    get endChar() {
        return (this.fragments.length === 0 ? 0 : this.fragments[this.fragments.length - 1].endChar);
    }
    
    addFragments(blk) {
        for (const fr of blk.fragments) {
            fr.m_Higher = this;
            this.fragments.push(fr);
        }
        for (const li of blk.links) {
            this.links.push(li);
        }
    }
    
    addFragmentVariantas(blk) {
        for (const fr of blk.fragments) {
            for (const fr1 of this.fragments) {
                if (fr1.beginChar === fr.beginChar && fr1.endChar === fr.endChar) {
                    fr1.altFragments.push(fr);
                    fr.m_Higher = this;
                    break;
                }
            }
        }
    }
    
    addLink(typ, src, tgt, ques = null) {
        for (const li of this.links) {
            if (li.typ === typ && li.source === src && li.target === tgt) 
                return li;
        }
        let res = SemFraglink._new3859(typ, src, tgt, ques);
        this.links.push(res);
        return res;
    }
    
    mergeWith(blk) {
        this.graph.mergeWith(blk.graph);
        for (const fr of blk.fragments) {
            this.fragments.push(fr);
            fr.m_Higher = this;
        }
        for (const li of blk.links) {
            this.links.push(li);
        }
    }
    
    toString() {
        let tmp = new StringBuilder();
        for (const fr of this.fragments) {
            let spel = fr.spelling;
            if (spel.length > 20) 
                spel = spel.substring(0, 0 + 20) + "...";
            tmp.append("[").append(spel).append("] ");
        }
        return tmp.toString();
    }
}


module.exports = SemBlock