/*
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 { Series } from "./Series";
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 { 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.abi = 0;
        _this.abj = 0;
        _this.abg = new List$1(Number_$type, 0);
        _this.abh = new List$1(Number_$type, 0);
        _this.ab = BollingerBandsOverlay.$;
        _this.xk = new CategoryFrame(4);
        _this.xm = new CategoryFrame(4);
        _this.xi = new CategoryFrame(4);
        return _this;
    }
    BollingerBandsOverlay.prototype.bk = function () {
        return new BollingerBandsOverlay();
    };
    BollingerBandsOverlay.prototype.rg = function (a, b, c, d) {
        if (this.yz().contains(b)) {
            this.aba = false;
        }
        _super.prototype.rg.call(this, a, b, c, d);
        switch (b) {
            case BollingerBandsOverlay.$$p[1]:
                this.aba = false;
                this.ry(false);
                break;
            case BollingerBandsOverlay.$$p[0]:
                this.ry(false);
                break;
        }
    };
    BollingerBandsOverlay.prototype.abb = function () {
        this.abg.clear();
        this.abh.clear();
        var a = getEnumerator(TrendCalculators.e(new SafeEnumerable(this.yy), this.abl));
        var b = getEnumerator(TrendCalculators.f(new SafeEnumerable(this.yy), this.abl));
        this.abj = 1.7976931348623157E+308;
        this.abi = -1.7976931348623157E+308;
        var c = true;
        var d = true;
        var e = this.abk;
        while (c || d) {
            if (a.moveNext()) {
                this.abg.add(a.current);
            }
            else {
                c = false;
            }
            if (b.moveNext()) {
                this.abh.add(b.current);
            }
            else {
                d = false;
            }
            if (c && d) {
                this.abj = Math.min(this.abj, a.current - b.current * e);
                this.abi = Math.max(this.abi, a.current + b.current * e);
            }
        }
        return true;
    };
    BollingerBandsOverlay.prototype.ay = function (a) {
        if (this.abg == null || this.abh == null || a == null || this.dp == null || a != this.xz) {
            return null;
        }
        var b = new AxisRange(this.abj, this.abi);
        return b;
    };
    BollingerBandsOverlay.prototype.aam = function (a, b) {
        _super.prototype.aam.call(this, a, b);
        var c = b.ca;
        var d = b.b9;
        var e = this.getEffectiveViewport1(b);
        var f = this.xg;
        var g = this.xz;
        var h = new ScalerParams(0, c, d, f.df, e);
        var i = new ScalerParams(0, c, d, g.df, e);
        a.h.clear();
        a.p.clear();
        a.s.clear();
        var j = typeCast(ISortingAxis_$type, this.xg);
        if (j != null && j.i$b.count != this.dp.count) {
            return;
        }
        var k = 0;
        k = this.y7(c, d, e);
        if (!this.aba) {
            this.aba = this.abb();
            if (this.xz != null) {
                this.xz.d3(true);
            }
        }
        i.b = this.i0();
        var l = this.xg.fw(2, h) - this.xg.fw(1, h);
        for (var m = b.cd.c; m <= b.cd.d; ++m) {
            var n = void 0;
            if (j == null) {
                n = b.cd.getBucket(m);
            }
            else {
                var o = j.i$b._inner[m];
                var p = j.i$d(o);
                var q = this.abg._inner[m];
                var r = this.abh._inner[m];
                var s = q;
                var t = r;
                var u = p;
                var v = 1;
                while (m < b.cd.d) {
                    o = j.i$b._inner[m + 1];
                    u = j.i$d(o);
                    if (u - p > l) {
                        break;
                    }
                    m++;
                    s += this.abg._inner[m];
                    t += this.abh._inner[m];
                    v++;
                }
                s /= v;
                t /= v;
                var w = s - t * this.abk;
                var x = s;
                var y = s + t * this.abk;
                var z = NaN;
                if (!isNaN_(p)) {
                    z = this.xg.ft(p, h);
                }
                n = [z, w, x, y];
            }
            var aa = Math.max(1, l);
            if (!isNaN_(n[0]) && m * aa >= this.abc) {
                if (this.xg != null && typeCast(ISortingAxis_$type, this.xg) !== null) {
                    n[0] = n[0] + k;
                }
                else {
                    n[0] = f.ft(n[0], h) + k;
                }
                n[1] = g.ft(n[1], i);
                n[2] = g.ft(n[2], i);
                n[3] = g.ft(n[3], i);
                a.h.add(n);
            }
        }
    };
    BollingerBandsOverlay.prototype.pb = function (a, b) {
        _super.prototype.pb.call(this, a, b);
        var c = b;
        if (c != null) {
            c.ck();
        }
    };
    BollingerBandsOverlay.prototype.i8 = function (a, b, c) {
        if (this.abg == null) {
            return _super.prototype.i8.call(this, a, b, c);
        }
        var d = this.getEffectiveViewport1(this.cw);
        var e = this.y7(this.cw.ca, this.cw.b9, d);
        var f = new ScalerParams(0, this.cw.ca, this.cw.b9, this.xg.df, d);
        f.c = this.dc.x4;
        return this.ja(this.abg, a, this.xg, f, e, runOn(this, this.y6), b, c);
    };
    BollingerBandsOverlay.prototype.ke = function (a, b) {
        if (this.abg == null) {
            return _super.prototype.ke.call(this, a, b);
        }
        return this.kf(a, b, this.xg, runOn(this, this.y6), this.abg);
    };
    BollingerBandsOverlay.prototype.kc = function (a, b) {
        if (this.abg == null) {
            return _super.prototype.kc.call(this, a, b);
        }
        return this.kd(a, b, this.xg, runOn(this, this.y6), this.abg);
    };
    BollingerBandsOverlay.prototype.hf = function (a, b, c) {
        var d = _super.prototype.hf.call(this, a, b, c);
        if (!this.yp(this.yz())) {
            d = false;
        }
        return d;
    };
    BollingerBandsOverlay.prototype.aao = function (a, b) {
        _super.prototype.aao.call(this, a, b);
        var c = typeCast(BollingerBandsOverlayView.$, b);
        var d = this.getEffectiveViewport1(b);
        if (b.cg(a)) {
            c.ck();
            var e_1 = a.h.count;
            var f = function (g) { return a.h._inner[g][0]; };
            var g = function (h) { return a.h._inner[e_1 - 1 - h][0]; };
            var h = function (i) { return a.h._inner[i][1]; };
            var i = function (j) { return a.h._inner[j][2]; };
            var j = function (k) { return a.h._inner[e_1 - 1 - k][3]; };
            c.cl(e_1, f, g, h, i, j);
            b.ci(a);
        }
        this.xp.a2(this, this.yo(), runOn(this, this.w8), this.zd(b), this.zf(b));
        var k = false;
        var l = this.xp.b;
        var m = a.h;
        var n = this.dp.count;
        if (l != null) {
            k = true;
        }
        if (k) {
            var o = new ScalerParams(0, b.ca, b.b9, this.xg.df, d);
            this.aal(m, -1, n, this.xg, o, b.t, -1);
        }
        var p = c.cs;
        var q = c.ct;
        var r = c.cu;
        var s = c.cn;
        this.xp.ad(q, true, false, true, true);
        this.xp.an = null;
        this.xp.am = null;
        this.xp.ad(p, true, false, false, true);
        this.xp.ad(r, true, false, false, true);
        this.xp.ad(s, false, true, false, false);
        s._opacity = this.xp.j * this.ia;
    };
    BollingerBandsOverlay.prototype.ct = function () {
        return new BollingerBandsOverlayView(this);
    };
    BollingerBandsOverlay.prototype.q7 = function (a) {
        _super.prototype.q7.call(this, a);
        this.abf = typeCast(BollingerBandsOverlayView.$, a);
    };
    BollingerBandsOverlay.prototype.zg = function () {
        return 3;
    };
    BollingerBandsOverlay.prototype.i0 = function () {
        return Series.i2(this.abg, null);
    };
    BollingerBandsOverlay.$t = markType(BollingerBandsOverlay, 'BollingerBandsOverlay', FinancialOverlay.$);
    BollingerBandsOverlay.$$p = markDep(DependencyProperty, PropertyMetadata, BollingerBandsOverlay, 'raisePropertyChanged', ['Multiplier:abk:abo', [1, 2], 'Period:abl:abp', [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.cf.dp.count - 1);
        if (b <= c) {
            var d = this.f.cj.abk;
            var e = 0;
            var f = 0;
            var g = 0;
            for (var h = b; h <= c; ++h) {
                if (!isNaN_(this.f.cj.abg._inner[h]) && !isNaN_(this.f.cj.abh._inner[h])) {
                    e += this.f.cj.abg._inner[h];
                    f += this.f.cj.abh._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.cj = null;
        _this.co = new Polygon();
        _this.cv = new Polyline();
        _this.cw = new Polyline();
        _this.cx = new Polyline();
        _this.cm = new Polygon();
        _this.cp = new Polyline();
        _this.cq = new Polyline();
        _this.cr = new Polyline();
        _this.cj = a;
        return _this;
    }
    BollingerBandsOverlayView.prototype.a4 = function () {
        _super.prototype.a4.call(this);
        if (!this.t) {
        }
    };
    BollingerBandsOverlayView.prototype.ce = function () {
        return new BollingerBandsBucketCalculator(this);
    };
    BollingerBandsOverlayView.prototype.cl = 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.f.ie))), _e = _d.next(); !_e.done; _e = _d.next()) {
                var g = _e.value;
                this.co.an.add({ $type: Point_$type, x: b(g), y: d(g) });
                this.cv.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.f.ie))), _g = _f.next(); !_g.done; _g = _f.next()) {
                var h = _g.value;
                this.cw.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.f.ie))), _j = _h.next(); !_j.done; _j = _h.next()) {
                var i = _j.value;
                this.co.an.add({ $type: Point_$type, x: c(i), y: f(i) });
                this.cx.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.a2();
    };
    BollingerBandsOverlayView.prototype.ck = function () {
        this.co.an.clear();
        this.cv.an.clear();
        this.cw.an.clear();
        this.cx.an.clear();
        this.a2();
    };
    Object.defineProperty(BollingerBandsOverlayView.prototype, "cn", {
        get: function () {
            return this.co;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(BollingerBandsOverlayView.prototype, "cs", {
        get: function () {
            return this.cv;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(BollingerBandsOverlayView.prototype, "ct", {
        get: function () {
            return this.cw;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(BollingerBandsOverlayView.prototype, "cu", {
        get: function () {
            return this.cx;
        },
        enumerable: false,
        configurable: true
    });
    BollingerBandsOverlayView.prototype.bq = function () {
        _super.prototype.bq.call(this);
        this.cm.an = this.co.an;
        this.cp.an = this.cv.an;
        this.cq.an = this.cw.an;
        this.cr.an = this.cx.an;
        var a = this.b3();
        this.cm._fill = a;
        this.cm._opacity = 1;
        this.cp._stroke = a;
        this.cp.ad = this.f.ig + 3;
        this.cq._stroke = a;
        this.cq.ad = this.f.ig + 3;
        this.cr._stroke = a;
        this.cr.ad = this.f.ig + 3;
    };
    BollingerBandsOverlayView.prototype.bj = function (a, b) {
        _super.prototype.bj.call(this, a, b);
        if (a.d) {
            if (b) {
                a.u(this.cm);
                a.v(this.cp);
                a.v(this.cq);
                a.v(this.cr);
            }
            else {
                a.u(this.co);
                a.v(this.cv);
                a.v(this.cw);
                a.v(this.cx);
            }
        }
    };
    BollingerBandsOverlayView.prototype.ax = function (a) {
        _super.prototype.ax.call(this, a);
        this.f.bc.exportPolygonData(a, this.co, "FillShape", ["Fill"]);
        this.f.bc.exportPolylineData(a, this.cv, "BottomLine", ["Lower", "Main"]);
        this.f.bc.exportPolylineData(a, this.cw, "CentralLine", ["Central"]);
        this.f.bc.exportPolylineData(a, this.cx, "TopLine", ["Upper"]);
    };
    BollingerBandsOverlayView.$t = markType(BollingerBandsOverlayView, 'BollingerBandsOverlayView', FinancialSeriesView.$);
    return BollingerBandsOverlayView;
}(FinancialSeriesView));
export { BollingerBandsOverlayView };
