/*
 * 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 PdfRect = require("./PdfRect");
const PdfFig = require("./PdfFig");
const PdfName = require("./PdfName");

class PdfPathState {
    
    constructor(p) {
        this.m_Page = null;
        this.m_x = null;
        this.m_y = null;
        this.m_Page = p;
        this.m_x = new Array();
        this.m_y = new Array();
    }
    
    parseOne(lex, i) {
        if (i >= lex.length) 
            return false;
        let nam = lex[i].name;
        if (nam === "m") {
            this.m_x.splice(0, this.m_x.length);
            this.m_y.splice(0, this.m_y.length);
            this.m_x.push(lex[i - 2].getDouble());
            this.m_y.push(this.m_Page.height - lex[i - 1].getDouble());
            return true;
        }
        if (nam === "l") {
            this.m_x.push(lex[i - 2].getDouble());
            this.m_y.push(this.m_Page.height - lex[i - 1].getDouble());
            return true;
        }
        if (nam === "h") {
            if (this.m_x.length > 0) {
                this.m_x.push(this.m_x[0]);
                this.m_y.push(this.m_y[0]);
            }
            return true;
        }
        if (nam === "re") {
            let x = lex[i - 4].getDouble();
            let y = this.m_Page.height - lex[i - 3].getDouble();
            let w = lex[i - 2].getDouble();
            let h = lex[i - 1].getDouble();
            this.m_x.push(x);
            this.m_y.push(y);
            this.m_x.push(x);
            this.m_y.push(y - h);
            this.m_x.push(x + w);
            this.m_y.push(y - h);
            this.m_x.push(x + w);
            this.m_y.push(y);
            this.m_x.push(x);
            this.m_y.push(y);
            return true;
        }
        if (nam === "n") {
            this.m_x.splice(0, this.m_x.length);
            this.m_y.splice(0, this.m_y.length);
            return true;
        }
        if (((((nam === "S" || nam === "s" || nam === "b") || nam === "B" || nam === "B*") || nam === "b*" || nam === "f") || nam === "F" || nam === "f*") || nam === "W" || nam === "W*") {
            if (this.m_x.length === 5 && this.m_x[0] === this.m_x[4] && this.m_y[0] === this.m_y[4]) {
                let re = null;
                if (this.m_x[1] === this.m_x[0] && this.m_y[1] === this.m_y[2] && this.m_x[2] === this.m_x[3]) {
                    re = new PdfFig();
                    re.x1 = this.m_x[0];
                    re.x2 = this.m_x[2];
                    re.y1 = this.m_y[0];
                    re.y2 = this.m_y[1];
                }
                else if (this.m_y[0] === this.m_y[1] && this.m_x[1] === this.m_x[2] && this.m_y[2] === this.m_y[3]) {
                    re = new PdfFig();
                    re.x1 = this.m_x[0];
                    re.x2 = this.m_x[1];
                    re.y1 = this.m_y[0];
                    re.y2 = this.m_y[2];
                }
                if (re !== null) {
                    if (re.x1 > re.x2) {
                        let d = re.x1;
                        re.x1 = re.x2;
                        re.x2 = d;
                    }
                    if (re.y1 > re.y2) {
                        let d = re.y1;
                        re.y1 = re.y2;
                        re.y2 = d;
                    }
                    this.m_Page.items.push(re);
                    return true;
                }
            }
            if (this.m_x.length > 1000) {
            }
            else 
                for (let ii = 0; ii < (this.m_x.length - 1); ii++) {
                    if (this.m_x[ii] === this.m_x[ii + 1] || this.m_y[ii] === this.m_y[ii + 1]) {
                        let re = new PdfFig();
                        re.x1 = this.m_x[ii];
                        re.x2 = this.m_x[ii + 1];
                        re.y1 = this.m_y[ii];
                        re.y2 = this.m_y[ii + 1];
                        if (re.x1 > re.x2) {
                            let d = re.x1;
                            re.x1 = re.x2;
                            re.x2 = d;
                        }
                        if (re.y1 > re.y2) {
                            let d = re.y1;
                            re.y1 = re.y2;
                            re.y2 = d;
                        }
                        this.m_Page.items.push(re);
                    }
                }
            return true;
        }
        return false;
    }
}


module.exports = PdfPathState