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

/**
 * Ссылка на ССЫЛКУ (BookLinkReferent или DecreeReferent)
 * 
 */
class BookLinkRefReferent extends Referent {
    
    constructor() {
        super(BookLinkRefReferent.OBJ_TYPENAME);
        this.instanceOf = MetaBookLinkRef.globalMeta;
    }
    
    toStringEx(shortVariant, lang = null, lev = 0) {
        let res = new StringBuilder();
        if (this.number !== null) 
            res.append("[").append(this.number).append("] ");
        if (this.pages !== null) 
            res.append((lang !== null && lang.isEn ? "pages" : "стр.")).append(" ").append(this.pages).append("; ");
        let _book = this.book;
        if (_book === null) 
            res.append("?");
        else 
            res.append(_book.toStringEx(shortVariant, lang, lev));
        return res.toString();
    }
    
    get parentReferent() {
        return Utils.as(this.getSlotValue(BookLinkRefReferent.ATTR_BOOK), Referent);
    }
    
    get typ() {
        let val = this.getStringValue(BookLinkRefReferent.ATTR_TYPE);
        if (val === null) 
            return BookLinkRefType.UNDEFINED;
        try {
            return BookLinkRefType.of(val);
        } catch (ex533) {
        }
        return BookLinkRefType.UNDEFINED;
    }
    set typ(value) {
        this.addSlot(BookLinkRefReferent.ATTR_TYPE, value.toString(), true, 0);
        return value;
    }
    
    get book() {
        return Utils.as(this.getSlotValue(BookLinkRefReferent.ATTR_BOOK), Referent);
    }
    set book(value) {
        this.addSlot(BookLinkRefReferent.ATTR_BOOK, value, true, 0);
        return value;
    }
    
    get number() {
        return this.getStringValue(BookLinkRefReferent.ATTR_NUMBER);
    }
    set number(value) {
        let num = value;
        if (num !== null && num.indexOf('-') > 0) 
            num = Utils.replaceString(num, " - ", "-");
        this.addSlot(BookLinkRefReferent.ATTR_NUMBER, num, true, 0);
        return value;
    }
    
    get pages() {
        return this.getStringValue(BookLinkRefReferent.ATTR_PAGES);
    }
    set pages(value) {
        this.addSlot(BookLinkRefReferent.ATTR_PAGES, value, true, 0);
        return value;
    }
    
    canBeEquals(obj, _typ = ReferentsEqualType.WITHINONETEXT) {
        let r = Utils.as(obj, BookLinkRefReferent);
        if (r === null) 
            return false;
        if (this.book !== r.book) 
            return false;
        if (this.number !== r.number) 
            return false;
        if (this.pages !== r.pages) 
            return false;
        if ((this.typ === BookLinkRefType.INLINE) !== (r.typ === BookLinkRefType.INLINE)) 
            return false;
        return true;
    }
    
    /**
     * Возвращает разницу номеров r2 - r1, иначе null, если номеров нет
     * @param r1 первая ссылка
     * @param r2 вторая ссылка
     * @return 
     */
    static getNumberDiff(r1, r2) {
        let num1 = r1.getStringValue(BookLinkRefReferent.ATTR_NUMBER);
        let num2 = r2.getStringValue(BookLinkRefReferent.ATTR_NUMBER);
        if (num1 === null || num2 === null) 
            return null;
        let n1 = 0;
        let n2 = 0;
        let wrapn1534 = new RefOutArgWrapper();
        let inoutres535 = Utils.tryParseInt(num1, wrapn1534);
        let wrapn2536 = new RefOutArgWrapper();
        let inoutres537 = Utils.tryParseInt(num2, wrapn2536);
        n1 = wrapn1534.value;
        n2 = wrapn2536.value;
        if (!inoutres535 || !inoutres537) 
            return null;
        return n2 - n1;
    }
    
    static _new525(_arg1) {
        let res = new BookLinkRefReferent();
        res.book = _arg1;
        return res;
    }
    
    static static_constructor() {
        BookLinkRefReferent.OBJ_TYPENAME = "BOOKLINKREF";
        BookLinkRefReferent.ATTR_BOOK = "BOOK";
        BookLinkRefReferent.ATTR_TYPE = "TYPE";
        BookLinkRefReferent.ATTR_PAGES = "PAGES";
        BookLinkRefReferent.ATTR_NUMBER = "NUMBER";
        BookLinkRefReferent.ATTR_MISC = "MISC";
    }
}


BookLinkRefReferent.static_constructor();

module.exports = BookLinkRefReferent