/*
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 { 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.yz = new BarFramePreparer(1, _this, _this.aco, _this, _this, _this.aco.cw);
        return _this;
    }
    BarSeries.prototype.bj = function () {
        return new BarSeries();
    };
    BarSeries.prototype.get_ex = function () {
        return true;
    };
    BarSeries.prototype.jk = function () {
        if (!isNaN_(this.jj)) {
            return _super.prototype.jk.call(this);
        }
        return Defaults.h;
    };
    BarSeries.prototype.get_fl = function () {
        return true;
    };
    BarSeries.prototype.cs = function () {
        return new BarSeriesView(this);
    };
    BarSeries.prototype.y4 = function () {
        return 1;
    };
    BarSeries.prototype.cy = function () {
        if (this.cz == 0) {
            return 2;
        }
        return _super.prototype.cy.call(this);
    };
    BarSeries.prototype.q1 = function (a) {
        _super.prototype.q1.call(this, a);
        this.aco = a;
    };
    BarSeries.prototype.get_ed = function () {
        return true;
    };
    BarSeries.prototype.wn = function (a) {
        if (this.xAxis == null || this.yAxis == null || this.yu.f.count == 0) {
            return Rect.empty;
        }
        var b = this.cv.by;
        var c = this.cv.bx;
        var d = this.getEffectiveViewport1(this.cv);
        var e = new ScalerParams(0, b, c, this.xAxis.ch, d);
        var f = this.v7(a);
        var g = this.ka(a, true);
        var h = this.j8(a, true);
        var i = truncate(Math.floor(g / this.zv(this.cv)));
        var j = truncate(Math.floor(h / this.zv(this.cv)));
        var k = i - this.zw(this.cv);
        var l = j - this.zw(this.cv);
        if (k < 0) {
            k = 0;
        }
        if (k > this.yu.f.count - 1) {
            k = this.yu.f.count - 1;
        }
        if (l < 0) {
            l = 0;
        }
        if (l > this.yu.f.count - 1) {
            l = this.yu.f.count - 1;
        }
        var m = this.yu.f._inner[k];
        var n = this.yu.f._inner[l];
        var o = Math.abs(f.y - m[0]);
        var p = Math.abs(f.y - n[0]);
        var q = this.aa9(this.y3);
        var r = this.yAxis.k8(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.gw = function (a, b) {
        if (this.gz(a, b)) {
            return true;
        }
        if (this.xh(a, b)) {
            return true;
        }
        return false;
    };
    BarSeries.prototype.ak = function (a, b) {
        var c = new List$1(ChartSelection.$, 0);
        this.p4(a, c, b);
        return c.toArray();
    };
    BarSeries.prototype.p4 = function (a, b, c) {
        this.aac(a, b, c, this.yt, 1);
    };
    BarSeries.prototype.y0 = 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.cw);
            }
            else {
                return new BarFramePreparer(1, this, typeCast(ISupportsMarkers_$type, b), this.db.c9.o, this, b.cw);
            }
        }
        else {
            return this.yz;
        }
    };
    BarSeries.prototype.ac = function () {
        _super.prototype.ac.call(this);
    };
    BarSeries.prototype.preferredCategoryMode = function (a) {
        return 2;
    };
    BarSeries.prototype.o5 = function (a, b) {
        _super.prototype.o5.call(this, a, b);
        var c = typeCast(BarSeriesView.$, b);
        if (a && c != null && c.dg != null) {
            c.dg.count = 0;
        }
    };
    BarSeries.prototype.zy = function () {
        var e_1, _a;
        if (this.bn != null) {
            var a = this.bn;
            return a.zy();
        }
        var b = 0;
        try {
            for (var _b = __values(fromEnum(this.db.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.aa9 = function (a) {
        var b = 0;
        var c = a.by;
        var d = a.bx;
        var e = this.getEffectiveViewport1(a);
        if (!c.isEmpty && !d.isEmpty && this.xAxis != null) {
            var f = new ScalerParams(0, c, d, this.xAxis.ch, e);
            b = this.xAxis.el(this.xAxis.me, 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.gd = function (a) {
        var b = this.cv.by;
        b = b.copy();
        var c = this.cv.bx;
        var d = new Rect(0, 0, 0, 1, 1);
        var e = !b.isEmpty && !c.isEmpty && this.dn != null ? this.dn.indexOf(a) : -1;
        var f = this.wl(this.cv);
        var g = new ScalerParams(0, d, d, this.xAxis.ch, f);
        var h = new ScalerParams(0, d, d, this.yAxis.ch, f);
        var i = this.yAxis != null ? this.yAxis.el(e, h) : NaN;
        var j = this.yAxis != null ? this.yz.r(this.yAxis, d, d, f) : 0;
        i += j;
        var k = this.xAxis != null && this.valueColumn != null && e < this.valueColumn.count ? this.xAxis.el(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.c3 != null) {
            this.c3.au(this.db, b, true);
        }
        return e >= 0;
    };
    BarSeries.prototype.iw = function () {
        return this.yAxis.k8(this.cv.by, this.cv.bx, this.getEffectiveViewport1(this.cv));
    };
    BarSeries.prototype.aak = function (a, b) {
        _super.prototype.aak.call(this, a, b);
        var c = a.f;
        if (!b.o()) {
            return;
        }
        var d = b.by;
        var e = b.bx;
        var f = this.getEffectiveViewport1(b);
        var g = new ScalerParams(0, d, e, this.xAxis.ch, f);
        var h = this.yAxis;
        var i = new ScalerParams(0, d, e, this.yAxis.ch, f);
        var j = this.xAxis;
        var k = j.el(j.me, g);
        var l = this.yAxis.k8(d, e, f);
        var m = typeCast(BarSeriesView.$, b);
        if (isNaN_(l) || isInfinity(l) || isNaN_(k)) {
            m.dg.count = 0;
            return;
        }
        this.y2.a2(this, this.zm(), runOn(this, this.ym), this.zv(b), this.zw(b));
        this.y2.r = this.acp;
        this.y2.s = this.acq;
        this.y2.k = this.acp;
        this.y2.l = this.acq;
        var n = false;
        var o = this.y2.b;
        if (o != null) {
            n = true;
        }
        var p = this.xAxis.cn;
        var q = p ? this.xAxis : null;
        var r = this.valueColumn.count;
        var s = this.zv(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.dg.item(t);
            t++;
            z.height = l;
            z.width = y;
            var aa = this.y2.aw(r, u)[0];
            if (n) {
                this.aag(c, u, r, h, i, b.q, aa);
            }
            if (z.dataContext != null) {
                var ab = z.dataContext;
                if (s == 1) {
                    if (aa >= 0 && aa < this.dn.count) {
                        if (q != null && q.i$b != null && q.i$b.count > aa) {
                            aa = q.i$b._inner[aa];
                        }
                        ab.item = this.dn.item(aa);
                    }
                }
                else {
                    ab.item = null;
                }
            }
            this.y2.ad(z, false, false, false, false);
            z.ap = this.y2.k;
            z.aq = this.y2.l;
            m.dl(z, Math.min(w, x), v);
        }
        m.dg.count = t;
        b.c3(a);
    };
    BarSeries.prototype.pb = function (a, b, c, d) {
        switch (a) {
            case 4:
            case 1:
            case 0:
                this.aav.cw.g(this.id);
                break;
        }
        if (this.aav.c8) {
            this.aav.c7.g$e(a, b, c, d);
        }
        switch (a) {
            case 4:
                if (this.xAxis != null && !this.xAxis.c1()) {
                    this.rr(true);
                }
                break;
            case 1:
                if (this.xAxis != null && !this.xAxis.c1()) {
                    this.rr(true);
                }
                break;
            case 0:
                if (this.xAxis != null && !this.xAxis.c1()) {
                    this.rr(true);
                }
                break;
            case 2:
                if (this.abn != null && this.aav.cw.d > 0) {
                    this.rr(true);
                }
                break;
            case 3:
                if (d == this.abn) {
                    if (this.xAxis != null && !this.xAxis.c1()) {
                        this.rr(true);
                    }
                }
                break;
        }
    };
    BarSeries.prototype.q9 = function (a, b, c, d) {
        _super.prototype.q9.call(this, a, b, c, d);
        switch (b) {
            case VerticalAnchoredCategorySeries.$$p[0]:
                if (c != null) {
                    c.b0(this);
                }
                if (d != null) {
                    d.ct(this);
                }
                this.aav.cw.g(this.id);
                if (this.xAxis != null && this.xAxis.c1()) {
                    this.rr(false);
                }
                break;
            case VerticalAnchoredCategorySeries.$$p[1]:
                if (c != null) {
                    c.b0(this);
                }
                if (d != null) {
                    d.ct(this);
                }
                if (this.aav.c8) {
                    this.aav.c7 = CategoryTrendLineManagerFactory.a(this.aav.c7, this.yAxis, this.sv, this, this.dr);
                }
                this.aav.cw.g(this.id);
                this.rr(false);
                break;
            case "FastItemsSource":
                if (this.xAxis != null && !this.xAxis.c1()) {
                    this.aav.cw.g(this.id);
                    this.rr(this.gg && c != null);
                }
                break;
            case "ValueColumn":
                if (this.xAxis != null && !this.xAxis.c1()) {
                    this.aav.cw.g(this.id);
                    this.rr(this.gg && this.d7);
                }
                break;
            case "SeriesViewer":
                if (c != null && d == null) {
                    this.pc(this.xAxis);
                    this.pc(this.yAxis);
                }
                if (c == null && d != null) {
                    this.rh(this.xAxis);
                    this.rh(this.yAxis);
                }
                this.aav.cw.g(this.id);
                this.rr(false);
                break;
        }
    };
    BarSeries.prototype.getPreferredCategoryMode = function () {
        return this.preferredCategoryMode(this.yAxis);
    };
    Object.defineProperty(BarSeries.prototype, "useHighMarkerFidelity", {
        get: function () {
            return this.zq;
        },
        enumerable: false,
        configurable: true
    });
    BarSeries.$t = markType(BarSeries, 'BarSeries', VerticalAnchoredCategorySeries.$, [IBarSeries_$type]);
    BarSeries.$$p = markDep(DependencyProperty, PropertyMetadata, BarSeries, 'raisePropertyChanged', ['RadiusX:acp:act', [1, 0], 'RadiusY:acq:acu', [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.by;
        var c = this.c.bx;
        var d = this.c.cy.dn;
        if (b.isEmpty || c.isEmpty || this.i.df.yAxis == null || d == null || d.count == 0) {
            this.d = 0;
            return;
        }
        var e = typeCast(BarSeries.$, this.c.cy);
        var f = e.wj(this.c);
        var g = e.getEffectiveViewport1(this.c);
        var h = new ScalerParams(0, b, c, e.yAxis.ch, g);
        var i = Math.floor(e.yAxis.ep(f.top, h, 0));
        var j = Math.ceil(e.yAxis.ep(f.bottom, h, 0));
        if (!e.yAxis.ch) {
            j = Math.ceil(e.yAxis.ep(f.top, h, 0));
            i = Math.floor(e.yAxis.ep(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.df.valueColumn.count;
        this.h = this.i.df.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.df = null;
        _this.dg = null;
        _this.dh = null;
        _this.dm = new Rectangle();
        _this.df = a;
        _this.dg = ((function () {
            var $ret = new Pool$1(Rectangle.$);
            $ret.create = runOn(_this, _this.dn);
            $ret.activate = runOn(_this, _this.di);
            $ret.disactivate = runOn(_this, _this.dk);
            $ret.destroy = runOn(_this, _this.dj);
            return $ret;
        })());
        _this.c7 = new BarTrendLineManager();
        return _this;
    }
    BarSeriesView.prototype.au = function () {
        _super.prototype.au.call(this);
        this.dh = new List$1(Rectangle.$, 0);
        if (!this.q) {
            this.df.xa = Defaults.c;
            this.df.jp = Defaults.i;
        }
    };
    BarSeriesView.prototype.dn = function () {
        var a = new Rectangle();
        this.dh.add(a);
        a._visibility = 1;
        return a;
    };
    BarSeriesView.prototype.di = function (a) {
        a._visibility = 0;
    };
    BarSeriesView.prototype.dk = function (a) {
        a._visibility = 1;
    };
    BarSeriesView.prototype.dj = function (a) {
        this.dh.remove(a);
    };
    BarSeriesView.prototype.dl = function (a, b, c) {
        if (!this.isDirty) {
            this.as();
        }
        a.o = c;
        a.n = b;
    };
    BarSeriesView.prototype.cx = function () {
        return new BarBucketCalculator(this);
    };
    BarSeriesView.prototype.bh = function (a, b) {
        _super.prototype.bh.call(this, a, b);
        var c = a;
        var d = this.bs(b);
        c._fill = d;
        c._stroke = d;
        c.ad = this.e.ie;
    };
    BarSeriesView.prototype.ac = function (a) {
        return this.dh._inner[a];
    };
    BarSeriesView.prototype.ab = function (a) {
        var b = this.dh._inner[a];
        this.dm.n = b.n;
        this.dm.o = b.o;
        this.dm.width = b.width;
        this.dm.height = b.height;
        var c = this.bs(a);
        this.dm._fill = c;
        this.dm._stroke = c;
        this.dm.ad = this.e.ie + 3;
        return this.dm;
    };
    BarSeriesView.prototype.a8 = function (a, b) {
        _super.prototype.a8.call(this, a, b);
        if (a.d) {
            for (var c = 0; c < this.dh.count; c++) {
                var d = this.aa(c, b);
                this.bf(d, c, b);
                a.w(d);
            }
        }
    };
    BarSeriesView.prototype.an = function (a) {
        var e_2, _a, e_3, _b;
        _super.prototype.an.call(this, a);
        var b = 0;
        var c = new List$1(Rectangle.$, 0);
        try {
            for (var _c = __values(fromEnum(this.dg.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.bc.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.v1 = a;
        this.e.jm = 5;
        this.e.jn = 5;
        this.e.jo = -5;
    };
    BarSeriesView.$t = markType(BarSeriesView, 'BarSeriesView', AnchoredCategorySeriesView.$);
    return BarSeriesView;
}(AnchoredCategorySeriesView));
export { BarSeriesView };
