/*
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 { CategoryAxisBase } from "./CategoryAxisBase";
import { IZoomableCategoryAxis_$type } from "./IZoomableCategoryAxis";
import { Base, Number_$type, typeCast, Array_$type, markType, markDep, 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 { MathUtil } from "igniteui-angular-core";
import { PropertyMetadata } from "igniteui-angular-core";
import { truncate, isNaN_, isInfinity } from "igniteui-angular-core";
import { CategoryAxisBaseView } from "./CategoryAxisBaseView";
import { AxisDefaults } from "./AxisDefaults";
/**
 * @hidden
 */
export let CategoryXAxis = /*@__PURE__*/ (() => {
    class CategoryXAxis extends CategoryAxisBase {
        constructor() {
            super();
            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.$;
        }
        a3() {
            return new CategoryXAxisView(this);
        }
        hr(a) {
            super.hr(a);
            this.mg = a;
        }
        get mv() {
            return this.mt;
        }
        set mv(a) {
            this.mt = a;
        }
        get mu() {
            return this.ms;
        }
        set mu(a) {
            this.ms = a;
        }
        au() {
            if (this.c5) {
                return (TypeRegistrar.create('HorizontalSmartAxisLabelPanel'));
            }
            else {
                return new HorizontalAxisLabelPanel();
            }
        }
        getCategorySize(a, b, c) {
            let d = this.ka(b, c);
            return d.width / (this.le * a.width);
        }
        k3(a, b, c) {
            let d = this.la(a, b, c);
            let e = 0;
            if (!isNaN_(this.k9)) {
                e = Math.min(this.k9, 1);
            }
            else {
                e = 0;
            }
            ;
            let f = 1 - 0.5 * d;
            let g = this.lg == 0 ? 1 : this.lg;
            let h = this.getCategorySize(a, b, c) * f / (g - (g - 1) * e);
            return h;
        }
        getGroupCenter(a, b, c, d) {
            let e = 0.5;
            if (this.lg > 1) {
                let f = this.la(b, c, d);
                let g = 0;
                if (!isNaN_(this.k9)) {
                    g = Math.min(this.k9, 1);
                }
                let h = 1 - 0.5 * f;
                let i = h / (this.lg - (this.lg - 1) * g);
                let j = (h - i) / (this.lg - 1);
                e = 0.25 * f + 0.5 * i + a * j;
            }
            return this.getCategorySize(b, c, d) * e;
        }
        d3() {
            return this.mq;
        }
        m6(a) {
            let b = this.bh != null ? this.bh.s9 : Rect.empty;
            b = b.copy();
            let c = this.kc;
            let d = this.j9();
            let e = new Rect(0, 0, 0, 1, 1);
            let f = new ScalerParams(0, e, e, this.cg, d);
            let g = !b.isEmpty && !c.isEmpty && this.kq != null ? this.kq.indexOf(a) : -1;
            let h = g > -1 ? this.ei(g, f) : NaN;
            if (!isNaN_(h) && this.bh.em) {
                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.pi(b, false, true);
            }
        }
        ei(a, b) {
            if (!b.c.isEmpty) {
                let 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);
        }
        mf(a) {
            if (typeCast(List$1.$.specialize(Array_$type), a) !== null) {
                return a.asArray();
            }
            return null;
        }
        hl(a, b, c, d, e) {
            let f;
            let g = e.e;
            let h = e.d;
            let i = e.c;
            let j = e.a;
            let k = !i.isEmpty;
            let l = this.categoryMode == 0 ? this.le - 1 : this.le;
            if (l < 0) {
                l = 0;
            }
            let m = i.left;
            let n = i.width;
            let o = g.left;
            let p = g.width;
            let q = h.left;
            let r = h.width;
            let s = 0;
            let t = 1;
            let u = this.mf(a);
            let v = false;
            if (u != null) {
                v = true;
            }
            let w;
            for (let x = 0; x < b.count; x++) {
                w = b.item(x);
                for (let y = c; y < d; y++) {
                    if (v) {
                        f = u[y];
                    }
                    else {
                        f = a.item(y);
                    }
                    if (k) {
                        let 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;
                        let aa = q + r * (z - o) / p;
                        f[w] = aa;
                    }
                    else {
                        let 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;
                    }
                }
            }
        }
        ej(a, b, c) {
            if (!b.c.isEmpty) {
                let 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);
        }
        mp(a, b, c, d) {
            let e = d == 0 ? this.le - 1 : this.le;
            if (e < 0) {
                e = 0;
            }
            let 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;
        }
        el(a, b) {
            if (!b.c.isEmpty) {
                let 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);
        }
        em(a, b, c) {
            if (!b.c.isEmpty) {
                let 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);
        }
        getUnscaledValue3(a, b, c, d) {
            let e = b.left + (a - c.left) * b.width / c.width;
            if (this.ch) {
                e = 1 - e;
            }
            let f = d == 0 ? this.le - 1 : this.le;
            if (f < 0) {
                f = 0;
            }
            return e * f;
        }
        m7(a, b) {
            if (isNaN_(a) || isNaN_(b) || this.kq == null || this.kq.count == 0) {
                return;
            }
            let c = a / this.kq.count;
            let d = (b + 1) / this.kq.count;
            this.bh.tp = new Rect(0, c, this.bh.tp.y, d - c, this.bh.tp.height);
        }
        h8(a) {
            super.h8(a);
            let b = this.bh != null ? this.bh.s9 : Rect.empty;
            let c = this.kc;
            let d = this.j7();
            let e = this.j6();
            let f = new ScalerParams(0, b, c, this.cg, d);
            let g = this.a4.a1();
            let h = this.a4.a4();
            let i = this.a4.a2();
            let j = this.a4.a3();
            let k = this.a4.e();
            let l = this.a4.f();
            let m = this.a4.g();
            let n = this.kq;
            let 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) {
                let p = this.el(e.left, f);
                let q = this.el(e.right, f);
                let r = 0;
                let s = false;
                if (this.c3) {
                    let 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--;
                            }
                        }
                    }
                }
                let u = c.bottom;
                let v = u - c.top;
                if (this.ai != null) {
                    let w = typeCast(NumericYAxis.$, this.ai);
                    if (w != null) {
                        let 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);
                let y = Math.min(p, q);
                let z = Math.max(p, q);
                let aa = e.width;
                if (this.c3) {
                    let ab = this.ei(y, f);
                    let ac = this.ei(z, f);
                    if (ab != ac) {
                        aa = Math.abs(ac - ab);
                    }
                }
                let ad = new LinearCategorySnapper(1, y, z, aa, this.mq, this.categoryMode, this.ex());
                let 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());
                }
                let af = Math.floor((y - 0) / ad.g);
                let ag = Math.ceil((z - 0) / ad.g);
                this.ml = ad.g;
                if (!isNaN_(af) && !isNaN_(ag)) {
                    let ah = truncate(af);
                    let ai = truncate(ag);
                    let aj = this.mo(0 + ah * ad.g, f);
                    let ak = this.ei(ad.g, f);
                    let al = Math.abs(ak - this.ei(0, f));
                    this.a4.as(ak);
                    let am = truncate(Math.ceil(e.right));
                    let an = truncate(Math.floor(e.left));
                    let ao = this.mr;
                    let ap = ao > 0 && ao < ad.g;
                    let aq = c.right;
                    let ar = this.kx;
                    let as = ad.g;
                    let at = truncate(as);
                    let au = this.lg;
                    let av = this.cg;
                    let aw = this.categoryMode;
                    this.mw = null;
                    for (let ax = ah; ax <= ai; ++ax) {
                        let 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) {
                                let az = al * this.mr;
                                if (this.cg) {
                                    let a0 = aj;
                                    while (a0 - az > ay) {
                                        a0 -= az;
                                        if (Math.abs(a0 - ay) < 0.0001) {
                                            break;
                                        }
                                        this.il(j, a0, c, m, false);
                                    }
                                }
                                else {
                                    let 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 (let a2 = 0; a2 < truncate(ad.g); a2++) {
                                    for (let a3 = 0; a3 < this.lg; a3++) {
                                        let a4 = this.getGroupCenter(a3, b, c, d);
                                        if (this.cg) {
                                            a4 = -a4;
                                        }
                                        let a5 = this.ei(a2 + ax * ad.g, f) + a4;
                                        this.il(j, a5, c, m, false);
                                    }
                                }
                                this.mm = 0.5;
                            }
                        }
                        let a6 = this.mn(ax * as, aj, aw == 0, f);
                        let a7 = truncate(Math.round(a6));
                        if (a7 >= an && a7 <= am) {
                            let 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) {
                                let a9 = o.getItem(a8);
                                let 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.l6();
                    }
                }
                this.a4.ax(this.db, this.dc);
                this.l3();
            }
        }
        c2() {
            if (this.cp && this.c3) {
                this.bb = this.ba();
            }
            if (this.kq == null) {
                return false;
            }
            let a = false;
            let b = this.kq.count;
            if (b != this.mu) {
                let c = new AxisRangeChangedEventArgs(0, 1, 1, this.mu, b);
                this.mu = b;
                this.h2(c);
                a = true;
            }
            this.m9();
            this.m8();
            return a;
        }
        getWindowZoomFromCategories(a) {
            let b = MathUtil.a(a) && a > 0;
            let c = this.kq.count;
            if (c > 0 && b) {
                let d = this.bh.to.width;
                let e = isNaN_(d) || isInfinity(d);
                if (e) {
                    return NaN;
                }
                let f = new Rect(0, 0, 0, 1, 1);
                let g = this.bh.to;
                let h = this.getCategorySize(f, g, g);
                let i = isNaN_(h) || isInfinity(h);
                if (i) {
                    return NaN;
                }
                let j = h * a / d;
                if (j >= 0 && j <= 1) {
                    return j;
                }
            }
            return NaN;
        }
        getWindowZoomFromItemSpan(a) {
            let b = MathUtil.a(a) && a > 0;
            let c = this.kq.count;
            if (c > 0 && b) {
                let d = this.bh.to.width;
                let e = isNaN_(d) || isInfinity(d);
                if (e) {
                    return NaN;
                }
                let f = new Rect(0, 0, 0, 1, 1);
                let g = this.bh.to;
                let h = this.k3(f, g, g);
                let i = a / h;
                let j = Math.min(1, 1 / i);
                if (j > 0 && j <= 1) {
                    return j;
                }
            }
            return NaN;
        }
        m9() {
            if (this.kq == null || this.bh == null) {
                return;
            }
            let a = !isNaN_(this.bh.hl) && !isNaN_(this.bh.hn);
            if (a) {
                return;
            }
            if (!isNaN_(this.zoomMaximumItemSpan) && !isInfinity(this.zoomMaximumItemSpan)) {
                let b = this.getWindowZoomFromItemSpan(this.zoomMaximumItemSpan);
                if (!isNaN_(b) && !isInfinity(b)) {
                    this.bh.gg = b;
                }
            }
            else if (!isNaN_(this.zoomMaximumCategoryRange) && !isInfinity(this.zoomMaximumCategoryRange)) {
                let c = this.getWindowZoomFromCategories(this.zoomMaximumCategoryRange);
                if (!isNaN_(c) && !isInfinity(c)) {
                    this.bh.gg = c;
                }
            }
        }
        m8() {
            if (this.kq == null || this.bh == null) {
                return;
            }
            let a = NaN;
            let b = NaN;
            let c = this.bh.gm();
            let d = this.bh.gn();
            let e = false;
            if (MathUtil.a(this.zoomToItemSpan) && this.zoomToItemSpan > 0) {
                e = true;
                c = this.getWindowZoomFromItemSpan(this.zoomToItemSpan);
                c = Math.max(c, this.bh.gg);
            }
            else if (MathUtil.a(this.zoomToCategoryRange) && this.zoomToCategoryRange > 0 && this.zoomToCategoryRange <= this.lf) {
                e = true;
                c = this.getWindowZoomFromCategories(this.zoomToCategoryRange);
                c = Math.max(c, this.bh.gg);
            }
            if (MathUtil.a(this.zoomToCategoryStart) && this.zoomToCategoryStart >= 0 && this.zoomToCategoryStart < this.lf) {
                e = true;
                a = this.getWindowZoomFromCategories(this.zoomToCategoryStart);
                let f = c + a - 1;
                let g = c - f;
                if (f > 0 && g > this.bh.gg) {
                    c = g;
                    c = Math.max(c, this.bh.gg);
                }
            }
            if (e) {
                this.bh.pg(a, b, c, d);
            }
        }
        hv(a, b, c, d) {
            super.hv(a, b, c, d);
            switch (b) {
                case CategoryXAxis.$$p[0]:
                case CategoryXAxis.$$p[1]:
                    this.m9();
                    break;
                case CategoryXAxis.$$p[2]:
                case CategoryXAxis.$$p[3]:
                case CategoryXAxis.$$p[4]:
                    this.m8();
                    break;
            }
        }
        get mq() {
            return this.c(CategoryXAxis.na);
        }
        set mq(a) {
            this.h(CategoryXAxis.na, a);
        }
        mi() {
            return !isNaN_(this.mq);
        }
        get ml() {
            return this.mj;
        }
        set ml(a) {
            if (this.ml != a) {
                let b = this.mj;
                this.mj = a;
                this.h1("ActualInterval", b, this.ml);
            }
        }
        eu() {
            return this.ml;
        }
        get mr() {
            return this.c(CategoryXAxis.nb);
        }
        set mr(a) {
            this.h(CategoryXAxis.nb, a);
        }
        get mm() {
            return this.mk;
        }
        set mm(a) {
            if (this.mm != a) {
                let b = this.mk;
                this.mk = a;
                this.h1("ActualMinorInterval", b, this.mm);
            }
        }
        kt(a) {
            if (a == null) {
                return false;
            }
            let b = this.bd();
            if (b == null) {
                return false;
            }
            return b.synchronizeHorizontally;
        }
        get_a1() {
            return 0;
        }
        get a1() {
            return this.get_a1();
        }
        mh() {
            return this.kq;
        }
        mo(a, b) {
            return this.ei(a, b);
        }
        mn(a, b, c, d) {
            if (!c) {
                let e = this.mo(a + 1, d);
                return (b + e) / 2;
            }
            return b;
        }
        get isVertical() {
            return this.co;
        }
        j$c(a, b) {
            return this.el.apply(this, arguments);
        }
        j$b(a, b) {
            return this.ei.apply(this, arguments);
        }
        j$d(a, b, c, d, e) {
            this.hl.apply(this, arguments);
        }
        j$e(a, b, c, d) {
            this.hm.apply(this, arguments);
        }
        j$f(a, b, c, d) {
            this.hn.apply(this, arguments);
        }
        get j$a() {
            return this.cg;
        }
    }
    CategoryXAxis.$t = markType(CategoryXAxis, 'CategoryXAxis', CategoryAxisBase.$, [IZoomableCategoryAxis_$type]);
    CategoryXAxis.na = DependencyProperty.i("Interval", Number_$type, CategoryXAxis.$, new PropertyMetadata(2, NaN, (a, b) => {
        a.h1("Interval", b.oldValue, b.newValue);
        a.h7(false);
    }));
    CategoryXAxis.nb = DependencyProperty.i("MinorInterval", Number_$type, CategoryXAxis.$, new PropertyMetadata(2, NaN, (a, b) => {
        (typeCast(CategoryXAxis.$, a)).h1("MinorInterval", b.oldValue, b.newValue);
        (typeCast(CategoryXAxis.$, a)).h7(false);
    }));
    CategoryXAxis.$$p = markDep(DependencyProperty, PropertyMetadata, CategoryXAxis, 'h1', ['ZoomMaximumCategoryRange::nc', [1, NaN], 'ZoomMaximumItemSpan::nd', [1, NaN], 'ZoomToCategoryRange::ne', [1, NaN], 'ZoomToCategoryStart::nf', [1, NaN], 'ZoomToItemSpan::ng', [1, NaN]]);
    return CategoryXAxis;
})();
/**
 * @hidden
 */
export let CategoryXAxisView = /*@__PURE__*/ (() => {
    class CategoryXAxisView extends CategoryAxisBaseView {
        constructor(a) {
            super(a);
            this.bj = null;
            this.bj = a;
        }
        aj() {
            super.aj();
            this.a.fg = AxisDefaults.f;
        }
    }
    CategoryXAxisView.$t = markType(CategoryXAxisView, 'CategoryXAxisView', CategoryAxisBaseView.$);
    return CategoryXAxisView;
})();
