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

const SentItemType = require("./SentItemType");
const NGLinkType = require("./NGLinkType");

class SentenceVariant {
    
    constructor() {
        this.coef = 0;
        this.segs = new Array();
    }
    
    toString() {
        let tmp = new StringBuilder();
        tmp.append(this.coef).append(": ");
        for (const s of this.segs) {
            if (s !== this.segs[0]) 
                tmp.append("; \r\n");
            if (s !== null) 
                tmp.append(s.toString());
            else 
                tmp.append("null");
        }
        return tmp.toString();
    }
    
    calcCoef() {
        this.coef = 0;
        for (let i = 0; i < this.segs.length; i++) {
            if (this.segs[i] !== null) 
                this.coef += this.segs[i].coef;
        }
        for (let i = 0; i < (this.segs.length - 1); i++) {
            let seg0 = this.segs[i];
            if (seg0 === null) 
                continue;
            let seg1 = this.segs[i + 1];
            if (seg1 === null) 
                continue;
            let hasAgent = false;
            let hasPacient = false;
            for (const li of seg0.links) {
                if (li !== null && li.toVerb === seg1.source.beforeVerb) {
                    if (li.typ === NGLinkType.AGENT) 
                        hasAgent = true;
                    else if (li.typ === NGLinkType.PACIENT) {
                        hasPacient = true;
                        for (const lii of li.from.links) {
                            if ((lii !== null && lii.typ === NGLinkType.AGENT && lii.coef >= li.coef) && lii.toVerb === li.toVerb) {
                                for (const liii of seg1.links) {
                                    if (liii !== null && liii.toVerb === li.toVerb && liii.typ === NGLinkType.AGENT) {
                                        if (liii.coef < ((lii.coef / (3)))) 
                                            return this.coef = -1;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            for (const li of seg1.links) {
                if (li !== null && li.toVerb === seg1.source.beforeVerb) {
                    if (li.typ === NGLinkType.AGENT && hasAgent) 
                        return this.coef = -1;
                    else if (li.typ === NGLinkType.PACIENT && hasPacient) 
                        return this.coef = -1;
                }
            }
        }
        return this.coef;
    }
    
    compareTo(other) {
        if (this.coef > other.coef) 
            return -1;
        if (this.coef < other.coef) 
            return 1;
        return 0;
    }
    
    createAltLinks() {
        let coef0 = this.coef;
        for (let i = 0; i < this.segs.length; i++) {
            let seg = this.segs[i];
            if (seg === null) 
                continue;
            for (let j = 0; j < seg.links.length; j++) {
                let li = seg.links[j];
                if (li === null || li.typ === NGLinkType.LIST) 
                    continue;
                if (li.from.links.length < 2) 
                    continue;
                if (li.from.source.typ === SentItemType.FORMULA) 
                    continue;
                if (li.to !== null && li.to.source.typ === SentItemType.FORMULA) 
                    continue;
                for (const l of li.from.links) {
                    if (l !== li && l.typ !== NGLinkType.LIST) {
                        if (l.to !== null && l.to.source.typ === SentItemType.FORMULA) 
                            continue;
                        if (l.typ === NGLinkType.ACTANT) {
                            if (li.typ === NGLinkType.AGENT || li.typ === NGLinkType.PACIENT) 
                                continue;
                        }
                        seg.links[j] = l;
                        seg.calcCoef();
                        let _coef = coef0 - (100);
                        if (seg.coef > 0) {
                            this.calcCoef();
                            _coef = this.coef;
                        }
                        this.coef = coef0;
                        seg.links[j] = li;
                        seg.calcCoef();
                        if (_coef >= this.coef) {
                            li.altLink = l;
                            break;
                        }
                    }
                }
            }
        }
    }
}


module.exports = SentenceVariant