/*
 * 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 IntOntologyItem = require("./../core/IntOntologyItem");
const Termin = require("./../core/Termin");
const ReferentsEqualType = require("./../core/ReferentsEqualType");
const ReferentClass = require("./../metadata/ReferentClass");
const MetaChemical = require("./MetaChemical");
const Referent = require("./../Referent");

/**
 * Химическая формула
 */
class ChemicalFormulaReferent extends Referent {
    
    constructor() {
        super(ChemicalFormulaReferent.OBJ_TYPENAME);
        this.instanceOf = MetaChemical.GLOBAL_META;
    }
    
    get value() {
        return this.getStringValue(ChemicalFormulaReferent.ATTR_VALUE);
    }
    set value(_value) {
        this.addSlot(ChemicalFormulaReferent.ATTR_VALUE, _value, true, 0);
        return _value;
    }
    
    get name() {
        return this.getStringValue(ChemicalFormulaReferent.ATTR_NAME);
    }
    set name(_value) {
        this.addSlot(ChemicalFormulaReferent.ATTR_NAME, _value, true, 0);
        return _value;
    }
    
    toStringEx(shortVariant, lang = null, lev = 0) {
        let res = this.value;
        if (res === null) {
            let nam = this.name;
            if (nam === null) 
                return "?";
            return nam.toLowerCase();
        }
        if (!shortVariant && this.name !== null) {
            let names = this.getStringValues(ChemicalFormulaReferent.ATTR_NAME);
            let tmp = new StringBuilder();
            tmp.append(res).append(" (");
            for (let i = 0; i < names.length; i++) {
                if (i > 0) 
                    tmp.append(", ");
                tmp.append(names[i].toLowerCase());
            }
            tmp.append(")");
            res = tmp.toString();
        }
        return res;
    }
    
    canBeEquals(obj, typ = ReferentsEqualType.WITHINONETEXT) {
        let cf = Utils.as(obj, ChemicalFormulaReferent);
        if (cf === null) 
            return false;
        if (this.value !== null && cf.value !== null) 
            return this.value === cf.value;
        if (this.name !== null && cf.name !== null) 
            return this.name === cf.name;
        return false;
    }
    
    createOntologyItem() {
        let oi = new IntOntologyItem(this);
        oi.termins.push(new Termin(Utils.notNull(this.value, this.name)));
        return oi;
    }
    
    static static_constructor() {
        ChemicalFormulaReferent.OBJ_TYPENAME = "CHEMICALFORMULA";
        ChemicalFormulaReferent.ATTR_VALUE = "VALUE";
        ChemicalFormulaReferent.ATTR_NAME = "NAME";
    }
}


ChemicalFormulaReferent.static_constructor();

module.exports = ChemicalFormulaReferent