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

class SingleNumComparer {
    
    constructor() {
        this.typ = ComplexNumCompareType.UNCOMPARABLE;
        this.rank = 0;
        this.delta = 0;
        this.first = null;
        this.second = null;
        this.val1 = null;
        this.val2 = null;
    }
    
    toString() {
        let res = new StringBuilder();
        if (this.rank > 0) 
            res.append(this.rank).append(": ");
        res.append(this.first);
        if (this.typ === ComplexNumCompareType.UNCOMPARABLE) 
            res.append(" ?? ");
        else if (this.typ === ComplexNumCompareType.EQUALS) 
            res.append(" == ");
        else if (this.typ === ComplexNumCompareType.LESS) 
            res.append(" < ");
        else if (this.typ === ComplexNumCompareType.GREAT) 
            res.append(" > ");
        res.append(this.second);
        if (this.delta > 0) 
            res.append(", Delt=").append(this.delta);
        return res.toString();
    }
    
    process(fir, sec) {
        this.first = fir;
        this.second = sec;
        this.typ = ComplexNumCompareType.UNCOMPARABLE;
        this.delta = 0;
        this.rank = 0;
        this.val1 = null;
        this.val2 = null;
        for (const v1 of fir.vals) {
            for (const v2 of sec.vals) {
                if (v1.typ !== v2.typ) 
                    continue;
                let _rank = 1;
                let ty = ComplexNumCompareType.UNCOMPARABLE;
                let delt = 0;
                if (v1.typ === SingleNumValueType.LETTER) {
                    let ch1 = v1.letter.toUpperCase();
                    let ch2 = v2.letter.toUpperCase();
                    if (LanguageHelper.isCyrillicChar(ch1) !== LanguageHelper.isCyrillicChar(ch2)) {
                        if (LanguageHelper.isCyrillicChar(ch1)) {
                            let ch11 = LanguageHelper.getLatForCyr(ch1);
                            if ((ch11.charCodeAt(0)) !== 0) 
                                ch1 = ch11;
                            else {
                                let ch22 = LanguageHelper.getCyrForLat(ch2);
                                if ((ch22.charCodeAt(0)) !== 0) 
                                    ch2 = ch22;
                            }
                        }
                        else {
                            let ch11 = LanguageHelper.getCyrForLat(ch1);
                            if ((ch11.charCodeAt(0)) !== 0) 
                                ch1 = ch11;
                            else {
                                let ch22 = LanguageHelper.getLatForCyr(ch2);
                                if ((ch22.charCodeAt(0)) !== 0) 
                                    ch2 = ch22;
                            }
                        }
                    }
                    if ((ch1.charCodeAt(0)) < (ch2.charCodeAt(0))) {
                        ty = ComplexNumCompareType.LESS;
                        delt = (ch2.charCodeAt(0)) - (ch1.charCodeAt(0));
                        if (ch1 === 'И' && ch2 === 'К') 
                            delt = 1;
                        else if (ch1 === 'Е' && ch2 === 'Ж') 
                            delt = 1;
                    }
                    else if ((ch1.charCodeAt(0)) > (ch2.charCodeAt(0))) {
                        ty = ComplexNumCompareType.GREAT;
                        delt = (ch1.charCodeAt(0)) - (ch2.charCodeAt(0));
                        if (ch2 === 'И' && ch1 === 'К') 
                            delt = 1;
                        else if (ch2 === 'Е' && ch1 === 'Ж') 
                            delt = 1;
                    }
                    else 
                        ty = ComplexNumCompareType.EQUALS;
                }
                else if (v1.val < v2.val) {
                    ty = ComplexNumCompareType.LESS;
                    delt = v2.val - v1.val;
                }
                else if (v1.val > v2.val) {
                    ty = ComplexNumCompareType.GREAT;
                    delt = v1.val - v2.val;
                }
                else 
                    ty = ComplexNumCompareType.EQUALS;
                if (ty === ComplexNumCompareType.GREAT) 
                    _rank /= (2);
                if (_rank > this.rank) {
                    this.rank = _rank;
                    this.typ = ty;
                    this.delta = delt;
                    this.val1 = v1;
                    this.val2 = v2;
                    if (this.delta > 1) 
                        this.rank *= 0.98;
                }
            }
        }
        if (fir.suffix !== null && sec.suffix !== null) {
            if (fir.suffix !== sec.suffix) 
                this.rank *= 0.8;
        }
        else if (fir.suffix !== null || sec.suffix !== null) 
            this.rank *= 0.9;
    }
}


module.exports = SingleNumComparer