/*
 * 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 ReferentsEqualType = require("./../core/ReferentsEqualType");
const Referent = require("./../Referent");
const MiscHelper = require("./../core/MiscHelper");
const ReferentClass = require("./../metadata/ReferentClass");
const InstrumentParticipantMeta = require("./internal/InstrumentParticipantMeta");

/**
 * Участник НПА (для договора: продавец, агент, исполнитель и т.п.)
 */
class InstrumentParticipantReferent extends Referent {
    
    constructor() {
        super(InstrumentParticipantReferent.OBJ_TYPENAME);
        this.instanceOf = InstrumentParticipantMeta.GLOBAL_META;
    }
    
    get typ() {
        return this.getStringValue(InstrumentParticipantReferent.ATTR_TYPE);
    }
    set typ(value) {
        this.addSlot(InstrumentParticipantReferent.ATTR_TYPE, (value === null ? null : value.toUpperCase()), true, 0);
        return value;
    }
    
    get ground() {
        return this.getSlotValue(InstrumentParticipantReferent.ATTR_GROUND);
    }
    set ground(value) {
        this.addSlot(InstrumentParticipantReferent.ATTR_GROUND, value, false, 0);
        return value;
    }
    
    toStringEx(shortVariant, lang, lev = 0) {
        let res = new StringBuilder();
        res.append(MiscHelper.convertFirstCharUpperAndOtherLower(Utils.notNull(this.typ, "?")));
        let org = Utils.as(this.getSlotValue(InstrumentParticipantReferent.ATTR_REF), Referent);
        let del = Utils.as(this.getSlotValue(InstrumentParticipantReferent.ATTR_DELEGATE), Referent);
        if (org !== null) {
            res.append(": ").append(org.toStringEx(shortVariant, lang, 0));
            if (!shortVariant && del !== null) 
                res.append(" (в лице ").append(del.toStringEx(true, lang, lev + 1)).append(")");
        }
        else if (del !== null) 
            res.append(": в лице ").append(del.toStringEx(shortVariant, lang, lev + 1));
        return res.toString();
    }
    
    canBeEquals(obj, _typ = ReferentsEqualType.WITHINONETEXT) {
        let p = Utils.as(obj, InstrumentParticipantReferent);
        if (p === null) 
            return false;
        if (this.typ !== p.typ) 
            return false;
        let re1 = Utils.as(this.getSlotValue(InstrumentParticipantReferent.ATTR_REF), Referent);
        let re2 = Utils.as(obj.getSlotValue(InstrumentParticipantReferent.ATTR_REF), Referent);
        if (re1 !== null && re2 !== null) {
            if (!re1.canBeEquals(re2, _typ)) 
                return false;
        }
        return true;
    }
    
    containsRef(r) {
        for (const s of this.slots) {
            if (((s.typeName === InstrumentParticipantReferent.ATTR_REF || s.typeName === InstrumentParticipantReferent.ATTR_DELEGATE)) && (s.value instanceof Referent)) {
                if (r === s.value || r.canBeEquals(Utils.as(s.value, Referent), ReferentsEqualType.WITHINONETEXT)) 
                    return true;
            }
        }
        return false;
    }
    
    static _new2166(_arg1) {
        let res = new InstrumentParticipantReferent();
        res.typ = _arg1;
        return res;
    }
    
    static static_constructor() {
        InstrumentParticipantReferent.OBJ_TYPENAME = "INSTRPARTICIPANT";
        InstrumentParticipantReferent.ATTR_TYPE = "TYPE";
        InstrumentParticipantReferent.ATTR_REF = "REF";
        InstrumentParticipantReferent.ATTR_DELEGATE = "DELEGATE";
        InstrumentParticipantReferent.ATTR_GROUND = "GROUND";
    }
}


InstrumentParticipantReferent.static_constructor();

module.exports = InstrumentParticipantReferent