/*
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 { markType, runOn, delegateCombine, typeCast, markDep, Point_$type } from "igniteui-react-core";
import { isNaN_, truncate, isInfinity } from "igniteui-react-core";
import { CategorySeries } from "./CategorySeries";
import { IIsCategoryBased_$type } from "./IIsCategoryBased";
import { IHasHighLowValueCategory_$type } from "./IHasHighLowValueCategory";
import { RangeCategoryFramePreparer } from "./RangeCategoryFramePreparer";
import { DependencyProperty } from "igniteui-react-core";
import { Series } from "./Series";
import { CategoryAxisBase } from "./CategoryAxisBase";
import { ICategoryScaler_$type } from "./ICategoryScaler";
import { IScaler_$type } from "./IScaler";
import { CategorySeriesView } from "./CategorySeriesView";
import { CategoryFrame } from "./CategoryFrame";
import { SeriesLayerPropertyOverlay } from "./SeriesLayerPropertyOverlay";
import { CategoryMarkerManager } from "./CategoryMarkerManager";
import { AxisRange } from "./AxisRange";
import { Rect } from "igniteui-react-core";
import { ScalerParams } from "./ScalerParams";
import { ISortingAxis_$type } from "./ISortingAxis";
import { IFastItemsSource_$type } from "igniteui-react-core";
import { CollisionAvoider } from "./CollisionAvoider";
import { ISupportsMarkers_$type } from "./ISupportsMarkers";
import { PropertyMetadata } from "igniteui-react-core";
import { FastFlattener } from "igniteui-react-core";
/**
 * @hidden
 */
export let RangeCategoryBucketCalculator = /*@__PURE__*/ (() => {
    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;
            if (h <= i) {
                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;
            if (h <= i) {
                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.c5.lowColumn.count;
            this.k = this.j.c5.highColumn.count;
            this.i = this.j.c5.lowColumn.asArray();
            this.h = this.j.c5.highColumn.asArray();
        }
        unCacheValues() {
            this.i = null;
            this.h = null;
        }
    }
    RangeCategoryBucketCalculator.$t = /*@__PURE__*/ markType(RangeCategoryBucketCalculator, 'RangeCategoryBucketCalculator', CategoryBucketCalculator.$);
    return RangeCategoryBucketCalculator;
})();
/**
 * @hidden
 */
export let RangeCategorySeries = /*@__PURE__*/ (() => {
    class RangeCategorySeries extends CategorySeries {
        cq() {
            return new RangeCategorySeriesView(this);
        }
        getHostReferenceValue() {
            return this.iw();
        }
        qw(a) {
            super.qw(a);
            this.aac = a;
        }
        constructor() {
            super();
            this._RangeFramePreparer = null;
            this.aae = null;
            this.aad = null;
            this.aaa = null;
            this.RangeFramePreparer = new RangeCategoryFramePreparer(1, this, this.aac, this, this, this.aac.cw);
        }
        get RangeFramePreparer() {
            return this._RangeFramePreparer;
        }
        set RangeFramePreparer(a) {
            this._RangeFramePreparer = a;
        }
        f9() {
            return this.highlightedItemsSource != null || (this.aak != null && this.aai != null);
        }
        hr() {
            let a = super.hr();
            {
                let b = ((() => {
                    let $ret = new SeriesLayerPropertyOverlay();
                    $ret.ad = "HighMemberPath";
                    $ret.p = true;
                    $ret.y = "HighlightedHighMemberPath";
                    $ret.q = true;
                    return $ret;
                })());
                b.valueResolving = delegateCombine(b.valueResolving, runOn(this, this.aar));
                a.add(b);
            }
            {
                let c = ((() => {
                    let $ret = new SeriesLayerPropertyOverlay();
                    $ret.ad = "LowMemberPath";
                    $ret.p = true;
                    $ret.y = "HighlightedLowMemberPath";
                    $ret.q = true;
                    return $ret;
                })());
                c.valueResolving = delegateCombine(c.valueResolving, runOn(this, this.aas));
                a.add(c);
            }
            return a;
        }
        aas(a, b) {
            if (this.aak != null) {
                b.value = this.aak;
                return;
            }
            b.value = this.aap;
        }
        aar(a, b) {
            if (this.aai != null) {
                b.value = this.aai;
                return;
            }
            b.value = this.aam;
        }
        ko(a, b) {
            let c = this.na(b);
            if (c == RangeCategorySeries.$$p[0]) {
                return this.kp(a, b, this.aam);
            }
            if (c == RangeCategorySeries.$$p[3]) {
                return this.kp(a, b, this.aap);
            }
            if (c == RangeCategorySeries.$$p[2]) {
                return this.kp(a, b, this.aak);
            }
            if (c == RangeCategorySeries.$$p[1]) {
                return this.kp(a, b, this.aai);
            }
            return super.ko(a, b);
        }
        ma(a) {
            let b = this.na(a);
            if (b == RangeCategorySeries.$$p[0]) {
                return this.aam;
            }
            if (b == RangeCategorySeries.$$p[3]) {
                return this.aap;
            }
            if (b == RangeCategorySeries.$$p[2]) {
                return this.aak;
            }
            if (b == RangeCategorySeries.$$p[1]) {
                return this.aai;
            }
            return super.ma(a);
        }
        get lowColumn() {
            return this.aae;
        }
        set lowColumn(a) {
            if (this.aae != a) {
                let b = this.aae;
                this.aae = a;
                this.raisePropertyChanged("LowColumn", b, this.aae);
            }
        }
        get highColumn() {
            return this.aad;
        }
        set highColumn(a) {
            if (this.aad != a) {
                let b = this.aad;
                this.aad = a;
                this.raisePropertyChanged("HighColumn", b, this.aad);
            }
        }
        yu() {
            return 11;
        }
        z3(a, b) {
            super.z3(a, b);
            b.b7 = true;
            CategoryMarkerManager.e(this, a.n, b.cz, this.w9);
            this.zx(b, a);
        }
        aat(a, b, c, d, e, f) {
            this.aac.c6(a, b, c, d, e, f);
        }
        ay(a) {
            if (a.isEmpty && this.c9 != null) {
                a = this.c9.vl;
            }
            let b = this.yj();
            if (this.lowColumn == null || this.highColumn == null) {
                return null;
            }
            let c = this.wc(this.ct);
            let d = new ScalerParams(0, a, this.c9.v0, b.ch, this.getEffectiveViewport());
            let e = (b.en(c.left, d));
            let f = (b.en(c.right, d));
            if (b.ch) {
                let g = e;
                e = f;
                f = g;
            }
            let h = null;
            if (b.cn) {
                h = b;
                e = h.i$f(e);
                f = h.i$f(f);
            }
            else {
                e = Math.floor(e);
                f = Math.ceil(f);
            }
            e = Math.max(0, Math.min(this.lowColumn.count - 1, e));
            f = Math.max(0, Math.min(this.lowColumn.count - 1, f));
            let i = 1.7976931348623157E+308;
            let j = -1.7976931348623157E+308;
            for (let k = truncate(e); k <= truncate(f); k++) {
                let l = k;
                if (h != null) {
                    l = h.i$b._inner[k];
                }
                let m = this.lowColumn.item(l);
                if (this.lowColumn.mayContainUnknowns) {
                    if (isInfinity(m) || isNaN_(m)) {
                        continue;
                    }
                }
                let n = this.highColumn.item(l);
                if (this.highColumn.mayContainUnknowns) {
                    if (isInfinity(n) || isNaN_(n)) {
                        continue;
                    }
                }
                i = Math.min(i, m);
                j = Math.max(j, n);
            }
            let o = new AxisRange(i, j);
            let p = this.yz();
            o = this.aw(o, p, a);
            return o;
        }
        gb(a) {
            let b = new Rect(0, 0, 0, 1, 1);
            let c = this.ct != null ? this.ct.by : Rect.empty;
            c = c.copy();
            let d = this.ct != null ? this.ct.bx : Rect.empty;
            let e = !c.isEmpty && !d.isEmpty && this.dl != null ? this.dl.indexOf(a) : -1;
            let f = this.fetchXAxis();
            let g = this.fetchYAxis();
            let h = this.we(this.ct);
            let i;
            if (f != null) {
                let j = new ScalerParams(0, b, b, f.ch, h);
                i = f.ek(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.ch, h);
                    l.b = this.iw();
                    let m = g.ek(this.highColumn.item(e), l);
                    let n = g.ek(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.c1 != null) {
                    this.c1.au(this.c9, c, true);
                }
            }
            return e >= 0;
        }
        q4(a, b, c, d) {
            super.q4(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.di(this.aap);
                        this.highColumn = this.di(this.aam);
                    }
                    if (!this.zf()) {
                        this.yt.cw.g(this.ib);
                        this.rm(this.ge && c != null);
                    }
                    break;
                case RangeCategorySeries.$$p[3]:
                    if (this.dl != null) {
                        this.dl.deregisterColumn(this.lowColumn);
                        this.lowColumn = this.di(this.aap);
                    }
                    break;
                case "LowColumn":
                    if (!this.zf()) {
                        this.yt.cw.g(this.ib);
                        this.rm(this.ge && this.d5);
                    }
                    break;
                case RangeCategorySeries.$$p[0]:
                    if (this.dl != null) {
                        this.dl.deregisterColumn(this.highColumn);
                        this.highColumn = this.di(this.aam);
                    }
                    break;
                case "HighColumn":
                    if (!this.zf()) {
                        this.yt.cw.g(this.ib);
                        this.rm(this.ge && this.d5);
                    }
                    break;
            }
        }
        ax(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;
        }
        o8(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.aap || d == this.aam) {
                        if (e != null) {
                            e.c1();
                        }
                        if (!this.zf()) {
                            this.rm(true);
                        }
                    }
                    break;
                case 1:
                    if (e != null) {
                        e.c1();
                    }
                    this.yt.cw.g(this.ib);
                    if (!this.zf()) {
                        this.rm(true);
                    }
                    break;
                case 0:
                    if (e != null) {
                        e.c1();
                    }
                    this.yt.cw.g(this.ib);
                    if (!this.zf()) {
                        this.rm(true);
                    }
                    break;
                case 2:
                    if (e != null) {
                        e.c1();
                    }
                    if (this.aap != null && this.aam != null && this.yt.cw.d > 0 && !this.zf()) {
                        this.rm(true);
                    }
                    break;
                case 4:
                    if (e != null) {
                        e.c1();
                    }
                    this.yt.cw.g(this.ib);
                    if (!this.zf()) {
                        this.rm(true);
                    }
                    break;
            }
        }
        hc(a, b, c) {
            let d = super.hc(a, b, c);
            if (this.lowColumn == null || this.lowColumn.count == 0 || this.highColumn == null || this.highColumn.count == 0) {
                d = false;
            }
            return d;
        }
        z0(a, b) {
            super.z0(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.yt.cw;
        }
        get currentMode2Index() {
            return this.zo();
        }
        provideCollisionDetector() {
            return new CollisionAvoider();
        }
        get mayContainUnknowns() {
            return this.lowColumn == null || this.lowColumn.mayContainUnknowns || this.highColumn == null || this.highColumn.mayContainUnknowns;
        }
        ro(a, b) {
            let c = this.gy;
            super.ro(a, b);
            if (!c) {
                this.ct.a4(b);
                return;
            }
            this.ct.a4(b);
            if (this.d3(this.cs)) {
                return;
            }
            let d = new RangeCategoryFramePreparer(1, this, typeCast(ISupportsMarkers_$type, this.cs), this.c9.c8.o, this, this.cs.cw);
            if (!this.gq) {
                this.yn = new CategoryFrame(3);
                this.yn.v();
                d.d(this.yn, this.cs);
            }
            this.gq = false;
            this.z3(this.yn, this.cs);
            this.gy = false;
        }
        rl(a, b, c, d, e) {
            super.rl(a, b, c, d, e);
            let f = this.a9.a$j.item(d);
            let g = f;
            g.cw.g(this.ib);
            f.a3(c);
            if (this.d3(g)) {
                return;
            }
            let h = new RangeCategoryFramePreparer(1, this, typeCast(ISupportsMarkers_$type, f), f, this, f.cw);
            if (this.aaa == null) {
                this.aaa = new CategoryFrame(3);
            }
            this.aaa.v();
            h.d(this.aaa, g);
            this.z3(this.aaa, g);
        }
        iw() {
            return Series.ix(this.lowColumn, typeCast(ISortingAxis_$type, this.fetchXAxis()));
        }
        get useHighMarkerFidelity() {
            return this.zg;
        }
    }
    RangeCategorySeries.$t = /*@__PURE__*/ markType(RangeCategorySeries, 'RangeCategorySeries', CategorySeries.$, [IIsCategoryBased_$type, IHasHighLowValueCategory_$type]);
    RangeCategorySeries.$$p = /*@__PURE__*/ markDep(DependencyProperty, PropertyMetadata, RangeCategorySeries, 'raisePropertyChanged', ['HighMemberPath:aam:aaw', [2, null], 'HighlightedHighMemberPath:aai:aau', [2, null], 'HighlightedLowMemberPath:aak:aav', [2, null], 'LowMemberPath:aap:aax', [2, null]]);
    return RangeCategorySeries;
})();
/**
 * @hidden
 */
export let RangeCategorySeriesView = /*@__PURE__*/ (() => {
    class RangeCategorySeriesView extends CategorySeriesView {
        constructor(a) {
            super(a);
            this.c5 = null;
            this.c5 = a;
        }
        c6(a, b, c, d, e, f) {
            a.an.clear();
            b.an.clear();
            c.an.clear();
            let g = FastFlattener.b(d, e, true, f, this.e.ib);
            let h = FastFlattener.b(d, e, false, f, this.e.ib);
            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.an.add({ $type: Point_$type, x: m, y: n });
                b.an.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.an.add({ $type: Point_$type, x: p, y: q });
                b.an.add({ $type: Point_$type, x: p, y: q });
            }
            a.ab = a.an.count > 0;
            b.ab = b.an.count > 0;
            c.ab = c.an.count > 0;
        }
        cx() {
            return new RangeCategoryBucketCalculator(this);
        }
    }
    RangeCategorySeriesView.$t = /*@__PURE__*/ markType(RangeCategorySeriesView, 'RangeCategorySeriesView', CategorySeriesView.$);
    return RangeCategorySeriesView;
})();
