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

class PersonNormalNode {
    
    constructor(fioLast) {
        this.items = new Array();
        if (!fioLast) 
            this.items.push(PersonNormalItem._new3450(PersonNormalItemType.LAST));
        this.items.push(PersonNormalItem._new3450(PersonNormalItemType.FIRST));
        this.items.push(PersonNormalItem._new3450(PersonNormalItemType.MIDDLE));
        if (fioLast) 
            this.items.push(PersonNormalItem._new3450(PersonNormalItemType.LAST));
    }
    
    static isAsian(str) {
        let gl = 0;
        for (const ch of str) {
            if (LanguageHelper.isCyrillicVowel(ch)) 
                gl++;
        }
        if (gl === 1) 
            return true;
        return false;
    }
    
    process(src) {
        let i = 0;
        for (i = 0; i < this.items.length; i++) {
            this.items[i].init(src);
        }
        if ((src.length === 3 && PersonNormalNode.isAsian(src[0].value) && PersonNormalNode.isAsian(src[1].value)) && PersonNormalNode.isAsian(src[2].value)) {
            if (this.items[0].typ === PersonNormalItemType.FIRST) 
                return 0;
            for (i = 0; i < 3; i++) {
                this.items[i].rnd0 = (this.items[i].rnd1 = i);
            }
            return 1;
        }
        let bestCoef = 0;
        for (let fem = 0; fem < 2; fem++) {
            for (let i0 = 0; i0 < (src.length - 2); i0++) {
                this.items[0].ind0 = 0;
                this.items[0].ind1 = i0;
                this.items[0].gender = fem + 1;
                let co = this.items[0].calcCoef();
                if (co <= bestCoef) 
                    continue;
                for (let i1 = i0 + 1; i1 < (src.length - 1); i1++) {
                    this.items[1].ind0 = i0 + 1;
                    this.items[1].ind1 = i1;
                    this.items[1].gender = fem + 1;
                    let co1 = this.items[1].calcCoef();
                    if ((co * co1) <= bestCoef) 
                        continue;
                    for (let i2 = i1 + 1; i2 < src.length; i2++) {
                        this.items[2].ind0 = i1 + 1;
                        this.items[2].ind1 = i2;
                        this.items[2].gender = fem + 1;
                        let co2 = this.items[2].calcCoef();
                        co2 *= (co1 * co);
                        for (i = i2 + 1; i < src.length; i++) {
                            if (src[i].chars.isAllLower) 
                                co2 *= 0.9;
                            else 
                                co2 *= 0.3;
                        }
                        if (co2 > bestCoef) {
                            bestCoef = co2;
                            for (i = 0; i < this.items.length; i++) {
                                this.items[i].fix();
                            }
                        }
                    }
                }
            }
        }
        if (bestCoef === 0 && src.length === 2) {
            for (i = 0; i < this.items.length; i++) {
                this.items[i].init(src);
            }
            for (let fem = 0; fem < 2; fem++) {
                for (let i0 = 0; i0 < (src.length - 1); i0++) {
                    this.items[0].ind0 = 0;
                    this.items[0].ind1 = i0;
                    this.items[0].gender = fem + 1;
                    let co = this.items[0].calcCoef();
                    if (co <= bestCoef) 
                        continue;
                    let k = 1;
                    if (this.items[k].typ === PersonNormalItemType.MIDDLE) 
                        k++;
                    for (let i1 = i0 + 1; i1 < src.length; i1++) {
                        this.items[k].ind0 = i0 + 1;
                        this.items[k].ind1 = i1;
                        this.items[k].gender = fem + 1;
                        let co1 = this.items[k].calcCoef();
                        if ((co * co1) <= bestCoef) 
                            continue;
                        bestCoef = co * co1;
                        for (i = 0; i < this.items.length; i++) {
                            this.items[i].fix();
                        }
                    }
                }
            }
        }
        return bestCoef;
    }
    
    createResult(res) {
        res.gender = this.items[0].render;
        for (const it of this.items) {
            if (it.typ === PersonNormalItemType.FIRST) {
                res.firstname = it.result;
                res.firstnameAlt = it.resultAlt;
            }
            else if (it.typ === PersonNormalItemType.MIDDLE) 
                res.middlename = it.result;
            else if (it.typ === PersonNormalItemType.LAST) 
                res.lastname = it.result;
        }
    }
}


module.exports = PersonNormalNode