/*
*
* An XmlReader implementation for loading SGML (including HTML) converting it
* to well formed XML, by adding missing quotes, empty attribute values, ignoring
* duplicate attributes, case folding on tag names, adding missing closing tags
* based on SGML DTD information, and so on.
*
* Copyright (c) 2002 Microsoft Corporation. All rights reserved.
*
* Chris Lovett
*
*/
import { __extends } from "tslib";
/* MD 2/27/13
 * This software is being re-used under the Microsoft Public License (MS-PL):
 * -----------------------------------------------------------------------------
 * This license governs use of the accompanying software. If you use the software, you accept this license. If you do not accept the
 * license, do not use the software.
 *
 * 1. Definitions
 * The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning here as under U.S. copyright law.
 * A "contribution" is the original software, or any additions or changes to the software. A "contributor" is any person that distributes
 * its contribution under this license. "Licensed patents" are a contributor's patent claims that read directly on its contribution.
 *
 * 2. Grant of Rights
 * (A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each
 *     contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare
 *     derivative works of its contribution, and distribute its contribution or any derivative works that you create.
 * (B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each
 *     contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use,
 *     sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution
 *     in the software.
 *
 * 3. Conditions and Limitations
 * (A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
 * (B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your patent license
 *     from such contributor to the software ends automatically.
 * (C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices that are
 *     present in the software.
 * (D) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete
 *     copy of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may
 *     only do so under a license that complies with this license.
 * (E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees or conditions.
 *     You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your
 *     local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement.
 * -----------------------------------------------------------------------------
 *
 * In addition, the public classes have been made internal and the namespace has been changed from Sgml to
 * Infragistics.Documents.Excel.Serialization.Excel2007.Sgml
 *
 * Also, I updated the code to work with Silverlight and WinRT.
 */
import { Base, BaseError, markType } from "igniteui-angular-core";
import { StringUtilitiesSgml } from "./StringUtilitiesSgml";
import { StringUtilities } from "./StringUtilities";
import { StringReader } from "igniteui-angular-core";
import { StringBuilder } from "igniteui-angular-core";
import { Convert } from "igniteui-angular-core";
import { stringCompare1, isLetter, stringFormat, isLetterOrDigit, stringFormat1 } from "igniteui-angular-core";
/**
 * @hidden
 */
var Entity = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(Entity, _super);
    function Entity(a) {
        var _rest = [];
        for (var _i = 1; _i < arguments.length; _i++) {
            _rest[_i - 1] = arguments[_i];
        }
        var _this = _super.call(this) || this;
        _this.v = null;
        _this.u = null;
        _this.e = false;
        _this.w = null;
        _this.aa = null;
        _this.t = null;
        _this.c = 0;
        _this.b = null;
        _this.d = false;
        _this.n = 0;
        _this.j = '\0';
        _this.g = false;
        _this.ab = null;
        _this.ad = null;
        _this.q = null;
        _this.h = false;
        _this.p = 0;
        _this.m = 0;
        a = (a == void 0) ? 0 : a;
        switch (a) {
            case 0:
                {
                    var c = _rest[0];
                    var d = _rest[1];
                    var e = _rest[2];
                    var f = _rest[3];
                    _this.u = c;
                    _this.w = d;
                    _this.aa = e;
                    _this.v = f;
                    _this.d = (c != null && StringUtilitiesSgml.a(c, "html"));
                }
                break;
            case 1:
                {
                    var c = _rest[0];
                    var d = _rest[1];
                    _this.u = c;
                    _this.t = d;
                    _this.e = true;
                }
                break;
            case 2:
                {
                    var c = _rest[0];
                    var d = _rest[1];
                    var e = _rest[2];
                    var f = _rest[3];
                    _this.u = c;
                    _this.e = true;
                    _this.q = e;
                    _this.ad = d;
                    _this.v = f;
                    _this.d = (stringCompare1(c, "html", StringUtilities.j) == 0);
                }
                break;
        }
        return _this;
    }
    Object.defineProperty(Entity.prototype, "ae", {
        get: function () {
            if (this.ad != null) {
                return this.ad;
            }
            else if (this.b != null) {
                return this.b.ae;
            }
            return null;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Entity.prototype, "o", {
        get: function () {
            return this.m - this.p + 1;
        },
        enumerable: false,
        configurable: true
    });
    Entity.prototype.k = function () {
        var a = String.fromCharCode(this.q.b());
        if (a.charCodeAt(0) == 0) {
            a = ' ';
        }
        this.m++;
        if (a.charCodeAt(0) == 10) {
            this.g = true;
            this.p = this.m + 1;
            this.n++;
        }
        else if (a == ' ' || a == '\t') {
            this.g = true;
            if (this.j.charCodeAt(0) == 13) {
                this.p = this.m;
                this.n++;
            }
        }
        else if (a.charCodeAt(0) == 13) {
            this.g = true;
        }
        else {
            this.g = false;
            if (this.j.charCodeAt(0) == 13) {
                this.n++;
                this.p = this.m;
            }
        }
        this.j = a;
        return a;
    };
    Entity.prototype.ak = function (a, b) {
        this.b = a;
        if (a != null) {
            this.d = a.d;
        }
        this.n = 1;
        if (this.e) {
            if (this.t != null) {
                this.q = new StringReader(this.t);
            }
        }
        else if (this.aa == null) {
            this.aj("Unresolvable entity '{0}'", this.u);
        }
        else {
            this.aj("Unresolvable entity '{0}'", this.u);
        }
    };
    Entity.prototype.ac = function () {
        return this.ab;
    };
    Entity.prototype.af = function () {
        if (this.h) {
            this.q.i();
        }
    };
    Entity.prototype.l = function () {
        var a = this.j;
        while (a != "\uffff" && (a == ' ' || a == '\r' || a == '\n' || a == '\t')) {
            a = this.k();
        }
        return a;
    };
    Entity.prototype.z = function (a, b, c) {
        a.c = 0;
        var d = this.j;
        if (c && d != '_' && !isLetter(d)) {
            throw new BaseError(1, stringFormat("Invalid name start character '{0}'", d));
        }
        while (d != "\uffff" && b.indexOf(d) < 0) {
            if (!c || d == '_' || d == '.' || d == '-' || d == ':' || isLetterOrDigit(d)) {
                a.h(d);
            }
            else {
                throw new BaseError(1, stringFormat("Invalid name character '{0}'", d));
            }
            d = this.k();
        }
        return a.toString();
    };
    Entity.prototype.x = function (a, b) {
        a.c = 0;
        var c = this.k();
        while (c != "\uffff" && c != b) {
            if (c == '&') {
                c = this.k();
                if (c == '#') {
                    var d = this.s();
                    a.l(d);
                    c = this.j;
                }
                else {
                    a.h('&');
                    a.h(c);
                    c = this.k();
                }
            }
            else {
                a.h(c);
                c = this.k();
            }
        }
        this.k();
        return a.toString();
    };
    Entity.prototype.y = function (a, b, c) {
        if (a != null) {
            a.c = 0;
        }
        var d = this.n;
        var e = this.k();
        var f = 0;
        var g = c.charAt(f);
        while (e != "\uffff") {
            if (e == g) {
                f++;
                if (f >= c.length) {
                    break;
                }
                g = c.charAt(f);
            }
            else if (f > 0) {
                var h = f - 1;
                var i = 0;
                while (h >= 0 && i == 0) {
                    if (c.charAt(h) == e) {
                        var j = 1;
                        while (h - j >= 0) {
                            if (c.charAt(h - j) != c.charAt(f - j)) {
                                break;
                            }
                            j++;
                        }
                        if (j > h) {
                            i = h + 1;
                        }
                    }
                    else {
                        h--;
                    }
                }
                if (a != null) {
                    h = (h < 0) ? 1 : 0;
                    for (var k = 0; k <= f - i - h; k++) {
                        a.h(c.charAt(k));
                    }
                    if (h > 0) {
                        a.h(e);
                    }
                }
                f = i;
                g = c.charAt(i);
            }
            else {
                if (a != null) {
                    a.h(e);
                }
            }
            e = this.k();
        }
        if (e.charCodeAt(0) == 0) {
            this.ai(b + " starting on line {0} was never closed", d);
        }
        this.k();
        if (a != null) {
            return a.toString();
        }
        return "";
    };
    Entity.prototype.s = function () {
        var a = this.k();
        var b = 0;
        if (a == 'x') {
            a = this.k();
            for (; a != "\uffff" && a != ';'; a = this.k()) {
                var c = 0;
                if (a.charCodeAt(0) >= '0'.charCodeAt(0) && a.charCodeAt(0) <= '9'.charCodeAt(0)) {
                    c = (a.charCodeAt(0) - '0'.charCodeAt(0));
                }
                else if (a.charCodeAt(0) >= 'a'.charCodeAt(0) && a.charCodeAt(0) <= 'f'.charCodeAt(0)) {
                    c = (a.charCodeAt(0) - 'a'.charCodeAt(0)) + 10;
                }
                else if (a.charCodeAt(0) >= 'A'.charCodeAt(0) && a.charCodeAt(0) <= 'F'.charCodeAt(0)) {
                    c = (a.charCodeAt(0) - 'A'.charCodeAt(0)) + 10;
                }
                else {
                    break;
                }
                b = (b * 16) + c;
            }
        }
        else {
            for (; a != "\uffff" && a != ';'; a = this.k()) {
                if (a.charCodeAt(0) >= '0'.charCodeAt(0) && a.charCodeAt(0) <= '9'.charCodeAt(0)) {
                    b = (b * 10) + (a.charCodeAt(0) - '0'.charCodeAt(0));
                }
                else {
                    break;
                }
            }
        }
        if (a.charCodeAt(0) == 0) {
            this.ah("Premature {0} parsing entity reference", a);
        }
        else if (a == ';') {
            this.k();
        }
        if (this.d && b >= 128 && b <= 159) {
            var d = Entity.a.length;
            var e = b - 128;
            var f = Entity.a[e];
            return Convert.toChar1(f).toString();
        }
        return Convert.toChar1(b).toString();
    };
    Entity.prototype.ag = function (a) {
        throw new BaseError(1, a);
    };
    Entity.prototype.ah = function (a, b) {
        var c = (b == "\uffff") ? "EOF" : b.toString();
        throw new BaseError(1, stringFormat(a, c));
    };
    Entity.prototype.ai = function (a, b) {
        throw new BaseError(1, stringFormat(a, b));
    };
    Entity.prototype.aj = function (a, b) {
        throw new BaseError(1, stringFormat(a, b));
    };
    Entity.prototype.r = function () {
        var a = this;
        var b = new StringBuilder(0);
        while (a != null) {
            var c = void 0;
            if (a.e) {
                c = stringFormat("\nReferenced on line {0}, position {1} of internal entity '{2}'", a.n, a.o, a.u);
            }
            else {
                c = stringFormat1("\nReferenced on line {0}, position {1} of '{2}' entity at [{3}]", a.n, a.o, a.u, a.ae.absolutePath);
            }
            b.l(c);
            a = a.b;
        }
        return b.toString();
    };
    Entity.f = function (a) {
        return (a == "CDATA" || a == "SDATA" || a == "PI");
    };
    Entity.prototype.al = function (a) {
        switch (a) {
            case "CDATA":
                this.c = 0;
                break;
            case "SDATA":
                this.c = 1;
                break;
            case "PI":
                this.c = 2;
                break;
        }
    };
    Entity.$t = markType(Entity, 'Entity');
    Entity.a = [8364, 129, 8218, 402, 8222, 8230, 8224, 8225, 710, 8240, 352, 8249, 338, 141, 381, 143, 144, 8216, 8217, 8220, 8221, 8226, 8211, 8212, 732, 8482, 353, 8250, 339, 157, 382, 376];
    return Entity;
}(Base));
export { Entity };
