/*
 * 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 ReferentToken = require("./../ReferentToken");
const MiscHelper = require("./../core/MiscHelper");
const ReferentClass = require("./../metadata/ReferentClass");
const DateReferent = require("./../date/DateReferent");
const DatePointerType = require("./../date/DatePointerType");
const DecreeReferent = require("./../decree/DecreeReferent");
const MetaInstrument = require("./internal/MetaInstrument");
const DecreeToken = require("./../decree/internal/DecreeToken");
const InstrumentBlockReferent = require("./InstrumentBlockReferent");
const DecreeHelper = require("./../decree/internal/DecreeHelper");

/**
 * Представление всего документа
 * 
 */
class InstrumentReferent extends InstrumentBlockReferent {
    
    constructor() {
        super(InstrumentReferent.OBJ_TYPENAME);
        this.instanceOf = MetaInstrument.GLOBAL_META;
    }
    
    toStringEx(shortVariant, lang, lev = 0) {
        let res = new StringBuilder();
        let str = null;
        if ((((str = this.getStringValue(InstrumentReferent.ATTR_APPENDIX)))) !== null) {
            let strs = this.getStringValues(InstrumentReferent.ATTR_APPENDIX);
            if (strs.length === 1) 
                res.append("Приложение").append((str.length === 0 ? "" : " ")).append(str).append("; ");
            else {
                res.append("Приложения ");
                for (let i = 0; i < strs.length; i++) {
                    if (i > 0) 
                        res.append(",");
                    res.append(strs[i]);
                }
                res.append("; ");
            }
        }
        if ((((str = this.getStringValue(InstrumentReferent.ATTR_PART)))) !== null) 
            res.append("Часть ").append(str).append("; ");
        if (this.typ !== null) 
            res.append(MiscHelper.convertFirstCharUpperAndOtherLower(this.typ));
        else 
            res.append("Документ");
        if (this.regNumber !== null) {
            res.append(" №").append(this.regNumber);
            for (const s of this.slots) {
                if (s.typeName === InstrumentReferent.ATTR_REGNUMBER && s.value.toString() !== this.regNumber) 
                    res.append("/").append(s.value);
            }
        }
        if (this.caseNumber !== null) 
            res.append(" дело №").append(this.caseNumber);
        let dt = this.getStringValue(InstrumentReferent.ATTR_DATE);
        if (dt !== null) 
            res.append(" от ").append(dt);
        if ((((str = this.getStringValue(InstrumentBlockReferent.ATTR_NAME)))) !== null) {
            if (str.length > 100) 
                str = str.substring(0, 0 + 100) + "...";
            res.append(" \"").append(str).append("\"");
        }
        if ((((str = this.getStringValue(InstrumentReferent.ATTR_GEO)))) !== null) 
            res.append(" (").append(str).append(")");
        return res.toString().trim();
    }
    
    get typ() {
        return this.getStringValue(InstrumentReferent.ATTR_TYPE);
    }
    set typ(_value) {
        this.addSlot(InstrumentReferent.ATTR_TYPE, _value, true, 0);
        return _value;
    }
    
    get regNumber() {
        return this.getStringValue(InstrumentReferent.ATTR_REGNUMBER);
    }
    set regNumber(_value) {
        if (Utils.isNullOrEmpty(_value)) {
            this.addSlot(InstrumentReferent.ATTR_REGNUMBER, null, true, 0);
            return _value;
        }
        if (".,".indexOf(_value[_value.length - 1]) >= 0) 
            _value = _value.substring(0, 0 + _value.length - 1);
        this.addSlot(InstrumentReferent.ATTR_REGNUMBER, _value, true, 0);
        return _value;
    }
    
    get caseNumber() {
        return this.getStringValue(InstrumentReferent.ATTR_CASENUMBER);
    }
    set caseNumber(_value) {
        if (Utils.isNullOrEmpty(_value)) 
            return _value;
        if (".,".indexOf(_value[_value.length - 1]) >= 0) 
            _value = _value.substring(0, 0 + _value.length - 1);
        this.addSlot(InstrumentReferent.ATTR_CASENUMBER, _value, true, 0);
        return _value;
    }
    
    get date() {
        let s = this.getStringValue(InstrumentReferent.ATTR_DATE);
        if (s === null) 
            return null;
        return DecreeHelper.parseDateTime(s);
    }
    
    addDate(dt) {
        if (dt === null) 
            return false;
        if (dt instanceof DecreeToken) {
            if (dt.ref instanceof ReferentToken) 
                return this.addDate(dt.ref.referent);
            if (dt.value !== null) {
                this.addSlot(InstrumentReferent.ATTR_DATE, dt.value, true, 0);
                return true;
            }
            return false;
        }
        if (dt instanceof ReferentToken) 
            return this.addDate(dt.referent);
        if (dt instanceof DateReferent) {
            let dr = Utils.as(dt, DateReferent);
            let year = dr.year;
            let mon = dr.month;
            let day = dr.day;
            if (year === 0) 
                return dr.pointer === DatePointerType.UNDEFINED;
            let exDate = this.date;
            if (exDate !== null && exDate.getFullYear() === year) {
                if (mon === 0 && Utils.getMonth(exDate) > 0) 
                    return false;
                if (day === 0 && exDate.getDate() > 0) 
                    return false;
                let delExist = false;
                if (mon > 0 && Utils.getMonth(exDate) === 0) 
                    delExist = true;
                if (delExist) {
                    for (const s of this.slots) {
                        if (s.typeName === InstrumentReferent.ATTR_DATE) {
                            Utils.removeItem(this.slots, s);
                            break;
                        }
                    }
                }
            }
            let tmp = new StringBuilder();
            tmp.append(year);
            if (mon > 0) 
                tmp.append(".").append(Utils.correctToString((mon).toString(10), 2, true));
            if (day > 0) 
                tmp.append(".").append(Utils.correctToString((day).toString(10), 2, true));
            this.addSlot(DecreeReferent.ATTR_DATE, tmp.toString(), false, 0);
            return true;
        }
        if ((typeof dt === 'string' || dt instanceof String)) {
            this.addSlot(InstrumentReferent.ATTR_DATE, Utils.asString(dt), true, 0);
            return true;
        }
        return false;
    }
    
    canBeEquals(obj, _typ) {
        return obj === this;
    }
    
    static static_constructor() {
        InstrumentReferent.OBJ_TYPENAME = "INSTRUMENT";
        InstrumentReferent.ATTR_TYPE = "TYPE";
        InstrumentReferent.ATTR_REGNUMBER = "NUMBER";
        InstrumentReferent.ATTR_CASENUMBER = "CASENUMBER";
        InstrumentReferent.ATTR_DATE = "DATE";
        InstrumentReferent.ATTR_SIGNER = "SIGNER";
        InstrumentReferent.ATTR_SOURCE = "SOURCE";
        InstrumentReferent.ATTR_GEO = "GEO";
        InstrumentReferent.ATTR_PART = "PART";
        InstrumentReferent.ATTR_APPENDIX = "APPENDIX";
        InstrumentReferent.ATTR_PARTICIPANT = "PARTICIPANT";
        InstrumentReferent.ATTR_ARTEFACT = "ARTEFACT";
    }
}


InstrumentReferent.static_constructor();

module.exports = InstrumentReferent