/*
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, __values } from "tslib";
import { VerticalAnchoredCategorySeries } from "./VerticalAnchoredCategorySeries";
import { IBarSeries_$type } from "./IBarSeries";
import { DependencyProperty } from "igniteui-angular-core";
import { Defaults } from "./Defaults";
import { Rect } from "igniteui-angular-core";
import { typeCast, fromEnum, runOn, markType, markDep } from "igniteui-angular-core";
import { ScalerParams } from "./ScalerParams";
import { List$1 } from "igniteui-angular-core";
import { CategorySeriesView } from "./CategorySeriesView";
import { BarFramePreparer } from "./BarFramePreparer";
import { Pool$1 } from "igniteui-angular-core";
import { Rectangle } from "igniteui-angular-core";
import { AxisRange } from "./AxisRange";
import { CategoryBucketCalculator } from "./CategoryBucketCalculator";
import { AnchoredCategorySeriesView } from "./AnchoredCategorySeriesView";
import { CategoryTrendLineManagerFactory } from "./CategoryTrendLineManagerFactory";
import { PropertyMetadata } from "igniteui-angular-core";
import { truncate, isNaN_, isInfinity } from "igniteui-angular-core";
import { ISupportsMarkers_$type } from "./ISupportsMarkers";
import { BarTrendLineManager } from "./BarTrendLineManager";
import { Color } from "igniteui-angular-core";
/**
 * @hidden
 */
var BarSeries = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(BarSeries, _super);
    function BarSeries() {
        var _this = _super.call(this) || this;
        _this.ab = BarSeries.$;
        _this.r2 = new BarFramePreparer(1, _this, _this.vb, _this, _this, _this.vb.cr);
        return _this;
    }
    BarSeries.prototype.get_di = function () {
        return true;
    };
    BarSeries.prototype.gz = function () {
        if (!isNaN_(this.gy)) {
            return _super.prototype.gz.call(this);
        }
        return Defaults.h;
    };
    BarSeries.prototype.get_d5 = function () {
        return true;
    };
    BarSeries.prototype.by = function () {
        return new BarSeriesView(this);
    };
    BarSeries.prototype.r7 = function () {
        return 1;
    };
    BarSeries.prototype.b4 = function () {
        if (this.b5 == 0) {
            return 2;
        }
        return _super.prototype.b4.call(this);
    };
    BarSeries.prototype.l4 = function (a) {
        _super.prototype.l4.call(this, a);
        this.vb = a;
    };
    BarSeries.prototype.get_c3 = function () {
        return true;
    };
    BarSeries.prototype.p3 = function (a) {
        if (this.xAxis == null || this.yAxis == null || this.rx.f.count == 0) {
            return Rect.empty;
        }
        var b = this.b1.bv;
        var c = this.b1.bu;
        var d = this.getEffectiveViewport1(this.b1);
        var e = new ScalerParams(0, b, c, this.xAxis.cg, d);
        var f = this.pn(a);
        var g = this.ho(a, true);
        var h = this.hm(a, true);
        var i = truncate(Math.floor(g / this.sx(this.b1)));
        var j = truncate(Math.floor(h / this.sx(this.b1)));
        var k = i - this.sy(this.b1);
        var l = j - this.sy(this.b1);
        if (k < 0) {
            k = 0;
        }
        if (k > this.rx.f.count - 1) {
            k = this.rx.f.count - 1;
        }
        if (l < 0) {
            l = 0;
        }
        if (l > this.rx.f.count - 1) {
            l = this.rx.f.count - 1;
        }
        var m = this.rx.f._inner[k];
        var n = this.rx.f._inner[l];
        var o = Math.abs(f.y - m[0]);
        var p = Math.abs(f.y - n[0]);
        var q = this.t1(this.r6);
        var r = this.yAxis.k3(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.ez = function (a, b) {
        if (this.e2(a, b)) {
            return true;
        }
        if (this.qo(a, b)) {
            return true;
        }
        return false;
    };
    BarSeries.prototype.r3 = function (a) {
        var b = typeCast(CategorySeriesView.$, a);
        if (b != null && b.q) {
            if (b.p) {
                return new BarFramePreparer(1, this, typeCast(ISupportsMarkers_$type, b), b, this, b.cr);
            }
            else {
                return new BarFramePreparer(1, this, typeCast(ISupportsMarkers_$type, b), this.ce.cu.o, this, b.cr);
            }
        }
        else {
            return this.r2;
        }
    };
    BarSeries.prototype.ac = function () {
        _super.prototype.ac.call(this);
    };
    BarSeries.prototype.preferredCategoryMode = function (a) {
        return 2;
    };
    BarSeries.prototype.k2 = function (a, b) {
        _super.prototype.k2.call(this, a, b);
        var c = typeCast(BarSeriesView.$, b);
        if (a && c != null && c.db != null) {
            c.db.count = 0;
        }
    };
    BarSeries.prototype.s0 = function () {
        var e_1, _a;
        var a = 0;
        try {
            for (var _b = __values(fromEnum(this.ce.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.t1 = function (a) {
        var b = 0;
        var c = a.bv;
        var d = a.bu;
        var e = this.getEffectiveViewport1(a);
        if (!c.isEmpty && !d.isEmpty && this.xAxis != null) {
            var f = new ScalerParams(0, c, d, this.xAxis.cg, e);
            b = this.xAxis.ei(this.xAxis.l6, f);
        }
        return b;
    };
    BarSeries.prototype.aq = 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.eq = function (a) {
        var b = this.b1.bv;
        b = b.copy();
        var c = this.b1.bu;
        var d = new Rect(0, 0, 0, 1, 1);
        var e = !b.isEmpty && !c.isEmpty && this.cl != null ? this.cl.indexOf(a) : -1;
        var f = this.p1(this.b1);
        var g = new ScalerParams(0, d, d, this.xAxis.cg, f);
        var h = new ScalerParams(0, d, d, this.yAxis.cg, f);
        var i = this.yAxis != null ? this.yAxis.ei(e, h) : NaN;
        var j = this.yAxis != null ? this.r2.r(this.yAxis, d, d, f) : 0;
        i += j;
        var k = this.xAxis != null && this.valueColumn != null && e < this.valueColumn.count ? this.xAxis.ei(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.b9 != null) {
            this.b9.au(this.ce, b, true);
        }
        return e >= 0;
    };
    BarSeries.prototype.ge = function () {
        return this.yAxis.k3(this.b1.bv, this.b1.bu, this.getEffectiveViewport1(this.b1));
    };
    BarSeries.prototype.tf = function (a, b) {
        _super.prototype.tf.call(this, a, b);
        var c = a.f;
        if (!b.o()) {
            return;
        }
        var d = b.bv;
        var e = b.bu;
        var f = this.getEffectiveViewport1(b);
        var g = new ScalerParams(0, d, e, this.xAxis.cg, f);
        var h = this.yAxis;
        var i = new ScalerParams(0, d, e, this.yAxis.cg, f);
        var j = this.xAxis;
        var k = j.ei(j.l6, g);
        var l = this.yAxis.k3(d, e, f);
        var m = typeCast(BarSeriesView.$, b);
        if (isNaN_(l) || isInfinity(l) || isNaN_(k)) {
            m.db.count = 0;
            return;
        }
        this.r5.a2(this, this.sp(), runOn(this, this.rq), this.sx(b), this.sy(b));
        this.r5.r = this.vc;
        this.r5.s = this.vd;
        this.r5.k = this.vc;
        this.r5.l = this.vd;
        var n = false;
        var o = this.r5.b;
        if (o != null) {
            n = true;
        }
        var p = this.xAxis.cm;
        var q = p ? this.xAxis : null;
        var r = this.valueColumn.count;
        var s = this.sx(b);
        var t = 0;
        for (var u = 0; u < c.count; ++u) {
            var v = c._inner[u][0] - 0.5 * l;
            var w = c._inner[u][1];
            var x = k;
            x = Math.max(x, -100);
            w = Math.min(w, e.right + 100);
            var y = Math.abs(w - x);
            if (isInfinity(y) || isNaN_(y)) {
                continue;
            }
            var z = m.db.item(t);
            t++;
            z.height = l;
            z.width = y;
            if (n) {
                this.tb(c, u, r, h, i, b.q);
            }
            if (z.dataContext != null) {
                var aa = z.dataContext;
                if (s == 1) {
                    var ab = this.r5.aw(r, u)[0];
                    if (ab >= 0 && ab < this.cl.count) {
                        if (q != null && q.i$b != null && q.i$b.count > ab) {
                            ab = q.i$b._inner[ab];
                        }
                        aa.item = this.cl.item(ab);
                    }
                }
                else {
                    aa.item = null;
                }
            }
            this.r5.ad(z, false, false, false, false);
            z.ap = this.r5.k;
            z.aq = this.r5.l;
            m.dg(z, Math.min(w, x), v);
        }
        m.db.count = t;
        b.cy(a);
    };
    BarSeries.prototype.k5 = function (a, b, c, d) {
        switch (a) {
            case 4:
            case 1:
            case 0:
                this.tp.cr.g(this.fx);
                break;
        }
        if (this.tp.c3) {
            this.tp.c2.g$e(a, b, c, d);
        }
        switch (a) {
            case 4:
                if (this.xAxis != null && !this.xAxis.c0()) {
                    this.mn(true);
                }
                break;
            case 1:
                if (this.xAxis != null && !this.xAxis.c0()) {
                    this.mn(true);
                }
                break;
            case 0:
                if (this.xAxis != null && !this.xAxis.c0()) {
                    this.mn(true);
                }
                break;
            case 2:
                if (this.uc != null && this.tp.cr.d > 0) {
                    this.mn(true);
                }
                break;
            case 3:
                if (d == this.uc) {
                    if (this.xAxis != null && !this.xAxis.c0()) {
                        this.mn(true);
                    }
                }
                break;
        }
    };
    BarSeries.prototype.l9 = function (a, b, c, d) {
        _super.prototype.l9.call(this, a, b, c, d);
        switch (b) {
            case VerticalAnchoredCategorySeries.$$p[0]:
                if (c != null) {
                    c.bz(this);
                }
                if (d != null) {
                    d.cs(this);
                }
                this.tp.cr.g(this.fx);
                if (this.xAxis != null && this.xAxis.c0()) {
                    this.mn(false);
                }
                break;
            case VerticalAnchoredCategorySeries.$$p[1]:
                if (c != null) {
                    c.bz(this);
                }
                if (d != null) {
                    d.cs(this);
                }
                if (this.tp.c3) {
                    this.tp.c2 = CategoryTrendLineManagerFactory.a(this.tp.c2, this.yAxis, this.nf, this, this.co);
                }
                this.tp.cr.g(this.fx);
                this.mn(false);
                break;
            case "FastItemsSource":
                if (this.xAxis != null && !this.xAxis.c0()) {
                    this.tp.cr.g(this.fx);
                    this.mn(false);
                }
                break;
            case "ValueColumn":
                if (this.xAxis != null && !this.xAxis.c0()) {
                    this.tp.cr.g(this.fx);
                    this.mn(false);
                }
                break;
            case "SeriesViewer":
                if (c != null && d == null) {
                    this.k6(this.xAxis);
                    this.k6(this.yAxis);
                }
                if (c == null && d != null) {
                    this.me(this.xAxis);
                    this.me(this.yAxis);
                }
                this.tp.cr.g(this.fx);
                this.mn(false);
                break;
        }
    };
    BarSeries.prototype.getPreferredCategoryMode = function () {
        return this.preferredCategoryMode(this.yAxis);
    };
    Object.defineProperty(BarSeries.prototype, "useHighMarkerFidelity", {
        get: function () {
            return this.ss;
        },
        enumerable: false,
        configurable: true
    });
    BarSeries.$t = markType(BarSeries, 'BarSeries', VerticalAnchoredCategorySeries.$, [IBarSeries_$type]);
    BarSeries.$$p = markDep(DependencyProperty, PropertyMetadata, BarSeries, 'raisePropertyChanged', ['RadiusX:vc:vg', [1, 0], 'RadiusY:vd:vh', [1, 0]]);
    return BarSeries;
}(VerticalAnchoredCategorySeries));
export { BarSeries };
/**
 * @hidden
 */
var BarBucketCalculator = /** @class */ /*@__PURE__*/ (function (_super) {
    __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.bv;
        var c = this.c.bu;
        var d = this.c.ct.cl;
        if (b.isEmpty || c.isEmpty || this.i.da.yAxis == null || d == null || d.count == 0) {
            this.d = 0;
            return;
        }
        var e = typeCast(BarSeries.$, this.c.ct);
        var f = e.pz(this.c);
        var g = e.getEffectiveViewport1(this.c);
        var h = new ScalerParams(0, b, c, e.yAxis.cg, g);
        var i = Math.floor(e.yAxis.em(f.top, h, 0));
        var j = Math.ceil(e.yAxis.em(f.bottom, h, 0));
        if (!e.yAxis.cg) {
            j = Math.ceil(e.yAxis.em(f.top, h, 0));
            i = Math.floor(e.yAxis.em(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.da.valueColumn.count;
        this.h = this.i.da.valueColumn.asArray();
    };
    BarBucketCalculator.prototype.unCacheValues = function () {
        this.h = null;
    };
    BarBucketCalculator.$t = markType(BarBucketCalculator, 'BarBucketCalculator', CategoryBucketCalculator.$);
    return BarBucketCalculator;
}(CategoryBucketCalculator));
export { BarBucketCalculator };
/**
 * @hidden
 */
var BarSeriesView = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(BarSeriesView, _super);
    function BarSeriesView(a) {
        var _this = _super.call(this, a) || this;
        _this.da = null;
        _this.db = null;
        _this.dc = null;
        _this.dh = new Rectangle();
        _this.da = a;
        _this.db = ((function () {
            var $ret = new Pool$1(Rectangle.$);
            $ret.create = runOn(_this, _this.di);
            $ret.activate = runOn(_this, _this.dd);
            $ret.disactivate = runOn(_this, _this.df);
            $ret.destroy = runOn(_this, _this.de);
            return $ret;
        })());
        _this.c2 = new BarTrendLineManager();
        return _this;
    }
    BarSeriesView.prototype.as = function () {
        _super.prototype.as.call(this);
        this.dc = new List$1(Rectangle.$, 0);
        if (!this.q) {
            this.da.ql = Defaults.c;
            this.da.g4 = Defaults.i;
        }
    };
    BarSeriesView.prototype.di = function () {
        var a = new Rectangle();
        this.dc.add(a);
        a._visibility = 1;
        return a;
    };
    BarSeriesView.prototype.dd = function (a) {
        a._visibility = 0;
    };
    BarSeriesView.prototype.df = function (a) {
        a._visibility = 1;
    };
    BarSeriesView.prototype.de = function (a) {
        this.dc.remove(a);
    };
    BarSeriesView.prototype.dg = function (a, b, c) {
        if (!this.isDirty) {
            this.aq();
        }
        a.o = c;
        a.n = b;
    };
    BarSeriesView.prototype.cs = function () {
        return new BarBucketCalculator(this);
    };
    BarSeriesView.prototype.bf = function (a, b) {
        _super.prototype.bf.call(this, a, b);
        var c = a;
        var d = this.bp(b);
        c._fill = d;
        c._stroke = d;
        c.ad = this.e.fy;
    };
    BarSeriesView.prototype.ac = function (a) {
        return this.dc._inner[a];
    };
    BarSeriesView.prototype.ab = function (a) {
        var b = this.dc._inner[a];
        this.dh.n = b.n;
        this.dh.o = b.o;
        this.dh.width = b.width;
        this.dh.height = b.height;
        var c = this.bp(a);
        this.dh._fill = c;
        this.dh._stroke = c;
        this.dh.ad = this.e.fy + 3;
        return this.dh;
    };
    BarSeriesView.prototype.a6 = function (a, b) {
        _super.prototype.a6.call(this, a, b);
        if (a.d) {
            for (var c = 0; c < this.dc.count; c++) {
                var d = this.aa(c, b);
                this.bd(d, c, b);
                a.w(d);
            }
        }
    };
    BarSeriesView.prototype.al = function (a) {
        var e_2, _a, e_3, _b;
        _super.prototype.al.call(this, a);
        var b = 0;
        var c = new List$1(Rectangle.$, 0);
        try {
            for (var _c = __values(fromEnum(this.db.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 = __values(fromEnum(c)), _f = _e.next(); !_f.done; _f = _e.next()) {
                var e = _f.value;
                this.e.a9.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.af = function () {
        var a = new Color();
        a.colorString = "rgba(95,95,95,0.5)";
        this.e.ph = a;
        this.e.g1 = 5;
        this.e.g2 = 5;
        this.e.g3 = -5;
    };
    BarSeriesView.$t = markType(BarSeriesView, 'BarSeriesView', AnchoredCategorySeriesView.$);
    return BarSeriesView;
}(AnchoredCategorySeriesView));
export { BarSeriesView };
