/*
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-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
 */
export let BollingerBandsOverlay = /*@__PURE__*/ (() => {
    class BollingerBandsOverlay extends FinancialOverlay {
        constructor() {
            super();
            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);
        }
        l9(a, b, c, d) {
            if (this.r3().contains(b)) {
                this.tu = false;
            }
            super.l9(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;
            }
        }
        tv() {
            this.t0.clear();
            this.t1.clear();
            let a = getEnumerator(TrendCalculators.e(new SafeEnumerable(this.r2), this.t5));
            let b = getEnumerator(TrendCalculators.f(new SafeEnumerable(this.r2), this.t5));
            this.t3 = 1.7976931348623157E+308;
            this.t2 = -1.7976931348623157E+308;
            let c = true;
            let d = true;
            let 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;
        }
        aq(a) {
            if (this.t0 == null || this.t1 == null || a == null || this.cl == null || a != this.q6) {
                return null;
            }
            let b = new AxisRange(this.t3, this.t2);
            return b;
        }
        tb(a, b) {
            super.tb(a, b);
            let c = b.bv;
            let d = b.bu;
            let e = this.getEffectiveViewport1(b);
            let f = this.qk;
            let g = this.q6;
            let h = new ScalerParams(0, c, d, f.cg, e);
            let i = new ScalerParams(0, c, d, g.cg, e);
            a.f.clear();
            a.m.clear();
            a.p.clear();
            let j = typeCast(ISortingAxis_$type, this.qk);
            if (j != null && j.i$b.count != this.cl.count) {
                return;
            }
            let 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();
            let l = this.qk.el(2, h) - this.qk.el(1, h);
            for (let m = b.bx.c; m <= b.bx.d; ++m) {
                let n;
                if (j == null) {
                    n = b.bx.getBucket(m);
                }
                else {
                    let o = j.i$b._inner[m];
                    let p = j.i$d(o);
                    let q = this.t0._inner[m];
                    let r = this.t1._inner[m];
                    let s = q;
                    let t = r;
                    let u = p;
                    let 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;
                    let w = s - t * this.t4;
                    let x = s;
                    let y = s + t * this.t4;
                    let z = NaN;
                    if (!isNaN_(p)) {
                        z = this.qk.ei(p, h);
                    }
                    n = [z, w, x, y];
                }
                let 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);
                }
            }
        }
        k2(a, b) {
            super.k2(a, b);
            let c = b;
            if (c != null) {
                c.b4();
            }
        }
        gn(a, b, c) {
            if (this.t0 == null) {
                return super.gn(a, b, c);
            }
            let d = this.getEffectiveViewport1(this.b1);
            let e = this.sb(this.b1.bv, this.b1.bu, d);
            let 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);
        }
        ho(a, b) {
            if (this.t0 == null) {
                return super.ho(a, b);
            }
            return this.hp(a, b, this.qk, runOn(this, this.sa), this.t0);
        }
        hm(a, b) {
            if (this.t0 == null) {
                return super.hm(a, b);
            }
            return this.hn(a, b, this.qk, runOn(this, this.sa), this.t0);
        }
        fd(a, b, c) {
            let d = super.fd(a, b, c);
            if (!this.rt(this.r3())) {
                d = false;
            }
            return d;
        }
        td(a, b) {
            super.td(a, b);
            let c = typeCast(BollingerBandsOverlayView.$, b);
            let d = this.getEffectiveViewport1(b);
            if (b.b0(a)) {
                c.b4();
                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.b5(e, f, g, h, i, j);
                b.b2(a);
            }
            this.qt.a2(this, this.rs(), runOn(this, this.qc), this.sh(b), this.sj(b));
            let k = false;
            let l = this.qt.b;
            let m = a.f;
            let n = this.cl.count;
            if (l != null) {
                k = true;
            }
            if (k) {
                let o = new ScalerParams(0, b.bv, b.bu, this.qk.cg, d);
                this.ta(m, -1, n, this.qk, o, b.q);
            }
            let p = c.cc;
            let q = c.cd;
            let r = c.ce;
            let 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;
        }
        by() {
            return new BollingerBandsOverlayView(this);
        }
        l4(a) {
            super.l4(a);
            this.tz = typeCast(BollingerBandsOverlayView.$, a);
        }
        sk() {
            return 3;
        }
        gg() {
            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;
})();
/**
 * @hidden
 */
export let BollingerBandsBucketCalculator = /*@__PURE__*/ (() => {
    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.bz.cl.count - 1);
            if (b <= c) {
                let d = this.f.b3.t4;
                let e = 0;
                let f = 0;
                let g = 0;
                for (let 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;
})();
/**
 * @hidden
 */
export let BollingerBandsOverlayView = /*@__PURE__*/ (() => {
    class BollingerBandsOverlayView extends FinancialSeriesView {
        constructor(a) {
            super(a);
            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;
        }
        as() {
            super.as();
            if (!this.q) {
            }
        }
        by() {
            return new BollingerBandsBucketCalculator(this);
        }
        b5(a, b, c, d, e, f) {
            for (let g of fromEnum(Flattener.d(a, b, d, this.e.fx))) {
                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) });
            }
            for (let h of fromEnum(Flattener.d(a, b, e, this.e.fx))) {
                this.cg.an.add({ $type: Point_$type, x: b(h), y: e(h) });
            }
            for (let i of fromEnum(Flattener.d(a, c, f, this.e.fx))) {
                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) });
            }
            this.aq();
        }
        b4() {
            this.b8.an.clear();
            this.cf.an.clear();
            this.cg.an.clear();
            this.ch.an.clear();
            this.aq();
        }
        get b7() {
            return this.b8;
        }
        get cc() {
            return this.cf;
        }
        get cd() {
            return this.cg;
        }
        get ce() {
            return this.ch;
        }
        bc() {
            super.bc();
            this.b6.an = this.b8.an;
            this.b9.an = this.cf.an;
            this.ca.an = this.cg.an;
            this.cb.an = this.ch.an;
            let 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;
        }
        a6(a, b) {
            super.a6(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);
                }
            }
        }
        al(a) {
            super.al(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;
})();
