/*
 * 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 MiscHelper = require("./../util/MiscHelper");
const UnilayOcrQuality = require("./UnilayOcrQuality");

/**
 * Прямоугольник тексто-графического слоя
 */
class UnilayRectangle {
    
    constructor() {
        this.page = null;
        this.left = 0;
        this.top = 0;
        this.right = 0;
        this.bottom = 0;
        this.text = null;
        this.imageContent = null;
        this.ignored = false;
        this.lineNumber = 0;
        this.quality = UnilayOcrQuality.UNDEFINED;
        this.noWhitespaceBefore = false;
        this.tag = null;
    }
    
    toString() {
        let res = (String(Utils.mathRound(this.left, 2)) + ".." + Utils.mathRound(this.right, 2) + " x " + Utils.mathRound(this.top, 2) + ".." + Utils.mathRound(this.bottom, 2));
        if (this.text !== null) 
            return (res + ": " + this.text);
        if (this.imageContent !== null) 
            return (res + ": Image");
        return res;
    }
    
    getXml(xml) {
        xml.writeStartElement("rect");
        if (this.lineNumber > 0) 
            xml.writeAttributeString("line", this.lineNumber.toString());
        xml.writeAttributeString("left", MiscHelper.outDouble(this.left));
        xml.writeAttributeString("top", MiscHelper.outDouble(this.top));
        xml.writeAttributeString("right", MiscHelper.outDouble(this.right));
        xml.writeAttributeString("bottom", MiscHelper.outDouble(this.bottom));
        if (this.noWhitespaceBefore) 
            xml.writeAttributeString("no_ws", "true");
        if (this.quality !== UnilayOcrQuality.UNDEFINED) 
            xml.writeAttributeString("q", this.quality.toString().toLowerCase());
        if (this.imageContent !== null) 
            xml.writeElementString("image", Utils.encodeBase64(this.imageContent));
        else if (this.text !== null) 
            xml.writeElementString("text", Utils.notNull(MiscHelper.correctXmlValue(this.text), ""));
        xml.writeEndElement();
    }
    
    restore(xml) {
        let d = 0;
        if (xml.attributes !== null) {
            for (const a of xml.attributes) {
                if (a.local_name === "line") {
                    let i = 0;
                    let wrapi576 = new RefOutArgWrapper();
                    let inoutres577 = Utils.tryParseInt(a.value, wrapi576);
                    i = wrapi576.value;
                    if (inoutres577) 
                        this.lineNumber = i;
                }
                else if (a.local_name === "left") {
                    let wrapd578 = new RefOutArgWrapper();
                    let inoutres579 = MiscHelper.tryParseDouble(a.value, wrapd578);
                    d = wrapd578.value;
                    if (inoutres579) 
                        this.left = d;
                }
                else if (a.local_name === "top") {
                    let wrapd580 = new RefOutArgWrapper();
                    let inoutres581 = MiscHelper.tryParseDouble(a.value, wrapd580);
                    d = wrapd580.value;
                    if (inoutres581) 
                        this.top = d;
                }
                else if (a.local_name === "right") {
                    let wrapd582 = new RefOutArgWrapper();
                    let inoutres583 = MiscHelper.tryParseDouble(a.value, wrapd582);
                    d = wrapd582.value;
                    if (inoutres583) 
                        this.right = d;
                }
                else if (a.local_name === "bottom") {
                    let wrapd584 = new RefOutArgWrapper();
                    let inoutres585 = MiscHelper.tryParseDouble(a.value, wrapd584);
                    d = wrapd584.value;
                    if (inoutres585) 
                        this.bottom = d;
                }
                else if (a.local_name === "no_ws") 
                    this.noWhitespaceBefore = a.value === "true";
                else if (a.local_name === "q") {
                    try {
                        this.quality = UnilayOcrQuality.of(a.value);
                    } catch (ex586) {
                    }
                }
            }
        }
        for (const x of xml.child_nodes) {
            if (x.local_name === "text") 
                this.text = x.inner_text;
            else if (x.local_name === "image") 
                this.imageContent = Utils.decodeBase64(x.inner_text);
        }
    }
    
    static _new575(_arg1) {
        let res = new UnilayRectangle();
        res.page = _arg1;
        return res;
    }
}


module.exports = UnilayRectangle