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

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

class Paragraph {
    
    get offset() {
        return this._offset;
    }
    set offset(value) {
        this._offset = value;
        return this._offset;
    }
    
    get pos() {
        return this.fileCharacterPosition.characterIndex + 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 papxInFkps() {
        return this._papxinfkps;
    }
    set papxInFkps(value) {
        this._papxinfkps = value;
        return this._papxinfkps;
    }
    
    toString() {
        let res = new StringBuilder();
        res.append("Pos:").append((this.fileCharacterPosition.characterIndex + this.offset)).append(", Len:").append(this.length);
        if (this.isInTable) 
            res.append(" InTable (").append(this.tableDepth).append(")");
        if (this.isList) 
            res.append(" IsList (").append(this.listLevel).append(")");
        if (this.isTableCellEnd) 
            res.append(" TableCellEnd");
        if (this.isTableRowEnd) 
            res.append(" TableRowEnd");
        res.append(" ").append(this.getStyles(FormattingLevel.ALL));
        if (this.papxInFkps !== null && this.papxInFkps.grpprlInPapx !== null && this.papxInFkps.grpprlInPapx.grpprl !== null) {
            for (const p of this.papxInFkps.grpprlInPapx.grpprl) {
                res.append(" ").append((Utils.notNull(SinglePropertyModifiers.getSprmName(p.sprm.sprm), "")));
            }
        }
        for (const p of this.fileCharacterPosition.prls) {
            res.append(" ").append((Utils.notNull(SinglePropertyModifiers.getSprmName(p.sprm.sprm), "")));
        }
        return res.toString();
    }
    
    get isInTable() {
        let data = this.getProperty(SinglePropertyModifiers.sprmPFInTable);
        return data !== null && data[0] !== (0);
    }
    
    get tableDepth() {
        let data = this.getProperty(SinglePropertyModifiers.sprmPItap);
        if (data === null) 
            return 0;
        else 
            return Utils.bytesToObject(data, 0, 'uint', 4);
    }
    
    get isTableRowEnd() {
        let _pos = (this.fileCharacterPosition.characterIndex + this.offset + this.length) - 1;
        if ((_pos < this.owner.text.length) && this.owner.text.charAt(_pos) === '\u0007') {
            let data = this.getProperty(SinglePropertyModifiers.sprmPFTtp);
            if (data !== null && data[0] !== (0)) 
                return true;
        }
        else {
            let data = this.getProperty(SinglePropertyModifiers.sprmPFInnerTtp);
            if (data !== null && data[0] !== (0)) 
                return true;
        }
        return false;
    }
    
    get isTableCellEnd() {
        let _pos = (this.fileCharacterPosition.characterIndex + this.offset + this.length) - 1;
        if ((_pos < this.owner.text.length) && this.owner.text.charAt(_pos) === '\u0007') 
            return true;
        else {
            let data = this.getProperty(SinglePropertyModifiers.sprmPFInnerTableCell);
            return data !== null && data[0] !== (0);
        }
    }
    
    get isList() {
        let data = this.getProperty(SinglePropertyModifiers.sprmPIlfo);
        return data !== null && Utils.bytesToObject(data, 0, 'short', 2) !== (0);
    }
    
    get listLevel() {
        let data = this.getProperty(SinglePropertyModifiers.sprmPIlvl);
        return (data !== null ? data[0] : 0);
    }
    
    get style() {
        return this.owner.styleDefinitionsMap.get(this.papxInFkps.grpprlInPapx.istd);
    }
    
    constructor(_owner, __offset, __length, fcp, _papxInFkps) {
        this.owner = null;
        this._offset = 0;
        this._length = 0;
        this._filecharacterposition = null;
        this._papxinfkps = null;
        this.owner = _owner;
        this.offset = __offset;
        this.length = __length;
        this.fileCharacterPosition = fcp;
        this.papxInFkps = _papxInFkps;
    }
    
    getProperty(sprm) {
        if (this.papxInFkps !== null && this.papxInFkps.grpprlInPapx !== null && this.papxInFkps.grpprlInPapx.grpprl !== null) {
            for (const p of this.papxInFkps.grpprlInPapx.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) {
        if (level !== FormattingLevel.PARAGRAPH && level !== FormattingLevel.PARAGRAPHSTYLE) 
            return null;
        let prls = new Array();
        prls.push(this.papxInFkps.grpprlInPapx.grpprl);
        if (level === FormattingLevel.PARAGRAPHSTYLE) 
            this.style.expandStyles(prls);
        return new StyleCollection(prls);
    }
    
    compareTo(other) {
        if (this.pos < other.pos) 
            return -1;
        if (this.pos > other.pos) 
            return 1;
        if (this.length < other.length) 
            return -1;
        if (this.length > other.length) 
            return 1;
        return 0;
    }
}


module.exports = Paragraph