/*
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-angular-core";
import { List$1 } from "igniteui-angular-core";
import { Number_$type, typeCast, runOn, markType, markDep, getEnumerator, Point_$type, fromEnum } from "igniteui-angular-core";
import { CategoryFrame } from "./CategoryFrame";
import { Series } from "./Series";
import { SafeEnumerable } from "./SafeEnumerable";
import { TrendCalculators } from "igniteui-angular-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-angular-core";
import { Polygon } from "igniteui-angular-core";
import { PropertyMetadata } from "igniteui-angular-core";
import { isNaN_ } from "igniteui-angular-core";
import { Flattener } from "igniteui-angular-core";
/**
 * @hidden
 */
var BollingerBandsOverlay = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(BollingerBandsOverlay, _super);
    function BollingerBandsOverlay() {
        var _this = _super.call(this) || this;
        _this.t2 = 0;
        _this.t3 = 0;
        _this.t0 = new List$1(Number_$type, 0);
        _this.t1 = new List$1(Number_$type, 0);
        _this.ab = BollingerBandsOverlay.$;
        _this.qo = new CategoryFrame(4);
        _this.qq = new CategoryFrame(4);
        _this.qm = new CategoryFrame(4);
        return _this;
    }
    BollingerBandsOverlay.prototype.l9 = function (a, b, c, d) {
        if (this.r3().contains(b)) {
            this.tu = false;
        }
        _super.prototype.l9.call(this, a, b, c, d);
        switch (b) {
            case BollingerBandsOverlay.$$p[1]:
                this.tu = false;
                this.mn(false);
                break;
            case BollingerBandsOverlay.$$p[0]:
                this.mn(false);
                break;
        }
    };
    BollingerBandsOverlay.prototype.tv = function () {
        this.t0.clear();
        this.t1.clear();
        var a = getEnumerator(TrendCalculators.e(new SafeEnumerable(this.r2), this.t5));
        var b = getEnumerator(TrendCalculators.f(new SafeEnumerable(this.r2), this.t5));
        this.t3 = 1.7976931348623157E+308;
        this.t2 = -1.7976931348623157E+308;
        var c = true;
        var d = true;
        var e = this.t4;
        while (c || d) {
            if (a.moveNext()) {
                this.t0.add(a.current);
            }
            else {
                c = false;
            }
            if (b.moveNext()) {
                this.t1.add(b.current);
            }
            else {
                d = false;
            }
            if (c && d) {
                this.t3 = Math.min(this.t3, a.current - b.current * e);
                this.t2 = Math.max(this.t2, a.current + b.current * e);
            }
        }
        return true;
    };
    BollingerBandsOverlay.prototype.aq = function (a) {
        if (this.t0 == null || this.t1 == null || a == null || this.cl == null || a != this.q6) {
            return null;
        }
        var b = new AxisRange(this.t3, this.t2);
        return b;
    };
    BollingerBandsOverlay.prototype.tb = function (a, b) {
        _super.prototype.tb.call(this, a, b);
        var c = b.bv;
        var d = b.bu;
        var e = this.getEffectiveViewport1(b);
        var f = this.qk;
        var g = this.q6;
        var h = new ScalerParams(0, c, d, f.cg, e);
        var i = new ScalerParams(0, c, d, g.cg, e);
        a.f.clear();
        a.m.clear();
        a.p.clear();
        var j = typeCast(ISortingAxis_$type, this.qk);
        if (j != null && j.i$b.count != this.cl.count) {
            return;
        }
        var k = 0;
        k = this.sb(c, d, e);
        if (!this.tu) {
            this.tu = this.tv();
            if (this.q6 != null) {
                this.q6.c1(true);
            }
        }
        i.b = this.gg();
        var l = this.qk.el(2, h) - this.qk.el(1, h);
        for (var m = b.bx.c; m <= b.bx.d; ++m) {
            var n = void 0;
            if (j == null) {
                n = b.bx.getBucket(m);
            }
            else {
                var o = j.i$b._inner[m];
                var p = j.i$d(o);
                var q = this.t0._inner[m];
                var r = this.t1._inner[m];
                var s = q;
                var t = r;
                var u = p;
                var v = 1;
                while (m < b.bx.d) {
                    o = j.i$b._inner[m + 1];
                    u = j.i$d(o);
                    if (u - p > l) {
                        break;
                    }
                    m++;
                    s += this.t0._inner[m];
                    t += this.t1._inner[m];
                    v++;
                }
                s /= v;
                t /= v;
                var w = s - t * this.t4;
                var x = s;
                var y = s + t * this.t4;
                var z = NaN;
                if (!isNaN_(p)) {
                    z = this.qk.ei(p, h);
                }
                n = [z, w, x, y];
            }
            var aa = Math.max(1, l);
            if (!isNaN_(n[0]) && m * aa >= this.tw) {
                if (this.qk != null && typeCast(ISortingAxis_$type, this.qk) !== null) {
                    n[0] = n[0] + k;
                }
                else {
                    n[0] = f.ei(n[0], h) + k;
                }
                n[1] = g.ei(n[1], i);
                n[2] = g.ei(n[2], i);
                n[3] = g.ei(n[3], i);
                a.f.add(n);
            }
        }
    };
    BollingerBandsOverlay.prototype.k2 = function (a, b) {
        _super.prototype.k2.call(this, a, b);
        var c = b;
        if (c != null) {
            c.b4();
        }
    };
    BollingerBandsOverlay.prototype.gn = function (a, b, c) {
        if (this.t0 == null) {
            return _super.prototype.gn.call(this, a, b, c);
        }
        var d = this.getEffectiveViewport1(this.b1);
        var e = this.sb(this.b1.bv, this.b1.bu, d);
        var f = new ScalerParams(0, this.b1.bv, this.b1.bu, this.qk.cg, d);
        f.c = this.ce.tp;
        return this.gp(this.t0, a, this.qk, f, e, runOn(this, this.sa), b, c);
    };
    BollingerBandsOverlay.prototype.ho = function (a, b) {
        if (this.t0 == null) {
            return _super.prototype.ho.call(this, a, b);
        }
        return this.hp(a, b, this.qk, runOn(this, this.sa), this.t0);
    };
    BollingerBandsOverlay.prototype.hm = function (a, b) {
        if (this.t0 == null) {
            return _super.prototype.hm.call(this, a, b);
        }
        return this.hn(a, b, this.qk, runOn(this, this.sa), this.t0);
    };
    BollingerBandsOverlay.prototype.fd = function (a, b, c) {
        var d = _super.prototype.fd.call(this, a, b, c);
        if (!this.rt(this.r3())) {
            d = false;
        }
        return d;
    };
    BollingerBandsOverlay.prototype.td = function (a, b) {
        _super.prototype.td.call(this, a, b);
        var c = typeCast(BollingerBandsOverlayView.$, b);
        var d = this.getEffectiveViewport1(b);
        if (b.b0(a)) {
            c.b4();
            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.b5(e_1, f, g, h, i, j);
            b.b2(a);
        }
        this.qt.a2(this, this.rs(), runOn(this, this.qc), this.sh(b), this.sj(b));
        var k = false;
        var l = this.qt.b;
        var m = a.f;
        var n = this.cl.count;
        if (l != null) {
            k = true;
        }
        if (k) {
            var o = new ScalerParams(0, b.bv, b.bu, this.qk.cg, d);
            this.ta(m, -1, n, this.qk, o, b.q);
        }
        var p = c.cc;
        var q = c.cd;
        var r = c.ce;
        var s = c.b7;
        this.qt.ad(q, true, false, true, true);
        this.qt.an = null;
        this.qt.am = null;
        this.qt.ad(p, true, false, false, true);
        this.qt.ad(r, true, false, false, true);
        this.qt.ad(s, false, true, false, false);
        s._opacity = this.qt.j * this.fu;
    };
    BollingerBandsOverlay.prototype.by = function () {
        return new BollingerBandsOverlayView(this);
    };
    BollingerBandsOverlay.prototype.l4 = function (a) {
        _super.prototype.l4.call(this, a);
        this.tz = typeCast(BollingerBandsOverlayView.$, a);
    };
    BollingerBandsOverlay.prototype.sk = function () {
        return 3;
    };
    BollingerBandsOverlay.prototype.gg = function () {
        return Series.gi(this.t0, null);
    };
    BollingerBandsOverlay.$t = markType(BollingerBandsOverlay, 'BollingerBandsOverlay', FinancialOverlay.$);
    BollingerBandsOverlay.$$p = markDep(DependencyProperty, PropertyMetadata, BollingerBandsOverlay, 'raisePropertyChanged', ['Multiplier:t4:t8', [1, 2], 'Period:t5:t9', [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.bz.cl.count - 1);
        if (b <= c) {
            var d = this.f.b3.t4;
            var e = 0;
            var f = 0;
            var g = 0;
            for (var h = b; h <= c; ++h) {
                if (!isNaN_(this.f.b3.t0._inner[h]) && !isNaN_(this.f.b3.t1._inner[h])) {
                    e += this.f.b3.t0._inner[h];
                    f += this.f.b3.t1._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.b3 = null;
        _this.b8 = new Polygon();
        _this.cf = new Polyline();
        _this.cg = new Polyline();
        _this.ch = new Polyline();
        _this.b6 = new Polygon();
        _this.b9 = new Polyline();
        _this.ca = new Polyline();
        _this.cb = new Polyline();
        _this.b3 = a;
        return _this;
    }
    BollingerBandsOverlayView.prototype.as = function () {
        _super.prototype.as.call(this);
        if (!this.q) {
        }
    };
    BollingerBandsOverlayView.prototype.by = function () {
        return new BollingerBandsBucketCalculator(this);
    };
    BollingerBandsOverlayView.prototype.b5 = 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.fx))), _e = _d.next(); !_e.done; _e = _d.next()) {
                var g = _e.value;
                this.b8.an.add({ $type: Point_$type, x: b(g), y: d(g) });
                this.cf.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.fx))), _g = _f.next(); !_g.done; _g = _f.next()) {
                var h = _g.value;
                this.cg.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.fx))), _j = _h.next(); !_j.done; _j = _h.next()) {
                var i = _j.value;
                this.b8.an.add({ $type: Point_$type, x: c(i), y: f(i) });
                this.ch.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.aq();
    };
    BollingerBandsOverlayView.prototype.b4 = function () {
        this.b8.an.clear();
        this.cf.an.clear();
        this.cg.an.clear();
        this.ch.an.clear();
        this.aq();
    };
    Object.defineProperty(BollingerBandsOverlayView.prototype, "b7", {
        get: function () {
            return this.b8;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(BollingerBandsOverlayView.prototype, "cc", {
        get: function () {
            return this.cf;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(BollingerBandsOverlayView.prototype, "cd", {
        get: function () {
            return this.cg;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(BollingerBandsOverlayView.prototype, "ce", {
        get: function () {
            return this.ch;
        },
        enumerable: false,
        configurable: true
    });
    BollingerBandsOverlayView.prototype.bc = function () {
        _super.prototype.bc.call(this);
        this.b6.an = this.b8.an;
        this.b9.an = this.cf.an;
        this.ca.an = this.cg.an;
        this.cb.an = this.ch.an;
        var a = this.bo();
        this.b6._fill = a;
        this.b6._opacity = 1;
        this.b9._stroke = a;
        this.b9.ad = this.e.fy + 3;
        this.ca._stroke = a;
        this.ca.ad = this.e.fy + 3;
        this.cb._stroke = a;
        this.cb.ad = this.e.fy + 3;
    };
    BollingerBandsOverlayView.prototype.a6 = function (a, b) {
        _super.prototype.a6.call(this, a, b);
        if (a.d) {
            if (b) {
                a.u(this.b6);
                a.v(this.b9);
                a.v(this.ca);
                a.v(this.cb);
            }
            else {
                a.u(this.b8);
                a.v(this.cf);
                a.v(this.cg);
                a.v(this.ch);
            }
        }
    };
    BollingerBandsOverlayView.prototype.al = function (a) {
        _super.prototype.al.call(this, a);
        this.e.a9.exportPolygonData(a, this.b8, "FillShape", ["Fill"]);
        this.e.a9.exportPolylineData(a, this.cf, "BottomLine", ["Lower", "Main"]);
        this.e.a9.exportPolylineData(a, this.cg, "CentralLine", ["Central"]);
        this.e.a9.exportPolylineData(a, this.ch, "TopLine", ["Upper"]);
    };
    BollingerBandsOverlayView.$t = markType(BollingerBandsOverlayView, 'BollingerBandsOverlayView', FinancialSeriesView.$);
    return BollingerBandsOverlayView;
}(FinancialSeriesView));
export { BollingerBandsOverlayView };
