/*
 * 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 RefOutArgWrapper = require("./../../unisharp/RefOutArgWrapper");
const StringBuilder = require("./../../unisharp/StringBuilder");

const UriReferent = require("./../uri/UriReferent");
const MiscHelper = require("./../core/MiscHelper");
const ReferentsEqualType = require("./../core/ReferentsEqualType");
const Referent = require("./../Referent");
const ReferentClass = require("./../metadata/ReferentClass");
const MetaBookLink = require("./internal/MetaBookLink");

/**
 * Ссылка на внешний литературный источник (статью, книгу и пр.)
 * 
 */
class BookLinkReferent extends Referent {
    
    constructor() {
        super(BookLinkReferent.OBJ_TYPENAME);
        this.instanceOf = MetaBookLink.globalMeta;
    }
    
    toStringEx(shortVariant, _lang = null, lev = 0) {
        let res = new StringBuilder();
        let a = this.getSlotValue(BookLinkReferent.ATTR_AUTHOR);
        if (a !== null) {
            for (const s of this.slots) {
                if (s.typeName === BookLinkReferent.ATTR_AUTHOR) {
                    if (a !== s.value) 
                        res.append(", ");
                    if (s.value instanceof Referent) 
                        res.append(s.value.toStringEx(true, _lang, lev + 1));
                    else if ((typeof s.value === 'string' || s.value instanceof String)) 
                        res.append(Utils.asString(s.value));
                }
            }
            if (this.authorsAndOther) 
                res.append(" и др.");
        }
        let nam = this.name;
        if (nam !== null) {
            if (res.length > 0) 
                res.append(' ');
            if (nam.length > 200) 
                nam = nam.substring(0, 0 + 200) + "...";
            res.append("\"").append(nam).append("\"");
        }
        let uri = Utils.as(this.getSlotValue(BookLinkReferent.ATTR_URL), UriReferent);
        if (uri !== null) 
            res.append(" [").append(uri.toString()).append("]");
        if (this.year > 0) 
            res.append(", ").append(this.year);
        return res.toString();
    }
    
    get name() {
        return this.getStringValue(BookLinkReferent.ATTR_NAME);
    }
    set name(value) {
        this.addSlot(BookLinkReferent.ATTR_NAME, value, true, 0);
        return value;
    }
    
    get lang() {
        return this.getStringValue(BookLinkReferent.ATTR_LANG);
    }
    set lang(value) {
        this.addSlot(BookLinkReferent.ATTR_LANG, value, true, 0);
        return value;
    }
    
    get typ() {
        return this.getStringValue(BookLinkReferent.ATTR_TYPE);
    }
    set typ(value) {
        this.addSlot(BookLinkReferent.ATTR_TYPE, value, true, 0);
        return value;
    }
    
    get url() {
        return Utils.as(this.getSlotValue(BookLinkReferent.ATTR_URL), UriReferent);
    }
    
    get year() {
        let _year = 0;
        let wrapyear531 = new RefOutArgWrapper();
        let inoutres532 = Utils.tryParseInt(Utils.notNull(this.getStringValue(BookLinkReferent.ATTR_YEAR), ""), wrapyear531);
        _year = wrapyear531.value;
        if (inoutres532) 
            return _year;
        else 
            return 0;
    }
    set year(value) {
        this.addSlot(BookLinkReferent.ATTR_YEAR, value.toString(), true, 0);
        return value;
    }
    
    get authorsAndOther() {
        return this.findSlot(BookLinkReferent.ATTR_MISC, "и др.", true) !== null;
    }
    set authorsAndOther(value) {
        this.addSlot(BookLinkReferent.ATTR_MISC, "и др.", false, 0);
        return value;
    }
    
    canBeEquals(obj, _typ = ReferentsEqualType.WITHINONETEXT) {
        let br = Utils.as(obj, BookLinkReferent);
        if (br === null) 
            return false;
        let eq = 0;
        if (this.year > 0 && br.year > 0) {
            if (this.year === br.year) 
                eq++;
            else 
                return false;
        }
        if (this.typ !== null && br.typ !== null) {
            if (this.typ !== br.typ) 
                return false;
        }
        let eqAuth = false;
        if (this.findSlot(BookLinkReferent.ATTR_AUTHOR, null, true) !== null && br.findSlot(BookLinkReferent.ATTR_AUTHOR, null, true) !== null) {
            let ok = false;
            for (const a of this.slots) {
                if (a.typeName === BookLinkReferent.ATTR_AUTHOR) {
                    if (br.findSlot(BookLinkReferent.ATTR_AUTHOR, a.value, true) !== null) {
                        eq++;
                        ok = true;
                        eqAuth = true;
                    }
                }
            }
            if (!ok) 
                return false;
        }
        if (br.name !== this.name) {
            if (this.name === null || br.name === null) 
                return false;
            if (this.name.startsWith(br.name) || br.name.startsWith(this.name)) 
                eq += 1;
            else if (eqAuth && MiscHelper.canBeEquals(this.name, br.name, false, true, false)) 
                eq += 1;
            else 
                return false;
        }
        else 
            eq += 2;
        return eq > 2;
    }
    
    static static_constructor() {
        BookLinkReferent.OBJ_TYPENAME = "BOOKLINK";
        BookLinkReferent.ATTR_AUTHOR = "AUTHOR";
        BookLinkReferent.ATTR_NAME = "NAME";
        BookLinkReferent.ATTR_YEAR = "YEAR";
        BookLinkReferent.ATTR_LANG = "LANG";
        BookLinkReferent.ATTR_GEO = "GEO";
        BookLinkReferent.ATTR_URL = "URL";
        BookLinkReferent.ATTR_MISC = "MISC";
        BookLinkReferent.ATTR_TYPE = "TYPE";
    }
}


BookLinkReferent.static_constructor();

module.exports = BookLinkReferent