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

const PdfName = require("./PdfName");
const IPdfDictionaryWrapper = require("./IPdfDictionaryWrapper");
const PdfRect = require("./PdfRect");
const MiscHelper = require("./../../../util/MiscHelper");
const PdfImage = require("./PdfImage");
const PdfText = require("./PdfText");
const PdfPage = require("./PdfPage");

class PdfPageOper {
    
    constructor() {
        this.args = new Array();
        this.name = null;
        this.text = null;
        this.beginPos = 0;
        this.endPos = 0;
        this.deleted = false;
        this.replaceBy = null;
        this.insertAfter = null;
        this.x1 = 0;
        this.x2 = 0;
        this.y1 = 0;
        this.y2 = 0;
        this.dic = null;
        this.isImage = false;
        this.isForm = false;
        this.tag = null;
    }
    
    toString() {
        let tmp = new StringBuilder();
        for (let i = 0; i < this.args.length; i++) {
            tmp.append(this.args[i]);
            tmp.append(' ');
        }
        tmp.append(this.name);
        return tmp.toString();
    }
    
    outHtml(res) {
        for (const a of this.args) {
            MiscHelper.correctHtmlValue(res, a, false, false, false);
            res.append(' ');
        }
        res.append("<b>").append(this.name).append("</b>");
        if (this.text !== null) {
            res.append(" text: <span style=\"color:green\">");
            MiscHelper.correctHtmlValue(res, this.text, false, false, false);
            res.append("</span>");
        }
        if (this.isImage && this.dic !== null) 
            res.append(" <span style=\"color:green\">image: size=").append(this.dic.getIntItem("Length")).append("</span>");
        if (this.isForm && this.dic !== null) 
            res.append(" <span style=\"color:green\">form (image): size=").append(this.dic.getIntItem("Length")).append("</span>");
        if (this.x2 > 0 || this.y2 > 0) 
            res.append(" <span style=\"color:blue\">x=[").append(Utils.mathRound(this.x1, 2)).append(" .. ").append(Utils.mathRound(this.x2, 2)).append("] y=[").append(Utils.mathRound(this.y1, 2)).append(" .. ").append(Utils.mathRound(this.y2, 2)).append("]</span>");
    }
    
    static parse(li) {
        let res = new Array();
        if (li === null) 
            return res;
        let _args = new Array();
        let pos = 0;
        for (let i = 0; i < li.length; i++) {
            let lex = li[i];
            let nam = PdfPageOper.checkEndOfOper(li, i);
            if (nam === null) {
                if (_args.length === 0) 
                    pos = lex.readPos;
                _args.push(lex.toString());
                continue;
            }
            let op = new PdfPageOper();
            op.name = nam;
            op.beginPos = lex.readPos;
            op.endPos = ((lex.readPos + (lex.readLen)) - (1));
            if (_args.length > 0) {
                op.beginPos = pos;
                op.args.splice(op.args.length, 0, ..._args);
                _args.splice(0, _args.length);
            }
            let r = null;
            if (lex.tag instanceof PdfText) {
                r = Utils.as(lex.tag, PdfRect);
                op.text = lex.tag.text;
            }
            else if (lex.tag instanceof PdfRect) {
                r = Utils.as(lex.tag, PdfRect);
                if (lex.tag instanceof PdfImage) 
                    op.isImage = true;
                else if (lex.tag instanceof PdfPage) 
                    op.isForm = true;
                if (lex.tag instanceof IPdfDictionaryWrapper) 
                    op.dic = lex.tag.dic;
            }
            res.push(op);
            if (res.length === 2686) {
            }
        }
        return res;
    }
    
    static checkEndOfOper(li, i) {
        if (i >= li.length) 
            return null;
        let pname = Utils.as(li[i], PdfName);
        if (pname === null || pname.name.length > 3 || ((((i + 1) < li.length) && (li[i + 1] instanceof PdfName) && li[i + 1].name === "Do"))) 
            return null;
        if (((i + 2) < li.length) && (li[i + 2] instanceof PdfName) && li[i + 2].name === "Tf") 
            return null;
        if (((i + 1) < li.length) && (li[i + 1] instanceof PdfName)) {
            if (Utils.compareStrings(li[i + 1].name, "SCN", true) === 0) 
                return null;
        }
        return pname.name;
    }
    
    getArgDouble(i) {
        if (i >= 0 && (i < this.args.length)) {
            let d = 0;
            let wrapd233 = new RefOutArgWrapper();
            let inoutres234 = MiscHelper.tryParseDouble(this.args[i], wrapd233);
            d = wrapd233.value;
            if (inoutres234) 
                return d;
        }
        return 0;
    }
    
    getBytesToStream(s) {
        for (const a of this.args) {
            if (Utils.isLetter(a[0])) 
                s.writeByte('/'.charCodeAt(0));
            for (const ch of a) {
                s.writeByte(ch.charCodeAt(0));
            }
            s.writeByte(0x20);
        }
        for (let i = 0; i < this.name.length; i++) {
            s.writeByte(this.name.charCodeAt(i));
        }
        s.writeByte(0x20);
    }
    
    getBytes() {
        let mem = new MemoryStream(); 
        try {
            mem.writeByte(0x20);
            this.getBytesToStream(mem);
            return mem.toByteArray();
        }
        finally {
            mem.close();
        }
    }
}


module.exports = PdfPageOper