/*
 * 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 ReferentClass = require("./../metadata/ReferentClass");
const DateReferent = require("./../date/DateReferent");
const LinkType = require("./LinkType");
const MetaLink = require("./MetaLink");
const Referent = require("./../Referent");

/**
 * Связь сущностей
 */
class LinkReferent extends Referent {
    
    constructor() {
        super(LinkReferent.OBJ_TYPENAME);
        this.instanceOf = MetaLink.GLOBAL_META;
    }
    
    get typ() {
        let str = this.getStringValue(LinkReferent.ATTR_TYPE);
        if (str === null) 
            return LinkType.UNDEFINED;
        try {
            return LinkType.of(str);
        } catch (ex2310) {
        }
        return LinkType.UNDEFINED;
    }
    set typ(value) {
        this.addSlot(LinkReferent.ATTR_TYPE, value.toString().toLowerCase(), true, 0);
        return value;
    }
    
    get typString() {
        let val = this.getStringValue(LinkReferent.ATTR_TYPE);
        if (val === null) 
            return null;
        let res = Utils.notNull(MetaLink.TYPES.convertInnerValueToOuterValue(val, null), val);
        return res.toLowerCase();
    }
    
    get param() {
        return this.getStringValue(LinkReferent.ATTR_PARAM);
    }
    set param(value) {
        this.addSlot(LinkReferent.ATTR_PARAM, value, true, 0);
        return value;
    }
    
    get parentReferent() {
        return this.object1;
    }
    
    get object1() {
        return Utils.as(this.getSlotValue(LinkReferent.ATTR_OBJECT1), Referent);
    }
    set object1(value) {
        this.addSlot(LinkReferent.ATTR_OBJECT1, value, true, 0);
        return value;
    }
    
    get object2() {
        return Utils.as(this.getSlotValue(LinkReferent.ATTR_OBJECT2), Referent);
    }
    set object2(value) {
        this.addSlot(LinkReferent.ATTR_OBJECT2, value, true, 0);
        return value;
    }
    
    get dateFrom() {
        return Utils.as(this.getSlotValue(LinkReferent.ATTR_DATEFROM), DateReferent);
    }
    set dateFrom(value) {
        this.addSlot(LinkReferent.ATTR_DATEFROM, value, true, 0);
        return value;
    }
    
    get dateTo() {
        return Utils.as(this.getSlotValue(LinkReferent.ATTR_DATETO), DateReferent);
    }
    set dateTo(value) {
        this.addSlot(LinkReferent.ATTR_DATETO, value, true, 0);
        return value;
    }
    
    toStringEx(shortVariant, lang = null, lev = 0) {
        let tmp = new StringBuilder();
        if (this.typ !== LinkType.UNDEFINED) 
            tmp.append(MetaLink.TYPES.convertInnerValueToOuterValue(this.getStringValue(LinkReferent.ATTR_TYPE), null));
        if (this.param !== null) {
            if (tmp.length > 0) 
                tmp.append(", ");
            tmp.append(this.param);
        }
        if (this.dateFrom !== null && this.dateTo !== null) {
            if (tmp.length > 0) 
                tmp.append(", ");
            if (this.dateFrom === this.dateTo) 
                tmp.append(this.dateFrom.toStringEx(true, null, 0));
            else 
                tmp.append("с ").append(this.dateFrom.toStringEx(true, null, 0)).append(" по ").append(this.dateTo.toStringEx(true, null, 0));
        }
        else if (this.dateFrom !== null) {
            if (tmp.length > 0) 
                tmp.append(", ");
            tmp.append("с ").append(this.dateFrom.toStringEx(true, null, 0));
        }
        else if (this.dateTo !== null) {
            if (tmp.length > 0) 
                tmp.append(", ");
            tmp.append("по ").append(this.dateTo.toStringEx(true, null, 0));
        }
        if (this.object2 !== null && !shortVariant) {
            if (tmp.length > 0) 
                tmp.append(", ");
            tmp.append(this.object2.toStringEx(true, null, 0));
        }
        return tmp.toString();
    }
    
    canBeEquals(obj, _typ = ReferentsEqualType.WITHINONETEXT) {
        let lr = Utils.as(obj, LinkReferent);
        if (lr === null) 
            return false;
        if (lr.object1 !== this.object1 || lr.object2 !== this.object2) 
            return false;
        return true;
    }
    
    static _new2298(_arg1, _arg2) {
        let res = new LinkReferent();
        res.object1 = _arg1;
        res.object2 = _arg2;
        return res;
    }
    
    static _new2299(_arg1, _arg2, _arg3) {
        let res = new LinkReferent();
        res.typ = _arg1;
        res.object1 = _arg2;
        res.object2 = _arg3;
        return res;
    }
    
    static _new2300(_arg1, _arg2) {
        let res = new LinkReferent();
        res.typ = _arg1;
        res.object2 = _arg2;
        return res;
    }
    
    static _new2306(_arg1, _arg2, _arg3) {
        let res = new LinkReferent();
        res.object1 = _arg1;
        res.object2 = _arg2;
        res.typ = _arg3;
        return res;
    }
    
    static _new2307(_arg1) {
        let res = new LinkReferent();
        res.object1 = _arg1;
        return res;
    }
    
    static static_constructor() {
        LinkReferent.OBJ_TYPENAME = "LINK";
        LinkReferent.ATTR_TYPE = "TYPE";
        LinkReferent.ATTR_PARAM = "PARAM";
        LinkReferent.ATTR_OBJECT1 = "OBJECT1";
        LinkReferent.ATTR_OBJECT2 = "OBJECT2";
        LinkReferent.ATTR_DATEFROM = "DATEFROM";
        LinkReferent.ATTR_DATETO = "DATETO";
    }
}


LinkReferent.static_constructor();

module.exports = LinkReferent