/*
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 { 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
 */
export let BollingerBandsOverlay = /*@__PURE__*/ (() => {
    class BollingerBandsOverlay extends FinancialOverlay {
        constructor() {
            super();
            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);
        }
        bk() {
            return new BollingerBandsOverlay();
        }
        rg(a, b, c, d) {
            if (this.yz().contains(b)) {
                this.aba = false;
            }
            super.rg(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;
            }
        }
        abb() {
            this.abg.clear();
            this.abh.clear();
            let a = getEnumerator(TrendCalculators.e(new SafeEnumerable(this.yy), this.abl));
            let b = getEnumerator(TrendCalculators.f(new SafeEnumerable(this.yy), this.abl));
            this.abj = 1.7976931348623157E+308;
            this.abi = -1.7976931348623157E+308;
            let c = true;
            let d = true;
            let 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;
        }
        ay(a) {
            if (this.abg == null || this.abh == null || a == null || this.dp == null || a != this.xz) {
                return null;
            }
            let b = new AxisRange(this.abj, this.abi);
            return b;
        }
        aam(a, b) {
            super.aam(a, b);
            let c = b.ca;
            let d = b.b9;
            let e = this.getEffectiveViewport1(b);
            let f = this.xg;
            let g = this.xz;
            let h = new ScalerParams(0, c, d, f.df, e);
            let i = new ScalerParams(0, c, d, g.df, e);
            a.h.clear();
            a.p.clear();
            a.s.clear();
            let j = typeCast(ISortingAxis_$type, this.xg);
            if (j != null && j.i$b.count != this.dp.count) {
                return;
            }
            let 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();
            let l = this.xg.fw(2, h) - this.xg.fw(1, h);
            for (let m = b.cd.c; m <= b.cd.d; ++m) {
                let n;
                if (j == null) {
                    n = b.cd.getBucket(m);
                }
                else {
                    let o = j.i$b._inner[m];
                    let p = j.i$d(o);
                    let q = this.abg._inner[m];
                    let r = this.abh._inner[m];
                    let s = q;
                    let t = r;
                    let u = p;
                    let 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;
                    let w = s - t * this.abk;
                    let x = s;
                    let y = s + t * this.abk;
                    let z = NaN;
                    if (!isNaN_(p)) {
                        z = this.xg.ft(p, h);
                    }
                    n = [z, w, x, y];
                }
                let 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);
                }
            }
        }
        pb(a, b) {
            super.pb(a, b);
            let c = b;
            if (c != null) {
                c.ck();
            }
        }
        i8(a, b, c) {
            if (this.abg == null) {
                return super.i8(a, b, c);
            }
            let d = this.getEffectiveViewport1(this.cw);
            let e = this.y7(this.cw.ca, this.cw.b9, d);
            let 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);
        }
        ke(a, b) {
            if (this.abg == null) {
                return super.ke(a, b);
            }
            return this.kf(a, b, this.xg, runOn(this, this.y6), this.abg);
        }
        kc(a, b) {
            if (this.abg == null) {
                return super.kc(a, b);
            }
            return this.kd(a, b, this.xg, runOn(this, this.y6), this.abg);
        }
        hf(a, b, c) {
            let d = super.hf(a, b, c);
            if (!this.yp(this.yz())) {
                d = false;
            }
            return d;
        }
        aao(a, b) {
            super.aao(a, b);
            let c = typeCast(BollingerBandsOverlayView.$, b);
            let d = this.getEffectiveViewport1(b);
            if (b.cg(a)) {
                c.ck();
                let e = a.h.count;
                let f = (g) => a.h._inner[g][0];
                let g = (h) => a.h._inner[e - 1 - h][0];
                let h = (i) => a.h._inner[i][1];
                let i = (j) => a.h._inner[j][2];
                let j = (k) => a.h._inner[e - 1 - k][3];
                c.cl(e, f, g, h, i, j);
                b.ci(a);
            }
            this.xp.a2(this, this.yo(), runOn(this, this.w8), this.zd(b), this.zf(b));
            let k = false;
            let l = this.xp.b;
            let m = a.h;
            let n = this.dp.count;
            if (l != null) {
                k = true;
            }
            if (k) {
                let o = new ScalerParams(0, b.ca, b.b9, this.xg.df, d);
                this.aal(m, -1, n, this.xg, o, b.t, -1);
            }
            let p = c.cs;
            let q = c.ct;
            let r = c.cu;
            let 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;
        }
        ct() {
            return new BollingerBandsOverlayView(this);
        }
        q7(a) {
            super.q7(a);
            this.abf = typeCast(BollingerBandsOverlayView.$, a);
        }
        zg() {
            return 3;
        }
        i0() {
            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;
})();
/**
 * @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.cf.dp.count - 1);
            if (b <= c) {
                let d = this.f.cj.abk;
                let e = 0;
                let f = 0;
                let g = 0;
                for (let 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;
})();
/**
 * @hidden
 */
export let BollingerBandsOverlayView = /*@__PURE__*/ (() => {
    class BollingerBandsOverlayView extends FinancialSeriesView {
        constructor(a) {
            super(a);
            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;
        }
        a4() {
            super.a4();
            if (!this.t) {
            }
        }
        ce() {
            return new BollingerBandsBucketCalculator(this);
        }
        cl(a, b, c, d, e, f) {
            for (let g of fromEnum(Flattener.d(a, b, d, this.f.ie))) {
                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) });
            }
            for (let h of fromEnum(Flattener.d(a, b, e, this.f.ie))) {
                this.cw.an.add({ $type: Point_$type, x: b(h), y: e(h) });
            }
            for (let i of fromEnum(Flattener.d(a, c, f, this.f.ie))) {
                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) });
            }
            this.a2();
        }
        ck() {
            this.co.an.clear();
            this.cv.an.clear();
            this.cw.an.clear();
            this.cx.an.clear();
            this.a2();
        }
        get cn() {
            return this.co;
        }
        get cs() {
            return this.cv;
        }
        get ct() {
            return this.cw;
        }
        get cu() {
            return this.cx;
        }
        bq() {
            super.bq();
            this.cm.an = this.co.an;
            this.cp.an = this.cv.an;
            this.cq.an = this.cw.an;
            this.cr.an = this.cx.an;
            let 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;
        }
        bj(a, b) {
            super.bj(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);
                }
            }
        }
        ax(a) {
            super.ax(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;
})();
