/*
THIS INFRAGISTICS ULTIMATE SOFTWARE LICENSE  AGREEMENT ("AGREEMENT") LOCATED HERE:
https://www.infragistics.com/legal/license/igultimate-la
https://www.infragistics.com/legal/license/igultimate-eula
GOVERNS THE LICENSING, INSTALLATION AND USE OF INFRAGISTICS SOFTWARE. BY DOWNLOADING AND/OR INSTALLING AND USING INFRAGISTICS SOFTWARE:  you are indicating that you have read and understand this Agreement, and agree to be legally bound by it on behalf of the yourself and your company.
*/
import { __extends, __values } from "tslib";
import { Base, Number_$type, fromEnum, markType } from "igniteui-angular-core";
import { BindingFormatter } from "igniteui-angular-core";
import { NumberFormatSpecifier } from "igniteui-angular-core";
import { Dictionary$2 } from "igniteui-angular-core";
import { DataAbbreviation } from "./DataAbbreviation";
import { DataAbbreviationMode_$type } from "igniteui-angular-core";
import { List$1 } from "igniteui-angular-core";
import { truncate, isNaN_, isInfinity } from "igniteui-angular-core";
import { stringContains, stringIsNullOrEmpty, stringToCharArray } from "igniteui-angular-core";
import { padRight, stringFormat, trimEnd } from "igniteui-angular-core";
import { round10N } from "igniteui-angular-core";
/**
 * @hidden
 */
var DataAbbreviator = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(DataAbbreviator, _super);
    function DataAbbreviator() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    DataAbbreviator.p = function (a, b, c) {
        if (!DataAbbreviator.e.containsKey(c)) {
            var d = new BindingFormatter();
            d.j = "{0}";
            d.d = [((function () {
                    var $ret = new NumberFormatSpecifier();
                    $ret.locale = "en-US";
                    $ret.useGrouping = false;
                    $ret.maximumFractionDigits = c;
                    return $ret;
                })())];
            DataAbbreviator.e.item(c, d);
        }
        var e = DataAbbreviator.e.item(c);
        var f = e.i(a);
        if (stringContains(f, ",")) {
            f = f.split(',')[1];
        }
        else if (stringContains(f, ".")) {
            f = f.split('.')[1];
        }
        else {
            f = "";
        }
        if (b != -1 && f.length < b) {
            f = padRight(f, f.length + b, '0');
        }
        if (c != -1 && f.length > c) {
            f = f.substr(0, c);
        }
        return f;
    };
    DataAbbreviator.o = function (a) {
        var b = stringFormat("0:0.0", a);
        if (stringContains(b, ",")) {
            return ",";
        }
        return ".";
    };
    DataAbbreviator.r = function (a) {
        var b = Math.floor(a);
        var c = stringFormat("{0}", b);
        return c;
    };
    DataAbbreviator.b = function (a, b, c, d) {
        var e = new DataAbbreviation();
        e.a = d;
        e.c = DataAbbreviator.l(d);
        e.l = DataAbbreviator.q(d);
        e.g = a;
        e.k = (e.g).toString();
        e.d = (e.g / e.c);
        if (c == -1 && d != 0 && d != 1 && d != 9 && d != 8) {
            c = 2;
            e.d = round10N(e.d, 2);
        }
        else if (c == -1) {
            c = 5;
            e.d = round10N(e.d, 5);
        }
        var f = DataAbbreviator.o(e.d);
        if (b < 0 && c < 0) {
            e.f = truncate(Math.floor(e.d));
            e.e = e.d - e.f;
            e.n = DataAbbreviator.r(e.d);
            e.j = trimEnd(DataAbbreviator.p(e.d, b, c), '0');
            e.i = DataAbbreviator.n(e.j);
            e.h = DataAbbreviator.m(e.j);
            e.m = e.n;
            if (!stringIsNullOrEmpty(e.j)) {
                e.m += f + e.j;
            }
        }
        else if (b >= 0) {
            if (c >= b) {
                e.d = round10N(e.d, c);
            }
            e.f = truncate(Math.floor(e.d));
            e.e = e.d - e.f;
            e.n = DataAbbreviator.r(e.d);
            e.j = DataAbbreviator.p(e.d, b, c);
            e.m = e.n;
            if (!stringIsNullOrEmpty(e.j)) {
                var g = Math.max(b, trimEnd(e.j, '0').length);
                g = Math.min(g, e.j.length);
                e.j = e.j.substr(0, g);
                e.m += f + e.j;
                e.i = e.j.length;
                e.h = e.j.length;
            }
            else {
                e.i = 0;
                e.h = -1;
            }
        }
        else if (c >= 0) {
            e.d = round10N(e.d, c);
            e.f = truncate(Math.floor(e.d));
            e.e = e.d - e.f;
            e.n = DataAbbreviator.r(e.d);
            e.j = trimEnd(DataAbbreviator.p(e.d, b, c), '0');
            e.i = DataAbbreviator.n(e.j);
            e.h = DataAbbreviator.m(e.j);
            e.m = e.n;
            if (!stringIsNullOrEmpty(e.j)) {
                e.m += f + e.j;
            }
        }
        return e;
    };
    DataAbbreviator.s = function () {
        var e_1, _a;
        if (DataAbbreviator.k == null) {
            DataAbbreviator.k = ((function () {
                var $ret = new List$1(DataAbbreviation.$, 0);
                $ret.add(((function () {
                    var $ret = new DataAbbreviation();
                    $ret.a = 7;
                    $ret.l = "Q";
                    $ret.c = 1E+15;
                    return $ret;
                })()));
                $ret.add(((function () {
                    var $ret = new DataAbbreviation();
                    $ret.a = 6;
                    $ret.l = "T";
                    $ret.c = 1000000000000;
                    return $ret;
                })()));
                $ret.add(((function () {
                    var $ret = new DataAbbreviation();
                    $ret.a = 5;
                    $ret.l = "B";
                    $ret.c = 1000000000;
                    return $ret;
                })()));
                $ret.add(((function () {
                    var $ret = new DataAbbreviation();
                    $ret.a = 4;
                    $ret.l = "M";
                    $ret.c = 1000000;
                    return $ret;
                })()));
                $ret.add(((function () {
                    var $ret = new DataAbbreviation();
                    $ret.a = 3;
                    $ret.l = "K";
                    $ret.c = 1000;
                    return $ret;
                })()));
                $ret.add(((function () {
                    var $ret = new DataAbbreviation();
                    $ret.a = 9;
                    $ret.l = "";
                    $ret.c = 1;
                    return $ret;
                })()));
                return $ret;
            })());
            DataAbbreviator.f = new Dictionary$2(DataAbbreviationMode_$type, DataAbbreviation.$, 0);
            try {
                for (var _b = __values(fromEnum(DataAbbreviator.k)), _c = _b.next(); !_c.done; _c = _b.next()) {
                    var a = _c.value;
                    DataAbbreviator.f.addItem(a.a, a);
                }
            }
            catch (e_1_1) {
                e_1 = { error: e_1_1 };
            }
            finally {
                try {
                    if (_c && !_c.done && (_a = _b.return))
                        _a.call(_b);
                }
                finally {
                    if (e_1)
                        throw e_1.error;
                }
            }
        }
    };
    DataAbbreviator.l = function (a) {
        if (DataAbbreviator.f.containsKey(a)) {
            return DataAbbreviator.f.item(a).c;
        }
        return 1;
    };
    DataAbbreviator.q = function (a) {
        if (DataAbbreviator.f.containsKey(a)) {
            return DataAbbreviator.f.item(a).l;
        }
        return "";
    };
    DataAbbreviator.a = function (a, b, c, d) {
        var e = [a];
        var f = DataAbbreviator.h(e, b, c, d);
        return f._inner[0];
    };
    DataAbbreviator.h = function (a, b, c, d) {
        if (c === void 0) {
            c = -1;
        }
        if (d === void 0) {
            d = -1;
        }
        DataAbbreviator.s();
        if (c > d) {
            d = c;
        }
        var e = b;
        if (e == 8 || e == 0) {
            e = 2;
        }
        if (e == 9) {
            return DataAbbreviator.i(a, c, d);
        }
        else if (e == 1) {
            return DataAbbreviator.g(a, c, d);
        }
        else if (e == 2) {
            e = DataAbbreviator.d(a);
        }
        if (e == 9 || e == 3 || e == 4 || e == 5 || e == 6 || e == 7) {
            return DataAbbreviator.j(e, a, c, d);
        }
        return DataAbbreviator.i(a, c, d);
    };
    DataAbbreviator.j = function (a, b, c, d) {
        var e_2, _a;
        if (c === void 0) {
            c = -1;
        }
        if (d === void 0) {
            d = -1;
        }
        var e = new List$1(DataAbbreviation.$, 0);
        for (var f = 0; f < b.length; f++) {
            var g = b[f];
            if (isNaN_(g) || isInfinity(g) || !DataAbbreviator.f.containsKey(a)) {
                e.add(new DataAbbreviation());
            }
            else {
                var h = DataAbbreviator.b(g, c, d, a);
                e.add(h);
            }
        }
        var i = c;
        var j = d;
        if (i < 0 || j < 0) {
            try {
                for (var _b = __values(fromEnum(e)), _c = _b.next(); !_c.done; _c = _b.next()) {
                    var k = _c.value;
                    i = Math.max(i, k.i);
                    j = Math.max(j, k.h);
                }
            }
            catch (e_2_1) {
                e_2 = { error: e_2_1 };
            }
            finally {
                try {
                    if (_c && !_c.done && (_a = _b.return))
                        _a.call(_b);
                }
                finally {
                    if (e_2)
                        throw e_2.error;
                }
            }
            if (j > i) {
                i = j;
            }
        }
        for (var l = 0; l < e.count; l++) {
            if (e._inner[l].b()) {
                var m = e._inner[l].g;
                var n = DataAbbreviator.b(m, i, j, a);
                e._inner[l] = n;
            }
        }
        return e;
    };
    DataAbbreviator.g = function (a, b, c) {
        if (b === void 0) {
            b = -1;
        }
        if (c === void 0) {
            c = -1;
        }
        DataAbbreviator.s();
        var d = new List$1(DataAbbreviation.$, 0);
        for (var e = 0; e < a.length; e++) {
            var f = a[e];
            if (isNaN_(f) || isInfinity(f)) {
                d.add(new DataAbbreviation());
            }
            else {
                var g = DataAbbreviator.c(f);
                var h = DataAbbreviator.b(f, b, c, g);
                d.add(h);
            }
        }
        return d;
    };
    DataAbbreviator.i = function (a, b, c) {
        if (b === void 0) {
            b = -1;
        }
        if (c === void 0) {
            c = -1;
        }
        var d = new List$1(DataAbbreviation.$, 0);
        for (var e = 0; e < a.length; e++) {
            var f = a[e];
            if (isNaN_(f) || isInfinity(f)) {
                d.add(new DataAbbreviation());
            }
            else {
                var g = DataAbbreviator.b(f, b, c, 9);
                d.add(g);
                DataAbbreviator.t("abbr.N min=" + g.i + " max=" + g.h + " abbr=" + g.m + g.l + "         for " + f);
            }
        }
        return d;
    };
    DataAbbreviator.m = function (a) {
        var b = stringToCharArray(a);
        if (stringContains(a, ".")) {
            b = stringToCharArray(a.split('.')[1]);
        }
        for (var c = b.length - 1; c >= 0; c--) {
            if (b[c] != '0') {
                return c + 1;
            }
        }
        return -1;
    };
    DataAbbreviator.n = function (a) {
        var b = stringToCharArray(a);
        if (stringContains(a, ".")) {
            b = stringToCharArray(a.split('.')[1]);
        }
        for (var c = 0; c < b.length; c++) {
            if (b[c] != '0') {
                return c + 1;
            }
        }
        return 0;
    };
    DataAbbreviator.d = function (a) {
        var b = DataAbbreviator.g(a);
        b.aa(function (c, d) {
            if (c.c < d.c) {
                return -1;
            }
            if (c.c > d.c) {
                return 1;
            }
            return 0;
        });
        var c = truncate(Math.floor(b.count / 2));
        var d = b._inner[c];
        return d.a;
    };
    DataAbbreviator.c = function (a) {
        if (isNaN_(a) || isInfinity(a)) {
            return 9;
        }
        var b = Math.abs(a);
        for (var c = 0; c < DataAbbreviator.k.count; c++) {
            var d = DataAbbreviator.k._inner[c];
            if (b >= d.c || c == DataAbbreviator.k.count - 1) {
                return d.a;
            }
        }
        return 9;
    };
    DataAbbreviator.t = function (message_) {
    };
    DataAbbreviator.$t = markType(DataAbbreviator, 'DataAbbreviator');
    DataAbbreviator.k = null;
    DataAbbreviator.f = null;
    DataAbbreviator.e = new Dictionary$2(Number_$type, BindingFormatter.$, 0);
    return DataAbbreviator;
}(Base));
export { DataAbbreviator };
