/*
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-react-core";
import { Rect } from "igniteui-react-core";
import { Base, typeCast, Array_$type, Number_$type, markType, markDep } from "igniteui-react-core";
import { List$1 } from "igniteui-react-core";
import { ScalerParams } from "./ScalerParams";
import { NumericScaler } from "./NumericScaler";
import { VerticalAxisLabelPanel } from "./VerticalAxisLabelPanel";
import { AxisRangeChangedEventArgs } from "./AxisRangeChangedEventArgs";
import { MathUtil } from "igniteui-react-core";
import { LinearCategorySnapper } from "./LinearCategorySnapper";
import { NumericXAxis } from "./NumericXAxis";
import { LabelPosition } from "./LabelPosition";
import { PropertyMetadata } from "igniteui-react-core";
import { truncate, isNaN_, isInfinity } from "igniteui-react-core";
import { CategoryAxisBaseView } from "./CategoryAxisBaseView";
import { AxisDefaults } from "./AxisDefaults";
/**
 * @hidden
 */
export let CategoryYAxis = /*@__PURE__*/ (() => {
    class CategoryYAxis extends CategoryAxisBase {
        a4() {
            return new CategoryYAxisView(this);
        }
        ht(a) {
            super.ht(a);
            this.mj = a;
        }
        get_cp() {
            return true;
        }
        ej() {
            return this.j8().height;
        }
        mg(a, b, c) {
            return this.mh(a, b, c, true);
        }
        mi(a) {
            if (typeCast(List$1.$.specialize(Array_$type), a) !== null) {
                return a.asArray();
            }
            return null;
        }
        hn(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.lg - 1 : this.lg;
            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.mi(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;
                    }
                }
            }
        }
        constructor() {
            super();
            this.ml = 0;
            this.mm = 0;
            this.mt = 1;
            this.ms = 1;
            this.kz = new List$1(Number_$type, 0);
            this.ab = CategoryYAxis.$;
        }
        get mq() {
            return this.c(CategoryYAxis.m9);
        }
        set mq(a) {
            this.h(CategoryYAxis.m9, a);
        }
        mk() {
            return !isNaN_(this.mq);
        }
        get mn() {
            return this.ml;
        }
        set mn(a) {
            if (this.mn != a) {
                let b = this.ml;
                this.ml = a;
                this.h3("ActualInterval", b, this.mn);
            }
        }
        ew() {
            return this.mn;
        }
        get mr() {
            return this.c(CategoryYAxis.na);
        }
        set mr(a) {
            this.h(CategoryYAxis.na, a);
        }
        get mo() {
            return this.mm;
        }
        set mo(a) {
            if (this.mo != a) {
                let b = this.mm;
                this.mm = a;
                this.h3("ActualMinorInterval", b, this.mo);
            }
        }
        get mv() {
            return this.mt;
        }
        set mv(a) {
            this.mt = a;
        }
        get mu() {
            return this.ms;
        }
        set mu(a) {
            this.ms = a;
        }
        ek(a, b) {
            if (!b.c.isEmpty) {
                let c = this.mp(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.mp(a, b.e, b.d);
        }
        mp(a, b, c) {
            let d = this.categoryMode == 0 ? this.lg - 1 : this.lg;
            if (d < 0) {
                d = 0;
            }
            let e = d >= 1 ? (a) / (d) : d == 0 ? 0.5 : NaN;
            if (!this.ci) {
                e = 1 - e;
            }
            return c.top + c.height * (e - b.top) / b.height;
        }
        en(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);
        }
        eo(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.ci) {
                e = 1 - e;
            }
            let f = d == 0 ? this.lg - 1 : this.lg;
            if (f < 0) {
                f = 0;
            }
            return e * f;
        }
        m6(a, b) {
            if (isNaN_(a) || isNaN_(b) || this.ks == null || this.ks.count == 0) {
                return;
            }
            let c = a / this.ks.count;
            let d = b / this.ks.count;
            this.bi.v1 = new Rect(0, this.bi.v1.x, 1 - d, this.bi.v1.width, d - c);
        }
        av() {
            return new VerticalAxisLabelPanel();
        }
        d5() {
            return this.mq;
        }
        getCategorySize(a, b, c) {
            let d = this.kc(b, c);
            return d.height / (this.lg * a.height);
        }
        k5(a, b, c) {
            let d = this.lc(a, b, c);
            let e = 0;
            if (!isNaN_(this.lb)) {
                e = Math.min(this.lb, 1);
            }
            let f = 1 - 0.5 * d;
            let g = this.getCategorySize(a, b, c) * f / (this.li - (this.li - 1) * e);
            return g;
        }
        getGroupCenter(a, b, c, d) {
            let e = 0.5;
            if (this.li > 1) {
                let f = this.lc(b, c, d);
                let g = 0;
                if (!isNaN_(this.lb)) {
                    g = Math.min(this.lb, 1);
                }
                let h = 1 - 0.5 * f;
                let i = h / (this.li - (this.li - 1) * g);
                let j = (h - i) / (this.li - 1);
                e = 0.25 * f + 0.5 * i + a * j;
            }
            return this.getCategorySize(b, c, d) * e;
        }
        m5(a) {
            let b = this.bi != null ? this.bi.vl : Rect.empty;
            b = b.copy();
            let c = this.ke;
            let d = new Rect(0, 0, 0, 1, 1);
            let e = this.kb();
            let f = new ScalerParams(0, d, d, this.ch, e);
            let g = !b.isEmpty && !c.isEmpty && this.ks != null ? this.ks.indexOf(a) : -1;
            let h = g > -1 ? this.ek(g, f) : NaN;
            if (!isNaN_(h) && this.bi.fc) {
                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.bi.rk(b, false, true);
            }
        }
        c3() {
            if (this.cq && this.c4) {
                this.bc = this.bb();
            }
            if (this.ks == null) {
                return false;
            }
            let a = false;
            let b = this.ks.count;
            if (b != this.mu) {
                let c = new AxisRangeChangedEventArgs(0, 1, 1, this.mu, b);
                this.mu = b;
                this.h4(c);
                a = true;
            }
            this.m8();
            this.m7();
            return a;
        }
        getWindowZoomFromCategories(a) {
            let b = MathUtil.a(a) && a > 0;
            let c = this.ks.count;
            if (c > 0 && b) {
                let d = this.bi.v0.height;
                let e = isNaN_(d) || isInfinity(d);
                if (e) {
                    return NaN;
                }
                let f = new Rect(0, 0, 0, 1, 1);
                let g = this.bi.v0;
                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.ks.count;
            if (c > 0 && b) {
                let d = this.bi.v0.height;
                let e = isNaN_(d) || isInfinity(d);
                if (e) {
                    return NaN;
                }
                let f = new Rect(0, 0, 0, 1, 1);
                let g = this.bi.v0;
                let h = this.k5(f, g, g);
                let i = a / h;
                let j = Math.min(1, 1 / i);
                if (j > 0 && j <= 1) {
                    return j;
                }
            }
            return NaN;
        }
        m8() {
            if (this.ks == null || this.bi == null) {
                return;
            }
            let a = !isNaN_(this.bi.ik) && !isNaN_(this.bi.im);
            if (a) {
                return;
            }
            if (!isNaN_(this.zoomMaximumItemSpan) && !isInfinity(this.zoomMaximumItemSpan)) {
                let b = this.getWindowZoomFromItemSpan(this.zoomMaximumItemSpan);
                if (!isNaN_(b) && !isInfinity(b)) {
                    this.bi.he = b;
                }
            }
            else if (!isNaN_(this.zoomMaximumCategoryRange) && !isInfinity(this.zoomMaximumCategoryRange)) {
                let c = this.getWindowZoomFromCategories(this.zoomMaximumCategoryRange);
                if (!isNaN_(c) && !isInfinity(c)) {
                    this.bi.he = c;
                }
            }
        }
        m7() {
            if (this.ks == null || this.bi == null) {
                return;
            }
            let a = NaN;
            let b = NaN;
            let c = this.bi.hl();
            let d = this.bi.hm();
            let e = false;
            if (MathUtil.a(this.zoomToItemSpan)) {
                e = true;
                d = this.getWindowZoomFromItemSpan(this.zoomToItemSpan);
                d = Math.max(d, this.bi.he);
            }
            else if (MathUtil.a(this.zoomToCategoryRange)) {
                e = true;
                d = this.getWindowZoomFromCategories(this.zoomToCategoryRange);
                d = Math.max(d, this.bi.he);
            }
            if (MathUtil.a(this.zoomToCategoryStart) && this.zoomToCategoryStart >= 0 && this.zoomToCategoryStart < this.lh) {
                e = true;
                b = this.getWindowZoomFromCategories(this.zoomToCategoryStart);
                let f = d + a - 1;
                let g = d - f;
                if (f > 0 && g > this.bi.he) {
                    d = g;
                    d = Math.max(d, this.bi.he);
                }
            }
            if (e) {
                this.bi.ri(a, b, c, d);
            }
        }
        hx(a, b, c, d) {
            super.hx(a, b, c, d);
            switch (b) {
                case CategoryYAxis.$$p[0]:
                case CategoryYAxis.$$p[1]:
                    this.m8();
                    break;
                case CategoryYAxis.$$p[2]:
                case CategoryYAxis.$$p[3]:
                case CategoryYAxis.$$p[4]:
                    this.m7();
                    break;
            }
        }
        kv(a) {
            if (a == null) {
                return false;
            }
            let b = this.be();
            if (b == null) {
                return false;
            }
            return b.synchronizeVertically;
        }
        ia(a) {
            super.ia(a);
            let b = this.bi != null ? this.bi.vl : Rect.empty;
            let c = this.ke;
            let d = this.j9();
            let e = this.j8();
            let f = new ScalerParams(0, b, c, this.ch, d);
            let g = this.a5.a4();
            let h = this.a5.a7();
            let i = this.a5.a5();
            let j = this.a5.a6();
            let k = this.a5.e();
            let l = this.a5.f();
            let m = this.a5.g();
            this.im();
            this.hj(g);
            this.hj(h);
            this.hj(i);
            this.hj(j);
            this.dc.clear();
            this.dd.clear();
            this.kz.clear();
            this.a5.az(this, b, c);
            if (b.isEmpty || c.isEmpty) {
                this.br.count = 0;
                this.bq.count = 0;
                this.bp.count = 0;
            }
            if (this.br.count == 0 && this.bq.count == 0 && this.a5.l()) {
                this.a5.ad();
            }
            if (this.az != null) {
                this.az.registerAxis(this);
            }
            if (this.itemsSource == null || this.ks == null || this.ks.count == 0) {
                this.br.count = 0;
                this.bq.count = 0;
                this.bp.count = 0;
                this.a5.ad();
                return;
            }
            if (!b.isEmpty && !c.isEmpty) {
                let n = this.en(e.top, f);
                let o = this.en(e.bottom, f);
                if (!this.ch) {
                    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.aj != null) {
                    let r = typeCast(NumericXAxis.$, this.aj);
                    if (r != null) {
                        let s = this.ig(p, q, b, c, d, c.left, c.right);
                        p = s.p0;
                        q = s.p1;
                    }
                }
                this.io(g, p, c, k, true);
                this.a5.au(q);
                let t = Math.min(n, o);
                let u = Math.max(n, o);
                let v = new LinearCategorySnapper(1, t, u, e.height, this.mq, this.categoryMode, this.ez());
                let w = v.g;
                w = this.d2(w, this.mk(), b, c, d);
                if (v.g != w) {
                    v = new LinearCategorySnapper(1, t, u, e.height, w, this.categoryMode, this.ez());
                }
                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.ek(0 + z * v.g, f);
                    let ac = Math.abs(this.ek(v.g, f) - this.ek(0, f));
                    this.aw.bw = this.ek(v.g, f);
                    for (let ad = z; ad <= aa; ++ad) {
                        let ae = this.ek(0 + (ad + 1) * v.g, f);
                        if (ab <= e.bottom) {
                            if (ad % 2 == 0) {
                                this.hs(h, ab, ae, c);
                            }
                            this.hr(i, ab, c, l, false);
                            this.kz.add(ab);
                        }
                        if (this.categoryMode != 0 && this.li != 0 && this.cy) {
                            if (this.mr > 0 && this.mr < v.g) {
                                let af = ac * this.mr;
                                if (!this.ch) {
                                    let ag = ab;
                                    while (ag - af > ae) {
                                        ag -= af;
                                        if (Math.abs(ag - ae) < 0.0001) {
                                            break;
                                        }
                                        this.hr(j, ag, c, m, false);
                                    }
                                }
                                else {
                                    let ah = ab;
                                    while (ah + af < ae) {
                                        ah += af;
                                        if (Math.abs(ah - ae) < 0.0001) {
                                            break;
                                        }
                                        this.hr(j, ah, c, m, false);
                                    }
                                }
                                this.mo = this.mr;
                            }
                            else {
                                for (let ai = 0; ai < truncate(v.g); ai++) {
                                    for (let aj = 0; aj < this.li; aj++) {
                                        let ak = this.getGroupCenter(aj, b, c, d);
                                        if (!this.ch) {
                                            ak = -ak;
                                        }
                                        let al = this.ek(ai + ad * v.g, f) + ak;
                                        this.hr(j, al, c, m, false);
                                    }
                                }
                                this.mo = 0.5;
                            }
                        }
                        let am = ab;
                        if (this.categoryMode != 0) {
                            let an = this.ek(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.ks != null && ao < this.ks.count && ao >= 0) {
                                let ap = this.ks.item(ao);
                                let aq = this.f0(ap);
                                if (!isNaN_(am) && !isInfinity(am) && aq != null) {
                                    if ((typeof aq === 'string') && Base.equalsStatic(aq, "")) {
                                    }
                                    else {
                                        this.dc.add1(aq);
                                        this.dd.add(new LabelPosition(am));
                                    }
                                }
                            }
                        }
                        ab = ae;
                    }
                }
                this.mn = v.g;
                if ((this.az == null || this.az.visibility == 0) && this.aj != null) {
                    if (this.az != null && (this.az.location == 6 || this.az.location == 7)) {
                        this.bi.nr();
                    }
                }
                this.a5.a0(this.dc, this.dd);
                this.l6();
            }
        }
        get_a2() {
            return 1;
        }
        get a2() {
            return this.get_a2();
        }
        get isVertical() {
            return this.cp;
        }
        j$d(a, b) {
            return this.en.apply(this, arguments);
        }
        j$c(a, b) {
            return this.ek.apply(this, arguments);
        }
        j$e(a, b, c, d, e) {
            this.hn.apply(this, arguments);
        }
        j$f(a, b, c, d) {
            this.ho.apply(this, arguments);
        }
        j$g(a, b, c, d) {
            this.hp.apply(this, arguments);
        }
        get j$b() {
            return this.ch;
        }
        j$a() {
            return this.ag.apply(this, arguments);
        }
    }
    CategoryYAxis.$t = /*@__PURE__*/ markType(CategoryYAxis, 'CategoryYAxis', CategoryAxisBase.$, [IZoomableCategoryAxis_$type]);
    CategoryYAxis.m9 = /*@__PURE__*/ DependencyProperty.i("Interval", Number_$type, CategoryYAxis.$, /*@__PURE__*/ new PropertyMetadata(2, NaN, (a, b) => {
        a.h3("Interval", b.oldValue, b.newValue);
        a.h9(false);
    }));
    CategoryYAxis.na = /*@__PURE__*/ DependencyProperty.i("MinorInterval", Number_$type, CategoryYAxis.$, /*@__PURE__*/ new PropertyMetadata(2, NaN, (a, b) => {
        (typeCast(CategoryYAxis.$, a)).h3("MinorInterval", b.oldValue, b.newValue);
        (typeCast(CategoryYAxis.$, a)).h9(false);
    }));
    CategoryYAxis.$$p = /*@__PURE__*/ markDep(DependencyProperty, PropertyMetadata, CategoryYAxis, 'h3', ['ZoomMaximumCategoryRange::nb', [1, NaN], 'ZoomMaximumItemSpan::nc', [1, NaN], 'ZoomToCategoryRange::nd', [1, NaN], 'ZoomToCategoryStart::ne', [1, NaN], 'ZoomToItemSpan::nf', [1, NaN]]);
    return CategoryYAxis;
})();
/**
 * @hidden
 */
export let CategoryYAxisView = /*@__PURE__*/ (() => {
    class CategoryYAxisView extends CategoryAxisBaseView {
        constructor(a) {
            super(a);
            this.bm = null;
            this.bm = a;
        }
        al() {
            super.al();
            this.a.fi = AxisDefaults.f;
        }
    }
    CategoryYAxisView.$t = /*@__PURE__*/ markType(CategoryYAxisView, 'CategoryYAxisView', CategoryAxisBaseView.$);
    return CategoryYAxisView;
})();
