/*
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-angular-core";
import { isNaN_, truncate, isInfinity } from "igniteui-angular-core";
import { CategorySeries } from "./CategorySeries";
import { IIsCategoryBased_$type } from "./IIsCategoryBased";
import { IHasHighLowValueCategory_$type } from "./IHasHighLowValueCategory";
import { RangeCategoryFramePreparer } from "./RangeCategoryFramePreparer";
import { DependencyProperty } from "igniteui-angular-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-angular-core";
import { ScalerParams } from "./ScalerParams";
import { ISortingAxis_$type } from "./ISortingAxis";
import { IFastItemsSource_$type } from "igniteui-angular-core";
import { CollisionAvoider } from "./CollisionAvoider";
import { ISupportsMarkers_$type } from "./ISupportsMarkers";
import { ChartSelection } from "./ChartSelection";
import { PropertyMetadata } from "igniteui-angular-core";
import { FastFlattener } from "igniteui-angular-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.di.lowColumn.count;
            this.k = this.j.di.highColumn.count;
            this.i = this.j.di.lowColumn.asArray();
            this.h = this.j.di.highColumn.asArray();
        }
        unCacheValues() {
            this.i = null;
            this.h = null;
        }
    }
    RangeCategoryBucketCalculator.$t = markType(RangeCategoryBucketCalculator, 'RangeCategoryBucketCalculator', CategoryBucketCalculator.$);
    return RangeCategoryBucketCalculator;
})();
/**
 * @hidden
 */
export let RangeCategorySeries = /*@__PURE__*/ (() => {
    class RangeCategorySeries extends CategorySeries {
        constructor() {
            super();
            this._RangeFramePreparer = null;
            this.aa8 = null;
            this.aa7 = null;
            this.aa4 = null;
            this.RangeFramePreparer = new RangeCategoryFramePreparer(1, this, this.aa6, this, this, this.aa6.c9);
        }
        ct() {
            return new RangeCategorySeriesView(this);
        }
        getHostReferenceValue() {
            return this.i0();
        }
        q7(a) {
            super.q7(a);
            this.aa6 = a;
        }
        get RangeFramePreparer() {
            return this._RangeFramePreparer;
        }
        set RangeFramePreparer(a) {
            this._RangeFramePreparer = a;
        }
        gc() {
            return this.highlightedItemsSource != null || (this.abe != null && this.abc != null);
        }
        hu() {
            let a = super.hu();
            {
                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.abl));
                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.abm));
                a.add(c);
            }
            return a;
        }
        abm(a, b) {
            if (this.abe != null) {
                b.value = this.abe;
                return;
            }
            b.value = this.abj;
        }
        abl(a, b) {
            if (this.abc != null) {
                b.value = this.abc;
                return;
            }
            b.value = this.abg;
        }
        ku(a, b) {
            let c = this.ni(b);
            if (c == RangeCategorySeries.$$p[0]) {
                return this.kv(a, b, this.abg);
            }
            if (c == RangeCategorySeries.$$p[3]) {
                return this.kv(a, b, this.abj);
            }
            if (c == RangeCategorySeries.$$p[2]) {
                return this.kv(a, b, this.abe);
            }
            if (c == RangeCategorySeries.$$p[1]) {
                return this.kv(a, b, this.abc);
            }
            return super.ku(a, b);
        }
        mi(a) {
            let b = this.ni(a);
            if (b == RangeCategorySeries.$$p[0]) {
                return this.abg;
            }
            if (b == RangeCategorySeries.$$p[3]) {
                return this.abj;
            }
            if (b == RangeCategorySeries.$$p[2]) {
                return this.abe;
            }
            if (b == RangeCategorySeries.$$p[1]) {
                return this.abc;
            }
            return super.mi(a);
        }
        get lowColumn() {
            return this.aa8;
        }
        set lowColumn(a) {
            if (this.aa8 != a) {
                let b = this.aa8;
                this.aa8 = a;
                this.raisePropertyChanged("LowColumn", b, this.aa8);
            }
        }
        get highColumn() {
            return this.aa7;
        }
        set highColumn(a) {
            if (this.aa7 != a) {
                let b = this.aa7;
                this.aa7 = a;
                this.raisePropertyChanged("HighColumn", b, this.aa7);
            }
        }
        ze() {
            return 11;
        }
        aax(a, b) {
            super.aax(a, b);
            b.ck = true;
            CategoryMarkerManager.e(this, a.p, b.dc, this.xs);
            this.aaj(b, a);
        }
        abn(a, b, c, d, e, f) {
            this.aa6.dj(a, b, c, d, e, f);
        }
        az(a) {
            if (a.isEmpty && this.dc != null) {
                a = this.dc.xm;
            }
            let b = this.y2();
            if (this.lowColumn == null || this.highColumn == null) {
                return null;
            }
            let c = this.wt(this.cw);
            let d = new ScalerParams(0, a, this.dc.x4, b.df, this.getEffectiveViewport());
            let e = (b.fw(c.left, d));
            let f = (b.fw(c.right, d));
            if (b.df) {
                let g = e;
                e = f;
                f = g;
            }
            let h = null;
            if (b.dm) {
                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.zj();
            o = this.ax(o, p, a);
            return o;
        }
        ge(a) {
            let b = new Rect(0, 0, 0, 1, 1);
            let c = this.cw != null ? this.cw.ca : Rect.empty;
            c = c.copy();
            let d = this.cw != null ? this.cw.b9 : Rect.empty;
            let e = !c.isEmpty && !d.isEmpty && this.dp != null ? this.dp.indexOf(a) : -1;
            let f = this.fetchXAxis();
            let g = this.fetchYAxis();
            let h = this.wv(this.cw);
            let i;
            if (f != null) {
                let j = new ScalerParams(0, b, b, f.df, h);
                i = f.ft(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.df, h);
                    l.b = this.i0();
                    let m = g.ft(this.highColumn.item(e), l);
                    let n = g.ft(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.c4 != null) {
                    this.c4.au(this.dc, c, true);
                }
            }
            return e >= 0;
        }
        rg(a, b, c, d) {
            super.rg(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.dl(this.abj);
                        this.highColumn = this.dl(this.abg);
                    }
                    if (!this.zz()) {
                        this.zd.c9.g(this.ie);
                        this.ry(this.gh && c != null);
                    }
                    break;
                case RangeCategorySeries.$$p[3]:
                    if (this.dp != null) {
                        this.dp.deregisterColumn(this.lowColumn);
                        this.lowColumn = this.dl(this.abj);
                    }
                    break;
                case "LowColumn":
                    if (!this.zz()) {
                        this.zd.c9.g(this.ie);
                        this.ry(this.gh && this.d9);
                    }
                    break;
                case RangeCategorySeries.$$p[0]:
                    if (this.dp != null) {
                        this.dp.deregisterColumn(this.highColumn);
                        this.highColumn = this.dl(this.abg);
                    }
                    break;
                case "HighColumn":
                    if (!this.zz()) {
                        this.zd.c9.g(this.ie);
                        this.ry(this.gh && this.d9);
                    }
                    break;
            }
        }
        ay(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;
        }
        ph(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.abj || d == this.abg) {
                        if (e != null) {
                            e.d2();
                        }
                        if (!this.zz()) {
                            this.ry(true);
                        }
                    }
                    break;
                case 1:
                    if (e != null) {
                        e.d2();
                    }
                    this.zd.c9.g(this.ie);
                    if (!this.zz()) {
                        this.ry(true);
                    }
                    break;
                case 0:
                    if (e != null) {
                        e.d2();
                    }
                    this.zd.c9.g(this.ie);
                    if (!this.zz()) {
                        this.ry(true);
                    }
                    break;
                case 2:
                    if (e != null) {
                        e.d2();
                    }
                    if (this.abj != null && this.abg != null && this.zd.c9.d > 0 && !this.zz()) {
                        this.ry(true);
                    }
                    break;
                case 4:
                    if (e != null) {
                        e.d2();
                    }
                    this.zd.c9.g(this.ie);
                    if (!this.zz()) {
                        this.ry(true);
                    }
                    break;
            }
        }
        hf(a, b, c) {
            let d = super.hf(a, b, c);
            if (this.lowColumn == null || this.lowColumn.count == 0 || this.highColumn == null || this.highColumn.count == 0) {
                d = false;
            }
            return d;
        }
        aau(a, b) {
            super.aau(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.zd.c9;
        }
        get currentMode2Index() {
            return this.z8();
        }
        provideCollisionDetector() {
            return new CollisionAvoider();
        }
        get mayContainUnknowns() {
            return this.lowColumn == null || this.lowColumn.mayContainUnknowns || this.highColumn == null || this.highColumn.mayContainUnknowns;
        }
        r0(a, b) {
            let c = this.g1;
            super.r0(a, b);
            if (!c) {
                this.cw.be(b);
                return;
            }
            this.cw.be(b);
            if (this.d7(this.cv)) {
                return;
            }
            let d = new RangeCategoryFramePreparer(1, this, typeCast(ISupportsMarkers_$type, this.cv), this.dc.dp.o, this, this.cv.c9);
            if (!this.gt) {
                this.y7 = new CategoryFrame(3);
                this.y7.x();
                d.d(this.y7, this.cv);
            }
            this.gt = false;
            this.aax(this.y7, this.cv);
            this.g1 = false;
        }
        rx(a, b, c, d, e) {
            super.rx(a, b, c, d, e);
            let f = this.ba.a$j.item(d);
            let g = f;
            g.c9.g(this.ie);
            f.bd(c);
            if (this.d7(g)) {
                return;
            }
            let h = new RangeCategoryFramePreparer(1, this, typeCast(ISupportsMarkers_$type, f), f, this, f.c9);
            if (this.aa4 == null) {
                this.aa4 = new CategoryFrame(3);
            }
            this.aa4.x();
            h.d(this.aa4, g);
            this.aax(this.aa4, g);
        }
        i0() {
            return Series.i1(this.lowColumn, typeCast(ISortingAxis_$type, this.fetchXAxis()));
        }
        aaq(a, b, c, d, e, f, g) {
            if (c == -1 || d == -1) {
                return;
            }
            for (let h = c; h <= d; h++) {
                let i = this.lowColumn.item(h);
                let j = this.highColumn.item(h);
                let k = Math.min(i, j);
                let l = Math.max(i, j);
                if (k < f && e < l) {
                    if (g == 7 || g == 6) {
                        b.add(((() => {
                            let $ret = new ChartSelection();
                            $ret.series = this;
                            return $ret;
                        })()));
                        return;
                    }
                    this.aah(b, h, g);
                }
            }
        }
        aah(a, b, c) {
            if (c == 2 || c == 1) {
                a.add(((() => {
                    let $ret = new ChartSelection();
                    $ret.item = this.dp.item(b);
                    return $ret;
                })()));
            }
            else {
                a.add(((() => {
                    let $ret = new ChartSelection();
                    $ret.item = this.dp.item(b);
                    $ret.series = this;
                    return $ret;
                })()));
            }
        }
        get useHighMarkerFidelity() {
            return this.z0;
        }
    }
    RangeCategorySeries.$t = markType(RangeCategorySeries, 'RangeCategorySeries', CategorySeries.$, [IIsCategoryBased_$type, IHasHighLowValueCategory_$type]);
    RangeCategorySeries.$$p = markDep(DependencyProperty, PropertyMetadata, RangeCategorySeries, 'raisePropertyChanged', ['HighMemberPath:abg:abq', [2, null], 'HighlightedHighMemberPath:abc:abo', [2, null], 'HighlightedLowMemberPath:abe:abp', [2, null], 'LowMemberPath:abj:abr', [2, null]]);
    return RangeCategorySeries;
})();
/**
 * @hidden
 */
export let RangeCategorySeriesView = /*@__PURE__*/ (() => {
    class RangeCategorySeriesView extends CategorySeriesView {
        constructor(a) {
            super(a);
            this.di = null;
            this.di = a;
        }
        dj(a, b, c, d, e, f) {
            a.an.clear();
            b.an.clear();
            c.an.clear();
            let g = FastFlattener.b(d, e, true, f, this.f.ie);
            let h = FastFlattener.b(d, e, false, f, this.f.ie);
            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;
        }
        da() {
            return new RangeCategoryBucketCalculator(this);
        }
    }
    RangeCategorySeriesView.$t = markType(RangeCategorySeriesView, 'RangeCategorySeriesView', CategorySeriesView.$);
    return RangeCategorySeriesView;
})();
