/*
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 } from "tslib";
import { CategoryAxisBase } from "./CategoryAxisBase";
import { Base, Number_$type, typeCast, Array_$type, markType, TypeRegistrar } from "igniteui-angular-core";
import { DependencyProperty } from "igniteui-angular-core";
import { List$1 } from "igniteui-angular-core";
import { HorizontalAxisLabelPanel } from "./HorizontalAxisLabelPanel";
import { Rect } from "igniteui-angular-core";
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-angular-core";
import { truncate, isNaN_, isInfinity } from "igniteui-angular-core";
import { CategoryAxisBaseView } from "./CategoryAxisBaseView";
import { AxisDefaults } from "./AxisDefaults";
/**
 * @hidden
 */
var CategoryXAxis = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(CategoryXAxis, _super);
    function CategoryXAxis() {
        var _this = _super.call(this) || this;
        _this.mt = 1;
        _this.ms = 1;
        _this.mw = null;
        _this.mj = 0;
        _this.mk = 0;
        _this.kx = new List$1(Number_$type, 0);
        _this.ab = CategoryXAxis.$;
        return _this;
    }
    CategoryXAxis.prototype.a3 = function () {
        return new CategoryXAxisView(this);
    };
    CategoryXAxis.prototype.hr = function (a) {
        _super.prototype.hr.call(this, a);
        this.mg = a;
    };
    Object.defineProperty(CategoryXAxis.prototype, "mv", {
        get: function () {
            return this.mt;
        },
        set: function (a) {
            this.mt = a;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(CategoryXAxis.prototype, "mu", {
        get: function () {
            return this.ms;
        },
        set: function (a) {
            this.ms = a;
        },
        enumerable: false,
        configurable: true
    });
    CategoryXAxis.prototype.au = function () {
        if (this.c5) {
            return (TypeRegistrar.create('HorizontalSmartAxisLabelPanel'));
        }
        else {
            return new HorizontalAxisLabelPanel();
        }
    };
    CategoryXAxis.prototype.getCategorySize = function (a, b, c) {
        var d = this.ka(b, c);
        return d.width / (this.le * a.width);
    };
    CategoryXAxis.prototype.k3 = function (a, b, c) {
        var d = this.la(a, b, c);
        var e = 0;
        if (!isNaN_(this.k9)) {
            e = Math.min(this.k9, 1);
        }
        else {
            e = 0;
        }
        ;
        var f = 1 - 0.5 * d;
        var g = this.lg == 0 ? 1 : this.lg;
        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.lg > 1) {
            var f = this.la(b, c, d);
            var g = 0;
            if (!isNaN_(this.k9)) {
                g = Math.min(this.k9, 1);
            }
            var h = 1 - 0.5 * f;
            var i = h / (this.lg - (this.lg - 1) * g);
            var j = (h - i) / (this.lg - 1);
            e = 0.25 * f + 0.5 * i + a * j;
        }
        return this.getCategorySize(b, c, d) * e;
    };
    CategoryXAxis.prototype.d3 = function () {
        return this.mq;
    };
    CategoryXAxis.prototype.m1 = function (a) {
        var b = this.bh != null ? this.bh.sf : Rect.empty;
        b = b.copy();
        var c = this.kc;
        var d = this.j9();
        var e = new Rect(0, 0, 0, 1, 1);
        var f = new ScalerParams(0, e, e, this.cg, d);
        var g = !b.isEmpty && !c.isEmpty && this.kq != null ? this.kq.indexOf(a) : -1;
        var h = g > -1 ? this.ei(g, f) : NaN;
        if (!isNaN_(h) && this.bh.ei) {
            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.bh.ou(b, false, true);
        }
    };
    CategoryXAxis.prototype.ei = function (a, b) {
        if (!b.c.isEmpty) {
            var c = this.mp(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.mp(a, b.e, b.d, this.categoryMode);
    };
    CategoryXAxis.prototype.mf = function (a) {
        if (typeCast(List$1.$.specialize(Array_$type), a) !== null) {
            return a.asArray();
        }
        return null;
    };
    CategoryXAxis.prototype.hl = 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.le - 1 : this.le;
        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.mf(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;
                    z = (z - q) / r;
                    var aa = q + r * (z - o) / 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.ej = function (a, b, c) {
        if (!b.c.isEmpty) {
            var d = this.mp(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.mp(a, b.e, b.d, c);
    };
    CategoryXAxis.prototype.mp = function (a, b, c, d) {
        var e = d == 0 ? this.le - 1 : this.le;
        if (e < 0) {
            e = 0;
        }
        var f = e >= 1 ? (a) / (e) : e == 0 ? 0.5 : NaN;
        if (this.ch) {
            f = 1 - f;
        }
        return c.left + c.width * (f - b.left) / b.width;
    };
    CategoryXAxis.prototype.el = 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.em = 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.ch) {
            e = 1 - e;
        }
        var f = d == 0 ? this.le - 1 : this.le;
        if (f < 0) {
            f = 0;
        }
        return e * f;
    };
    CategoryXAxis.prototype.m2 = function (a, b) {
        if (a == NaN || b == NaN || this.kq == null || this.kq.count == 0) {
            return;
        }
        var c = a / this.kq.count;
        var d = (b + 1) / this.kq.count;
        this.bh.sv = new Rect(0, c, this.bh.sv.y, d - c, this.bh.sv.height);
    };
    CategoryXAxis.prototype.h8 = function (a) {
        _super.prototype.h8.call(this, a);
        var b = this.bh != null ? this.bh.sf : Rect.empty;
        var c = this.kc;
        var d = this.j7();
        var e = this.j6();
        var f = new ScalerParams(0, b, c, this.cg, d);
        var g = this.a4.a1();
        var h = this.a4.a4();
        var i = this.a4.a2();
        var j = this.a4.a3();
        var k = this.a4.e();
        var l = this.a4.f();
        var m = this.a4.g();
        var n = this.kq;
        var o = this.mh();
        this.ik();
        this.hh(g);
        this.hh(h);
        this.hh(i);
        this.hh(j);
        this.db.clear();
        this.dc.clear();
        this.kx.clear();
        this.a4.aw(this, b, c);
        if (b.isEmpty || c.isEmpty) {
            this.bq.count = 0;
            this.bp.count = 0;
            this.bo.count = 0;
        }
        if (this.bq.count == 0 && this.bp.count == 0) {
            this.a4.ab();
        }
        if (this.ay != null) {
            this.ay.registerAxis(this);
        }
        if (this.itemsSource == null || o == null || n.count == 0) {
            this.bq.count = 0;
            this.bp.count = 0;
            this.bo.count = 0;
            this.a4.ab();
            return;
        }
        if (!b.isEmpty && !c.isEmpty) {
            var p = this.el(e.left, f);
            var q = this.el(e.right, f);
            var r = 0;
            var s = false;
            if (this.c3) {
                var t = Math.abs(q - p);
                r = Math.ceil(t) + 1;
                if (this.cg) {
                    if (Math.abs(q - Math.floor(q)) < Math.abs(p - Math.ceil(p))) {
                        s = true;
                    }
                }
                else {
                    if (Math.abs(q - Math.ceil(q)) < Math.abs(p - Math.floor(p))) {
                        s = true;
                    }
                }
            }
            if (this.cg) {
                p = Math.ceil(p);
                q = Math.floor(q);
            }
            else {
                p = Math.floor(p);
                q = Math.ceil(q);
            }
            if (this.c3) {
                if (Math.abs(q - p) < r) {
                    if (this.cg) {
                        if (s) {
                            q--;
                        }
                        else {
                            p++;
                        }
                    }
                    else {
                        if (s) {
                            q++;
                        }
                        else {
                            p--;
                        }
                    }
                }
            }
            var u = c.bottom;
            var v = u - c.top;
            if (this.ai != null) {
                var w = typeCast(NumericYAxis.$, this.ai);
                if (w != null) {
                    var x = this.id(u, v, b, c, d, c.top, c.bottom);
                    u = x.p0;
                    v = x.p1;
                }
            }
            this.hp(g, u, c, k, true);
            this.a4.ar(v);
            var y = Math.min(p, q);
            var z = Math.max(p, q);
            var aa = e.width;
            if (this.c3) {
                var ab = this.ei(y, f);
                var ac = this.ei(z, f);
                if (ab != ac) {
                    aa = Math.abs(ac - ab);
                }
            }
            var ad = new LinearCategorySnapper(1, y, z, aa, this.mq, this.categoryMode, this.ex());
            var ae = ad.g;
            ae = this.d1(ae, this.mi(), b, c, d);
            if (ad.g != ae) {
                ad = new LinearCategorySnapper(1, y, z, aa, ae, this.categoryMode, this.ex());
            }
            var af = Math.floor((y - 0) / ad.g);
            var ag = Math.ceil((z - 0) / ad.g);
            this.ml = ad.g;
            if (!isNaN_(af) && !isNaN_(ag)) {
                var ah = truncate(af);
                var ai = truncate(ag);
                var aj = this.mo(0 + ah * ad.g, f);
                var ak = this.ei(ad.g, f);
                var al = Math.abs(ak - this.ei(0, f));
                this.a4.as(ak);
                var am = truncate(Math.ceil(e.right));
                var an = truncate(Math.floor(e.left));
                var ao = this.mr;
                var ap = ao > 0 && ao < ad.g;
                var aq = c.right;
                var ar = this.kx;
                var as = ad.g;
                var at = truncate(as);
                var au = this.lg;
                var av = this.cg;
                var aw = this.categoryMode;
                this.mw = null;
                for (var ax = ah; ax <= ai; ++ax) {
                    var ay = this.mo(0 + (ax + 1) * ad.g, f);
                    if (aj <= aq) {
                        if (ax % 2 == 0) {
                            this.im(h, aj, ay, c);
                        }
                        this.il(i, aj, c, l, false);
                        ar.add(aj);
                    }
                    if (this.categoryMode != 0 && this.lg != 0 && this.cx) {
                        if (ap) {
                            var az = al * this.mr;
                            if (this.cg) {
                                var a0 = aj;
                                while (a0 - az > ay) {
                                    a0 -= az;
                                    if (Math.abs(a0 - ay) < 0.0001) {
                                        break;
                                    }
                                    this.il(j, a0, c, m, false);
                                }
                            }
                            else {
                                var a1 = aj;
                                while (a1 + az < ay) {
                                    a1 += az;
                                    if (Math.abs(a1 - ay) < 0.0001) {
                                        break;
                                    }
                                    this.il(j, a1, c, m, false);
                                }
                            }
                            this.mm = this.mr;
                        }
                        else {
                            for (var a2 = 0; a2 < truncate(ad.g); a2++) {
                                for (var a3 = 0; a3 < this.lg; a3++) {
                                    var a4 = this.getGroupCenter(a3, b, c, d);
                                    if (this.cg) {
                                        a4 = -a4;
                                    }
                                    var a5 = this.ei(a2 + ax * ad.g, f) + a4;
                                    this.il(j, a5, c, m, false);
                                }
                            }
                            this.mm = 0.5;
                        }
                    }
                    var a6 = this.mn(ax * as, aj, aw == 0, f);
                    var a7 = truncate(Math.round(a6));
                    if (a7 >= an && a7 <= am) {
                        var a8 = 0;
                        if (as >= 1) {
                            a8 = ax * truncate(Math.floor(as));
                        }
                        else {
                            if ((ax * as) * 2 % 2 == 0) {
                                a8 = truncate(Math.floor(ax * as));
                            }
                            else {
                                a8 = -1;
                            }
                        }
                        if (o != null && a8 < n.count && a8 >= 0) {
                            var a9 = o.getItem(a8);
                            var ba = this.fy(a9);
                            this.mw = a9;
                            if (!isNaN_(a6) && !isInfinity(a6) && ba != null) {
                                if ((typeof ba === 'string') && Base.equalsStatic(ba, "")) {
                                }
                                else {
                                    this.db.add1(ba);
                                    this.dc.add(new LabelPosition(a6));
                                }
                            }
                        }
                    }
                    aj = ay;
                }
            }
            this.mw = null;
            if ((this.ay == null || this.ay.visibility == 0) && this.ai != null) {
                if (this.ay != null && (this.ay.location == 4 || this.ay.location == 5)) {
                    this.bh.lj();
                }
            }
            this.a4.ax(this.db, this.dc);
            this.l3();
        }
    };
    CategoryXAxis.prototype.c2 = function () {
        if (this.cp && this.c3) {
            this.bb = this.ba();
        }
        if (this.kq == null) {
            return false;
        }
        var a = this.kq.count;
        if (a != this.mu) {
            var b = new AxisRangeChangedEventArgs(0, 1, 1, this.mu, a);
            this.mu = a;
            this.h2(b);
            return true;
        }
        return false;
    };
    Object.defineProperty(CategoryXAxis.prototype, "mq", {
        get: function () {
            return this.c(CategoryXAxis.m3);
        },
        set: function (a) {
            this.h(CategoryXAxis.m3, a);
        },
        enumerable: false,
        configurable: true
    });
    CategoryXAxis.prototype.mi = function () {
        return !isNaN_(this.mq);
    };
    Object.defineProperty(CategoryXAxis.prototype, "ml", {
        get: function () {
            return this.mj;
        },
        set: function (a) {
            if (this.ml != a) {
                var b = this.mj;
                this.mj = a;
                this.h1("ActualInterval", b, this.ml);
            }
        },
        enumerable: false,
        configurable: true
    });
    CategoryXAxis.prototype.eu = function () {
        return this.ml;
    };
    Object.defineProperty(CategoryXAxis.prototype, "mr", {
        get: function () {
            return this.c(CategoryXAxis.m4);
        },
        set: function (a) {
            this.h(CategoryXAxis.m4, a);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(CategoryXAxis.prototype, "mm", {
        get: function () {
            return this.mk;
        },
        set: function (a) {
            if (this.mm != a) {
                var b = this.mk;
                this.mk = a;
                this.h1("ActualMinorInterval", b, this.mm);
            }
        },
        enumerable: false,
        configurable: true
    });
    CategoryXAxis.prototype.kt = function (a) {
        if (a == null) {
            return false;
        }
        var b = this.bd();
        if (b == null) {
            return false;
        }
        return b.synchronizeHorizontally;
    };
    CategoryXAxis.prototype.get_a1 = function () {
        return 0;
    };
    Object.defineProperty(CategoryXAxis.prototype, "a1", {
        get: function () {
            return this.get_a1();
        },
        enumerable: false,
        configurable: true
    });
    CategoryXAxis.prototype.mh = function () {
        return this.kq;
    };
    CategoryXAxis.prototype.mo = function (a, b) {
        return this.ei(a, b);
    };
    CategoryXAxis.prototype.mn = function (a, b, c, d) {
        if (!c) {
            var e = this.mo(a + 1, d);
            return (b + e) / 2;
        }
        return b;
    };
    Object.defineProperty(CategoryXAxis.prototype, "isVertical", {
        get: function () {
            return this.co;
        },
        enumerable: false,
        configurable: true
    });
    CategoryXAxis.prototype.j$c = function (a, b) {
        return this.el.apply(this, arguments);
    };
    CategoryXAxis.prototype.j$b = function (a, b) {
        return this.ei.apply(this, arguments);
    };
    CategoryXAxis.prototype.j$d = function (a, b, c, d, e) {
        this.hl.apply(this, arguments);
    };
    CategoryXAxis.prototype.j$e = function (a, b, c, d) {
        this.hm.apply(this, arguments);
    };
    CategoryXAxis.prototype.j$f = function (a, b, c, d) {
        this.hn.apply(this, arguments);
    };
    Object.defineProperty(CategoryXAxis.prototype, "j$a", {
        get: function () {
            return this.cg;
        },
        enumerable: false,
        configurable: true
    });
    CategoryXAxis.$t = markType(CategoryXAxis, 'CategoryXAxis', CategoryAxisBase.$);
    CategoryXAxis.m3 = DependencyProperty.i("Interval", Number_$type, CategoryXAxis.$, new PropertyMetadata(2, NaN, function (a, b) {
        a.h1("Interval", b.oldValue, b.newValue);
        a.h7(false);
    }));
    CategoryXAxis.m4 = DependencyProperty.i("MinorInterval", Number_$type, CategoryXAxis.$, new PropertyMetadata(2, NaN, function (a, b) {
        (typeCast(CategoryXAxis.$, a)).h1("MinorInterval", b.oldValue, b.newValue);
        (typeCast(CategoryXAxis.$, a)).h7(false);
    }));
    return CategoryXAxis;
}(CategoryAxisBase));
export { CategoryXAxis };
/**
 * @hidden
 */
var CategoryXAxisView = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(CategoryXAxisView, _super);
    function CategoryXAxisView(a) {
        var _this = _super.call(this, a) || this;
        _this.bj = null;
        _this.bj = a;
        return _this;
    }
    CategoryXAxisView.prototype.aj = function () {
        _super.prototype.aj.call(this);
        this.a.fg = AxisDefaults.f;
    };
    CategoryXAxisView.$t = markType(CategoryXAxisView, 'CategoryXAxisView', CategoryAxisBaseView.$);
    return CategoryXAxisView;
}(CategoryAxisBaseView));
export { CategoryXAxisView };
