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

/**
 * Семантический граф
 * 
 */
class SemGraph {
    
    constructor() {
        this.owner = null;
        this.objects = new Array();
        this.links = new Array();
    }
    
    get higher() {
        if (this.owner !== null && this.owner.higher !== null) 
            return this.owner.higher.graph;
        else 
            return null;
    }
    
    toString() {
        let tmp = new StringBuilder();
        tmp.append(this.objects.length).append("obj ").append(this.links.length).append("links: ");
        for (const li of this.links) {
            if (li !== this.links[0]) 
                tmp.append("; ");
            tmp.append(li);
            if (tmp.length > 100) 
                break;
        }
        if (this.links.length === 0) {
            for (const o of this.objects) {
                if (o !== this.objects[0]) 
                    tmp.append("; ");
                tmp.append(o);
                if (tmp.length > 100) 
                    break;
            }
        }
        return tmp.toString();
    }
    
    addLink(typ, src, tgt, ques = null, or = false, prep = null) {
        if (src === null || tgt === null) 
            return null;
        for (const li of src.graph.links) {
            if (li.typ === typ && li.source === src && li.target === tgt) 
                return li;
        }
        if (src.graph !== tgt.graph) {
            for (const li of tgt.graph.links) {
                if (li.typ === typ && li.source === src && li.target === tgt) 
                    return li;
            }
        }
        if (tgt.morph.normalCase === "ДОМ") {
        }
        let res = SemLink._new3860(this, src, tgt, typ, ques, or, prep);
        this.links.push(res);
        return res;
    }
    
    removeLink(li) {
        if (this.links.includes(li)) 
            Utils.removeItem(this.links, li);
        if (li.source.linksFrom.includes(li)) 
            Utils.removeItem(li.source.linksFrom, li);
        if (li.target.linksTo.includes(li)) 
            Utils.removeItem(li.target.linksTo, li);
        if (li.altLink !== null && li.altLink.altLink === li) 
            li.altLink.altLink = null;
    }
    
    mergeWith(gr) {
        for (const o of gr.objects) {
            if (!this.objects.includes(o)) {
                this.objects.push(o);
                o.graph = this;
            }
        }
        for (const li of gr.links) {
            if (!this.links.includes(li)) 
                this.links.push(li);
        }
    }
    
    removeObject(obj) {
        for (const li of obj.linksFrom) {
            if (li.target.linksTo.includes(li)) 
                Utils.removeItem(li.target.linksTo, li);
            if (this.links.includes(li)) 
                Utils.removeItem(this.links, li);
            else if (li.target.graph.links.includes(li)) 
                Utils.removeItem(li.target.graph.links, li);
        }
        for (const li of obj.linksTo) {
            if (li.source.linksFrom.includes(li)) 
                Utils.removeItem(li.source.linksFrom, li);
            if (this.links.includes(li)) 
                Utils.removeItem(this.links, li);
            else if (li.source.graph.links.includes(li)) 
                Utils.removeItem(li.source.graph.links, li);
        }
        if (this.objects.includes(obj)) 
            Utils.removeItem(this.objects, obj);
    }
}


module.exports = SemGraph