/*
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 { Series } from "./Series";
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";
/**
 * @hidden
 */
var BollingerBandsOverlay = /** @class */ (function (_super) {
    tslib_1.__extends(BollingerBandsOverlay, _super);
    function BollingerBandsOverlay() {
        var _this = _super.call(this) || this;
        _this.pj = 0;
        _this.pk = 0;
        _this.ph = new List$1(Number_$type, 0);
        _this.pi = new List$1(Number_$type, 0);
        _this.ab = BollingerBandsOverlay.$;
        _this.l4 = new CategoryFrame(4);
        _this.l6 = new CategoryFrame(4);
        _this.l3 = new CategoryFrame(4);
        return _this;
    }
    BollingerBandsOverlay.prototype.is = function (a, b, c, d) {
        if (this.nk().contains(b)) {
            this.pb = false;
        }
        _super.prototype.is.call(this, a, b, c, d);
        switch (b) {
            case BollingerBandsOverlay.$$p[1]:
                this.pb = false;
                this.i6(false);
                break;
            case BollingerBandsOverlay.$$p[0]:
                this.i6(false);
                break;
        }
    };
    BollingerBandsOverlay.prototype.pc = function () {
        this.ph.clear();
        this.pi.clear();
        var a = getEnumerator(TrendCalculators.e(new SafeEnumerable(this.nj), this.pm));
        var b = getEnumerator(TrendCalculators.f(new SafeEnumerable(this.nj), this.pm));
        this.pk = 1.7976931348623157E+308;
        this.pj = -1.7976931348623157E+308;
        var c = true;
        var d = true;
        var e = this.pl;
        while (c || d) {
            if (a.moveNext()) {
                this.ph.add(a.current);
            }
            else {
                c = false;
            }
            if (b.moveNext()) {
                this.pi.add(b.current);
            }
            else {
                d = false;
            }
            if (c && d) {
                this.pk = Math.min(this.pk, a.current - b.current * e);
                this.pj = Math.max(this.pj, a.current + b.current * e);
            }
        }
        return true;
    };
    BollingerBandsOverlay.prototype.an = function (a) {
        if (this.ph == null || this.pi == null || a == null || this.bz == null || a != this.mm) {
            return null;
        }
        var b = new AxisRange(this.pk, this.pj);
        return b;
    };
    BollingerBandsOverlay.prototype.os = function (a, b) {
        _super.prototype.os.call(this, a, b);
        var c = b.bs;
        var d = b.br;
        var e = this.getEffectiveViewport1(b);
        var f = this.l1;
        var g = this.mm;
        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.l1);
        if (j != null && j.i$b.count != this.bz.count) {
            return;
        }
        var k = 0;
        k = this.ns(c, d, e);
        if (!this.pb) {
            this.pb = this.pc();
            if (this.mm != null) {
                this.mm.cl(true);
            }
        }
        i.b = this.ee();
        var l = this.l1.dw(2, h) - this.l1.dw(1, h);
        for (var m = b.bu.c; m <= b.bu.d; ++m) {
            var n = void 0;
            if (j == null) {
                n = b.bu.getBucket(m);
            }
            else {
                var o = j.i$b._inner[m];
                var p = j.i$d(o);
                var q = this.ph._inner[m];
                var r = this.pi._inner[m];
                var s = q;
                var t = r;
                var u = p;
                var v = 1;
                while (m < b.bu.d) {
                    o = j.i$b._inner[m + 1];
                    u = j.i$d(o);
                    if (u - p > l) {
                        break;
                    }
                    m++;
                    s += this.ph._inner[m];
                    t += this.pi._inner[m];
                    v++;
                }
                s /= v;
                t /= v;
                var w = s - t * this.pl;
                var x = s;
                var y = s + t * this.pl;
                var z = NaN;
                if (!isNaN_(p)) {
                    z = this.l1.dt(p, h);
                }
                n = [z, w, x, y];
            }
            var aa = Math.max(1, l);
            if (!isNaN_(n[0]) && m * aa >= this.pd) {
                if (this.l1 != null && typeCast(ISortingAxis_$type, this.l1) !== 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.hp = function (a, b) {
        _super.prototype.hp.call(this, a, b);
        var c = b;
        if (c != null) {
            c.b1();
        }
    };
    BollingerBandsOverlay.prototype.ek = function (a, b, c) {
        if (this.ph == null) {
            return _super.prototype.ek.call(this, a, b, c);
        }
        var d = this.getEffectiveViewport1(this.bi);
        var e = this.ns(this.bi.bs, this.bi.br, d);
        var f = new ScalerParams(0, this.bi.bs, this.bi.br, this.l1.b8, d);
        f.c = this.bs.m3;
        return this.em(this.ph, a, this.l1, f, e, runOn(this, this.nr), b, c);
    };
    BollingerBandsOverlay.prototype.fb = function (a, b) {
        if (this.ph == null) {
            return _super.prototype.fb.call(this, a, b);
        }
        return this.fc(a, b, this.l1, runOn(this, this.nr), this.ph);
    };
    BollingerBandsOverlay.prototype.e9 = function (a, b) {
        if (this.ph == null) {
            return _super.prototype.e9.call(this, a, b);
        }
        return this.fa(a, b, this.l1, runOn(this, this.nr), this.ph);
    };
    BollingerBandsOverlay.prototype.dv = function (a, b, c) {
        var d = _super.prototype.dv.call(this, a, b, c);
        if (!this.na(this.nk())) {
            d = false;
        }
        return d;
    };
    BollingerBandsOverlay.prototype.ou = function (a, b) {
        _super.prototype.ou.call(this, a, b);
        var c = typeCast(BollingerBandsOverlayView.$, b);
        var d = this.getEffectiveViewport1(b);
        if (b.bx(a)) {
            c.b1();
            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.b2(e_1, f, g, h, i, j);
            b.bz(a);
        }
        this.l9.x(this, this.m9(), this.l1, runOn(this, this.lt), this.ny(b), this.n0(b));
        var k = false;
        var l = this.l9.c;
        var m = a.f;
        var n = this.bz.count;
        if (l != null) {
            k = true;
        }
        if (k) {
            var o = new ScalerParams(0, b.bs, b.br, this.l1.b8, d);
            this.or(m, -1, n, this.l1, o, b.n);
        }
        var p = c.b9;
        var q = c.ca;
        var r = c.cb;
        var s = c.b4;
        this.l9.ae(q, true, false, true, true);
        this.l9.ao = null;
        this.l9.an = null;
        this.l9.ae(p, true, false, false, true);
        this.l9.ae(r, true, false, false, true);
        this.l9.ae(s, false, true, false, false);
        s._opacity = this.l9.i * this.d3;
    };
    BollingerBandsOverlay.prototype.bf = function () {
        return new BollingerBandsOverlayView(this);
    };
    BollingerBandsOverlay.prototype.im = function (a) {
        _super.prototype.im.call(this, a);
        this.pg = typeCast(BollingerBandsOverlayView.$, a);
    };
    BollingerBandsOverlay.prototype.n1 = function () {
        return 3;
    };
    BollingerBandsOverlay.prototype.ee = function () {
        return Series.ef(this.ph, null);
    };
    BollingerBandsOverlay.$t = markType(BollingerBandsOverlay, 'BollingerBandsOverlay', FinancialOverlay.$);
    return BollingerBandsOverlay;
}(FinancialOverlay));
export { BollingerBandsOverlay };
Type.dep(DependencyProperty, PropertyMetadata, BollingerBandsOverlay, 'raisePropertyChanged', ['Multiplier:pl:pp', [1, 2], 'Period:pm:pq', [1, 14]]);
/**
 * @hidden
 */
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.bw.bz.count - 1);
        if (b <= c) {
            var d = this.f.b0.pl;
            var e = 0;
            var f = 0;
            var g = 0;
            for (var h = b; h <= c; ++h) {
                if (!isNaN_(this.f.b0.ph._inner[h]) && !isNaN_(this.f.b0.pi._inner[h])) {
                    e += this.f.b0.ph._inner[h];
                    f += this.f.b0.pi._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 };
/**
 * @hidden
 */
var BollingerBandsOverlayView = /** @class */ (function (_super) {
    tslib_1.__extends(BollingerBandsOverlayView, _super);
    function BollingerBandsOverlayView(a) {
        var _this = _super.call(this, a) || this;
        _this.b0 = null;
        _this.b5 = new Polygon();
        _this.cc = new Polyline();
        _this.cd = new Polyline();
        _this.ce = new Polyline();
        _this.b3 = new Polygon();
        _this.b6 = new Polyline();
        _this.b7 = new Polyline();
        _this.b8 = new Polyline();
        _this.b0 = a;
        return _this;
    }
    BollingerBandsOverlayView.prototype.ap = function () {
        _super.prototype.ap.call(this);
        if (!this.n) {
            this.e.bj = 1;
        }
    };
    BollingerBandsOverlayView.prototype.bv = function () {
        return new BollingerBandsBucketCalculator(this);
    };
    BollingerBandsOverlayView.prototype.b2 = function (a, b, c, d, e, f) {
        var e_2, _a, e_3, _b, e_4, _c;
        try {
            for (var _d = tslib_1.__values(fromEnum(Flattener.d(a, b, d, this.e.er))), _e = _d.next(); !_e.done; _e = _d.next()) {
                var g = _e.value;
                this.b5.aj.add({ $type: Point_$type, x: b(g), y: d(g) });
                this.cc.aj.add({ $type: Point_$type, x: b(g), y: d(g) });
            }
        }
        catch (e_2_1) { e_2 = { error: e_2_1 }; }
        finally {
            try {
                if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
            }
            finally { if (e_2) throw e_2.error; }
        }
        try {
            for (var _f = tslib_1.__values(fromEnum(Flattener.d(a, b, e, this.e.er))), _g = _f.next(); !_g.done; _g = _f.next()) {
                var h = _g.value;
                this.cd.aj.add({ $type: Point_$type, x: b(h), y: e(h) });
            }
        }
        catch (e_3_1) { e_3 = { error: e_3_1 }; }
        finally {
            try {
                if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
            }
            finally { if (e_3) throw e_3.error; }
        }
        try {
            for (var _h = tslib_1.__values(fromEnum(Flattener.d(a, c, f, this.e.er))), _j = _h.next(); !_j.done; _j = _h.next()) {
                var i = _j.value;
                this.b5.aj.add({ $type: Point_$type, x: c(i), y: f(i) });
                this.ce.aj.add({ $type: Point_$type, x: c(i), y: f(i) });
            }
        }
        catch (e_4_1) { e_4 = { error: e_4_1 }; }
        finally {
            try {
                if (_j && !_j.done && (_c = _h.return)) _c.call(_h);
            }
            finally { if (e_4) throw e_4.error; }
        }
        this.an();
    };
    BollingerBandsOverlayView.prototype.b1 = function () {
        this.b5.aj.clear();
        this.cc.aj.clear();
        this.cd.aj.clear();
        this.ce.aj.clear();
        this.an();
    };
    Object.defineProperty(BollingerBandsOverlayView.prototype, "b4", {
        get: function () {
            return this.b5;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BollingerBandsOverlayView.prototype, "b9", {
        get: function () {
            return this.cc;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BollingerBandsOverlayView.prototype, "ca", {
        get: function () {
            return this.cd;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BollingerBandsOverlayView.prototype, "cb", {
        get: function () {
            return this.ce;
        },
        enumerable: true,
        configurable: true
    });
    BollingerBandsOverlayView.prototype.a9 = function () {
        _super.prototype.a9.call(this);
        this.b3.aj = this.b5.aj;
        this.b6.aj = this.cc.aj;
        this.b7.aj = this.cd.aj;
        this.b8.aj = this.ce.aj;
        var a = this.bl();
        this.b3._fill = a;
        this.b3._opacity = 1;
        this.b6._stroke = a;
        this.b6.ac = this.e.ev + 3;
        this.b7._stroke = a;
        this.b7.ac = this.e.ev + 3;
        this.b8._stroke = a;
        this.b8.ac = this.e.ev + 3;
    };
    BollingerBandsOverlayView.prototype.a3 = function (a, b) {
        _super.prototype.a3.call(this, a, b);
        if (a.d) {
            if (b) {
                a.t(this.b3);
                a.u(this.b6);
                a.u(this.b7);
                a.u(this.b8);
            }
            else {
                a.t(this.b5);
                a.u(this.cc);
                a.u(this.cd);
                a.u(this.ce);
            }
        }
    };
    BollingerBandsOverlayView.prototype.ai = function (a) {
        _super.prototype.ai.call(this, a);
        this.e.a2.exportPolygonData(a, this.b5, "FillShape", ["Fill"]);
        this.e.a2.exportPolylineData(a, this.cc, "BottomLine", ["Lower", "Main"]);
        this.e.a2.exportPolylineData(a, this.cd, "CentralLine", ["Central"]);
        this.e.a2.exportPolylineData(a, this.ce, "TopLine", ["Upper"]);
    };
    BollingerBandsOverlayView.$t = markType(BollingerBandsOverlayView, 'BollingerBandsOverlayView', FinancialSeriesView.$);
    return BollingerBandsOverlayView;
}(FinancialSeriesView));
export { BollingerBandsOverlayView };
