/*
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 { __extends } from "tslib";
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
 */
var RangeCategoryBucketCalculator = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(RangeCategoryBucketCalculator, _super);
    function RangeCategoryBucketCalculator(a) {
        var _this = _super.call(this, a) || this;
        _this.j = null;
        _this.l = 0;
        _this.k = 0;
        _this.i = null;
        _this.h = null;
        _this.j = a;
        return _this;
    }
    RangeCategoryBucketCalculator.prototype.getBucketWithoutUnknowns = function (a) {
        var b = false;
        var c = this.i;
        var d = this.h;
        var e = this.l;
        var f = this.k;
        var g = Math.min(e, f);
        var h = a * this.d;
        var i = Math.min(h + this.d - 1, g - 1);
        var j = 1.7976931348623157E+308;
        var k = -1.7976931348623157E+308;
        var l = true;
        var m = 0;
        var n = 0;
        var o;
        var p;
        if (h <= i) {
            for (var 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) {
            var r = j;
            j = k;
            k = r;
        }
        if (!l) {
            var s = new Array(3);
            s[0] = 0.5 * (h + i);
            s[1] = j;
            s[2] = k;
            return s;
        }
        var t = new Array(3);
        t[0] = NaN;
        t[1] = NaN;
        t[2] = NaN;
        return t;
    };
    RangeCategoryBucketCalculator.prototype.getBucket = function (a) {
        var b = false;
        var c = this.i;
        var d = this.h;
        var e = this.l;
        var f = this.k;
        var g = Math.min(e, f);
        var h = a * this.d;
        var i = Math.min(h + this.d - 1, g - 1);
        var j = NaN;
        var k = NaN;
        var l = true;
        var m = 0;
        var n = 0;
        var o;
        var p;
        if (h <= i) {
            for (var 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) {
            var r = j;
            j = k;
            k = r;
        }
        if (!l) {
            var s = new Array(3);
            s[0] = 0.5 * (h + i);
            s[1] = j;
            s[2] = k;
            return s;
        }
        var t = new Array(3);
        t[0] = NaN;
        t[1] = NaN;
        t[2] = NaN;
        return t;
    };
    RangeCategoryBucketCalculator.prototype.cacheValues = function () {
        this.l = this.j.c3.lowColumn.count;
        this.k = this.j.c3.highColumn.count;
        this.i = this.j.c3.lowColumn.asArray();
        this.h = this.j.c3.highColumn.asArray();
    };
    RangeCategoryBucketCalculator.prototype.unCacheValues = function () {
        this.i = null;
        this.h = null;
    };
    RangeCategoryBucketCalculator.$t = markType(RangeCategoryBucketCalculator, 'RangeCategoryBucketCalculator', CategoryBucketCalculator.$);
    return RangeCategoryBucketCalculator;
}(CategoryBucketCalculator));
export { RangeCategoryBucketCalculator };
/**
 * @hidden
 */
var RangeCategorySeries = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(RangeCategorySeries, _super);
    function RangeCategorySeries() {
        var _this = _super.call(this) || this;
        _this._RangeFramePreparer = null;
        _this.xi = null;
        _this.xh = null;
        _this.xe = null;
        _this.RangeFramePreparer = new RangeCategoryFramePreparer(1, _this, _this.xg, _this, _this, _this.xg.cu);
        return _this;
    }
    RangeCategorySeries.prototype.cd = function () {
        return new RangeCategorySeriesView(this);
    };
    RangeCategorySeries.prototype.getHostReferenceValue = function () {
        return this.h3();
    };
    RangeCategorySeries.prototype.o2 = function (a) {
        _super.prototype.o2.call(this, a);
        this.xg = a;
    };
    Object.defineProperty(RangeCategorySeries.prototype, "RangeFramePreparer", {
        get: function () {
            return this._RangeFramePreparer;
        },
        set: function (a) {
            this._RangeFramePreparer = a;
        },
        enumerable: false,
        configurable: true
    });
    RangeCategorySeries.prototype.fo = function () {
        return this.highlightedItemsSource != null || (this.xo != null && this.xm != null);
    };
    RangeCategorySeries.prototype.g1 = function () {
        var a = _super.prototype.g1.call(this);
        {
            var b = ((function () {
                var $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.xv));
            a.add(b);
        }
        {
            var c = ((function () {
                var $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.xw));
            a.add(c);
        }
        return a;
    };
    RangeCategorySeries.prototype.xw = function (a, b) {
        if (this.xo != null) {
            b.value = this.xo;
            return;
        }
        b.value = this.xt;
    };
    RangeCategorySeries.prototype.xv = function (a, b) {
        if (this.xm != null) {
            b.value = this.xm;
            return;
        }
        b.value = this.xq;
    };
    RangeCategorySeries.prototype.jv = function (a, b) {
        var c = this.lw(b);
        if (c == RangeCategorySeries.$$p[0]) {
            return this.jw(a, b, this.xq);
        }
        if (c == RangeCategorySeries.$$p[3]) {
            return this.jw(a, b, this.xt);
        }
        if (c == RangeCategorySeries.$$p[2]) {
            return this.jw(a, b, this.xo);
        }
        if (c == RangeCategorySeries.$$p[1]) {
            return this.jw(a, b, this.xm);
        }
        return _super.prototype.jv.call(this, a, b);
    };
    Object.defineProperty(RangeCategorySeries.prototype, "lowColumn", {
        get: function () {
            return this.xi;
        },
        set: function (a) {
            if (this.xi != a) {
                var b = this.xi;
                this.xi = a;
                this.raisePropertyChanged("LowColumn", b, this.xi);
            }
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(RangeCategorySeries.prototype, "highColumn", {
        get: function () {
            return this.xh;
        },
        set: function (a) {
            if (this.xh != a) {
                var b = this.xh;
                this.xh = a;
                this.raisePropertyChanged("HighColumn", b, this.xh);
            }
        },
        enumerable: false,
        configurable: true
    });
    RangeCategorySeries.prototype.vy = function () {
        return 11;
    };
    RangeCategorySeries.prototype.w7 = function (a, b) {
        _super.prototype.w7.call(this, a, b);
        b.b6 = true;
        CategoryMarkerManager.e(this, a.m, b.cx, this.ug);
        this.w1(b, a);
    };
    RangeCategorySeries.prototype.xx = function (a, b, c, d, e, f) {
        this.xg.c4(a, b, c, d, e, f);
    };
    RangeCategorySeries.prototype.ar = function (a) {
        if (a.isEmpty && this.cv != null) {
            a = this.cv.tr;
        }
        var b = this.vn();
        if (this.lowColumn == null || this.highColumn == null) {
            return null;
        }
        var c = this.tq(this.cg);
        var d = new ScalerParams(0, a, this.cv.t6, b.ch, this.getEffectiveViewport());
        var e = (b.em(c.left, d));
        var f = (b.em(c.right, d));
        if (b.ch) {
            var g = e;
            e = f;
            f = g;
        }
        var 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));
        var i = 1.7976931348623157E+308;
        var j = -1.7976931348623157E+308;
        for (var k = truncate(e); k <= truncate(f); k++) {
            var l = k;
            if (h != null) {
                l = h.i$b._inner[k];
            }
            var m = this.lowColumn.item(l);
            if (this.lowColumn.mayContainUnknowns) {
                if (isInfinity(m) || isNaN_(m)) {
                    continue;
                }
            }
            var 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);
        }
        var o = new AxisRange(i, j);
        var p = this.v3();
        o = this.ap(o, p, a);
        return o;
    };
    RangeCategorySeries.prototype.fp = function (a) {
        var b = new Rect(0, 0, 0, 1, 1);
        var c = this.cg != null ? this.cg.by : Rect.empty;
        c = c.copy();
        var d = this.cg != null ? this.cg.bx : Rect.empty;
        var e = !c.isEmpty && !d.isEmpty && this.c7 != null ? this.c7.indexOf(a) : -1;
        var f = this.fetchXAxis();
        var g = this.fetchYAxis();
        var h = this.ts(this.cg);
        var i;
        if (f != null) {
            var j = new ScalerParams(0, b, b, f.ch, h);
            i = f.ej(e, j);
        }
        else {
            i = NaN;
        }
        var 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) {
                var l = new ScalerParams(0, b, b, g.ch, h);
                l.b = this.h3();
                var m = g.ej(this.highColumn.item(e), l);
                var n = g.ej(this.lowColumn.item(e), l);
                if (!isNaN_(m) && !isNaN_(n)) {
                    var 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.co != null) {
                this.co.au(this.cv, c, true);
            }
        }
        return e >= 0;
    };
    RangeCategorySeries.prototype.pa = function (a, b, c, d) {
        _super.prototype.pa.call(this, 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.c4(this.xt);
                    this.highColumn = this.c4(this.xq);
                }
                if (!this.wj()) {
                    this.vx.cu.g(this.hk);
                    this.pr(this.fr && c != null);
                }
                break;
            case RangeCategorySeries.$$p[3]:
                if (this.c7 != null) {
                    this.c7.deregisterColumn(this.lowColumn);
                    this.lowColumn = this.c4(this.xt);
                }
                break;
            case "LowColumn":
                if (!this.wj()) {
                    this.vx.cu.g(this.hk);
                    this.pr(this.fr && this.dp);
                }
                break;
            case RangeCategorySeries.$$p[0]:
                if (this.c7 != null) {
                    this.c7.deregisterColumn(this.highColumn);
                    this.highColumn = this.c4(this.xq);
                }
                break;
            case "HighColumn":
                if (!this.wj()) {
                    this.vx.cu.g(this.hk);
                    this.pr(this.fr && this.dp);
                }
                break;
        }
    };
    RangeCategorySeries.prototype.aq = function (a) {
        if (this.lowColumn == null || this.lowColumn.count == 0 || this.highColumn == null || this.highColumn.count == 0) {
            return null;
        }
        if (a == this.fetchXAxis()) {
            var b = Math.min(this.lowColumn.count, this.highColumn.count);
            return new AxisRange(0, b - 1);
        }
        if (a == this.fetchYAxis()) {
            var c = Math.min(this.lowColumn.minimum, this.highColumn.minimum);
            var d = Math.max(this.lowColumn.maximum, this.highColumn.maximum);
            return new AxisRange(Math.min(c, d), Math.max(c, d));
        }
        return null;
    };
    RangeCategorySeries.prototype.np = function (a, b, c, d) {
        if (this.fetchXAxis() != null && typeCast(ISortingAxis_$type, this.fetchXAxis()) !== null) {
            this.fetchXAxis().i$i();
        }
        var e = this.fetchXAxis();
        switch (a) {
            case 3:
                if (d == this.xt || d == this.xq) {
                    if (e != null) {
                        e.c1();
                    }
                    if (!this.wj()) {
                        this.pr(true);
                    }
                }
                break;
            case 1:
                if (e != null) {
                    e.c1();
                }
                this.vx.cu.g(this.hk);
                if (!this.wj()) {
                    this.pr(true);
                }
                break;
            case 0:
                if (e != null) {
                    e.c1();
                }
                this.vx.cu.g(this.hk);
                if (!this.wj()) {
                    this.pr(true);
                }
                break;
            case 2:
                if (e != null) {
                    e.c1();
                }
                if (this.xt != null && this.xq != null && this.vx.cu.d > 0 && !this.wj()) {
                    this.pr(true);
                }
                break;
            case 4:
                if (e != null) {
                    e.c1();
                }
                this.vx.cu.g(this.hk);
                if (!this.wj()) {
                    this.pr(true);
                }
                break;
        }
    };
    RangeCategorySeries.prototype.gm = function (a, b, c) {
        var d = _super.prototype.gm.call(this, a, b, c);
        if (this.lowColumn == null || this.lowColumn.count == 0 || this.highColumn == null || this.highColumn.count == 0) {
            d = false;
        }
        return d;
    };
    RangeCategorySeries.prototype.w4 = function (a, b) {
        _super.prototype.w4.call(this, a, b);
        this.RangeFramePreparer.d(a, b);
    };
    Object.defineProperty(RangeCategorySeries.prototype, "currentCategoryMode", {
        get: function () {
            return this.preferredCategoryMode(typeCast(CategoryAxisBase.$, this.fetchXAxis()));
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(RangeCategorySeries.prototype, "scaler", {
        get: function () {
            return typeCast(ICategoryScaler_$type, this.fetchXAxis());
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(RangeCategorySeries.prototype, "yScaler", {
        get: function () {
            return typeCast(IScaler_$type, this.fetchYAxis());
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(RangeCategorySeries.prototype, "bucketizer", {
        get: function () {
            return this.vx.cu;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(RangeCategorySeries.prototype, "currentMode2Index", {
        get: function () {
            return this.ws();
        },
        enumerable: false,
        configurable: true
    });
    RangeCategorySeries.prototype.provideCollisionDetector = function () {
        return new CollisionAvoider();
    };
    Object.defineProperty(RangeCategorySeries.prototype, "mayContainUnknowns", {
        get: function () {
            return this.lowColumn == null || this.lowColumn.mayContainUnknowns || this.highColumn == null || this.highColumn.mayContainUnknowns;
        },
        enumerable: false,
        configurable: true
    });
    RangeCategorySeries.prototype.pt = function (a, b) {
        var c = this.f9;
        _super.prototype.pt.call(this, a, b);
        if (!c) {
            this.cg.a4(b);
            return;
        }
        this.cg.a4(b);
        if (this.dm(this.cf)) {
            return;
        }
        var d = new RangeCategoryFramePreparer(1, this, typeCast(ISupportsMarkers_$type, this.cf), this.cv.cu.o, this, this.cf.cu);
        if (!this.f2) {
            this.vr = new CategoryFrame(3);
            this.vr.u();
            d.d(this.vr, this.cf);
        }
        this.f2 = false;
        this.w7(this.vr, this.cf);
        this.f9 = false;
    };
    RangeCategorySeries.prototype.pq = function (a, b, c, d, e) {
        _super.prototype.pq.call(this, a, b, c, d, e);
        var f = this.a3.a$h.item(d);
        var g = f;
        g.cu.g(this.hk);
        f.a3(c);
        if (this.dm(g)) {
            return;
        }
        var h = new RangeCategoryFramePreparer(1, this, typeCast(ISupportsMarkers_$type, f), f, this, f.cu);
        if (this.xe == null) {
            this.xe = new CategoryFrame(3);
        }
        this.xe.u();
        h.d(this.xe, g);
        this.w7(this.xe, g);
    };
    RangeCategorySeries.prototype.h3 = function () {
        return Series.h4(this.lowColumn, typeCast(ISortingAxis_$type, this.fetchXAxis()));
    };
    Object.defineProperty(RangeCategorySeries.prototype, "useHighMarkerFidelity", {
        get: function () {
            return this.wk;
        },
        enumerable: false,
        configurable: true
    });
    RangeCategorySeries.$t = markType(RangeCategorySeries, 'RangeCategorySeries', CategorySeries.$, [IIsCategoryBased_$type, IHasHighLowValueCategory_$type]);
    RangeCategorySeries.$$p = markDep(DependencyProperty, PropertyMetadata, RangeCategorySeries, 'raisePropertyChanged', ['HighMemberPath:xq:x0', [2, null], 'HighlightedHighMemberPath:xm:xy', [2, null], 'HighlightedLowMemberPath:xo:xz', [2, null], 'LowMemberPath:xt:x1', [2, null]]);
    return RangeCategorySeries;
}(CategorySeries));
export { RangeCategorySeries };
/**
 * @hidden
 */
var RangeCategorySeriesView = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(RangeCategorySeriesView, _super);
    function RangeCategorySeriesView(a) {
        var _this = _super.call(this, a) || this;
        _this.c3 = null;
        _this.c3 = a;
        return _this;
    }
    RangeCategorySeriesView.prototype.c4 = function (a, b, c, d, e, f) {
        a.an.clear();
        b.an.clear();
        c.an.clear();
        var g = FastFlattener.b(d, e, true, f, this.e.hk);
        var h = FastFlattener.b(d, e, false, f, this.e.hk);
        var i = g.count;
        var j = h.count;
        var k;
        var l;
        var m;
        var n;
        for (var 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 });
        }
        var p;
        var q;
        for (var 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;
    };
    RangeCategorySeriesView.prototype.cv = function () {
        return new RangeCategoryBucketCalculator(this);
    };
    RangeCategorySeriesView.$t = markType(RangeCategorySeriesView, 'RangeCategorySeriesView', CategorySeriesView.$);
    return RangeCategorySeriesView;
}(CategorySeriesView));
export { RangeCategorySeriesView };
