/*
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 { FinancialOverlay } from "./FinancialOverlay";
import { DependencyProperty } from "igniteui-react-core/ES2015/DependencyProperty";
import { List$1 } from "igniteui-react-core/ES2015/List$1";
import { Type, Number_$type, typeCast, runOn, markType, getEnumerator, Point_$type, fromEnum } from "igniteui-react-core/ES2015/type";
import { CategoryFrame } from "./CategoryFrame";
import { SafeEnumerable } from "./SafeEnumerable";
import { TrendCalculators } from "igniteui-react-core/ES2015/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/ES2015/Polyline";
import { Polygon } from "igniteui-react-core/ES2015/Polygon";
import { PropertyMetadata } from "igniteui-react-core/ES2015/PropertyMetadata";
import { isNaN_ } from "igniteui-react-core/ES2015/number";
import { Flattener } from "igniteui-react-core/ES2015/Flattener";
export class BollingerBandsOverlay extends FinancialOverlay {
    constructor() {
        super();
        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);
    }
    io(a, b, c, d) {
        if (this.nd().contains(b)) {
            this.o6 = false;
        }
        super.io(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;
        }
    }
    o7() {
        this.pc.clear();
        this.pd.clear();
        let a = getEnumerator(TrendCalculators.e(new SafeEnumerable(this.nc), this.ph));
        let b = getEnumerator(TrendCalculators.f(new SafeEnumerable(this.nc), this.ph));
        this.pf = 1.7976931348623157E+308;
        this.pe = -1.7976931348623157E+308;
        let c = true;
        let d = true;
        let 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;
    }
    an(a) {
        if (this.pc == null || this.pd == null || a == null || this.bz == null || a != this.mg) {
            return null;
        }
        let b = new AxisRange(this.pf, this.pe);
        return b;
    }
    ol(a, b) {
        super.ol(a, b);
        let c = b.bq;
        let d = b.bp;
        let e = this.getEffectiveViewport1(b);
        let f = this.lw;
        let g = this.mg;
        let h = new ScalerParams(0, c, d, f.b8, e);
        let i = new ScalerParams(0, c, d, g.b8, e);
        a.f.clear();
        a.m.clear();
        a.p.clear();
        let j = typeCast(ISortingAxis_$type, this.lw);
        if (j != null && j.i$b.count != this.bz.count) {
            return;
        }
        let 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();
        let l = this.lw.dw(2, h) - this.lw.dw(1, h);
        for (let m = b.bs.c; m <= b.bs.d; ++m) {
            let n;
            if (j == null) {
                n = b.bs.getBucket(m);
            }
            else {
                let o = j.i$b._inner[m];
                let p = j.i$d(o);
                let q = this.pc._inner[m];
                let r = this.pd._inner[m];
                let s = q;
                let t = r;
                let u = p;
                let 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;
                let w = s - t * this.pg;
                let x = s;
                let y = s + t * this.pg;
                let z = NaN;
                if (!isNaN_(p)) {
                    z = this.lw.dt(p, h);
                }
                n = [z, w, x, y];
            }
            let 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);
            }
        }
    }
    hl(a, b) {
        super.hl(a, b);
        let c = b;
        if (c != null) {
            c.bz();
        }
    }
    eh(a, b, c) {
        if (this.pc == null) {
            return super.eh(a, b, c);
        }
        let d = this.getEffectiveViewport1(this.bi);
        let e = this.nl(this.bi.bq, this.bi.bp, d);
        let 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);
    }
    e8(a, b) {
        if (this.pc == null) {
            return super.e8(a, b);
        }
        return this.e9(a, b, this.lw, runOn(this, this.nk), this.pc);
    }
    e6(a, b) {
        if (this.pc == null) {
            return super.e6(a, b);
        }
        return this.e7(a, b, this.lw, runOn(this, this.nk), this.pc);
    }
    dt(a, b, c) {
        let d = super.dt(a, b, c);
        if (!this.m3(this.nd())) {
            d = false;
        }
        return d;
    }
    on(a, b) {
        super.on(a, b);
        let c = typeCast(BollingerBandsOverlayView.$, b);
        let d = this.getEffectiveViewport1(b);
        if (b.bv(a)) {
            c.bz();
            let e = a.f.count;
            let f = (g) => a.f._inner[g][0];
            let g = (h) => a.f._inner[e - 1 - h][0];
            let h = (i) => a.f._inner[i][1];
            let i = (j) => a.f._inner[j][2];
            let j = (k) => a.f._inner[e - 1 - k][3];
            c.b0(e, 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));
        let k = false;
        let l = this.l4.c;
        let m = a.f;
        let n = this.bz.count;
        if (l != null) {
            k = true;
        }
        if (k) {
            let o = new ScalerParams(0, b.bq, b.bp, this.lw.b8, d);
            this.ok(m, -1, n, this.lw, o, b.n);
        }
        let p = c.b7;
        let q = c.b8;
        let r = c.b9;
        let 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;
    }
    bf() {
        return new BollingerBandsOverlayView(this);
    }
    ii(a) {
        super.ii(a);
        this.pb = typeCast(BollingerBandsOverlayView.$, a);
    }
    nu() {
        return 3;
    }
    ec() {
        return this.pc == null || this.pc.count == 0 ? NaN : this.pc._inner[0];
    }
}
BollingerBandsOverlay.$t = markType(BollingerBandsOverlay, 'BollingerBandsOverlay', FinancialOverlay.$);
Type.dep(DependencyProperty, PropertyMetadata, BollingerBandsOverlay, 'raisePropertyChanged', ['Multiplier:pg:pk', [1, 2], 'Period:ph:pl', [1, 14]]);
export class BollingerBandsBucketCalculator extends FinancialBucketCalculator {
    constructor(a) {
        super(a);
        this.f = null;
        this.f = a;
    }
    getBucket(a) {
        let b = a * this.b;
        let c = Math.min(b + this.b - 1, this.a.bu.bz.count - 1);
        if (b <= c) {
            let d = this.f.by.pg;
            let e = 0;
            let f = 0;
            let g = 0;
            for (let 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.$);
export class BollingerBandsOverlayView extends FinancialSeriesView {
    constructor(a) {
        super(a);
        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;
    }
    ap() {
        super.ap();
        if (!this.n) {
            this.e.bj = 1;
        }
    }
    bt() {
        return new BollingerBandsBucketCalculator(this);
    }
    b0(a, b, c, d, e, f) {
        for (let g of fromEnum(Flattener.d(a, b, d, this.e.eo))) {
            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) });
        }
        for (let h of fromEnum(Flattener.d(a, b, e, this.e.eo))) {
            this.cb.aj.add({ $type: Point_$type, x: b(h), y: e(h) });
        }
        for (let i of fromEnum(Flattener.d(a, c, f, this.e.eo))) {
            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) });
        }
        this.an();
    }
    bz() {
        this.b3.aj.clear();
        this.ca.aj.clear();
        this.cb.aj.clear();
        this.cc.aj.clear();
        this.an();
    }
    get b2() {
        return this.b3;
    }
    get b7() {
        return this.ca;
    }
    get b8() {
        return this.cb;
    }
    get b9() {
        return this.cc;
    }
    a9() {
        super.a9();
        this.b1.aj = this.b3.aj;
        this.b4.aj = this.ca.aj;
        this.b5.aj = this.cb.aj;
        this.b6.aj = this.cc.aj;
        let 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;
    }
    a3(a, b) {
        super.a3(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);
            }
        }
    }
    ai(a) {
        super.ai(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.$);
/*<BeginStatics Name="Infragistics.Controls.Charts.BollingerBandsOverlay" />*/
/*<EndStatics Name="Infragistics.Controls.Charts.BollingerBandsOverlay" />*/
