/*
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 * as tslib_1 from "tslib";
import { CategoryAxisBase } from "./CategoryAxisBase";
import { DependencyProperty } from "igniteui-react-core/ES5/DependencyProperty";
import { List$1 } from "igniteui-react-core/ES5/List$1";
import { Base, Number_$type, typeCast, Array_$type, markType, TypeRegistrar } from "igniteui-react-core/ES5/type";
import { HorizontalAxisLabelPanel } from "./HorizontalAxisLabelPanel";
import { Rect } from "igniteui-react-core/ES5/Rect";
import { MathUtil } from "igniteui-react-core/ES5/MathUtil";
import { ScalerParams } from "./ScalerParams";
import { NumericScaler } from "./NumericScaler";
import { LinearCategorySnapper } from "./LinearCategorySnapper";
import { NumericYAxis } from "./NumericYAxis";
import { LabelPosition } from "./LabelPosition";
import { AxisRangeChangedEventArgs } from "./AxisRangeChangedEventArgs";
import { PropertyMetadata } from "igniteui-react-core/ES5/PropertyMetadata";
import { truncate, isNaN_, isInfinity } from "igniteui-react-core/ES5/number";
import { CategoryAxisBaseView } from "./CategoryAxisBaseView";
/**
 * @hidden
 */
var CategoryXAxis = /** @class */ (function (_super) {
    tslib_1.__extends(CategoryXAxis, _super);
    function CategoryXAxis() {
        var _this = _super.call(this) || this;
        _this.k2 = 1;
        _this.k1 = 1;
        _this.ks = 0;
        _this.kt = 0;
        _this.jf = new List$1(Number_$type, 0);
        _this.ab = CategoryXAxis.$;
        return _this;
    }
    CategoryXAxis.prototype.a1 = function () {
        return new CategoryXAxisView(this);
    };
    CategoryXAxis.prototype.gm = function (a) {
        _super.prototype.gm.call(this, a);
        this.kq = a;
    };
    Object.defineProperty(CategoryXAxis.prototype, "k4", {
        get: function () {
            return this.k2;
        },
        set: function (a) {
            this.k2 = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(CategoryXAxis.prototype, "k3", {
        get: function () {
            return this.k1;
        },
        set: function (a) {
            this.k1 = a;
        },
        enumerable: true,
        configurable: true
    });
    CategoryXAxis.prototype.as = function () {
        if (this.co) {
            return (TypeRegistrar.create('HorizontalSmartAxisLabelPanel'));
        }
        else {
            return new HorizontalAxisLabelPanel();
        }
    };
    CategoryXAxis.prototype.getCategorySize = function (a, b, c) {
        var d = this.is(b, c);
        return d.width / (this.jr * a.width);
    };
    CategoryXAxis.prototype.jk = function (a, b, c) {
        var d = !isNaN_(this.jh) ? MathUtil.b(this.jh, 0, 1) : 0;
        var e = 0;
        if (!isNaN_(this.jo)) {
            e = Math.min(this.jo, 1);
        }
        else {
            e = 0;
        }
        ;
        var f = 1 - 0.5 * d;
        var g = this.ju == 0 ? 1 : this.ju;
        var h = this.getCategorySize(a, b, c) * f / (g - (g - 1) * e);
        return h;
    };
    CategoryXAxis.prototype.getGroupCenter = function (a, b, c, d) {
        var e = 0.5;
        if (this.ju > 1) {
            var f = !isNaN_(this.jh) ? MathUtil.b(this.jh, 0, 1) : 0;
            var g = 0;
            if (!isNaN_(this.jo)) {
                g = Math.min(this.jo, 1);
            }
            var h = 1 - 0.5 * f;
            var i = h / (this.ju - (this.ju - 1) * g);
            var j = (h - i) / (this.ju - 1);
            e = 0.25 * f + 0.5 * i + a * j;
        }
        return this.getCategorySize(b, c, d) * e;
    };
    CategoryXAxis.prototype.dg = function () {
        return this.kz;
    };
    CategoryXAxis.prototype.k9 = function (a) {
        var b = this.bf != null ? this.bf.mq : Rect.empty;
        b = b.copy();
        var c = this.iu;
        var d = this.ir();
        var e = new Rect(0, 0, 0, 1, 1);
        var f = new ScalerParams(0, e, e, this.b8, d);
        var g = !b.isEmpty && !c.isEmpty && this.i8 != null ? this.i8.indexOf(a) : -1;
        var h = g > -1 ? this.dt(g, f) : NaN;
        if (!isNaN_(h) && this.bf.c0) {
            if (!isNaN_(h)) {
                if (h < b.left + 0.1 * b.width) {
                    h = h + 0.4 * b.width;
                    b.x = h - 0.5 * b.width;
                }
                if (h > b.right - 0.1 * b.width) {
                    h = h - 0.4 * b.width;
                    b.x = h - 0.5 * b.width;
                }
            }
            this.bf.km(b, false, true);
        }
    };
    CategoryXAxis.prototype.dt = function (a, b) {
        if (!b.c.isEmpty) {
            var c = this.ky(a, NumericScaler.ac, b.c, this.categoryMode);
            c = (c - b.d.left) / b.d.width;
            c = b.d.left + b.d.width * (c - b.e.left) / b.e.width;
            return c;
        }
        return this.ky(a, b.e, b.d, this.categoryMode);
    };
    CategoryXAxis.prototype.kp = function (a) {
        if (typeCast(List$1.$.specialize(Array_$type), a) !== null) {
            return a.asArray();
        }
        return null;
    };
    CategoryXAxis.prototype.gg = function (a, b, c, d, e) {
        var f;
        var g = e.e;
        var h = e.d;
        var i = e.c;
        var j = e.a;
        var k = !i.isEmpty;
        var l = this.categoryMode == 0 ? this.jr - 1 : this.jr;
        if (l < 0) {
            l = 0;
        }
        var m = i.left;
        var n = i.width;
        var o = g.left;
        var p = g.width;
        var q = h.left;
        var r = h.width;
        var s = 0;
        var t = 1;
        var u = this.kp(a);
        var v = false;
        if (u != null) {
            v = true;
        }
        var w;
        for (var x = 0; x < b.count; x++) {
            w = b.item(x);
            for (var y = c; y < d; y++) {
                if (v) {
                    f = u[y];
                }
                else {
                    f = a.item(y);
                }
                if (k) {
                    var z = l >= 1 ? (f[w]) / (l) : l == 0 ? 0.5 : NaN;
                    if (j) {
                        z = 1 - z;
                    }
                    z = m + n * (z - s) / t;
                    var aa = (z - (o * r)) / p;
                    f[w] = aa;
                }
                else {
                    var ab = l >= 1 ? (f[w]) / (l) : l == 0 ? 0.5 : NaN;
                    if (j) {
                        ab = 1 - ab;
                    }
                    ab = q + r * (ab - o) / p;
                    f[w] = ab;
                }
            }
        }
    };
    CategoryXAxis.prototype.du = function (a, b, c) {
        if (!b.c.isEmpty) {
            var d = this.ky(a, NumericScaler.ac, b.c, c);
            d = (d - b.d.left) / b.d.width;
            d = b.d.left + b.d.width * (d - b.e.left) / b.e.width;
            return d;
        }
        return this.ky(a, b.e, b.d, c);
    };
    CategoryXAxis.prototype.ky = function (a, b, c, d) {
        var e = d == 0 ? this.jr - 1 : this.jr;
        if (e < 0) {
            e = 0;
        }
        var f = e >= 1 ? (a) / (e) : e == 0 ? 0.5 : NaN;
        if (this.b9) {
            f = 1 - f;
        }
        return c.left + c.width * (f - b.left) / b.width;
    };
    CategoryXAxis.prototype.dw = function (a, b) {
        if (!b.c.isEmpty) {
            var c = b.e.left + b.e.width * (a - b.d.left) / b.d.width;
            c = (c * b.d.width) + b.d.left;
            c = this.getUnscaledValue3(c, NumericScaler.ac, b.c, this.categoryMode);
            return c;
        }
        return this.getUnscaledValue3(a, b.e, b.d, this.categoryMode);
    };
    CategoryXAxis.prototype.dx = function (a, b, c) {
        if (!b.c.isEmpty) {
            var d = b.e.left + b.e.width * (a - b.d.left) / b.d.width;
            d = (d * b.d.width) + b.d.left;
            d = this.getUnscaledValue3(d, NumericScaler.ac, b.c, c);
            return d;
        }
        return this.getUnscaledValue3(a, b.e, b.d, c);
    };
    CategoryXAxis.prototype.getUnscaledValue3 = function (a, b, c, d) {
        var e = b.left + (a - c.left) * b.width / c.width;
        if (this.b9) {
            e = 1 - e;
        }
        var f = d == 0 ? this.jr - 1 : this.jr;
        if (f < 0) {
            f = 0;
        }
        return e * f;
    };
    CategoryXAxis.prototype.la = function (a, b) {
        if (a == NaN || b == NaN || this.i8 == null || this.i8.count == 0) {
            return;
        }
        var c = a / this.i8.count;
        var d = (b + 1) / this.i8.count;
        this.bf.m4 = new Rect(0, c, this.bf.m4.y, d - c, this.bf.m4.height);
    };
    CategoryXAxis.prototype.g1 = function (a) {
        _super.prototype.g1.call(this, a);
        var b = this.bf != null ? this.bf.mq : Rect.empty;
        var c = this.iu;
        var d = this.ip();
        var e = this.io();
        var f = new ScalerParams(0, b, c, this.b8, d);
        var g = this.a2.ax();
        var h = this.a2.a0();
        var i = this.a2.ay();
        var j = this.a2.az();
        var k = this.a2.e();
        var l = this.a2.f();
        var m = this.a2.g();
        var n = this.i8;
        var o = this.kr();
        this.g8();
        this.gc(g);
        this.gc(h);
        this.gc(i);
        this.gc(j);
        this.cs.clear();
        this.ct.clear();
        this.jf.clear();
        this.a2.as(this, b, c);
        if (b.isEmpty || c.isEmpty) {
            this.bo.count = 0;
            this.bn.count = 0;
            this.bm.count = 0;
        }
        if (this.bo.count == 0 && this.bn.count == 0) {
            this.a2.x();
        }
        if (this.aw != null) {
            this.aw.registerAxis(this);
        }
        if (this.itemsSource == null || o == null || n.count == 0) {
            this.bo.count = 0;
            this.bn.count = 0;
            this.bm.count = 0;
            this.a2.x();
            return;
        }
        if (!b.isEmpty && !c.isEmpty) {
            var p = this.dw(e.left, f);
            var q = this.dw(e.right, f);
            if (this.b8) {
                p = Math.ceil(p);
                q = Math.floor(q);
            }
            else {
                p = Math.floor(p);
                q = Math.ceil(q);
            }
            var r = c.bottom;
            var s = r - c.top;
            if (this.ag != null) {
                var t = typeCast(NumericYAxis.$, this.ag);
                if (t != null) {
                    var u = this.g4(r, s, b, c, d, c.top, c.bottom);
                    r = u.p0;
                    s = u.p1;
                }
            }
            this.gk(g, r, c, k, true);
            this.a2.an(s);
            var v = Math.min(p, q);
            var w = Math.max(p, q);
            var x = new LinearCategorySnapper(1, v, w, e.width, this.kz, this.categoryMode, this.d6());
            var y = Math.floor((v - 0) / x.g);
            var z = Math.ceil((w - 0) / x.g);
            this.ku = x.g;
            if (!isNaN_(y) && !isNaN_(z)) {
                var aa = truncate(y);
                var ab = truncate(z);
                var ac = this.kx(0 + aa * x.g, f);
                var ad = this.dt(x.g, f);
                var ae = Math.abs(ad - this.dt(0, f));
                this.a2.ao(ad);
                var af = truncate(Math.ceil(e.right));
                var ag = truncate(Math.floor(e.left));
                var ah = this.k0;
                var ai = ah > 0 && ah < x.g;
                var aj = c.right;
                var ak = this.jf;
                var al = x.g;
                var am = truncate(al);
                var an = this.ju;
                var ao = this.b8;
                var ap = this.categoryMode;
                for (var aq = aa; aq <= ab; ++aq) {
                    var ar = this.kx(0 + (aq + 1) * x.g, f);
                    if (ac <= aj) {
                        if (aq % 2 == 0) {
                            this.ha(h, ac, ar, c);
                        }
                        this.g9(i, ac, c, l, false);
                        ak.add(ac);
                    }
                    if (this.categoryMode != 0 && this.ju != 0 && this.ci) {
                        if (ai) {
                            var as = ae * this.k0;
                            if (this.b8) {
                                var at = ac;
                                while (at - as > ar) {
                                    at -= as;
                                    if (Math.abs(at - ar) < 0.0001) {
                                        break;
                                    }
                                    this.g9(j, at, c, m, false);
                                }
                            }
                            else {
                                var au = ac;
                                while (au + as < ar) {
                                    au += as;
                                    if (Math.abs(au - ar) < 0.0001) {
                                        break;
                                    }
                                    this.g9(j, au, c, m, false);
                                }
                            }
                            this.kv = this.k0;
                        }
                        else {
                            for (var av = 0; av < truncate(x.g); av++) {
                                for (var aw = 0; aw < this.ju; aw++) {
                                    var ax = this.getGroupCenter(aw, b, c, d);
                                    if (this.b8) {
                                        ax = -ax;
                                    }
                                    var ay = this.dt(av + aq * x.g, f) + ax;
                                    this.g9(j, ay, c, m, false);
                                }
                            }
                            this.kv = 0.5;
                        }
                    }
                    var az = this.kw(aq * al, ac, ap == 0, f);
                    var a0 = truncate(Math.round(az));
                    if (a0 >= ag && a0 <= af) {
                        var a1 = 0;
                        if (al >= 1) {
                            a1 = aq * truncate(Math.floor(al));
                        }
                        else {
                            if ((aq * al) * 2 % 2 == 0) {
                                a1 = truncate(Math.floor(aq * al));
                            }
                            else {
                                a1 = -1;
                            }
                        }
                        if (o != null && a1 < n.count && a1 >= 0) {
                            var a2 = o.getItem(a1);
                            var a3 = this.e1(a2);
                            if (!isNaN_(az) && !isInfinity(az) && a3 != null) {
                                if ((typeof a3 === 'string') && Base.equalsStatic(a3, "")) {
                                }
                                else {
                                    this.cs.add1(a3);
                                    this.ct.add(new LabelPosition(az));
                                }
                            }
                        }
                    }
                    ac = ar;
                }
            }
            if ((this.aw == null || this.aw.visibility == 0) && this.ag != null) {
                if (this.aw != null && (this.aw.location == 4 || this.aw.location == 5)) {
                    this.bf.ht();
                }
            }
            this.a2.at(this.cs, this.ct);
            this.kf();
        }
    };
    CategoryXAxis.prototype.cm = function () {
        if (this.cf && this.cn) {
            this.a9 = this.a8();
        }
        if (this.i8 == null) {
            return false;
        }
        var a = this.i8.count;
        if (a != this.k3) {
            var b = new AxisRangeChangedEventArgs(0, 1, 1, this.k3, a);
            this.k3 = a;
            this.gv(b);
            return true;
        }
        return false;
    };
    Object.defineProperty(CategoryXAxis.prototype, "kz", {
        get: function () {
            return this.c(CategoryXAxis.lb);
        },
        set: function (a) {
            this.h(CategoryXAxis.lb, a);
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(CategoryXAxis.prototype, "ku", {
        get: function () {
            return this.ks;
        },
        set: function (a) {
            if (this.ku != a) {
                var b = this.ks;
                this.ks = a;
                this.gu("ActualInterval", b, this.ku);
            }
        },
        enumerable: true,
        configurable: true
    });
    CategoryXAxis.prototype.d4 = function () {
        return this.ku;
    };
    Object.defineProperty(CategoryXAxis.prototype, "k0", {
        get: function () {
            return this.c(CategoryXAxis.lc);
        },
        set: function (a) {
            this.h(CategoryXAxis.lc, a);
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(CategoryXAxis.prototype, "kv", {
        get: function () {
            return this.kt;
        },
        set: function (a) {
            if (this.kv != a) {
                var b = this.kt;
                this.kt = a;
                this.gu("ActualMinorInterval", b, this.kv);
            }
        },
        enumerable: true,
        configurable: true
    });
    CategoryXAxis.prototype.jb = function (a) {
        if (a == null) {
            return false;
        }
        var b = this.bb();
        if (b == null) {
            return false;
        }
        return b.synchronizeHorizontally;
    };
    CategoryXAxis.prototype.get_az = function () {
        return 0;
    };
    Object.defineProperty(CategoryXAxis.prototype, "az", {
        get: function () {
            return this.get_az();
        },
        enumerable: true,
        configurable: true
    });
    CategoryXAxis.prototype.kr = function () {
        return this.i8;
    };
    CategoryXAxis.prototype.kx = function (a, b) {
        return this.dt(a, b);
    };
    CategoryXAxis.prototype.kw = function (a, b, c, d) {
        if (!c) {
            var e = this.kx(a + 1, d);
            return (b + e) / 2;
        }
        return b;
    };
    Object.defineProperty(CategoryXAxis.prototype, "isVertical", {
        get: function () {
            return this.ce;
        },
        enumerable: true,
        configurable: true
    });
    CategoryXAxis.prototype.j$c = function (a, b) {
        return this.dw.apply(this, arguments);
    };
    CategoryXAxis.prototype.j$b = function (a, b) {
        return this.dt.apply(this, arguments);
    };
    CategoryXAxis.prototype.j$d = function (a, b, c, d, e) {
        this.gg.apply(this, arguments);
    };
    CategoryXAxis.prototype.j$e = function (a, b, c, d) {
        this.gh.apply(this, arguments);
    };
    CategoryXAxis.prototype.j$f = function (a, b, c, d) {
        this.gi.apply(this, arguments);
    };
    Object.defineProperty(CategoryXAxis.prototype, "j$a", {
        get: function () {
            return this.b8;
        },
        enumerable: true,
        configurable: true
    });
    CategoryXAxis.$t = markType(CategoryXAxis, 'CategoryXAxis', CategoryAxisBase.$);
    CategoryXAxis.lb = DependencyProperty.i("Interval", Number_$type, CategoryXAxis.$, new PropertyMetadata(2, NaN, function (a, b) {
        a.gu("Interval", b.oldValue, b.newValue);
        a.g0(false);
    }));
    CategoryXAxis.lc = DependencyProperty.i("MinorInterval", Number_$type, CategoryXAxis.$, new PropertyMetadata(2, NaN, function (a, b) {
        (typeCast(CategoryXAxis.$, a)).gu("MinorInterval", b.oldValue, b.newValue);
        (typeCast(CategoryXAxis.$, a)).g0(false);
    }));
    return CategoryXAxis;
}(CategoryAxisBase));
export { CategoryXAxis };
/**
 * @hidden
 */
var CategoryXAxisView = /** @class */ (function (_super) {
    tslib_1.__extends(CategoryXAxisView, _super);
    function CategoryXAxisView(a) {
        var _this = _super.call(this, a) || this;
        _this.bf = null;
        _this.bf = a;
        return _this;
    }
    CategoryXAxisView.$t = markType(CategoryXAxisView, 'CategoryXAxisView', CategoryAxisBaseView.$);
    return CategoryXAxisView;
}(CategoryAxisBaseView));
export { CategoryXAxisView };
