/*
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 { FinancialOverlay } from "./FinancialOverlay";
import { DependencyProperty } from "igniteui-react-core/ES5/DependencyProperty";
import { List$1 } from "igniteui-react-core/ES5/List$1";
import { Type, Number_$type, typeCast, runOn, markType, getEnumerator, Point_$type, fromEnum } from "igniteui-react-core/ES5/type";
import { CategoryFrame } from "./CategoryFrame";
import { SafeEnumerable } from "./SafeEnumerable";
import { TrendCalculators } from "igniteui-react-core/ES5/TrendCalculators";
import { AxisRange } from "./AxisRange";
import { FinancialSeriesView } from "./FinancialSeriesView";
import { ScalerParams } from "./ScalerParams";
import { ISortingAxis_$type } from "./ISortingAxis";
import { FinancialBucketCalculator } from "./FinancialBucketCalculator";
import { Polyline } from "igniteui-react-core/ES5/Polyline";
import { Polygon } from "igniteui-react-core/ES5/Polygon";
import { PropertyMetadata } from "igniteui-react-core/ES5/PropertyMetadata";
import { isNaN_ } from "igniteui-react-core/ES5/number";
import { Flattener } from "igniteui-react-core/ES5/Flattener";
var BollingerBandsOverlay = /** @class */ (function (_super) {
    tslib_1.__extends(BollingerBandsOverlay, _super);
    function BollingerBandsOverlay() {
        var _this = _super.call(this) || this;
        _this.pe = 0;
        _this.pf = 0;
        _this.pc = new List$1(Number_$type, 0);
        _this.pd = new List$1(Number_$type, 0);
        _this.ab = BollingerBandsOverlay.$;
        _this.lz = new CategoryFrame(4);
        _this.l1 = new CategoryFrame(4);
        _this.ly = new CategoryFrame(4);
        return _this;
    }
    BollingerBandsOverlay.prototype.io = function (a, b, c, d) {
        if (this.nd().contains(b)) {
            this.o6 = false;
        }
        _super.prototype.io.call(this, a, b, c, d);
        switch (b) {
            case BollingerBandsOverlay.$$p[1]:
                this.o6 = false;
                this.i1(false);
                break;
            case BollingerBandsOverlay.$$p[0]:
                this.i1(false);
                break;
        }
    };
    BollingerBandsOverlay.prototype.o7 = function () {
        this.pc.clear();
        this.pd.clear();
        var a = getEnumerator(TrendCalculators.e(new SafeEnumerable(this.nc), this.ph));
        var b = getEnumerator(TrendCalculators.f(new SafeEnumerable(this.nc), this.ph));
        this.pf = 1.7976931348623157E+308;
        this.pe = -1.7976931348623157E+308;
        var c = true;
        var d = true;
        var e = this.pg;
        while (c || d) {
            if (a.moveNext()) {
                this.pc.add(a.current);
            }
            else {
                c = false;
            }
            if (b.moveNext()) {
                this.pd.add(b.current);
            }
            else {
                d = false;
            }
            if (c && d) {
                this.pf = Math.min(this.pf, a.current - b.current * e);
                this.pe = Math.max(this.pe, a.current + b.current * e);
            }
        }
        return true;
    };
    BollingerBandsOverlay.prototype.an = function (a) {
        if (this.pc == null || this.pd == null || a == null || this.bz == null || a != this.mg) {
            return null;
        }
        var b = new AxisRange(this.pf, this.pe);
        return b;
    };
    BollingerBandsOverlay.prototype.ol = function (a, b) {
        _super.prototype.ol.call(this, a, b);
        var c = b.bq;
        var d = b.bp;
        var e = this.getEffectiveViewport1(b);
        var f = this.lw;
        var g = this.mg;
        var h = new ScalerParams(0, c, d, f.b8, e);
        var i = new ScalerParams(0, c, d, g.b8, e);
        a.f.clear();
        a.m.clear();
        a.p.clear();
        var j = typeCast(ISortingAxis_$type, this.lw);
        if (j != null && j.i$b.count != this.bz.count) {
            return;
        }
        var k = 0;
        k = this.nl(c, d, e);
        if (!this.o6) {
            this.o6 = this.o7();
            if (this.mg != null) {
                this.mg.cl(true);
            }
        }
        i.b = this.ec();
        var l = this.lw.dw(2, h) - this.lw.dw(1, h);
        for (var m = b.bs.c; m <= b.bs.d; ++m) {
            var n = void 0;
            if (j == null) {
                n = b.bs.getBucket(m);
            }
            else {
                var o = j.i$b._inner[m];
                var p = j.i$d(o);
                var q = this.pc._inner[m];
                var r = this.pd._inner[m];
                var s = q;
                var t = r;
                var u = p;
                var v = 1;
                while (m < b.bs.d) {
                    o = j.i$b._inner[m + 1];
                    u = j.i$d(o);
                    if (u - p > l) {
                        break;
                    }
                    m++;
                    s += this.pc._inner[m];
                    t += this.pd._inner[m];
                    v++;
                }
                s /= v;
                t /= v;
                var w = s - t * this.pg;
                var x = s;
                var y = s + t * this.pg;
                var z = NaN;
                if (!isNaN_(p)) {
                    z = this.lw.dt(p, h);
                }
                n = [z, w, x, y];
            }
            var aa = Math.max(1, l);
            if (!isNaN_(n[0]) && m * aa >= this.o8) {
                if (this.lw != null && typeCast(ISortingAxis_$type, this.lw) !== null) {
                    n[0] = n[0] + k;
                }
                else {
                    n[0] = f.dt(n[0], h) + k;
                }
                n[1] = g.dt(n[1], i);
                n[2] = g.dt(n[2], i);
                n[3] = g.dt(n[3], i);
                a.f.add(n);
            }
        }
    };
    BollingerBandsOverlay.prototype.hl = function (a, b) {
        _super.prototype.hl.call(this, a, b);
        var c = b;
        if (c != null) {
            c.bz();
        }
    };
    BollingerBandsOverlay.prototype.eh = function (a, b, c) {
        if (this.pc == null) {
            return _super.prototype.eh.call(this, a, b, c);
        }
        var d = this.getEffectiveViewport1(this.bi);
        var e = this.nl(this.bi.bq, this.bi.bp, d);
        var f = new ScalerParams(0, this.bi.bq, this.bi.bp, this.lw.b8, d);
        f.c = this.bs.mx;
        return this.ej(this.pc, a, this.lw, f, e, runOn(this, this.nk), b, c);
    };
    BollingerBandsOverlay.prototype.e8 = function (a, b) {
        if (this.pc == null) {
            return _super.prototype.e8.call(this, a, b);
        }
        return this.e9(a, b, this.lw, runOn(this, this.nk), this.pc);
    };
    BollingerBandsOverlay.prototype.e6 = function (a, b) {
        if (this.pc == null) {
            return _super.prototype.e6.call(this, a, b);
        }
        return this.e7(a, b, this.lw, runOn(this, this.nk), this.pc);
    };
    BollingerBandsOverlay.prototype.dt = function (a, b, c) {
        var d = _super.prototype.dt.call(this, a, b, c);
        if (!this.m3(this.nd())) {
            d = false;
        }
        return d;
    };
    BollingerBandsOverlay.prototype.on = function (a, b) {
        _super.prototype.on.call(this, a, b);
        var c = typeCast(BollingerBandsOverlayView.$, b);
        var d = this.getEffectiveViewport1(b);
        if (b.bv(a)) {
            c.bz();
            var e_1 = a.f.count;
            var f = function (g) { return a.f._inner[g][0]; };
            var g = function (h) { return a.f._inner[e_1 - 1 - h][0]; };
            var h = function (i) { return a.f._inner[i][1]; };
            var i = function (j) { return a.f._inner[j][2]; };
            var j = function (k) { return a.f._inner[e_1 - 1 - k][3]; };
            c.b0(e_1, f, g, h, i, j);
            b.bx(a);
        }
        this.l4.x(this, this.m2(), this.lw, runOn(this, this.lo), this.nr(b), this.nt(b));
        var k = false;
        var l = this.l4.c;
        var m = a.f;
        var n = this.bz.count;
        if (l != null) {
            k = true;
        }
        if (k) {
            var o = new ScalerParams(0, b.bq, b.bp, this.lw.b8, d);
            this.ok(m, -1, n, this.lw, o, b.n);
        }
        var p = c.b7;
        var q = c.b8;
        var r = c.b9;
        var s = c.b2;
        this.l4.ae(q, true, false, true, true);
        this.l4.ao = null;
        this.l4.an = null;
        this.l4.ae(p, true, false, false, true);
        this.l4.ae(r, true, false, false, true);
        this.l4.ae(s, false, true, false, false);
        s._opacity = this.l4.i * this.d1;
    };
    BollingerBandsOverlay.prototype.bf = function () {
        return new BollingerBandsOverlayView(this);
    };
    BollingerBandsOverlay.prototype.ii = function (a) {
        _super.prototype.ii.call(this, a);
        this.pb = typeCast(BollingerBandsOverlayView.$, a);
    };
    BollingerBandsOverlay.prototype.nu = function () {
        return 3;
    };
    BollingerBandsOverlay.prototype.ec = function () {
        return this.pc == null || this.pc.count == 0 ? NaN : this.pc._inner[0];
    };
    BollingerBandsOverlay.$t = markType(BollingerBandsOverlay, 'BollingerBandsOverlay', FinancialOverlay.$);
    return BollingerBandsOverlay;
}(FinancialOverlay));
export { BollingerBandsOverlay };
Type.dep(DependencyProperty, PropertyMetadata, BollingerBandsOverlay, 'raisePropertyChanged', ['Multiplier:pg:pk', [1, 2], 'Period:ph:pl', [1, 14]]);
var BollingerBandsBucketCalculator = /** @class */ (function (_super) {
    tslib_1.__extends(BollingerBandsBucketCalculator, _super);
    function BollingerBandsBucketCalculator(a) {
        var _this = _super.call(this, a) || this;
        _this.f = null;
        _this.f = a;
        return _this;
    }
    BollingerBandsBucketCalculator.prototype.getBucket = function (a) {
        var b = a * this.b;
        var c = Math.min(b + this.b - 1, this.a.bu.bz.count - 1);
        if (b <= c) {
            var d = this.f.by.pg;
            var e = 0;
            var f = 0;
            var g = 0;
            for (var h = b; h <= c; ++h) {
                if (!isNaN_(this.f.by.pc._inner[h]) && !isNaN_(this.f.by.pd._inner[h])) {
                    e += this.f.by.pc._inner[h];
                    f += this.f.by.pd._inner[h];
                    ++g;
                }
            }
            if (g > 0) {
                e = e / g;
                f = f / g;
                return [0.5 * (b + c), e - f * d, e, e + f * d];
            }
        }
        return [NaN, NaN, NaN, NaN];
    };
    BollingerBandsBucketCalculator.$t = markType(BollingerBandsBucketCalculator, 'BollingerBandsBucketCalculator', FinancialBucketCalculator.$);
    return BollingerBandsBucketCalculator;
}(FinancialBucketCalculator));
export { BollingerBandsBucketCalculator };
var BollingerBandsOverlayView = /** @class */ (function (_super) {
    tslib_1.__extends(BollingerBandsOverlayView, _super);
    function BollingerBandsOverlayView(a) {
        var _this = _super.call(this, a) || this;
        _this.by = null;
        _this.b3 = new Polygon();
        _this.ca = new Polyline();
        _this.cb = new Polyline();
        _this.cc = new Polyline();
        _this.b1 = new Polygon();
        _this.b4 = new Polyline();
        _this.b5 = new Polyline();
        _this.b6 = new Polyline();
        _this.by = a;
        return _this;
    }
    BollingerBandsOverlayView.prototype.ap = function () {
        _super.prototype.ap.call(this);
        if (!this.n) {
            this.e.bj = 1;
        }
    };
    BollingerBandsOverlayView.prototype.bt = function () {
        return new BollingerBandsBucketCalculator(this);
    };
    BollingerBandsOverlayView.prototype.b0 = function (a, b, c, d, e, f) {
        try {
            for (var _a = tslib_1.__values(fromEnum(Flattener.d(a, b, d, this.e.eo))), _b = _a.next(); !_b.done; _b = _a.next()) {
                var g = _b.value;
                this.b3.aj.add({ $type: Point_$type, x: b(g), y: d(g) });
                this.ca.aj.add({ $type: Point_$type, x: b(g), y: d(g) });
            }
        }
        catch (e_2_1) { e_2 = { error: e_2_1 }; }
        finally {
            try {
                if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
            }
            finally { if (e_2) throw e_2.error; }
        }
        try {
            for (var _d = tslib_1.__values(fromEnum(Flattener.d(a, b, e, this.e.eo))), _e = _d.next(); !_e.done; _e = _d.next()) {
                var h = _e.value;
                this.cb.aj.add({ $type: Point_$type, x: b(h), y: e(h) });
            }
        }
        catch (e_3_1) { e_3 = { error: e_3_1 }; }
        finally {
            try {
                if (_e && !_e.done && (_f = _d.return)) _f.call(_d);
            }
            finally { if (e_3) throw e_3.error; }
        }
        try {
            for (var _g = tslib_1.__values(fromEnum(Flattener.d(a, c, f, this.e.eo))), _h = _g.next(); !_h.done; _h = _g.next()) {
                var i = _h.value;
                this.b3.aj.add({ $type: Point_$type, x: c(i), y: f(i) });
                this.cc.aj.add({ $type: Point_$type, x: c(i), y: f(i) });
            }
        }
        catch (e_4_1) { e_4 = { error: e_4_1 }; }
        finally {
            try {
                if (_h && !_h.done && (_j = _g.return)) _j.call(_g);
            }
            finally { if (e_4) throw e_4.error; }
        }
        this.an();
        var e_2, _c, e_3, _f, e_4, _j;
    };
    BollingerBandsOverlayView.prototype.bz = function () {
        this.b3.aj.clear();
        this.ca.aj.clear();
        this.cb.aj.clear();
        this.cc.aj.clear();
        this.an();
    };
    Object.defineProperty(BollingerBandsOverlayView.prototype, "b2", {
        get: function () {
            return this.b3;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BollingerBandsOverlayView.prototype, "b7", {
        get: function () {
            return this.ca;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BollingerBandsOverlayView.prototype, "b8", {
        get: function () {
            return this.cb;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BollingerBandsOverlayView.prototype, "b9", {
        get: function () {
            return this.cc;
        },
        enumerable: true,
        configurable: true
    });
    BollingerBandsOverlayView.prototype.a9 = function () {
        _super.prototype.a9.call(this);
        this.b1.aj = this.b3.aj;
        this.b4.aj = this.ca.aj;
        this.b5.aj = this.cb.aj;
        this.b6.aj = this.cc.aj;
        var a = this.bk();
        this.b1._fill = a;
        this.b1._opacity = 1;
        this.b4._stroke = a;
        this.b4.ac = this.e.es + 3;
        this.b5._stroke = a;
        this.b5.ac = this.e.es + 3;
        this.b6._stroke = a;
        this.b6.ac = this.e.es + 3;
    };
    BollingerBandsOverlayView.prototype.a3 = function (a, b) {
        _super.prototype.a3.call(this, a, b);
        if (a.d) {
            if (b) {
                a.t(this.b1);
                a.u(this.b4);
                a.u(this.b5);
                a.u(this.b6);
            }
            else {
                a.t(this.b3);
                a.u(this.ca);
                a.u(this.cb);
                a.u(this.cc);
            }
        }
    };
    BollingerBandsOverlayView.prototype.ai = function (a) {
        _super.prototype.ai.call(this, a);
        this.e.a2.exportPolygonData(a, this.b3, "FillShape", ["Fill"]);
        this.e.a2.exportPolylineData(a, this.ca, "BottomLine", ["Lower", "Main"]);
        this.e.a2.exportPolylineData(a, this.cb, "CentralLine", ["Central"]);
        this.e.a2.exportPolylineData(a, this.cc, "TopLine", ["Upper"]);
    };
    BollingerBandsOverlayView.$t = markType(BollingerBandsOverlayView, 'BollingerBandsOverlayView', FinancialSeriesView.$);
    return BollingerBandsOverlayView;
}(FinancialSeriesView));
export { BollingerBandsOverlayView };
/*<BeginStatics Name="Infragistics.Controls.Charts.BollingerBandsOverlay" />*/
/*<EndStatics Name="Infragistics.Controls.Charts.BollingerBandsOverlay" />*/
