/*
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 { ChartSelection } from "./ChartSelection";
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 { BarTrendLineManager } from "./BarTrendLineManager";
import { PropertyMetadata } from "igniteui-angular-core";
import { truncate, isNaN_, isInfinity } from "igniteui-angular-core";
import { ISupportsMarkers_$type } from "./ISupportsMarkers";
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.y9 = new BarFramePreparer(1, _this, _this.ac0, _this, _this, _this.ac0.c9);
        return _this;
    }
    BarSeries.prototype.bk = function () {
        return new BarSeries();
    };
    BarSeries.prototype.get_ez = function () {
        return true;
    };
    BarSeries.prototype.jn = function () {
        if (!isNaN_(this.jm)) {
            return _super.prototype.jn.call(this);
        }
        return Defaults.h;
    };
    BarSeries.prototype.get_fm = function () {
        return true;
    };
    BarSeries.prototype.ct = function () {
        return new BarSeriesView(this);
    };
    BarSeries.prototype.ze = function () {
        return 1;
    };
    BarSeries.prototype.cz = function () {
        if (this.c0 == 0) {
            return 2;
        }
        return _super.prototype.cz.call(this);
    };
    BarSeries.prototype.q7 = function (a) {
        _super.prototype.q7.call(this, a);
        this.ac0 = a;
    };
    BarSeries.prototype.get_ef = function () {
        return true;
    };
    BarSeries.prototype.wx = function (a) {
        if (this.xAxis == null || this.yAxis == null || this.y4.h.count == 0) {
            return Rect.empty;
        }
        var b = this.cw.ca;
        var c = this.cw.b9;
        var d = this.getEffectiveViewport1(this.cw);
        var e = new ScalerParams(0, b, c, this.xAxis.df, d);
        var f = this.wh(a);
        var g = this.ke(a, true);
        var h = this.kc(a, true);
        var i = truncate(Math.floor(g / this.z5(this.cw)));
        var j = truncate(Math.floor(h / this.z5(this.cw)));
        var k = i - this.z6(this.cw);
        var l = j - this.z6(this.cw);
        if (k < 0) {
            k = 0;
        }
        if (k > this.y4.h.count - 1) {
            k = this.y4.h.count - 1;
        }
        if (l < 0) {
            l = 0;
        }
        if (l > this.y4.h.count - 1) {
            l = this.y4.h.count - 1;
        }
        var m = this.y4.h._inner[k];
        var n = this.y4.h._inner[l];
        var o = Math.abs(f.y - m[0]);
        var p = Math.abs(f.y - n[0]);
        var q = this.abl(this.zd);
        var r = this.yAxis.ou(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.gx = function (a, b) {
        if (this.g0(a, b)) {
            return true;
        }
        if (this.xr(a, b)) {
            return true;
        }
        return false;
    };
    BarSeries.prototype.ak = function (a, b) {
        var c = new List$1(ChartSelection.$, 0);
        this.qa(a, c, b);
        return c.toArray();
    };
    BarSeries.prototype.qa = function (a, b, c) {
        this.aap(a, b, c, this.y3, 1);
    };
    BarSeries.prototype.za = function (a) {
        var b = typeCast(CategorySeriesView.$, a);
        if (b != null && b.t) {
            if (b.s) {
                return new BarFramePreparer(1, this, typeCast(ISupportsMarkers_$type, b), b, this, b.c9);
            }
            else {
                return new BarFramePreparer(1, this, typeCast(ISupportsMarkers_$type, b), this.dc.dp.o, this, b.c9);
            }
        }
        else {
            return this.y9;
        }
    };
    BarSeries.prototype.ac = function () {
        _super.prototype.ac.call(this);
    };
    BarSeries.prototype.preferredCategoryMode = function (a) {
        return 2;
    };
    BarSeries.prototype.pb = function (a, b) {
        _super.prototype.pb.call(this, a, b);
        var c = typeCast(BarSeriesView.$, b);
        if (a && c != null && c.du != null) {
            c.du.count = 0;
        }
    };
    BarSeries.prototype.z8 = function () {
        var e_1, _a;
        if (this.bo != null) {
            var a = this.bo;
            return a.z8();
        }
        var b = 0;
        try {
            for (var _b = __values(fromEnum(this.dc.series)), _c = _b.next(); !_c.done; _c = _b.next()) {
                var c = _c.value;
                if (c == this) {
                    return b;
                }
                var d = typeCast(IBarSeries_$type, c);
                if (d != null && d.yAxis == this.yAxis && d.getPreferredCategoryMode() == 2) {
                    b++;
                }
            }
        }
        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.abl = function (a) {
        var b = 0;
        var c = a.ca;
        var d = a.b9;
        var e = this.getEffectiveViewport1(a);
        if (!c.isEmpty && !d.isEmpty && this.xAxis != null) {
            var f = new ScalerParams(0, c, d, this.xAxis.df, e);
            b = this.xAxis.ft(this.xAxis.p5, f);
        }
        return b;
    };
    BarSeries.prototype.ay = 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.ge = function (a) {
        var b = this.cw.ca;
        b = b.copy();
        var c = this.cw.b9;
        var d = new Rect(0, 0, 0, 1, 1);
        var e = !b.isEmpty && !c.isEmpty && this.dp != null ? this.dp.indexOf(a) : -1;
        var f = this.wv(this.cw);
        var g = new ScalerParams(0, d, d, this.xAxis.df, f);
        var h = new ScalerParams(0, d, d, this.yAxis.df, f);
        var i = this.yAxis != null ? this.yAxis.ft(e, h) : NaN;
        var j = this.yAxis != null ? this.y9.r(this.yAxis, d, d, f) : 0;
        i += j;
        var k = this.xAxis != null && this.valueColumn != null && e < this.valueColumn.count ? this.xAxis.ft(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.c4 != null) {
            this.c4.au(this.dc, b, true);
        }
        return e >= 0;
    };
    BarSeries.prototype.iy = function () {
        return this.yAxis.ou(this.cw.ca, this.cw.b9, this.getEffectiveViewport1(this.cw));
    };
    BarSeries.prototype.aax = function (a, b) {
        _super.prototype.aax.call(this, a, b);
        var c = a.h;
        if (!b.r()) {
            return;
        }
        var d = b.ca;
        var e = b.b9;
        var f = this.getEffectiveViewport1(b);
        var g = new ScalerParams(0, d, e, this.xAxis.df, f);
        var h = this.yAxis;
        var i = new ScalerParams(0, d, e, this.yAxis.df, f);
        var j = this.xAxis;
        var k = j.ft(j.p5, g);
        var l = this.yAxis.ou(d, e, f);
        var m = typeCast(BarSeriesView.$, b);
        if (isNaN_(l) || isInfinity(l) || isNaN_(k)) {
            m.du.count = 0;
            return;
        }
        this.zc.a2(this, this.zw(), runOn(this, this.yw), this.z5(b), this.z6(b));
        this.zc.r = this.ac1;
        this.zc.s = this.ac2;
        this.zc.k = this.ac1;
        this.zc.l = this.ac2;
        var n = false;
        var o = this.zc.b;
        if (o != null) {
            n = true;
        }
        var p = this.xAxis.dm;
        var q = p ? this.xAxis : null;
        var r = this.valueColumn.count;
        var s = this.z5(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.du.item(t);
            t++;
            z.height = l;
            z.width = y;
            var aa = this.zc.aw(r, u)[0];
            if (n) {
                this.aat(c, u, r, h, i, b.t, aa);
            }
            if (z.dataContext != null) {
                var ab = z.dataContext;
                if (s == 1) {
                    if (aa >= 0 && aa < this.dp.count) {
                        if (q != null && q.i$b != null && q.i$b.count > aa) {
                            aa = q.i$b._inner[aa];
                        }
                        ab.item = this.dp.item(aa);
                    }
                }
                else {
                    ab.item = null;
                }
            }
            this.zc.ad(z, false, false, false, false);
            z.ap = this.zc.k;
            z.aq = this.zc.l;
            m.dz(z, Math.min(w, x), v);
        }
        m.du.count = t;
        b.dg(a);
    };
    BarSeries.prototype.ph = function (a, b, c, d) {
        var e_2, _a;
        switch (a) {
            case 4:
            case 1:
            case 0:
                this.aa7.c9.g(this.ie);
                break;
        }
        if (this.aa7.dl) {
            this.aa7.dk.g$i(a, b, c, d);
        }
        if (this.aa7.aa.count > 0) {
            try {
                for (var _b = __values(fromEnum(this.aa7.aa)), _c = _b.next(); !_c.done; _c = _b.next()) {
                    var e = _c.value;
                    e.g$i(a, b, c, d);
                }
            }
            catch (e_2_1) {
                e_2 = { error: e_2_1 };
            }
            finally {
                try {
                    if (_c && !_c.done && (_a = _b.return))
                        _a.call(_b);
                }
                finally {
                    if (e_2)
                        throw e_2.error;
                }
            }
        }
        switch (a) {
            case 4:
                if (this.xAxis != null && !this.xAxis.d2()) {
                    this.ry(true);
                }
                break;
            case 1:
                if (this.xAxis != null && !this.xAxis.d2()) {
                    this.ry(true);
                }
                break;
            case 0:
                if (this.xAxis != null && !this.xAxis.d2()) {
                    this.ry(true);
                }
                break;
            case 2:
                if (this.abz != null && this.aa7.c9.d > 0) {
                    this.ry(true);
                }
                break;
            case 3:
                if (d == this.abz) {
                    if (this.xAxis != null && !this.xAxis.d2()) {
                        this.ry(true);
                    }
                }
                break;
        }
    };
    BarSeries.prototype.rg = function (a, b, c, d) {
        _super.prototype.rg.call(this, a, b, c, d);
        switch (b) {
            case VerticalAnchoredCategorySeries.$$p[0]:
                if (c != null) {
                    c.cq(this);
                }
                if (d != null) {
                    d.du(this);
                }
                this.aa7.c9.g(this.ie);
                if (this.xAxis != null && this.xAxis.d2()) {
                    this.ry(false);
                }
                break;
            case VerticalAnchoredCategorySeries.$$p[1]:
                if (c != null) {
                    c.cq(this);
                }
                if (d != null) {
                    d.du(this);
                }
                if (this.aa7.dl) {
                    this.aa7.dk = CategoryTrendLineManagerFactory.a(this.aa7.dk, this.yAxis, this.s3, this, this.ds);
                }
                this.aa7.c9.g(this.ie);
                this.ry(false);
                break;
            case "FastItemsSource":
                if (this.xAxis != null && !this.xAxis.d2()) {
                    this.aa7.c9.g(this.ie);
                    this.ry(this.gh && c != null);
                }
                break;
            case "ValueColumn":
                if (this.xAxis != null && !this.xAxis.d2()) {
                    this.aa7.c9.g(this.ie);
                    this.ry(this.gh && this.d9);
                }
                break;
            case "SeriesViewer":
                if (c != null && d == null) {
                    this.pi(this.xAxis);
                    this.pi(this.yAxis);
                }
                if (c == null && d != null) {
                    this.ro(this.xAxis);
                    this.ro(this.yAxis);
                }
                this.aa7.c9.g(this.ie);
                this.ry(false);
                break;
        }
    };
    BarSeries.prototype.getPreferredCategoryMode = function () {
        return this.preferredCategoryMode(this.yAxis);
    };
    BarSeries.prototype.bd = function () {
        return new BarTrendLineManager();
    };
    Object.defineProperty(BarSeries.prototype, "useHighMarkerFidelity", {
        get: function () {
            return this.z0;
        },
        enumerable: false,
        configurable: true
    });
    BarSeries.$t = markType(BarSeries, 'BarSeries', VerticalAnchoredCategorySeries.$, [IBarSeries_$type]);
    BarSeries.$$p = markDep(DependencyProperty, PropertyMetadata, BarSeries, 'raisePropertyChanged', ['RadiusX:ac1:ac5', [1, 0], 'RadiusY:ac2:ac6', [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.ca;
        var c = this.c.b9;
        var d = this.c.db.dp;
        if (b.isEmpty || c.isEmpty || this.i.dt.yAxis == null || d == null || d.count == 0) {
            this.d = 0;
            return;
        }
        var e = typeCast(BarSeries.$, this.c.db);
        var f = e.wt(this.c);
        var g = e.getEffectiveViewport1(this.c);
        var h = new ScalerParams(0, b, c, e.yAxis.df, g);
        var i = Math.floor(e.yAxis.fx(f.top, h, 0));
        var j = Math.ceil(e.yAxis.fx(f.bottom, h, 0));
        if (!e.yAxis.df) {
            j = Math.ceil(e.yAxis.fx(f.top, h, 0));
            i = Math.floor(e.yAxis.fx(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.dt.valueColumn.count;
        this.h = this.i.dt.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.dt = null;
        _this.du = null;
        _this.dv = null;
        _this.d0 = new Rectangle();
        _this.dt = a;
        _this.du = ((function () {
            var $ret = new Pool$1(Rectangle.$);
            $ret.create = runOn(_this, _this.d1);
            $ret.activate = runOn(_this, _this.dw);
            $ret.disactivate = runOn(_this, _this.dy);
            $ret.destroy = runOn(_this, _this.dx);
            return $ret;
        })());
        _this.dk = new BarTrendLineManager();
        return _this;
    }
    BarSeriesView.prototype.a4 = function () {
        _super.prototype.a4.call(this);
        this.dv = new List$1(Rectangle.$, 0);
        if (!this.t) {
            this.dt.xk = Defaults.c;
            this.dt.jt = Defaults.i;
        }
    };
    BarSeriesView.prototype.d1 = function () {
        var a = new Rectangle();
        this.dv.add(a);
        a._visibility = 1;
        return a;
    };
    BarSeriesView.prototype.dw = function (a) {
        a._visibility = 0;
    };
    BarSeriesView.prototype.dy = function (a) {
        a._visibility = 1;
    };
    BarSeriesView.prototype.dx = function (a) {
        this.dv.remove(a);
    };
    BarSeriesView.prototype.dz = function (a, b, c) {
        if (!this.isDirty) {
            this.a2();
        }
        a.o = c;
        a.n = b;
    };
    BarSeriesView.prototype.da = function () {
        return new BarBucketCalculator(this);
    };
    BarSeriesView.prototype.bt = function (a, b) {
        _super.prototype.bt.call(this, a, b);
        var c = a;
        var d = this.b4(b);
        c._fill = d;
        c._stroke = d;
        c.ad = this.f.ig;
    };
    BarSeriesView.prototype.ak = function (a) {
        return this.dv._inner[a];
    };
    BarSeriesView.prototype.aj = function (a) {
        var b = this.dv._inner[a];
        this.d0.n = b.n;
        this.d0.o = b.o;
        this.d0.width = b.width;
        this.d0.height = b.height;
        var c = this.b4(a);
        this.d0._fill = c;
        this.d0._stroke = c;
        this.d0.ad = this.f.ig + 3;
        return this.d0;
    };
    BarSeriesView.prototype.bj = function (a, b) {
        _super.prototype.bj.call(this, a, b);
        if (a.d) {
            for (var c = 0; c < this.dv.count; c++) {
                var d = this.ai(c, b);
                this.br(d, c, b);
                a.w(d);
            }
        }
    };
    BarSeriesView.prototype.ax = function (a) {
        var e_3, _a, e_4, _b;
        _super.prototype.ax.call(this, a);
        var b = 0;
        var c = new List$1(Rectangle.$, 0);
        try {
            for (var _c = __values(fromEnum(this.du.a)), _d = _c.next(); !_d.done; _d = _c.next()) {
                var d = _d.value;
                c.add(d);
            }
        }
        catch (e_3_1) {
            e_3 = { error: e_3_1 };
        }
        finally {
            try {
                if (_d && !_d.done && (_a = _c.return))
                    _a.call(_c);
            }
            finally {
                if (e_3)
                    throw e_3.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.f.bc.exportRectangleData(a, e, "column" + b, ["Main", "Fill"]);
            }
        }
        catch (e_4_1) {
            e_4 = { error: e_4_1 };
        }
        finally {
            try {
                if (_f && !_f.done && (_b = _e.return))
                    _b.call(_e);
            }
            finally {
                if (e_4)
                    throw e_4.error;
            }
        }
        b++;
    };
    BarSeriesView.prototype.an = function () {
        var a = new Color();
        a.colorString = "rgba(95,95,95,0.5)";
        this.f.wb = a;
        this.f.jq = 5;
        this.f.jr = 5;
        this.f.js = -5;
    };
    BarSeriesView.$t = markType(BarSeriesView, 'BarSeriesView', AnchoredCategorySeriesView.$);
    return BarSeriesView;
}(AnchoredCategorySeriesView));
export { BarSeriesView };
