/*
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 { FinancialOverlay } from "./FinancialOverlay";
import { DependencyProperty } from "igniteui-react-core";
import { Series } from "./Series";
import { List$1 } from "igniteui-react-core";
import { Number_$type, typeCast, runOn, markType, markDep, getEnumerator, Point_$type, fromEnum } from "igniteui-react-core";
import { CategoryFrame } from "./CategoryFrame";
import { SafeEnumerable } from "./SafeEnumerable";
import { TrendCalculators } from "igniteui-react-core";
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";
import { Polygon } from "igniteui-react-core";
import { PropertyMetadata } from "igniteui-react-core";
import { isNaN_ } from "igniteui-react-core";
import { Flattener } from "igniteui-react-core";
/**
 * @hidden
 */
var BollingerBandsOverlay = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(BollingerBandsOverlay, _super);
    function BollingerBandsOverlay() {
        var _this = _super.call(this) || this;
        _this.aa6 = 0;
        _this.aa7 = 0;
        _this.aa4 = new List$1(Number_$type, 0);
        _this.aa5 = new List$1(Number_$type, 0);
        _this.ab = BollingerBandsOverlay.$;
        _this.w8 = new CategoryFrame(4);
        _this.xa = new CategoryFrame(4);
        _this.w6 = new CategoryFrame(4);
        return _this;
    }
    BollingerBandsOverlay.prototype.bj = function () {
        return new BollingerBandsOverlay();
    };
    BollingerBandsOverlay.prototype.q7 = function (a, b, c, d) {
        if (this.yn().contains(b)) {
            this.aay = false;
        }
        _super.prototype.q7.call(this, a, b, c, d);
        switch (b) {
            case BollingerBandsOverlay.$$p[1]:
                this.aay = false;
                this.rp(false);
                break;
            case BollingerBandsOverlay.$$p[0]:
                this.rp(false);
                break;
        }
    };
    BollingerBandsOverlay.prototype.aaz = function () {
        this.aa4.clear();
        this.aa5.clear();
        var a = getEnumerator(TrendCalculators.e(new SafeEnumerable(this.ym), this.aa9));
        var b = getEnumerator(TrendCalculators.f(new SafeEnumerable(this.ym), this.aa9));
        this.aa7 = 1.7976931348623157E+308;
        this.aa6 = -1.7976931348623157E+308;
        var c = true;
        var d = true;
        var e = this.aa8;
        while (c || d) {
            if (a.moveNext()) {
                this.aa4.add(a.current);
            }
            else {
                c = false;
            }
            if (b.moveNext()) {
                this.aa5.add(b.current);
            }
            else {
                d = false;
            }
            if (c && d) {
                this.aa7 = Math.min(this.aa7, a.current - b.current * e);
                this.aa6 = Math.max(this.aa6, a.current + b.current * e);
            }
        }
        return true;
    };
    BollingerBandsOverlay.prototype.ay = function (a) {
        if (this.aa4 == null || this.aa5 == null || a == null || this.dn == null || a != this.xn) {
            return null;
        }
        var b = new AxisRange(this.aa7, this.aa6);
        return b;
    };
    BollingerBandsOverlay.prototype.aaa = function (a, b) {
        _super.prototype.aaa.call(this, a, b);
        var c = b.by;
        var d = b.bx;
        var e = this.getEffectiveViewport1(b);
        var f = this.w4;
        var g = this.xn;
        var h = new ScalerParams(0, c, d, f.ch, e);
        var i = new ScalerParams(0, c, d, g.ch, e);
        a.f.clear();
        a.n.clear();
        a.q.clear();
        var j = typeCast(ISortingAxis_$type, this.w4);
        if (j != null && j.i$b.count != this.dn.count) {
            return;
        }
        var k = 0;
        k = this.yv(c, d, e);
        if (!this.aay) {
            this.aay = this.aaz();
            if (this.xn != null) {
                this.xn.c2(true);
            }
        }
        i.b = this.iy();
        var l = this.w4.eo(2, h) - this.w4.eo(1, h);
        for (var m = b.b0.c; m <= b.b0.d; ++m) {
            var n = void 0;
            if (j == null) {
                n = b.b0.getBucket(m);
            }
            else {
                var o = j.i$b._inner[m];
                var p = j.i$d(o);
                var q = this.aa4._inner[m];
                var r = this.aa5._inner[m];
                var s = q;
                var t = r;
                var u = p;
                var v = 1;
                while (m < b.b0.d) {
                    o = j.i$b._inner[m + 1];
                    u = j.i$d(o);
                    if (u - p > l) {
                        break;
                    }
                    m++;
                    s += this.aa4._inner[m];
                    t += this.aa5._inner[m];
                    v++;
                }
                s /= v;
                t /= v;
                var w = s - t * this.aa8;
                var x = s;
                var y = s + t * this.aa8;
                var z = NaN;
                if (!isNaN_(p)) {
                    z = this.w4.el(p, h);
                }
                n = [z, w, x, y];
            }
            var aa = Math.max(1, l);
            if (!isNaN_(n[0]) && m * aa >= this.aa0) {
                if (this.w4 != null && typeCast(ISortingAxis_$type, this.w4) !== null) {
                    n[0] = n[0] + k;
                }
                else {
                    n[0] = f.el(n[0], h) + k;
                }
                n[1] = g.el(n[1], i);
                n[2] = g.el(n[2], i);
                n[3] = g.el(n[3], i);
                a.f.add(n);
            }
        }
    };
    BollingerBandsOverlay.prototype.o5 = function (a, b) {
        _super.prototype.o5.call(this, a, b);
        var c = b;
        if (c != null) {
            c.b7();
        }
    };
    BollingerBandsOverlay.prototype.i5 = function (a, b, c) {
        if (this.aa4 == null) {
            return _super.prototype.i5.call(this, a, b, c);
        }
        var d = this.getEffectiveViewport1(this.cv);
        var e = this.yv(this.cv.by, this.cv.bx, d);
        var f = new ScalerParams(0, this.cv.by, this.cv.bx, this.w4.ch, d);
        f.c = this.db.wj;
        return this.i7(this.aa4, a, this.w4, f, e, runOn(this, this.yu), b, c);
    };
    BollingerBandsOverlay.prototype.ka = function (a, b) {
        if (this.aa4 == null) {
            return _super.prototype.ka.call(this, a, b);
        }
        return this.kb(a, b, this.w4, runOn(this, this.yu), this.aa4);
    };
    BollingerBandsOverlay.prototype.j8 = function (a, b) {
        if (this.aa4 == null) {
            return _super.prototype.j8.call(this, a, b);
        }
        return this.j9(a, b, this.w4, runOn(this, this.yu), this.aa4);
    };
    BollingerBandsOverlay.prototype.he = function (a, b, c) {
        var d = _super.prototype.he.call(this, a, b, c);
        if (!this.yd(this.yn())) {
            d = false;
        }
        return d;
    };
    BollingerBandsOverlay.prototype.aac = function (a, b) {
        _super.prototype.aac.call(this, a, b);
        var c = typeCast(BollingerBandsOverlayView.$, b);
        var d = this.getEffectiveViewport1(b);
        if (b.b3(a)) {
            c.b7();
            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.b8(e_1, f, g, h, i, j);
            b.b5(a);
        }
        this.xd.a2(this, this.yc(), runOn(this, this.ww), this.y1(b), this.y3(b));
        var k = false;
        var l = this.xd.b;
        var m = a.f;
        var n = this.dn.count;
        if (l != null) {
            k = true;
        }
        if (k) {
            var o = new ScalerParams(0, b.by, b.bx, this.w4.ch, d);
            this.z9(m, -1, n, this.w4, o, b.q, -1);
        }
        var p = c.cf;
        var q = c.cg;
        var r = c.ch;
        var s = c.ca;
        this.xd.ad(q, true, false, true, true);
        this.xd.an = null;
        this.xd.am = null;
        this.xd.ad(p, true, false, false, true);
        this.xd.ad(r, true, false, false, true);
        this.xd.ad(s, false, true, false, false);
        s._opacity = this.xd.j * this.h9;
    };
    BollingerBandsOverlay.prototype.cs = function () {
        return new BollingerBandsOverlayView(this);
    };
    BollingerBandsOverlay.prototype.qz = function (a) {
        _super.prototype.qz.call(this, a);
        this.aa3 = typeCast(BollingerBandsOverlayView.$, a);
    };
    BollingerBandsOverlay.prototype.y4 = function () {
        return 3;
    };
    BollingerBandsOverlay.prototype.iy = function () {
        return Series.i0(this.aa4, null);
    };
    BollingerBandsOverlay.$t = markType(BollingerBandsOverlay, 'BollingerBandsOverlay', FinancialOverlay.$);
    BollingerBandsOverlay.$$p = markDep(DependencyProperty, PropertyMetadata, BollingerBandsOverlay, 'raisePropertyChanged', ['Multiplier:aa8:abc', [1, 2], 'Period:aa9:abd', [1, 14]]);
    return BollingerBandsOverlay;
}(FinancialOverlay));
export { BollingerBandsOverlay };
/**
 * @hidden
 */
var BollingerBandsBucketCalculator = /** @class */ /*@__PURE__*/ (function (_super) {
    __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.b2.dn.count - 1);
        if (b <= c) {
            var d = this.f.b6.aa8;
            var e = 0;
            var f = 0;
            var g = 0;
            for (var h = b; h <= c; ++h) {
                if (!isNaN_(this.f.b6.aa4._inner[h]) && !isNaN_(this.f.b6.aa5._inner[h])) {
                    e += this.f.b6.aa4._inner[h];
                    f += this.f.b6.aa5._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 */ /*@__PURE__*/ (function (_super) {
    __extends(BollingerBandsOverlayView, _super);
    function BollingerBandsOverlayView(a) {
        var _this = _super.call(this, a) || this;
        _this.b6 = null;
        _this.cb = new Polygon();
        _this.ci = new Polyline();
        _this.cj = new Polyline();
        _this.ck = new Polyline();
        _this.b9 = new Polygon();
        _this.cc = new Polyline();
        _this.cd = new Polyline();
        _this.ce = new Polyline();
        _this.b6 = a;
        return _this;
    }
    BollingerBandsOverlayView.prototype.au = function () {
        _super.prototype.au.call(this);
        if (!this.q) {
        }
    };
    BollingerBandsOverlayView.prototype.b1 = function () {
        return new BollingerBandsBucketCalculator(this);
    };
    BollingerBandsOverlayView.prototype.b8 = function (a, b, c, d, e, f) {
        var e_2, _a, e_3, _b, e_4, _c;
        try {
            for (var _d = __values(fromEnum(Flattener.d(a, b, d, this.e.id))), _e = _d.next(); !_e.done; _e = _d.next()) {
                var g = _e.value;
                this.cb.an.add({ $type: Point_$type, x: b(g), y: d(g) });
                this.ci.an.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 = __values(fromEnum(Flattener.d(a, b, e, this.e.id))), _g = _f.next(); !_g.done; _g = _f.next()) {
                var h = _g.value;
                this.cj.an.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 = __values(fromEnum(Flattener.d(a, c, f, this.e.id))), _j = _h.next(); !_j.done; _j = _h.next()) {
                var i = _j.value;
                this.cb.an.add({ $type: Point_$type, x: c(i), y: f(i) });
                this.ck.an.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.as();
    };
    BollingerBandsOverlayView.prototype.b7 = function () {
        this.cb.an.clear();
        this.ci.an.clear();
        this.cj.an.clear();
        this.ck.an.clear();
        this.as();
    };
    Object.defineProperty(BollingerBandsOverlayView.prototype, "ca", {
        get: function () {
            return this.cb;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(BollingerBandsOverlayView.prototype, "cf", {
        get: function () {
            return this.ci;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(BollingerBandsOverlayView.prototype, "cg", {
        get: function () {
            return this.cj;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(BollingerBandsOverlayView.prototype, "ch", {
        get: function () {
            return this.ck;
        },
        enumerable: false,
        configurable: true
    });
    BollingerBandsOverlayView.prototype.be = function () {
        _super.prototype.be.call(this);
        this.b9.an = this.cb.an;
        this.cc.an = this.ci.an;
        this.cd.an = this.cj.an;
        this.ce.an = this.ck.an;
        var a = this.br();
        this.b9._fill = a;
        this.b9._opacity = 1;
        this.cc._stroke = a;
        this.cc.ad = this.e.ie + 3;
        this.cd._stroke = a;
        this.cd.ad = this.e.ie + 3;
        this.ce._stroke = a;
        this.ce.ad = this.e.ie + 3;
    };
    BollingerBandsOverlayView.prototype.a8 = function (a, b) {
        _super.prototype.a8.call(this, a, b);
        if (a.d) {
            if (b) {
                a.u(this.b9);
                a.v(this.cc);
                a.v(this.cd);
                a.v(this.ce);
            }
            else {
                a.u(this.cb);
                a.v(this.ci);
                a.v(this.cj);
                a.v(this.ck);
            }
        }
    };
    BollingerBandsOverlayView.prototype.an = function (a) {
        _super.prototype.an.call(this, a);
        this.e.bc.exportPolygonData(a, this.cb, "FillShape", ["Fill"]);
        this.e.bc.exportPolylineData(a, this.ci, "BottomLine", ["Lower", "Main"]);
        this.e.bc.exportPolylineData(a, this.cj, "CentralLine", ["Central"]);
        this.e.bc.exportPolylineData(a, this.ck, "TopLine", ["Upper"]);
    };
    BollingerBandsOverlayView.$t = markType(BollingerBandsOverlayView, 'BollingerBandsOverlayView', FinancialSeriesView.$);
    return BollingerBandsOverlayView;
}(FinancialSeriesView));
export { BollingerBandsOverlayView };
