/*
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 { DependencyProperty } from "igniteui-react-core/ES2015/DependencyProperty";
import { List$1 } from "igniteui-react-core/ES2015/List$1";
import { Base, Number_$type, typeCast, Array_$type, markType, TypeRegistrar } from "igniteui-react-core/ES2015/type";
import { HorizontalAxisLabelPanel } from "./HorizontalAxisLabelPanel";
import { Rect } from "igniteui-react-core/ES2015/Rect";
import { MathUtil } from "igniteui-react-core/ES2015/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/ES2015/PropertyMetadata";
import { truncate, isNaN_, isInfinity } from "igniteui-react-core/ES2015/number";
import { CategoryAxisBaseView } from "./CategoryAxisBaseView";
export class CategoryXAxis extends CategoryAxisBase {
    constructor() {
        super();
        this.k1 = 1;
        this.k0 = 1;
        this.kr = 0;
        this.ks = 0;
        this.je = new List$1(Number_$type, 0);
        this.ab = CategoryXAxis.$;
    }
    a1() {
        return new CategoryXAxisView(this);
    }
    gl(a) {
        super.gl(a);
        this.kp = a;
    }
    get k3() {
        return this.k1;
    }
    set k3(a) {
        this.k1 = a;
    }
    get k2() {
        return this.k0;
    }
    set k2(a) {
        this.k0 = a;
    }
    as() {
        if (this.co) {
            return (TypeRegistrar.create('HorizontalSmartAxisLabelPanel'));
        }
        else {
            return new HorizontalAxisLabelPanel();
        }
    }
    getCategorySize(a, b, c) {
        let d = this.ir(b, c);
        return d.width / (this.jq * a.width);
    }
    jj(a, b, c) {
        let d = !isNaN_(this.jg) ? MathUtil.b(this.jg, 0, 1) : 0;
        let e = 0;
        if (!isNaN_(this.jn)) {
            e = Math.min(this.jn, 1);
        }
        else {
            e = 0;
        }
        ;
        let f = 1 - 0.5 * d;
        let g = this.jt == 0 ? 1 : this.jt;
        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.jt > 1) {
            let f = !isNaN_(this.jg) ? MathUtil.b(this.jg, 0, 1) : 0;
            let g = 0;
            if (!isNaN_(this.jn)) {
                g = Math.min(this.jn, 1);
            }
            let h = 1 - 0.5 * f;
            let i = h / (this.jt - (this.jt - 1) * g);
            let j = (h - i) / (this.jt - 1);
            e = 0.25 * f + 0.5 * i + a * j;
        }
        return this.getCategorySize(b, c, d) * e;
    }
    dg() {
        return this.ky;
    }
    k8(a) {
        let b = this.bf != null ? this.bf.mk : Rect.empty;
        b = b.copy();
        let c = this.it;
        let d = this.iq();
        let e = new Rect(0, 0, 0, 1, 1);
        let f = new ScalerParams(0, e, e, this.b8, d);
        let g = !b.isEmpty && !c.isEmpty && this.i7 != null ? this.i7.indexOf(a) : -1;
        let h = g > -1 ? this.dt(g, f) : NaN;
        if (!isNaN_(h) && this.bf.cx) {
            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.kh(b, false, true);
        }
    }
    dt(a, b) {
        if (!b.c.isEmpty) {
            let c = this.kx(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.kx(a, b.e, b.d, this.categoryMode);
    }
    ko(a) {
        if (typeCast(List$1.$.specialize(Array_$type), a) !== null) {
            return a.asArray();
        }
        return null;
    }
    gf(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.jq - 1 : this.jq;
        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.ko(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;
                    let aa = (z - (o * r)) / 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;
                }
            }
        }
    }
    du(a, b, c) {
        if (!b.c.isEmpty) {
            let d = this.kx(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.kx(a, b.e, b.d, c);
    }
    kx(a, b, c, d) {
        let e = d == 0 ? this.jq - 1 : this.jq;
        if (e < 0) {
            e = 0;
        }
        let 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;
    }
    dw(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);
    }
    dx(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.b9) {
            e = 1 - e;
        }
        let f = d == 0 ? this.jq - 1 : this.jq;
        if (f < 0) {
            f = 0;
        }
        return e * f;
    }
    k9(a, b) {
        if (a == NaN || b == NaN || this.i7 == null || this.i7.count == 0) {
            return;
        }
        let c = a / this.i7.count;
        let d = (b + 1) / this.i7.count;
        this.bf.my = new Rect(0, c, this.bf.my.y, d - c, this.bf.my.height);
    }
    g0(a) {
        super.g0(a);
        let b = this.bf != null ? this.bf.mk : Rect.empty;
        let c = this.it;
        let d = this.io();
        let e = this.im();
        let f = new ScalerParams(0, b, c, this.b8, d);
        let g = this.a2.ax();
        let h = this.a2.a0();
        let i = this.a2.ay();
        let j = this.a2.az();
        let k = this.a2.e();
        let l = this.a2.f();
        let m = this.a2.g();
        let n = this.i7;
        let o = this.kq();
        this.g7();
        this.gb(g);
        this.gb(h);
        this.gb(i);
        this.gb(j);
        this.cs.clear();
        this.ct.clear();
        this.je.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) {
            let p = this.dw(e.left, f);
            let 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);
            }
            let r = c.bottom;
            let s = r - c.top;
            if (this.ag != null) {
                let t = typeCast(NumericYAxis.$, this.ag);
                if (t != null) {
                    let u = this.g3(r, s, b, c, d, c.top, c.bottom);
                    r = u.p0;
                    s = u.p1;
                }
            }
            this.gj(g, r, c, k, true);
            this.a2.an(s);
            let v = Math.min(p, q);
            let w = Math.max(p, q);
            let x = new LinearCategorySnapper(1, v, w, e.width, this.ky, this.categoryMode, this.d6());
            let y = Math.floor((v - 0) / x.g);
            let z = Math.ceil((w - 0) / x.g);
            this.kt = x.g;
            if (!isNaN_(y) && !isNaN_(z)) {
                let aa = truncate(y);
                let ab = truncate(z);
                let ac = this.kw(0 + aa * x.g, f);
                let ad = this.dt(x.g, f);
                let ae = Math.abs(ad - this.dt(0, f));
                this.a2.ao(ad);
                let af = truncate(Math.ceil(e.right));
                let ag = truncate(Math.floor(e.left));
                let ah = this.kz;
                let ai = ah > 0 && ah < x.g;
                let aj = c.right;
                let ak = this.je;
                let al = x.g;
                let am = truncate(al);
                let an = this.jt;
                let ao = this.b8;
                let ap = this.categoryMode;
                for (let aq = aa; aq <= ab; ++aq) {
                    let ar = this.kw(0 + (aq + 1) * x.g, f);
                    if (ac <= aj) {
                        if (aq % 2 == 0) {
                            this.g9(h, ac, ar, c);
                        }
                        this.g8(i, ac, c, l, false);
                        ak.add(ac);
                    }
                    if (this.categoryMode != 0 && this.jt != 0 && this.ci) {
                        if (ai) {
                            let as = ae * this.kz;
                            if (this.b8) {
                                let at = ac;
                                while (at - as > ar) {
                                    at -= as;
                                    if (Math.abs(at - ar) < 0.0001) {
                                        break;
                                    }
                                    this.g8(j, at, c, m, false);
                                }
                            }
                            else {
                                let au = ac;
                                while (au + as < ar) {
                                    au += as;
                                    if (Math.abs(au - ar) < 0.0001) {
                                        break;
                                    }
                                    this.g8(j, au, c, m, false);
                                }
                            }
                            this.ku = this.kz;
                        }
                        else {
                            for (let av = 0; av < truncate(x.g); av++) {
                                for (let aw = 0; aw < this.jt; aw++) {
                                    let ax = this.getGroupCenter(aw, b, c, d);
                                    if (this.b8) {
                                        ax = -ax;
                                    }
                                    let ay = this.dt(av + aq * x.g, f) + ax;
                                    this.g8(j, ay, c, m, false);
                                }
                            }
                            this.ku = 0.5;
                        }
                    }
                    let az = this.kv(aq * al, ac, ap == 0, f);
                    let a0 = truncate(Math.round(az));
                    if (a0 >= ag && a0 <= af) {
                        let 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) {
                            let a2 = o.getItem(a1);
                            let a3 = this.e0(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.hp();
                }
            }
            this.a2.at(this.cs, this.ct);
            this.ke();
        }
    }
    cm() {
        if (this.cf && this.cn) {
            this.a9 = this.a8();
        }
        if (this.i7 == null) {
            return false;
        }
        let a = this.i7.count;
        if (a != this.k2) {
            let b = new AxisRangeChangedEventArgs(0, 1, 1, this.k2, a);
            this.k2 = a;
            this.gu(b);
            return true;
        }
        return false;
    }
    get ky() {
        return this.c(CategoryXAxis.la);
    }
    set ky(a) {
        this.h(CategoryXAxis.la, a);
    }
    get kt() {
        return this.kr;
    }
    set kt(a) {
        if (this.kt != a) {
            let b = this.kr;
            this.kr = a;
            this.gt("ActualInterval", b, this.kt);
        }
    }
    d4() {
        return this.kt;
    }
    get kz() {
        return this.c(CategoryXAxis.lb);
    }
    set kz(a) {
        this.h(CategoryXAxis.lb, a);
    }
    get ku() {
        return this.ks;
    }
    set ku(a) {
        if (this.ku != a) {
            let b = this.ks;
            this.ks = a;
            this.gt("ActualMinorInterval", b, this.ku);
        }
    }
    ja(a) {
        if (a == null) {
            return false;
        }
        let b = this.bb();
        if (b == null) {
            return false;
        }
        return b.ac;
    }
    get_az() {
        return 0;
    }
    get az() {
        return this.get_az();
    }
    kq() {
        return this.i7;
    }
    kw(a, b) {
        return this.dt(a, b);
    }
    kv(a, b, c, d) {
        if (!c) {
            let e = this.kw(a + 1, d);
            return (b + e) / 2;
        }
        return b;
    }
    get isVertical() {
        return this.ce;
    }
    j$c(a, b) {
        return this.dw.apply(this, arguments);
    }
    j$b(a, b) {
        return this.dt.apply(this, arguments);
    }
    j$d(a, b, c, d, e) {
        this.gf.apply(this, arguments);
    }
    j$e(a, b, c, d) {
        this.gg.apply(this, arguments);
    }
    j$f(a, b, c, d) {
        this.gh.apply(this, arguments);
    }
    get j$a() {
        return this.b8;
    }
}
CategoryXAxis.la = DependencyProperty.i("Interval", Number_$type, CategoryXAxis.$, new PropertyMetadata(2, NaN, (a, b) => {
    a.gt("Interval", b.oldValue, b.newValue);
    a.gz(false);
}));
CategoryXAxis.lb = DependencyProperty.i("MinorInterval", Number_$type, CategoryXAxis.$, new PropertyMetadata(2, NaN, (a, b) => {
    (typeCast(CategoryXAxis.$, a)).gt("MinorInterval", b.oldValue, b.newValue);
    (typeCast(CategoryXAxis.$, a)).gz(false);
}));
CategoryXAxis.$t = markType(CategoryXAxis, 'CategoryXAxis', CategoryAxisBase.$);
export class CategoryXAxisView extends CategoryAxisBaseView {
    constructor(a) {
        super(a);
        this.bf = null;
        this.bf = a;
    }
}
CategoryXAxisView.$t = markType(CategoryXAxisView, 'CategoryXAxisView', CategoryAxisBaseView.$);
/*<BeginStatics Name="Infragistics.Controls.Charts.CategoryXAxis" />*/
/*<EndStatics Name="Infragistics.Controls.Charts.CategoryXAxis" />*/
