/*
 * 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 MorphGender = require("./../../../morph/MorphGender");
const LanguageHelper = require("./../../../morph/LanguageHelper");
const BracketHelper = require("./../../core/BracketHelper");
const PersonNormalItemType = require("./PersonNormalItemType");

class PersonNormalItem {
    
    constructor() {
        this.typ = PersonNormalItemType.LAST;
        this.ind0 = 0;
        this.ind1 = 0;
        this.src = null;
        this.gender = 0;
        this.rnd0 = 0;
        this.rnd1 = 0;
        this.render = 0;
        this.resultAlt = null;
    }
    
    toString() {
        if ((this.rnd0 < 0) || (this.rnd1 < this.rnd0) || this.src === null) 
            return "?";
        if (this.rnd0 === this.rnd1) 
            return (String(this.typ) + ": " + this.src[this.rnd0].value);
        let res = new StringBuilder();
        res.append(String(this.typ)).append(": ");
        for (let i = this.rnd0; i <= this.rnd1; i++) {
            res.append((i > this.rnd0 ? "-" : "")).append(this.src[i].value);
        }
        return res.toString();
    }
    
    get result() {
        this.resultAlt = null;
        if ((this.rnd0 < 0) || (this.rnd1 < this.rnd0) || this.src === null) 
            return null;
        if ((this.rnd0 + 1) === this.rnd1 && this.typ !== PersonNormalItemType.LAST) {
            if (this.src[this.rnd0].value.length === 1 && this.src[this.rnd1].value.length > 1) {
                if (!this.src[this.rnd0].endToken.next.isHiphen) 
                    return this.src[this.rnd1].value;
            }
            if (this.src[this.rnd0].value.length > 1 && this.src[this.rnd1].value.length === 1) 
                return this.src[this.rnd0].value;
        }
        let res = this.src[this.rnd0].value;
        for (let i = this.rnd0 + 1; i <= this.rnd1; i++) {
            if (this.typ === PersonNormalItemType.LAST && (this.rnd0 + 1) === this.rnd1 && res.length === 1) {
                if (res === "Д" || res === "О") {
                    res = (res + "'" + this.src[i].value);
                    continue;
                }
                if (!LanguageHelper.isCyrillicVowel(res[0])) {
                    res = (res + this.src[i].value);
                    continue;
                }
            }
            if (i === this.rnd1 && !this.src[i].endToken.previous.isHiphen) {
                let ii = 0;
                let v = this.src[i].value;
                for (ii = 1; ii < v.length; ii++) {
                    if (v[ii] !== v[0]) 
                        break;
                }
                if (ii >= v.length) 
                    continue;
            }
            res = (res + "-" + this.src[i].value);
        }
        if (this.typ === PersonNormalItemType.FIRST && this.rnd0 === this.rnd1) {
            let pit = this.src[this.rnd0];
            if (pit.firstname !== null && pit.firstname.isInDictionary && pit.firstname.vars.length > 0) {
                for (const v of pit.firstname.vars) {
                    if (v.gender === MorphGender.MASCULINE && this.render === 1) {
                        let exi = false;
                        for (const vv of pit.firstname.vars) {
                            if (vv.gender === MorphGender.MASCULINE && this.render === 1 && vv.value === pit.value) 
                                exi = true;
                        }
                        if ((!exi && v.value !== pit.value && pit.value !== "ВЛАД") && pit.value !== "АЛЕКС") {
                            this.resultAlt = pit.value;
                            return v.value;
                        }
                    }
                    else if (v.gender === MorphGender.FEMINIE && this.render === 2) {
                        let exi = false;
                        for (const vv of pit.firstname.vars) {
                            if (vv.gender === MorphGender.FEMINIE && this.render === 2 && vv.value === pit.value) 
                                exi = true;
                        }
                        if ((!exi && v.value !== pit.value && pit.value !== "КАТЕРИНА") && pit.value !== "МАРЬЯ" && pit.value !== "МАРИЯ") {
                            this.resultAlt = pit.value;
                            if (pit.value === "ЛЕНА") 
                                return "ЕЛЕНА";
                            return v.value;
                        }
                    }
                }
            }
        }
        if ((this.typ === PersonNormalItemType.LAST && this.rnd0 === this.rnd1 && ((res[0] === 'О' || res[0] === 'Д'))) && res.length > 3) {
            let pit = this.src[this.rnd0];
            let txt = pit.getSourceText();
            for (let i = 0; (i < txt.length) && (i < 3); i++) {
                if (BracketHelper.isBracketChar(txt[i], false)) {
                    res = (res[0] + "'" + res.substring(1));
                    break;
                }
            }
        }
        return res;
    }
    
    init(_src) {
        this.ind0 = 0;
        this.ind1 = -1;
        this.rnd0 = (this.rnd1 = -1);
        this.src = _src;
        this.render = 0;
        this.gender = 0;
    }
    
    fix() {
        this.rnd0 = this.ind0;
        this.rnd1 = this.ind1;
        this.render = this.gender;
    }
    
    calcCoef() {
        if (this.ind1 < this.ind0) {
            if (this.typ === PersonNormalItemType.MIDDLE) 
                return 1;
            return 0;
        }
        if (this.ind1 > this.ind0) {
            for (let i = this.ind0; i < this.ind1; i++) {
                if (this.src[i].whitespacesAfterCount >= 4) 
                    return 0;
            }
        }
        if (this.ind0 > 0 && this.src[this.ind0].beginToken.previous.isHiphen) {
            if (this.src[this.ind0].whitespacesBeforeCount < 4) 
                return 0;
        }
        if (((this.ind1 + 1) < this.src.length) && this.src[this.ind1].endToken.next.isHiphen) {
            if (this.src[this.ind1].endToken.next.whitespacesAfterCount < 4) 
                return 0;
        }
        let co = 1;
        for (let i = this.ind0; i <= this.ind1; i++) {
            let pit = this.src[i];
            if (this.typ === PersonNormalItemType.FIRST) {
                if (this.ind0 === this.ind1 && pit.value.length === 1) {
                }
                else if (pit.firstname !== null) {
                    if (this.gender === 2 && pit.firstname.morph.gender === MorphGender.FEMINIE) {
                    }
                    else if (this.gender === 1 && pit.firstname.morph.gender === MorphGender.MASCULINE) {
                    }
                    else 
                        co *= 0.98;
                }
                else if (pit.middlename !== null) 
                    co *= 0.8;
                else if (pit.lastname !== null && ((pit.lastname.isInDictionary || pit.lastname.isLastnameHasStdTail))) 
                    co *= 0.8;
                else if (pit.lastname !== null && pit.lastname.isLastnameHasStdTail) 
                    co *= 0.9;
                else if (this.gender === 2 && pit.morph.gender === MorphGender.FEMINIE) 
                    co *= 0.98;
                else if (this.gender === 1 && pit.morph.gender === MorphGender.MASCULINE) 
                    co *= 0.98;
                else 
                    co *= 0.7;
            }
            else if (this.typ === PersonNormalItemType.MIDDLE) {
                if (this.ind0 === this.ind1 && pit.value.length === 1) {
                }
                else if (pit.middlename !== null) {
                    if (this.gender === 2 && pit.middlename.morph.gender === MorphGender.FEMINIE) {
                    }
                    else if (this.gender === 1 && pit.middlename.morph.gender === MorphGender.MASCULINE) {
                    }
                    else 
                        co *= 0.7;
                }
                else if (((pit.value.endsWith("ВНА") || pit.value.endsWith("ЧНА") || pit.value.endsWith("КЫЗЫ"))) && this.gender === 2) {
                }
                else if (((pit.value.endsWith("ИЧ") || pit.value.endsWith("ОГЛЫ"))) && this.gender === 1) 
                    co *= 0.99;
                else if (pit.firstname !== null) 
                    co *= 0.98;
                else if (((this.gender === 2 && pit.value.endsWith("ВА") && this.ind0 === this.ind1) && this.ind0 === (this.src.length - 1) && this.src[0].lastname !== null) && this.src[0].value.length > 2) 
                    co *= 0.99;
                else if (pit.lastname !== null && pit.lastname.isInDictionary) 
                    co *= 0.8;
                else 
                    co *= 0.7;
            }
            else if (this.typ === PersonNormalItemType.LAST) {
                if (pit.value.endsWith("КЫЗЫ")) 
                    return 0;
                if (this.ind0 === this.ind1 && pit.value.length === 1) {
                    if (LanguageHelper.isCyrillicVowel(pit.value[0])) 
                        co *= 0.5;
                    else 
                        return 0;
                }
                if (pit.value.endsWith("ВНА") || pit.value.endsWith("ЧНА")) {
                    if (this.gender === 2) {
                        if (this.ind0 === 0) 
                            co *= 0.7;
                        else 
                            co *= 0.2;
                    }
                    else 
                        co *= 0.3;
                }
                else if ((this.gender === 2 && pit.value.endsWith("ВА") && this.ind0 === this.ind1) && this.ind0 === 2 && this.src[0].lastname !== null) 
                    co *= 0.3;
                else if (pit.value.endsWith("ИЧ")) {
                    if ((i + 1) >= this.src.length) 
                        co *= 0.3;
                    else if (this.ind0 === 0 || this.gender === 2) {
                    }
                    else 
                        co *= 0.85;
                }
                else if (pit.value.endsWith("ОГЛЫ")) 
                    co *= 0.3;
                else if (pit.lastname !== null && ((pit.lastname.isInDictionary || pit.lastname.isLastnameHasStdTail))) {
                    if (this.gender === 2 && pit.lastname.morph.gender === MorphGender.FEMINIE) {
                    }
                    else if (this.gender === 1 && pit.lastname.morph.gender === MorphGender.MASCULINE) {
                    }
                    else 
                        co *= 0.98;
                }
                else if (pit.firstname !== null || pit.middlename !== null) 
                    co *= 0.8;
                else 
                    co *= 0.98;
            }
        }
        if ((this.typ === PersonNormalItemType.LAST && this.ind0 === 0 && this.ind1 > (this.ind0 + 1)) && (co < 0.6)) 
            co *= 1.5;
        return co;
    }
    
    static _new3450(_arg1) {
        let res = new PersonNormalItem();
        res.typ = _arg1;
        return res;
    }
}


module.exports = PersonNormalItem