/*
 * SDK Pullenti Unitext, 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# project. 
 * The latest version of the code is available on the site www.pullenti.ru
 */

const Utils = require("./../../../unisharp/Utils");

const SinglePropertyModifiers = require("./SinglePropertyModifiers");
const StyleCollection = require("./StyleCollection");
const FormattingLevel = require("./FormattingLevel");

class CharacterFormatting {
    
    get offset() {
        return this._offset;
    }
    set offset(value) {
        this._offset = value;
        return this._offset;
    }
    
    get length() {
        return this._length;
    }
    set length(value) {
        this._length = value;
        return this._length;
    }
    
    get fileCharacterPosition() {
        return this._filecharacterposition;
    }
    set fileCharacterPosition(value) {
        this._filecharacterposition = value;
        return this._filecharacterposition;
    }
    
    get pos() {
        return this.fileCharacterPosition.characterIndex + this.offset;
    }
    
    get chpx() {
        return this._chpx;
    }
    set chpx(value) {
        this._chpx = value;
        return this._chpx;
    }
    
    toString() {
        return ("Pos:" + this.pos + " Len:" + this.length + " Styles:" + this.getStyles(FormattingLevel.CHARACTER).toString());
    }
    
    get style() {
        let istdData = this.getProperty(SinglePropertyModifiers.sprmCIstd);
        if (istdData !== null) {
            let istd = Utils.bytesToObject(istdData, 0, 'ushort', 2);
            if (this.owner.styleDefinitionsMap.containsKey(istd)) 
                return this.owner.styleDefinitionsMap.get(istd);
        }
        return null;
    }
    
    constructor(_owner, __offset, __length, fcp, __chpx) {
        this.owner = null;
        this._offset = 0;
        this._length = 0;
        this._filecharacterposition = null;
        this._chpx = null;
        this.owner = _owner;
        this.offset = __offset;
        this.length = __length;
        this.fileCharacterPosition = fcp;
        this.chpx = __chpx;
    }
    
    getProperty(sprm) {
        for (const p of this.chpx.grpprl) {
            if (Utils.compareUnsigned(p.sprm.sprm, sprm) == 0) 
                return p.operand;
        }
        for (const p of this.fileCharacterPosition.prls) {
            if (Utils.compareUnsigned(p.sprm.sprm, sprm) == 0) 
                return p.operand;
        }
        return null;
    }
    
    getStyles(level = FormattingLevel.CHARACTER) {
        let prls = new Array();
        prls.push(this.chpx.grpprl);
        if (level === FormattingLevel.CHARACTERSTYLE) {
            let def = this.style;
            if (def !== null) 
                def.expandStyles(prls);
        }
        return new StyleCollection(prls);
    }
}


module.exports = CharacterFormatting