/*
 * 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 MorphNumber = require("./../../morph/MorphNumber");
const MorphGender = require("./../../morph/MorphGender");
const SemObjectType = require("./../SemObjectType");
const SemLinkType = require("./../SemLinkType");

class AnaforHelper {
    
    static processAnafors(objs) {
        for (let i = objs.length - 1; i >= 0; i--) {
            let it = objs[i];
            if (it.typ === SemObjectType.PERSONALPRONOUN) {
            }
            else if (it.morph.normalFull === "КОТОРЫЙ" && it.linksFrom.length === 0) {
            }
            else 
                continue;
            let vars = new Array();
            for (let j = i - 1; j >= 0; j--) {
                let a = AnaforHelper.AnaforLink.tryCreate(it, objs[j]);
                if (a === null) 
                    continue;
                vars.push(a);
                a.correct();
            }
            if (vars.length < 1) 
                continue;
            AnaforHelper.AnaforLink.sort(vars);
            if (vars[0].coef <= 0.1) 
                continue;
            if (vars[0].targetList !== null) {
                for (const tgt of vars[0].targetList) {
                    it.graph.addLink(SemLinkType.ANAFOR, it, tgt, null, false, null);
                }
            }
            else {
                let li = it.graph.addLink(SemLinkType.ANAFOR, it, vars[0].target, null, false, null);
                if (vars.length > 1 && vars[0].coef <= (vars[1].coef * (2)) && vars[1].targetList === null) {
                    let li1 = it.graph.addLink(SemLinkType.ANAFOR, it, vars[1].target, null, false, null);
                    li1.altLink = li;
                    li.altLink = li1;
                }
            }
        }
        return false;
    }
}


AnaforHelper.AnaforLink = class  {
    
    constructor() {
        this.coef = 0;
        this.target = null;
        this.targetList = null;
    }
    
    toString() {
        if (this.targetList === null) 
            return (String(this.coef) + ": " + this.target);
        let tmp = new StringBuilder();
        tmp.append(this.coef).append(": ");
        for (const v of this.targetList) {
            tmp.append(v).append("; ");
        }
        return tmp.toString();
    }
    
    static tryCreate(src, tgt) {
        const MorphGender = require("./../../morph/MorphGender");
        const MorphNumber = require("./../../morph/MorphNumber");
        const SemObjectType = require("./../SemObjectType");
        if (tgt.typ !== SemObjectType.NOUN) 
            return null;
        if (((src.morph.number.value()) & (MorphNumber.PLURAL.value())) === (MorphNumber.PLURAL.value())) {
            if (((tgt.morph.number.value()) & (MorphNumber.PLURAL.value())) !== (MorphNumber.UNDEFINED.value())) 
                return AnaforHelper.AnaforLink._new3795(1, tgt);
            let res = AnaforHelper.AnaforLink._new3795(0.5, tgt);
            res.targetList = new Array();
            for (const li of tgt.linksTo) {
                let frm = li.source;
                for (let i = 0; i < frm.linksFrom.length; i++) {
                    res.targetList.splice(0, res.targetList.length);
                    let li0 = frm.linksFrom[i];
                    if (li0.target.typ !== SemObjectType.NOUN) 
                        continue;
                    res.targetList.push(li0.target);
                    for (let j = i + 1; j < frm.linksFrom.length; j++) {
                        let li1 = frm.linksFrom[j];
                        if (li1.typ === li0.typ && li1.preposition === li0.preposition && li1.target.typ === li0.target.typ) 
                            res.targetList.push(li1.target);
                    }
                    if (res.targetList.length > 1) 
                        return res;
                }
            }
            return null;
        }
        if (tgt.morph.number !== MorphNumber.UNDEFINED && ((tgt.morph.number.value()) & (MorphNumber.SINGULAR.value())) === (MorphNumber.UNDEFINED.value())) 
            return null;
        if (tgt.morph.gender !== MorphGender.UNDEFINED) {
            if (((tgt.morph.gender.value()) & (src.morph.gender.value())) === (MorphGender.UNDEFINED.value())) 
                return null;
            return AnaforHelper.AnaforLink._new3795(1, tgt);
        }
        return AnaforHelper.AnaforLink._new3795(0.1, tgt);
    }
    
    static sort(li) {
        for (let i = 0; i < li.length; i++) {
            let ch = false;
            for (let j = 0; j < (li.length - 1); j++) {
                if (li[j].compareTo(li[j + 1]) > 0) {
                    let a = li[j];
                    li[j] = li[j + 1];
                    li[j + 1] = a;
                    ch = true;
                }
            }
            if (!ch) 
                break;
        }
    }
    
    correct() {
        const SemLinkType = require("./../SemLinkType");
        for (const li of this.target.linksTo) {
            if (li.typ === SemLinkType.NAMING) 
                this.coef = 0;
            else if (li.typ === SemLinkType.AGENT) 
                this.coef *= (2);
            else if (li.typ === SemLinkType.PACIENT) {
                if (li.altLink === null) 
                    this.coef *= (2);
            }
            else if (!Utils.isNullOrEmpty(li.preposition)) 
                this.coef /= (2);
        }
    }
    
    compareTo(other) {
        if (this.coef > other.coef) 
            return -1;
        if (this.coef < other.coef) 
            return 1;
        return 0;
    }
    
    static _new3795(_arg1, _arg2) {
        let res = new AnaforHelper.AnaforLink();
        res.coef = _arg1;
        res.target = _arg2;
        return res;
    }
}


module.exports = AnaforHelper