/*
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 { CategoryBucketCalculator } from "./CategoryBucketCalculator";
import { Type, markType, typeCast, Point_$type } from "igniteui-react-core/ES2015/type";
import { isNaN_ } from "igniteui-react-core/ES2015/number";
import { CategorySeries } from "./CategorySeries";
import { IIsCategoryBased_$type } from "./IIsCategoryBased";
import { IHasHighLowValueCategory_$type } from "./IHasHighLowValueCategory";
import { RangeCategoryFramePreparer } from "./RangeCategoryFramePreparer";
import { DependencyProperty } from "igniteui-react-core/ES2015/DependencyProperty";
import { CategoryAxisBase } from "./CategoryAxisBase";
import { ICategoryScaler_$type } from "./ICategoryScaler";
import { IScaler_$type } from "./IScaler";
import { CategorySeriesView } from "./CategorySeriesView";
import { CategoryFrame } from "./CategoryFrame";
import { CategoryMarkerManager } from "./CategoryMarkerManager";
import { Rect } from "igniteui-react-core/ES2015/Rect";
import { ScalerParams } from "./ScalerParams";
import { IFastItemsSource_$type } from "igniteui-react-core/ES2015/IFastItemsSource";
import { AxisRange } from "./AxisRange";
import { ISortingAxis_$type } from "./ISortingAxis";
import { CollisionAvoider } from "./CollisionAvoider";
import { ISupportsMarkers_$type } from "./ISupportsMarkers";
import { PropertyMetadata } from "igniteui-react-core/ES2015/PropertyMetadata";
import { FastFlattener } from "igniteui-react-core/ES2015/FastFlattener";
export class RangeCategoryBucketCalculator extends CategoryBucketCalculator {
    constructor(a) {
        super(a);
        this.j = null;
        this.l = 0;
        this.k = 0;
        this.i = null;
        this.h = null;
        this.j = a;
    }
    getBucketWithoutUnknowns(a) {
        let b = false;
        let c = this.i;
        let d = this.h;
        let e = this.l;
        let f = this.k;
        let g = Math.min(e, f);
        let h = a * this.d;
        let i = Math.min(h + this.d - 1, g - 1);
        let j = 1.7976931348623157E+308;
        let k = -1.7976931348623157E+308;
        let l = true;
        let m = 0;
        let n = 0;
        let o;
        let p;
        for (let q = h; q <= i; ++q) {
            m = c[q];
            n = d[q];
            if (m < n) {
                o = m;
                p = n;
            }
            else {
                p = m;
                o = n;
            }
            if (!l) {
                j = j < o ? j : o;
                k = k > o ? k : o;
                j = j < p ? j : p;
                k = k > p ? k : p;
            }
            else {
                j = Math.min(j, o);
                k = Math.max(k, o);
                k = Math.max(k, p);
                j = Math.min(j, p);
                l = false;
            }
        }
        if (b && n < m) {
            let r = j;
            j = k;
            k = r;
        }
        if (!l) {
            let s = new Array(3);
            s[0] = 0.5 * (h + i);
            s[1] = j;
            s[2] = k;
            return s;
        }
        let t = new Array(3);
        t[0] = NaN;
        t[1] = NaN;
        t[2] = NaN;
        return t;
    }
    getBucket(a) {
        let b = false;
        let c = this.i;
        let d = this.h;
        let e = this.l;
        let f = this.k;
        let g = Math.min(e, f);
        let h = a * this.d;
        let i = Math.min(h + this.d - 1, g - 1);
        let j = NaN;
        let k = NaN;
        let l = true;
        let m = 0;
        let n = 0;
        let o;
        let p;
        for (let q = h; q <= i; ++q) {
            m = c[q];
            n = d[q];
            if (m < n) {
                o = m;
                p = n;
            }
            else {
                p = m;
                o = n;
            }
            if (!l) {
                if (!isNaN_(o)) {
                    j = j < o ? j : o;
                    k = k > o ? k : o;
                }
                if (!isNaN_(p)) {
                    j = j < p ? j : p;
                    k = k > p ? k : p;
                }
            }
            else {
                if (!isNaN_(o)) {
                    if (isNaN_(j)) {
                        j = o;
                    }
                    else {
                        j = Math.min(j, o);
                    }
                    if (!isNaN_(k)) {
                        k = Math.max(k, o);
                    }
                }
                if (!isNaN_(p)) {
                    if (isNaN_(k)) {
                        k = p;
                    }
                    else {
                        k = Math.max(k, p);
                    }
                    if (!isNaN_(j)) {
                        j = Math.min(j, p);
                    }
                }
                if (!isNaN_(j) && !isNaN_(k)) {
                    l = false;
                }
            }
        }
        if (n < m && b) {
            let r = j;
            j = k;
            k = r;
        }
        if (!l) {
            let s = new Array(3);
            s[0] = 0.5 * (h + i);
            s[1] = j;
            s[2] = k;
            return s;
        }
        let t = new Array(3);
        t[0] = NaN;
        t[1] = NaN;
        t[2] = NaN;
        return t;
    }
    cacheValues() {
        this.l = this.j.cu.lowColumn.count;
        this.k = this.j.cu.highColumn.count;
        this.i = this.j.cu.lowColumn.asArray();
        this.h = this.j.cu.highColumn.asArray();
    }
    unCacheValues() {
        this.i = null;
        this.h = null;
    }
}
RangeCategoryBucketCalculator.$t = markType(RangeCategoryBucketCalculator, 'RangeCategoryBucketCalculator', CategoryBucketCalculator.$);
export class RangeCategorySeries extends CategorySeries {
    constructor() {
        super();
        this._RangeFramePreparer = null;
        this.op = null;
        this.on = null;
        this.ok = null;
        this.RangeFramePreparer = new RangeCategoryFramePreparer(1, this, this.om, this, this, this.om.cl);
    }
    bf() {
        return new RangeCategorySeriesView(this);
    }
    ii(a) {
        super.ii(a);
        this.om = a;
    }
    get RangeFramePreparer() {
        return this._RangeFramePreparer;
    }
    set RangeFramePreparer(a) {
        this._RangeFramePreparer = a;
    }
    get lowColumn() {
        return this.op;
    }
    set lowColumn(a) {
        if (this.op != a) {
            let b = this.op;
            this.op = a;
            this.raisePropertyChanged("LowColumn", b, this.op);
        }
    }
    get highColumn() {
        return this.on;
    }
    set highColumn(a) {
        if (this.on != a) {
            let b = this.on;
            this.on = a;
            this.raisePropertyChanged("HighColumn", b, this.on);
        }
    }
    m4() {
        return 11;
    }
    ob(a, b) {
        super.ob(a, b);
        b.by = true;
        CategoryMarkerManager.b(this, a.m, b.co, this.lt);
        this.n5(b, a);
    }
    ox(a, b, c, d, e, f) {
        this.om.cv(a, b, c, d, e, f);
    }
    c6(a) {
        let b = new Rect(0, 0, 0, 1, 1);
        let c = this.bi != null ? this.bi.bq : Rect.empty;
        c = c.copy();
        let d = this.bi != null ? this.bi.bp : Rect.empty;
        let e = !c.isEmpty && !d.isEmpty && this.bz != null ? this.bz.indexOf(a) : -1;
        let f = this.fetchXAxis();
        let g = this.fetchYAxis();
        let h = this.lg(this.bi);
        let i;
        if (f != null) {
            let j = new ScalerParams(0, b, b, f.b8, h);
            i = f.dt(e, j);
        }
        else {
            i = NaN;
        }
        let k = f != null ? this.RangeFramePreparer.r(typeCast(ICategoryScaler_$type, f), b, b, h) : 0;
        i += k;
        if (e >= 0 && Rect.l_op_Inequality(c, null) && Rect.l_op_Inequality(d, null)) {
            if (!isNaN_(i)) {
                if (i < c.left + 0.1 * c.width) {
                    i = i + 0.4 * c.width;
                }
                if (i > c.right - 0.1 * c.width) {
                    i = i - 0.4 * c.width;
                }
                c.x = i - 0.5 * c.width;
            }
            if (g != null && this.highColumn != null && e < this.highColumn.count) {
                let l = new ScalerParams(0, b, b, g.b8, h);
                let m = g.dt(this.highColumn.item(e), l);
                let n = g.dt(this.lowColumn.item(e), l);
                if (!isNaN_(m) && !isNaN_(n)) {
                    let o = Math.abs(n - m);
                    if (c.height < o) {
                        c.height = o;
                        c.y = Math.min(n, m);
                    }
                    else {
                        if (n < c.top + 0.1 * c.height) {
                            n = n + 0.4 * c.height;
                        }
                        if (n > c.bottom - 0.1 * c.height) {
                            n = n - 0.4 * c.height;
                        }
                        c.y = n - 0.5 * c.height;
                    }
                }
            }
            if (this.bn != null) {
                this.bn.ap(this.bs, c, true);
            }
        }
        return e >= 0;
    }
    io(a, b, c, d) {
        super.io(a, b, c, d);
        switch (b) {
            case "FastItemsSource":
                if (typeCast(IFastItemsSource_$type, c) != null) {
                    c.deregisterColumn(this.lowColumn);
                    c.deregisterColumn(this.highColumn);
                    this.lowColumn = null;
                    this.highColumn = null;
                }
                if (typeCast(IFastItemsSource_$type, d) != null) {
                    this.lowColumn = this.bw(this.ov);
                    this.highColumn = this.bw(this.os);
                }
                if (!this.np()) {
                    this.m3.cl.g(this.eo);
                    this.i1(false);
                }
                break;
            case RangeCategorySeries.$$p[1]:
                if (this.bz != null) {
                    this.bz.deregisterColumn(this.lowColumn);
                    this.lowColumn = this.bw(this.ov);
                }
                break;
            case "LowColumn":
                if (!this.np()) {
                    this.m3.cl.g(this.eo);
                    this.i1(false);
                }
                break;
            case RangeCategorySeries.$$p[0]:
                if (this.bz != null) {
                    this.bz.deregisterColumn(this.highColumn);
                    this.highColumn = this.bw(this.os);
                }
                break;
            case "HighColumn":
                if (!this.np()) {
                    this.m3.cl.g(this.eo);
                    this.i1(false);
                }
                break;
        }
    }
    an(a) {
        if (this.lowColumn == null || this.lowColumn.count == 0 || this.highColumn == null || this.highColumn.count == 0) {
            return null;
        }
        if (a == this.fetchXAxis()) {
            let b = Math.min(this.lowColumn.count, this.highColumn.count);
            return new AxisRange(0, b - 1);
        }
        if (a == this.fetchYAxis()) {
            let c = Math.min(this.lowColumn.minimum, this.highColumn.minimum);
            let d = Math.max(this.lowColumn.maximum, this.highColumn.maximum);
            return new AxisRange(Math.min(c, d), Math.max(c, d));
        }
        return null;
    }
    ho(a, b, c, d) {
        if (this.fetchXAxis() != null && typeCast(ISortingAxis_$type, this.fetchXAxis()) !== null) {
            this.fetchXAxis().i$i();
        }
        let e = this.fetchXAxis();
        switch (a) {
            case 3:
                if (d == this.ov || d == this.os) {
                    if (e != null) {
                        e.ck();
                    }
                    if (!this.np()) {
                        this.i1(true);
                    }
                }
                break;
            case 1:
                if (e != null) {
                    e.ck();
                }
                this.m3.cl.g(this.eo);
                if (!this.np()) {
                    this.i1(true);
                }
                break;
            case 0:
                if (e != null) {
                    e.ck();
                }
                this.m3.cl.g(this.eo);
                if (!this.np()) {
                    this.i1(true);
                }
                break;
            case 2:
                if (e != null) {
                    e.ck();
                }
                if (this.ov != null && this.os != null && this.m3.cl.d > 0 && !this.np()) {
                    this.i1(true);
                }
                break;
            case 4:
                if (e != null) {
                    e.ck();
                }
                this.m3.cl.g(this.eo);
                if (!this.np()) {
                    this.i1(true);
                }
                break;
        }
    }
    dt(a, b, c) {
        let d = super.dt(a, b, c);
        if (this.lowColumn == null || this.lowColumn.count == 0 || this.highColumn == null || this.highColumn.count == 0) {
            d = false;
        }
        return d;
    }
    n8(a, b) {
        super.n8(a, b);
        this.RangeFramePreparer.d(a, b);
    }
    get currentCategoryMode() {
        return this.preferredCategoryMode(typeCast(CategoryAxisBase.$, this.fetchXAxis()));
    }
    get scaler() {
        return typeCast(ICategoryScaler_$type, this.fetchXAxis());
    }
    get yScaler() {
        return typeCast(IScaler_$type, this.fetchYAxis());
    }
    get bucketizer() {
        return this.m3.cl;
    }
    get currentMode2Index() {
        return this.nx();
    }
    provideCollisionDetector() {
        return new CollisionAvoider();
    }
    get mayContainUnknowns() {
        return this.lowColumn == null || this.lowColumn.mayContainUnknowns || this.highColumn == null || this.highColumn.mayContainUnknowns;
    }
    i3(a, b) {
        let c = this.di;
        super.i3(a, b);
        if (!c) {
            this.bi.az(b);
            return;
        }
        this.bi.az(b);
        if (this.cc(this.bh)) {
            return;
        }
        let d = new RangeCategoryFramePreparer(1, this, typeCast(ISupportsMarkers_$type, this.bh), this.bs.bk.m, this, this.bh.cl);
        if (!this.dc) {
            this.mx = new CategoryFrame(3);
            this.mx.u();
            d.d(this.mx, this.bh);
        }
        this.dc = false;
        this.ob(this.mx, this.bh);
        this.di = false;
    }
    i0(a, b, c, d, e) {
        super.i0(a, b, c, d, e);
        let f = this.a0.a$h.item(d);
        let g = f;
        g.cl.g(this.eo);
        f.ay(c);
        if (this.cc(g)) {
            return;
        }
        let h = new RangeCategoryFramePreparer(1, this, typeCast(ISupportsMarkers_$type, f), f, this, f.cl);
        if (this.ok == null) {
            this.ok = new CategoryFrame(3);
        }
        this.ok.u();
        h.d(this.ok, g);
        this.ob(this.ok, g);
    }
    ec() {
        return this.lowColumn == null || this.lowColumn.count == 0 ? NaN : this.lowColumn.item(0);
    }
    get useHighMarkerFidelity() {
        return this.nq;
    }
}
RangeCategorySeries.$t = markType(RangeCategorySeries, 'RangeCategorySeries', CategorySeries.$, [IIsCategoryBased_$type, IHasHighLowValueCategory_$type]);
Type.dep(DependencyProperty, PropertyMetadata, RangeCategorySeries, 'raisePropertyChanged', ['HighMemberPath:os:oy', [2, null], 'LowMemberPath:ov:oz', [2, null]]);
export class RangeCategorySeriesView extends CategorySeriesView {
    constructor(a) {
        super(a);
        this.cu = null;
        this.cu = a;
    }
    cv(a, b, c, d, e, f) {
        a.aj.clear();
        b.aj.clear();
        c.aj.clear();
        let g = FastFlattener.b(d, e, true, f, this.e.eo);
        let h = FastFlattener.b(d, e, false, f, this.e.eo);
        let i = g.count;
        let j = h.count;
        let k;
        let l;
        let m;
        let n;
        for (let o = 0; o < i; o++) {
            k = g.item(o);
            l = e._inner[k];
            m = l[0];
            n = l[1];
            a.aj.add({ $type: Point_$type, x: m, y: n });
            b.aj.add({ $type: Point_$type, x: m, y: n });
        }
        let p;
        let q;
        for (let r = 0; r < j; r++) {
            k = h.item(r);
            l = e._inner[k];
            if (f) {
                p = l[0];
                q = l[2];
            }
            else {
                p = l[2];
                q = l[3];
            }
            c.aj.add({ $type: Point_$type, x: p, y: q });
            b.aj.add({ $type: Point_$type, x: p, y: q });
        }
        a.ab = a.aj.count > 0;
        b.ab = b.aj.count > 0;
        c.ab = c.aj.count > 0;
    }
    cm() {
        return new RangeCategoryBucketCalculator(this);
    }
}
RangeCategorySeriesView.$t = markType(RangeCategorySeriesView, 'RangeCategorySeriesView', CategorySeriesView.$);
/*<BeginStatics Name="Infragistics.Controls.Charts.RangeCategorySeries" />*/
/*<EndStatics Name="Infragistics.Controls.Charts.RangeCategorySeries" />*/
