/*
 * 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 GetTextAttr = require("./../core/GetTextAttr");
const GeoReferent = require("./../geo/GeoReferent");
const DateReferent = require("./../date/DateReferent");
const OrganizationReferent = require("./../org/OrganizationReferent");
const BracketParseAttr = require("./../core/BracketParseAttr");
const ReferentClass = require("./../metadata/ReferentClass");
const MiscHelper = require("./../core/MiscHelper");
const MetaTitleInfo = require("./internal/MetaTitleInfo");
const BracketHelper = require("./../core/BracketHelper");
const Termin = require("./../core/Termin");
const Referent = require("./../Referent");

/**
 * Сущность, описывающая информацию из заголовков статей, книг, диссертация и пр.
 * 
 */
class TitlePageReferent extends Referent {
    
    constructor(name = null) {
        super((name != null ? name : TitlePageReferent.OBJ_TYPENAME));
        this.instanceOf = MetaTitleInfo.globalMeta;
    }
    
    toStringEx(shortVariant, lang, lev = 0) {
        let res = new StringBuilder();
        let str = this.getStringValue(TitlePageReferent.ATTR_NAME);
        res.append("\"").append(((str != null ? str : "?"))).append("\"");
        if (!shortVariant) {
            for (const r of this.slots) {
                if (r.typeName === TitlePageReferent.ATTR_TYPE) {
                    res.append(" (").append(r.value).append(")");
                    break;
                }
            }
            for (const r of this.slots) {
                if (r.typeName === TitlePageReferent.ATTR_AUTHOR && (r.value instanceof Referent)) 
                    res.append(", ").append(r.value.toStringEx(true, lang, 0));
            }
        }
        if (this.city !== null && !shortVariant) 
            res.append(", ").append(this.city.toStringEx(true, lang, 0));
        if (this.date !== null) {
            if (!shortVariant) 
                res.append(", ").append(this.date.toStringEx(true, lang, 0));
            else 
                res.append(", ").append(this.date.year);
        }
        return res.toString();
    }
    
    get types() {
        let res = new Array();
        for (const s of this.slots) {
            if (s.typeName === TitlePageReferent.ATTR_TYPE) 
                res.push(s.value.toString());
        }
        return res;
    }
    
    addType(typ) {
        if (!Utils.isNullOrEmpty(typ)) {
            this.addSlot(TitlePageReferent.ATTR_TYPE, typ.toLowerCase(), false, 0);
            this.correctData();
        }
    }
    
    get names() {
        let res = new Array();
        for (const s of this.slots) {
            if (s.typeName === TitlePageReferent.ATTR_NAME) 
                res.push(s.value.toString());
        }
        return res;
    }
    
    addName(begin, end) {
        if (BracketHelper.canBeStartOfSequence(begin, true, false)) {
            let br = BracketHelper.tryParse(begin, BracketParseAttr.NO, 100);
            if (br !== null && br.endToken === end) {
                begin = begin.next;
                end = end.previous;
            }
        }
        let val = MiscHelper.getTextValue(begin, end, GetTextAttr.of((GetTextAttr.KEEPREGISTER.value()) | (GetTextAttr.KEEPQUOTES.value())));
        if (val === null) 
            return null;
        if (val.endsWith(".") && !val.endsWith("..")) 
            val = val.substring(0, 0 + val.length - 1).trim();
        this.addSlot(TitlePageReferent.ATTR_NAME, val, false, 0);
        return new Termin(val.toUpperCase());
    }
    
    correctData() {
        
    }
    
    get date() {
        return Utils.as(this.getSlotValue(TitlePageReferent.ATTR_DATE), DateReferent);
    }
    set date(value) {
        if (value === null) 
            return value;
        if (this.date === null) {
            this.addSlot(TitlePageReferent.ATTR_DATE, value, true, 0);
            return value;
        }
        if (this.date.month > 0 && value.month === 0) 
            return value;
        if (this.date.day > 0 && value.day === 0) 
            return value;
        this.addSlot(TitlePageReferent.ATTR_DATE, value, true, 0);
        return value;
    }
    
    get studentYear() {
        return this.getIntValue(TitlePageReferent.ATTR_STUDENTYEAR, 0);
    }
    set studentYear(value) {
        this.addSlot(TitlePageReferent.ATTR_STUDENTYEAR, value, true, 0);
        return value;
    }
    
    get org() {
        return Utils.as(this.getSlotValue(TitlePageReferent.ATTR_ORG), OrganizationReferent);
    }
    set org(value) {
        this.addSlot(TitlePageReferent.ATTR_ORG, value, true, 0);
        return value;
    }
    
    get city() {
        return Utils.as(this.getSlotValue(TitlePageReferent.ATTR_CITY), GeoReferent);
    }
    set city(value) {
        this.addSlot(TitlePageReferent.ATTR_CITY, value, true, 0);
        return value;
    }
    
    get speciality() {
        return this.getStringValue(TitlePageReferent.ATTR_SPECIALITY);
    }
    set speciality(value) {
        this.addSlot(TitlePageReferent.ATTR_SPECIALITY, value, true, 0);
        return value;
    }
    
    static static_constructor() {
        TitlePageReferent.OBJ_TYPENAME = "TITLEPAGE";
        TitlePageReferent.ATTR_NAME = "NAME";
        TitlePageReferent.ATTR_TYPE = "TYPE";
        TitlePageReferent.ATTR_AUTHOR = "AUTHOR";
        TitlePageReferent.ATTR_SUPERVISOR = "SUPERVISOR";
        TitlePageReferent.ATTR_EDITOR = "EDITOR";
        TitlePageReferent.ATTR_CONSULTANT = "CONSULTANT";
        TitlePageReferent.ATTR_OPPONENT = "OPPONENT";
        TitlePageReferent.ATTR_TRANSLATOR = "TRANSLATOR";
        TitlePageReferent.ATTR_AFFIRMANT = "AFFIRMANT";
        TitlePageReferent.ATTR_ORG = "ORGANIZATION";
        TitlePageReferent.ATTR_STUDENTYEAR = "STUDENTYEAR";
        TitlePageReferent.ATTR_DATE = "DATE";
        TitlePageReferent.ATTR_CITY = "CITY";
        TitlePageReferent.ATTR_SPECIALITY = "SPECIALITY";
        TitlePageReferent.ATTR_ATTR = "ATTR";
    }
}


TitlePageReferent.static_constructor();

module.exports = TitlePageReferent