/*
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 { DependencyProperty } from "igniteui-angular-core";
import { Rect } from "igniteui-angular-core";
import { Base, typeCast, Array_$type, Number_$type, markType, markDep } from "igniteui-angular-core";
import { List$1 } from "igniteui-angular-core";
import { ScalerParams } from "./ScalerParams";
import { NumericScaler } from "./NumericScaler";
import { VerticalAxisLabelPanel } from "./VerticalAxisLabelPanel";
import { AxisRangeChangedEventArgs } from "./AxisRangeChangedEventArgs";
import { MathUtil } from "igniteui-angular-core";
import { LinearCategorySnapper } from "./LinearCategorySnapper";
import { NumericXAxis } from "./NumericXAxis";
import { LabelPosition } from "./LabelPosition";
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 CategoryYAxis = /*@__PURE__*/ (() => {
    class CategoryYAxis extends CategoryAxisBase {
        constructor() {
            super();
            this.mi = 0;
            this.mj = 0;
            this.mq = 1;
            this.mp = 1;
            this.kx = new List$1(Number_$type, 0);
            this.ab = CategoryYAxis.$;
        }
        a3() {
            return new CategoryYAxisView(this);
        }
        hr(a) {
            super.hr(a);
            this.mg = a;
        }
        get_co() {
            return true;
        }
        eh() {
            return this.j6().height;
        }
        md(a, b, c) {
            return this.me(a, b, c, true);
        }
        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.top;
            let n = i.height;
            let o = g.top;
            let p = g.height;
            let q = h.top;
            let r = h.height;
            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;
                        let aa = (z - e.d.top) / e.d.height;
                        aa = e.d.top + e.d.height * (aa - e.e.top) / e.e.height;
                        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;
                    }
                }
            }
        }
        get mn() {
            return this.c(CategoryYAxis.m6);
        }
        set mn(a) {
            this.h(CategoryYAxis.m6, a);
        }
        mh() {
            return !isNaN_(this.mn);
        }
        get mk() {
            return this.mi;
        }
        set mk(a) {
            if (this.mk != a) {
                let b = this.mi;
                this.mi = a;
                this.h1("ActualInterval", b, this.mk);
            }
        }
        eu() {
            return this.mk;
        }
        get mo() {
            return this.c(CategoryYAxis.m7);
        }
        set mo(a) {
            this.h(CategoryYAxis.m7, a);
        }
        get ml() {
            return this.mj;
        }
        set ml(a) {
            if (this.ml != a) {
                let b = this.mj;
                this.mj = a;
                this.h1("ActualMinorInterval", b, this.ml);
            }
        }
        get ms() {
            return this.mq;
        }
        set ms(a) {
            this.mq = a;
        }
        get mr() {
            return this.mp;
        }
        set mr(a) {
            this.mp = a;
        }
        ei(a, b) {
            if (!b.c.isEmpty) {
                let c = this.mm(a, NumericScaler.ac, b.c);
                c = (c - b.d.top) / b.d.height;
                c = b.d.top + b.d.height * (c - b.e.top) / b.e.height;
                return c;
            }
            return this.mm(a, b.e, b.d);
        }
        mm(a, b, c) {
            let d = this.categoryMode == 0 ? this.le - 1 : this.le;
            if (d < 0) {
                d = 0;
            }
            let e = d >= 1 ? (a) / (d) : d == 0 ? 0.5 : NaN;
            if (!this.ch) {
                e = 1 - e;
            }
            return c.top + c.height * (e - b.top) / b.height;
        }
        el(a, b) {
            if (!b.c.isEmpty) {
                let c = b.e.top + b.e.height * (a - b.d.top) / b.d.height;
                c = (c * b.d.height) + b.d.top;
                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.top + b.e.height * (a - b.d.top) / b.d.height;
                d = (d * b.d.height) + b.d.top;
                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.top + (a - c.top) * b.height / c.height;
            if (!this.ch) {
                e = 1 - e;
            }
            let f = d == 0 ? this.le - 1 : this.le;
            if (f < 0) {
                f = 0;
            }
            return e * f;
        }
        m3(a, b) {
            if (isNaN_(a) || isNaN_(b) || this.kq == null || this.kq.count == 0) {
                return;
            }
            let c = a / this.kq.count;
            let d = b / this.kq.count;
            this.bh.tp = new Rect(0, this.bh.tp.x, 1 - d, this.bh.tp.width, d - c);
        }
        au() {
            return new VerticalAxisLabelPanel();
        }
        d3() {
            return this.mn;
        }
        getCategorySize(a, b, c) {
            let d = this.ka(b, c);
            return d.height / (this.le * a.height);
        }
        k3(a, b, c) {
            let d = this.la(a, b, c);
            let e = 0;
            if (!isNaN_(this.k9)) {
                e = Math.min(this.k9, 1);
            }
            let f = 1 - 0.5 * d;
            let g = this.getCategorySize(a, b, c) * f / (this.lg - (this.lg - 1) * e);
            return g;
        }
        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;
        }
        m2(a) {
            let b = this.bh != null ? this.bh.s9 : Rect.empty;
            b = b.copy();
            let c = this.kc;
            let d = new Rect(0, 0, 0, 1, 1);
            let e = this.j9();
            let f = new ScalerParams(0, d, d, this.cg, e);
            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.top + 0.1 * b.height) {
                        h = h + 0.4 * b.height;
                        b.y = h - 0.5 * b.height;
                    }
                    if (h > b.bottom - 0.1 * b.height) {
                        h = h - 0.4 * b.height;
                        b.y = h - 0.5 * b.height;
                    }
                }
                this.bh.pi(b, false, true);
            }
        }
        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.mr) {
                let c = new AxisRangeChangedEventArgs(0, 1, 1, this.mr, b);
                this.mr = b;
                this.h2(c);
                a = true;
            }
            this.m5();
            this.m4();
            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.height;
                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.height;
                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;
        }
        m5() {
            if (this.kq == null || this.bh == null) {
                return;
            }
            let a = !isNaN_(this.bh.hk) && !isNaN_(this.bh.hm);
            if (a) {
                return;
            }
            if (!isNaN_(this.zoomMaximumItemSpan) && !isInfinity(this.zoomMaximumItemSpan)) {
                let b = this.getWindowZoomFromItemSpan(this.zoomMaximumItemSpan);
                if (!isNaN_(b) && !isInfinity(b)) {
                    this.bh.gf = b;
                }
            }
            else if (!isNaN_(this.zoomMaximumCategoryRange) && !isInfinity(this.zoomMaximumCategoryRange)) {
                let c = this.getWindowZoomFromCategories(this.zoomMaximumCategoryRange);
                if (!isNaN_(c) && !isInfinity(c)) {
                    this.bh.gf = c;
                }
            }
        }
        m4() {
            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)) {
                e = true;
                d = this.getWindowZoomFromItemSpan(this.zoomToItemSpan);
                d = Math.max(d, this.bh.gf);
            }
            else if (MathUtil.a(this.zoomToCategoryRange)) {
                e = true;
                d = this.getWindowZoomFromCategories(this.zoomToCategoryRange);
                d = Math.max(d, this.bh.gf);
            }
            if (MathUtil.a(this.zoomToCategoryStart) && this.zoomToCategoryStart >= 0 && this.zoomToCategoryStart < this.lf) {
                e = true;
                b = this.getWindowZoomFromCategories(this.zoomToCategoryStart);
                let f = d + a - 1;
                let g = d - f;
                if (f > 0 && g > this.bh.gf) {
                    d = g;
                    d = Math.max(d, this.bh.gf);
                }
            }
            if (e) {
                this.bh.pg(a, b, c, d);
            }
        }
        hv(a, b, c, d) {
            super.hv(a, b, c, d);
            switch (b) {
                case CategoryYAxis.$$p[0]:
                case CategoryYAxis.$$p[1]:
                    this.m5();
                    break;
                case CategoryYAxis.$$p[2]:
                case CategoryYAxis.$$p[3]:
                case CategoryYAxis.$$p[4]:
                    this.m4();
                    break;
            }
        }
        kt(a) {
            if (a == null) {
                return false;
            }
            let b = this.bd();
            if (b == null) {
                return false;
            }
            return b.synchronizeVertically;
        }
        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();
            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 || this.kq == null || this.kq.count == 0) {
                this.bq.count = 0;
                this.bp.count = 0;
                this.bo.count = 0;
                this.a4.ab();
                return;
            }
            if (!b.isEmpty && !c.isEmpty) {
                let n = this.el(e.top, f);
                let o = this.el(e.bottom, f);
                if (!this.cg) {
                    n = Math.ceil(n);
                    o = Math.floor(o);
                }
                else {
                    n = Math.floor(n);
                    o = Math.ceil(o);
                }
                let p = c.left;
                let q = 0;
                if (this.ai != null) {
                    let r = typeCast(NumericXAxis.$, this.ai);
                    if (r != null) {
                        let s = this.id(p, q, b, c, d, c.left, c.right);
                        p = s.p0;
                        q = s.p1;
                    }
                }
                this.il(g, p, c, k, true);
                this.a4.ar(q);
                let t = Math.min(n, o);
                let u = Math.max(n, o);
                let v = new LinearCategorySnapper(1, t, u, e.height, this.mn, this.categoryMode, this.ex());
                let w = v.g;
                w = this.d1(w, this.mh(), b, c, d);
                if (v.g != w) {
                    v = new LinearCategorySnapper(1, t, u, e.height, w, this.categoryMode, this.ex());
                }
                let x = Math.floor((t - 0) / v.g);
                let y = Math.ceil((u - 0) / v.g);
                if (!isNaN_(x) && !isNaN_(y)) {
                    let z = truncate(x);
                    let aa = truncate(y);
                    let ab = this.ei(0 + z * v.g, f);
                    let ac = Math.abs(this.ei(v.g, f) - this.ei(0, f));
                    this.av.bw = this.ei(v.g, f);
                    for (let ad = z; ad <= aa; ++ad) {
                        let ae = this.ei(0 + (ad + 1) * v.g, f);
                        if (ab <= e.bottom) {
                            if (ad % 2 == 0) {
                                this.hq(h, ab, ae, c);
                            }
                            this.hp(i, ab, c, l, false);
                            this.kx.add(ab);
                        }
                        if (this.categoryMode != 0 && this.lg != 0 && this.cx) {
                            if (this.mo > 0 && this.mo < v.g) {
                                let af = ac * this.mo;
                                if (!this.cg) {
                                    let ag = ab;
                                    while (ag - af > ae) {
                                        ag -= af;
                                        if (Math.abs(ag - ae) < 0.0001) {
                                            break;
                                        }
                                        this.hp(j, ag, c, m, false);
                                    }
                                }
                                else {
                                    let ah = ab;
                                    while (ah + af < ae) {
                                        ah += af;
                                        if (Math.abs(ah - ae) < 0.0001) {
                                            break;
                                        }
                                        this.hp(j, ah, c, m, false);
                                    }
                                }
                                this.ml = this.mo;
                            }
                            else {
                                for (let ai = 0; ai < truncate(v.g); ai++) {
                                    for (let aj = 0; aj < this.lg; aj++) {
                                        let ak = this.getGroupCenter(aj, b, c, d);
                                        if (!this.cg) {
                                            ak = -ak;
                                        }
                                        let al = this.ei(ai + ad * v.g, f) + ak;
                                        this.hp(j, al, c, m, false);
                                    }
                                }
                                this.ml = 0.5;
                            }
                        }
                        let am = ab;
                        if (this.categoryMode != 0) {
                            let an = this.ei(ad * v.g + 1, f);
                            am = (ab + an) / 2;
                        }
                        if (am <= e.bottom && am >= e.top) {
                            let ao = 0;
                            if (v.g >= 1) {
                                ao = ad * truncate(Math.floor(v.g));
                            }
                            else {
                                if ((ad * v.g) * 2 % 2 == 0) {
                                    ao = truncate(Math.floor(ad * v.g));
                                }
                                else {
                                    ao = -1;
                                }
                            }
                            if (this.kq != null && ao < this.kq.count && ao >= 0) {
                                let ap = this.kq.item(ao);
                                let aq = this.fy(ap);
                                if (!isNaN_(am) && !isInfinity(am) && aq != null) {
                                    if ((typeof aq === 'string') && Base.equalsStatic(aq, "")) {
                                    }
                                    else {
                                        this.db.add1(aq);
                                        this.dc.add(new LabelPosition(am));
                                    }
                                }
                            }
                        }
                        ab = ae;
                    }
                }
                this.mk = v.g;
                if ((this.ay == null || this.ay.visibility == 0) && this.ai != null) {
                    if (this.ay != null && (this.ay.location == 6 || this.ay.location == 7)) {
                        this.bh.l6();
                    }
                }
                this.a4.ax(this.db, this.dc);
                this.l3();
            }
        }
        get_a1() {
            return 1;
        }
        get a1() {
            return this.get_a1();
        }
        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;
        }
    }
    CategoryYAxis.$t = markType(CategoryYAxis, 'CategoryYAxis', CategoryAxisBase.$, [IZoomableCategoryAxis_$type]);
    CategoryYAxis.m6 = DependencyProperty.i("Interval", Number_$type, CategoryYAxis.$, new PropertyMetadata(2, NaN, (a, b) => {
        a.h1("Interval", b.oldValue, b.newValue);
        a.h7(false);
    }));
    CategoryYAxis.m7 = DependencyProperty.i("MinorInterval", Number_$type, CategoryYAxis.$, new PropertyMetadata(2, NaN, (a, b) => {
        (typeCast(CategoryYAxis.$, a)).h1("MinorInterval", b.oldValue, b.newValue);
        (typeCast(CategoryYAxis.$, a)).h7(false);
    }));
    CategoryYAxis.$$p = markDep(DependencyProperty, PropertyMetadata, CategoryYAxis, 'h1', ['ZoomMaximumCategoryRange::m8', [1, NaN], 'ZoomMaximumItemSpan::m9', [1, NaN], 'ZoomToCategoryRange::na', [1, NaN], 'ZoomToCategoryStart::nb', [1, NaN], 'ZoomToItemSpan::nc', [1, NaN]]);
    return CategoryYAxis;
})();
/**
 * @hidden
 */
export let CategoryYAxisView = /*@__PURE__*/ (() => {
    class CategoryYAxisView extends CategoryAxisBaseView {
        constructor(a) {
            super(a);
            this.bj = null;
            this.bj = a;
        }
        aj() {
            super.aj();
            this.a.fg = AxisDefaults.f;
        }
    }
    CategoryYAxisView.$t = markType(CategoryYAxisView, 'CategoryYAxisView', CategoryAxisBaseView.$);
    return CategoryYAxisView;
})();
