/*
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 * as tslib_1 from "tslib";
import { VerticalAnchoredCategorySeries } from "./VerticalAnchoredCategorySeries";
import { IIsCategoryBased_$type } from "./IIsCategoryBased";
import { IBarSeries_$type } from "./IBarSeries";
import { DependencyProperty } from "igniteui-react-core/ES5/DependencyProperty";
import { Rect } from "igniteui-react-core/ES5/Rect";
import { Type, typeCast, fromEnum, runOn, markType } from "igniteui-react-core/ES5/type";
import { ScalerParams } from "./ScalerParams";
import { List$1 } from "igniteui-react-core/ES5/List$1";
import { CategorySeriesView } from "./CategorySeriesView";
import { BarFramePreparer } from "./BarFramePreparer";
import { Pool$1 } from "igniteui-react-core/ES5/Pool$1";
import { Rectangle } from "igniteui-react-core/ES5/Rectangle";
import { AxisRange } from "./AxisRange";
import { CategoryBucketCalculator } from "./CategoryBucketCalculator";
import { AnchoredCategorySeriesView } from "./AnchoredCategorySeriesView";
import { CategoryTrendLineManagerFactory } from "./CategoryTrendLineManagerFactory";
import { PropertyMetadata } from "igniteui-react-core/ES5/PropertyMetadata";
import { truncate, isNaN_, isInfinity } from "igniteui-react-core/ES5/number";
import { ISupportsMarkers_$type } from "./ISupportsMarkers";
import { BarTrendLineManager } from "./BarTrendLineManager";
import { Color } from "igniteui-react-core/ES5/Color";
/**
 * @hidden
 */
var BarSeries = /** @class */ (function (_super) {
    tslib_1.__extends(BarSeries, _super);
    function BarSeries() {
        var _this = _super.call(this) || this;
        _this.ab = BarSeries.$;
        _this.m4 = new BarFramePreparer(1, _this, _this.p2, _this, _this, _this.p2.cn);
        return _this;
    }
    BarSeries.prototype.bf = function () {
        return new BarSeriesView(this);
    };
    BarSeries.prototype.m9 = function () {
        return 1;
    };
    BarSeries.prototype.getHostReferenceValue = function () {
        return this.ee();
    };
    BarSeries.prototype.im = function (a) {
        _super.prototype.im.call(this, a);
        this.p2 = a;
    };
    BarSeries.prototype.get_no = function () {
        return true;
    };
    BarSeries.prototype.ln = function (a) {
        if (this.xAxis == null || this.yAxis == null || this.m0.f.count == 0) {
            return Rect.empty;
        }
        var b = this.bi.bs;
        var c = this.bi.br;
        var d = this.getEffectiveViewport1(this.bi);
        var e = new ScalerParams(0, b, c, this.xAxis.b8, d);
        var f = this.k9(a);
        var g = this.fb(a, true);
        var h = this.e9(a, true);
        var i = truncate(Math.floor(g / this.n0(this.bi)));
        var j = truncate(Math.floor(h / this.n0(this.bi)));
        var k = i - this.n1(this.bi);
        var l = j - this.n1(this.bi);
        if (k < 0) {
            k = 0;
        }
        if (k > this.m0.f.count - 1) {
            k = this.m0.f.count - 1;
        }
        if (l < 0) {
            l = 0;
        }
        if (l > this.m0.f.count - 1) {
            l = this.m0.f.count - 1;
        }
        var m = this.m0.f._inner[k];
        var n = this.m0.f._inner[l];
        var o = Math.abs(f.y - m[0]);
        var p = Math.abs(f.y - n[0]);
        var q = this.o0(this.m8);
        var r = this.yAxis.jk(b, c, d);
        if (o < p) {
            var s = m[0] - 0.5 * r;
            var t = m[1];
            var u = q;
            return new Rect(0, Math.min(u, t), s, Math.max(u, t) - Math.min(u, t), r);
        }
        else {
            var v = n[0] - 0.5 * r;
            var w = n[1];
            var x = q;
            return new Rect(0, Math.min(x, w), v, Math.max(x, w) - Math.min(x, w), r);
        }
    };
    BarSeries.prototype.dg = function (a, b) {
        if (this.dj(a, b)) {
            return true;
        }
        if (this.lx(a, b)) {
            return true;
        }
        return false;
    };
    BarSeries.prototype.m5 = function (a) {
        var b = typeCast(CategorySeriesView.$, a);
        if (b != null && b.n) {
            if (b.m) {
                return new BarFramePreparer(1, this, typeCast(ISupportsMarkers_$type, b), b, this, b.cn);
            }
            else {
                return new BarFramePreparer(1, this, typeCast(ISupportsMarkers_$type, b), this.bs.bn.m, this, b.cn);
            }
        }
        else {
            return this.m4;
        }
    };
    BarSeries.prototype.ac = function () {
        _super.prototype.ac.call(this);
    };
    BarSeries.prototype.preferredCategoryMode = function (a) {
        return 2;
    };
    BarSeries.prototype.hp = function (a, b) {
        _super.prototype.hp.call(this, a, b);
        var c = typeCast(BarSeriesView.$, b);
        if (a && c != null && c.c7 != null) {
            c.c7.count = 0;
        }
    };
    BarSeries.prototype.n2 = function () {
        var e_1, _a;
        var a = 0;
        try {
            for (var _b = tslib_1.__values(fromEnum(this.bs.series)), _c = _b.next(); !_c.done; _c = _b.next()) {
                var b = _c.value;
                if (b == this) {
                    return a;
                }
                var c = typeCast(IBarSeries_$type, b);
                if (c != null && c.yAxis == this.yAxis && c.getPreferredCategoryMode() == 2) {
                    a++;
                }
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
            }
            finally { if (e_1) throw e_1.error; }
        }
        return -1;
    };
    BarSeries.prototype.o0 = function (a) {
        var b = 0;
        var c = a.bs;
        var d = a.br;
        var e = this.getEffectiveViewport1(a);
        if (!c.isEmpty && !d.isEmpty && this.xAxis != null) {
            var f = new ScalerParams(0, c, d, this.xAxis.b8, e);
            b = this.xAxis.dt(this.xAxis.j7, f);
        }
        return b;
    };
    BarSeries.prototype.an = function (a) {
        if (this.valueColumn == null || this.valueColumn.count == 0) {
            return null;
        }
        if (a == this.yAxis) {
            return new AxisRange(0, this.valueColumn.count - 1);
        }
        if (a == this.xAxis) {
            return new AxisRange(this.valueColumn.minimum, this.valueColumn.maximum);
        }
        return null;
    };
    BarSeries.prototype.c8 = function (a) {
        var b = this.bi.bs;
        b = b.copy();
        var c = this.bi.br;
        var d = new Rect(0, 0, 0, 1, 1);
        var e = !b.isEmpty && !c.isEmpty && this.bz != null ? this.bz.indexOf(a) : -1;
        var f = this.ll(this.bi);
        var g = new ScalerParams(0, d, d, this.xAxis.b8, f);
        var h = new ScalerParams(0, d, d, this.yAxis.b8, f);
        var i = this.yAxis != null ? this.yAxis.dt(e, h) : NaN;
        var j = this.yAxis != null ? this.m4.r(this.yAxis, d, d, f) : 0;
        i += j;
        var k = this.xAxis != null && this.valueColumn != null && e < this.valueColumn.count ? this.xAxis.dt(this.valueColumn.item(e), g) : NaN;
        if (!isNaN_(k)) {
            if (k < b.left + 0.1 * b.width) {
                k = k + 0.4 * b.width;
                b.x = k - 0.5 * b.width;
            }
            if (k > b.right - 0.1 * b.width) {
                k = k - 0.4 * b.width;
                b.x = k - 0.5 * b.width;
            }
        }
        if (!isNaN_(i)) {
            if (i < b.top + 0.1 * b.height) {
                i = i + 0.4 * b.height;
                b.y = i - 0.5 * b.height;
            }
            if (i > b.bottom - 0.1 * b.height) {
                i = i - 0.4 * b.height;
                b.y = i - 0.5 * b.height;
            }
        }
        if (this.bn != null) {
            this.bn.ap(this.bs, b, true);
        }
        return e >= 0;
    };
    BarSeries.prototype.ed = function () {
        return this.yAxis.jk(this.bi.bs, this.bi.br, this.getEffectiveViewport1(this.bi));
    };
    BarSeries.prototype.oh = function (a, b) {
        _super.prototype.oh.call(this, a, b);
        var c = a.f;
        if (!b.l()) {
            return;
        }
        var d = b.bs;
        var e = b.br;
        var f = this.getEffectiveViewport1(b);
        var g = new ScalerParams(0, d, e, this.xAxis.b8, f);
        var h = this.yAxis;
        var i = new ScalerParams(0, d, e, this.yAxis.b8, f);
        var j = this.xAxis;
        var k = j.dt(j.j7, g);
        var l = this.yAxis.jk(d, e, f);
        var m = typeCast(BarSeriesView.$, b);
        if (isNaN_(l) || isInfinity(l) || isNaN_(k)) {
            m.c7.count = 0;
            return;
        }
        this.m7.x(this, this.ns(), this.yAxis, runOn(this, this.mt), this.n0(b), this.n1(b));
        this.m7.p = this.p3;
        this.m7.q = this.p4;
        this.m7.j = this.p3;
        this.m7.k = this.p4;
        var n = false;
        var o = this.m7.c;
        if (o != null) {
            n = true;
        }
        var p = this.xAxis.cc;
        var q = this.valueColumn.count;
        for (var r = 0; r < c.count; ++r) {
            var s = c._inner[r][0] - 0.5 * l;
            var t = c._inner[r][1];
            var u = k;
            u = Math.max(u, -100);
            t = Math.min(t, e.right + 100);
            var v = m.c7.item(r);
            v.height = l;
            v.width = Math.abs(t - u);
            if (n) {
                this.od(c, r, q, h, i, b.n);
            }
            this.m7.ae(v, false, false, false, false);
            v.al = this.m7.j;
            v.am = this.m7.k;
            m.dc(v, Math.min(t, u), s);
        }
        m.c7.count = c.count;
        b.cu(a);
    };
    BarSeries.prototype.hs = function (a, b, c, d) {
        switch (a) {
            case 4:
            case 1:
            case 0:
                this.or.cn.g(this.er);
                break;
        }
        if (this.or.cz) {
            this.or.cy.g$e(a, b, c, d);
        }
        switch (a) {
            case 4:
                if (this.xAxis != null && !this.xAxis.ck()) {
                    this.i6(true);
                }
                break;
            case 1:
                if (this.xAxis != null && !this.xAxis.ck()) {
                    this.i6(true);
                }
                break;
            case 0:
                if (this.xAxis != null && !this.xAxis.ck()) {
                    this.i6(true);
                }
                break;
            case 2:
                if (this.o6 != null && this.or.cn.d > 0) {
                    this.i6(true);
                }
                break;
            case 3:
                if (d == this.o6) {
                    if (this.xAxis != null && !this.xAxis.ck()) {
                        this.i6(true);
                    }
                }
                break;
        }
    };
    BarSeries.prototype.is = function (a, b, c, d) {
        _super.prototype.is.call(this, a, b, c, d);
        switch (b) {
            case VerticalAnchoredCategorySeries.$$p[0]:
                if (c != null) {
                    c.bv(this);
                }
                if (d != null) {
                    d.ch(this);
                }
                this.or.cn.g(this.er);
                if (this.xAxis != null && this.xAxis.ck()) {
                    this.i6(false);
                }
                break;
            case VerticalAnchoredCategorySeries.$$p[1]:
                if (c != null) {
                    c.bv(this);
                }
                if (d != null) {
                    d.ch(this);
                }
                if (this.or.cz) {
                    this.or.cy = CategoryTrendLineManagerFactory.a(this.or.cy, this.yAxis, this.jq, this, this.b2);
                }
                this.or.cn.g(this.er);
                this.i6(false);
                break;
            case "FastItemsSource":
                if (this.xAxis != null && !this.xAxis.ck()) {
                    this.or.cn.g(this.er);
                    this.i6(false);
                }
                break;
            case "ValueColumn":
                if (this.xAxis != null && !this.xAxis.ck()) {
                    this.or.cn.g(this.er);
                    this.i6(false);
                }
                break;
            case "SeriesViewer":
                if (c != null && d == null) {
                    this.ht(this.xAxis);
                    this.ht(this.yAxis);
                }
                if (c == null && d != null) {
                    this.ix(this.xAxis);
                    this.ix(this.yAxis);
                }
                this.or.cn.g(this.er);
                this.i6(false);
                break;
        }
    };
    BarSeries.prototype.getPreferredCategoryMode = function () {
        return this.preferredCategoryMode(this.yAxis);
    };
    Object.defineProperty(BarSeries.prototype, "currentCategoryMode", {
        get: function () {
            return this.preferredCategoryMode(this.yAxis);
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BarSeries.prototype, "scaler", {
        get: function () {
            return this.yAxis;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BarSeries.prototype, "yScaler", {
        get: function () {
            return this.xAxis;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BarSeries.prototype, "useHighMarkerFidelity", {
        get: function () {
            return this.nv;
        },
        enumerable: true,
        configurable: true
    });
    BarSeries.$t = markType(BarSeries, 'BarSeries', VerticalAnchoredCategorySeries.$, [IIsCategoryBased_$type, IBarSeries_$type]);
    return BarSeries;
}(VerticalAnchoredCategorySeries));
export { BarSeries };
Type.dep(DependencyProperty, PropertyMetadata, BarSeries, 'raisePropertyChanged', ['RadiusX:p3:p7', [1, 2], 'RadiusY:p4:p8', [1, 2]]);
/**
 * @hidden
 */
var BarBucketCalculator = /** @class */ (function (_super) {
    tslib_1.__extends(BarBucketCalculator, _super);
    function BarBucketCalculator(a) {
        var _this = _super.call(this, a) || this;
        _this.i = null;
        _this.h = null;
        _this.j = 0;
        _this.i = a;
        return _this;
    }
    BarBucketCalculator.prototype.g = function (a) {
        var b = this.c.bs;
        var c = this.c.br;
        var d = this.c.cp.bz;
        if (b.isEmpty || c.isEmpty || this.i.c6.yAxis == null || d == null || d.count == 0) {
            this.d = 0;
            return;
        }
        var e = typeCast(BarSeries.$, this.c.cp);
        var f = e.lj(this.c);
        var g = e.getEffectiveViewport1(this.c);
        var h = new ScalerParams(0, b, c, e.yAxis.b8, g);
        var i = Math.floor(e.yAxis.dx(f.top, h, 0));
        var j = Math.ceil(e.yAxis.dx(f.bottom, h, 0));
        if (!e.yAxis.b8) {
            j = Math.ceil(e.yAxis.dx(f.top, h, 0));
            i = Math.floor(e.yAxis.dx(f.bottom, h, 0));
        }
        var k = Math.floor((j - i + 1) * a / f.height);
        this.d = truncate(Math.max(1, k));
        this.e = truncate(Math.max(0, Math.floor(i / this.d) - 1));
        this.f = truncate(Math.ceil(j / this.d));
    };
    BarBucketCalculator.prototype.getBucketWithoutUnknowns = function (a) {
        var b = this.h;
        var c = this.j;
        var d = a * this.d;
        var e = d + this.d - 1;
        var f = c - 1;
        d = d < f ? d : f;
        e = e < f ? e : f;
        var g = 1.7976931348623157E+308;
        var h = -1.7976931348623157E+308;
        var i = true;
        for (var j = d; j <= e; ++j) {
            var k = b[j];
            g = g < k ? g : k;
            h = h > k ? h : k;
            Math.max(h, k);
            i = false;
        }
        if (!i) {
            return [0.5 * (d + e), g, h];
        }
        return [0.5 * (d + e), NaN, NaN];
    };
    BarBucketCalculator.prototype.getBucket = function (a) {
        var b = this.h;
        var c = this.j;
        var d = a * this.d;
        var e = d + this.d - 1;
        var f = c - 1;
        d = d < f ? d : f;
        e = e < f ? e : f;
        var g = NaN;
        var h = NaN;
        for (var i = d; i <= e; ++i) {
            var j = b[i];
            if (!isNaN_(g)) {
                if (!isNaN_(j)) {
                    g = Math.min(g, j);
                    h = Math.max(h, j);
                }
            }
            else {
                g = j;
                h = j;
            }
        }
        if (!isNaN_(g)) {
            return [0.5 * (d + e), g, h];
        }
        return [0.5 * (d + e), NaN, NaN];
    };
    BarBucketCalculator.prototype.cacheValues = function () {
        this.j = this.i.c6.valueColumn.count;
        this.h = this.i.c6.valueColumn.asArray();
    };
    BarBucketCalculator.prototype.unCacheValues = function () {
        this.h = null;
    };
    BarBucketCalculator.$t = markType(BarBucketCalculator, 'BarBucketCalculator', CategoryBucketCalculator.$);
    return BarBucketCalculator;
}(CategoryBucketCalculator));
export { BarBucketCalculator };
/**
 * @hidden
 */
var BarSeriesView = /** @class */ (function (_super) {
    tslib_1.__extends(BarSeriesView, _super);
    function BarSeriesView(a) {
        var _this = _super.call(this, a) || this;
        _this.c6 = null;
        _this.c7 = null;
        _this.c8 = null;
        _this.dd = new Rectangle();
        _this.c6 = a;
        _this.c7 = ((function () {
            var $ret = new Pool$1(Rectangle.$);
            $ret.create = runOn(_this, _this.de);
            $ret.activate = runOn(_this, _this.c9);
            $ret.disactivate = runOn(_this, _this.db);
            $ret.destroy = runOn(_this, _this.da);
            return $ret;
        })());
        _this.cy = new BarTrendLineManager();
        return _this;
    }
    BarSeriesView.prototype.ap = function () {
        _super.prototype.ap.call(this);
        this.c8 = new List$1(Rectangle.$, 0);
        if (!this.n) {
            this.e.er = 4;
            this.e.bj = 1;
        }
    };
    BarSeriesView.prototype.de = function () {
        var a = new Rectangle();
        this.c8.add(a);
        a._visibility = 1;
        return a;
    };
    BarSeriesView.prototype.c9 = function (a) {
        a._visibility = 0;
    };
    BarSeriesView.prototype.db = function (a) {
        a._visibility = 1;
    };
    BarSeriesView.prototype.da = function (a) {
        this.c8.remove(a);
    };
    BarSeriesView.prototype.dc = function (a, b, c) {
        if (!this.isDirty) {
            this.an();
        }
        a.o = c;
        a.n = b;
    };
    BarSeriesView.prototype.co = function () {
        return new BarBucketCalculator(this);
    };
    BarSeriesView.prototype.bc = function (a, b) {
        _super.prototype.bc.call(this, a, b);
        var c = a;
        var d = this.bm(b);
        c._fill = d;
        c._stroke = d;
        c.ac = this.e.ev;
    };
    BarSeriesView.prototype.z = function (a) {
        return this.c8._inner[a];
    };
    BarSeriesView.prototype.y = function (a) {
        var b = this.c8._inner[a];
        this.dd.n = b.n;
        this.dd.o = b.o;
        this.dd.width = b.width;
        this.dd.height = b.height;
        var c = this.bm(a);
        this.dd._fill = c;
        this.dd._stroke = c;
        this.dd.ac = this.e.ev + 3;
        return this.dd;
    };
    BarSeriesView.prototype.a3 = function (a, b) {
        _super.prototype.a3.call(this, a, b);
        if (a.d) {
            for (var c = 0; c < this.c8.count; c++) {
                var d = this.x(c, b);
                this.ba(d, c, b);
                a.v(d);
            }
        }
    };
    BarSeriesView.prototype.ai = function (a) {
        var e_2, _a, e_3, _b;
        _super.prototype.ai.call(this, a);
        var b = 0;
        var c = new List$1(Rectangle.$, 0);
        try {
            for (var _c = tslib_1.__values(fromEnum(this.c7.a)), _d = _c.next(); !_d.done; _d = _c.next()) {
                var d = _d.value;
                c.add(d);
            }
        }
        catch (e_2_1) { e_2 = { error: e_2_1 }; }
        finally {
            try {
                if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
            }
            finally { if (e_2) throw e_2.error; }
        }
        c.aa(function (e, f) {
            if (e.o > f.o) {
                return -1;
            }
            else if (e.o < f.o) {
                return 1;
            }
            else {
                return 0;
            }
        });
        try {
            for (var _e = tslib_1.__values(fromEnum(c)), _f = _e.next(); !_f.done; _f = _e.next()) {
                var e = _f.value;
                this.e.a2.exportRectangleData(a, e, "column" + b, ["Main", "Fill"]);
            }
        }
        catch (e_3_1) { e_3 = { error: e_3_1 }; }
        finally {
            try {
                if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
            }
            finally { if (e_3) throw e_3.error; }
        }
        b++;
    };
    BarSeriesView.prototype.ac = function () {
        var a = new Color();
        a.colorString = "rgba(95,95,95,0.5)";
        this.e.k3 = a;
        this.e.es = 5;
        this.e.et = 5;
        this.e.eu = -5;
    };
    BarSeriesView.$t = markType(BarSeriesView, 'BarSeriesView', AnchoredCategorySeriesView.$);
    return BarSeriesView;
}(AnchoredCategorySeriesView));
export { BarSeriesView };
