/*
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";
import { Series } from "./Series";
import { List$1 } from "igniteui-react-core";
import { Number_$type, typeCast, runOn, markType, markDep, getEnumerator, Point_$type, fromEnum } from "igniteui-react-core";
import { CategoryFrame } from "./CategoryFrame";
import { SafeEnumerable } from "./SafeEnumerable";
import { TrendCalculators } from "igniteui-react-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-react-core";
import { Polygon } from "igniteui-react-core";
import { PropertyMetadata } from "igniteui-react-core";
import { isNaN_ } from "igniteui-react-core";
import { Flattener } from "igniteui-react-core";
/**
 * @hidden
 */
export let BollingerBandsOverlay = /*@__PURE__*/ (() => {
    class BollingerBandsOverlay extends FinancialOverlay {
        bc() {
            return new BollingerBandsOverlay();
        }
        constructor() {
            super();
            this.yd = 0;
            this.ye = 0;
            this.yb = new List$1(Number_$type, 0);
            this.yc = new List$1(Number_$type, 0);
            this.ab = BollingerBandsOverlay.$;
            this.uf = new CategoryFrame(4);
            this.uh = new CategoryFrame(4);
            this.ud = new CategoryFrame(4);
        }
        pa(a, b, c, d) {
            if (this.vu().contains(b)) {
                this.x5 = false;
            }
            super.pa(a, b, c, d);
            switch (b) {
                case BollingerBandsOverlay.$$p[1]:
                    this.x5 = false;
                    this.pr(false);
                    break;
                case BollingerBandsOverlay.$$p[0]:
                    this.pr(false);
                    break;
            }
        }
        x6() {
            this.yb.clear();
            this.yc.clear();
            let a = getEnumerator(TrendCalculators.e(new SafeEnumerable(this.vt), this.yg));
            let b = getEnumerator(TrendCalculators.f(new SafeEnumerable(this.vt), this.yg));
            this.ye = 1.7976931348623157E+308;
            this.yd = -1.7976931348623157E+308;
            let c = true;
            let d = true;
            let e = this.yf;
            while (c || d) {
                if (a.moveNext()) {
                    this.yb.add(a.current);
                }
                else {
                    c = false;
                }
                if (b.moveNext()) {
                    this.yc.add(b.current);
                }
                else {
                    d = false;
                }
                if (c && d) {
                    this.ye = Math.min(this.ye, a.current - b.current * e);
                    this.yd = Math.max(this.yd, a.current + b.current * e);
                }
            }
            return true;
        }
        aq(a) {
            if (this.yb == null || this.yc == null || a == null || this.c7 == null || a != this.uu) {
                return null;
            }
            let b = new AxisRange(this.ye, this.yd);
            return b;
        }
        xh(a, b) {
            super.xh(a, b);
            let c = b.by;
            let d = b.bx;
            let e = this.getEffectiveViewport1(b);
            let f = this.ub;
            let g = this.uu;
            let h = new ScalerParams(0, c, d, f.ch, e);
            let i = new ScalerParams(0, c, d, g.ch, e);
            a.f.clear();
            a.m.clear();
            a.p.clear();
            let j = typeCast(ISortingAxis_$type, this.ub);
            if (j != null && j.i$b.count != this.c7.count) {
                return;
            }
            let k = 0;
            k = this.v2(c, d, e);
            if (!this.x5) {
                this.x5 = this.x6();
                if (this.uu != null) {
                    this.uu.c2(true);
                }
            }
            i.b = this.h3();
            let l = this.ub.em(2, h) - this.ub.em(1, h);
            for (let m = b.b0.c; m <= b.b0.d; ++m) {
                let n;
                if (j == null) {
                    n = b.b0.getBucket(m);
                }
                else {
                    let o = j.i$b._inner[m];
                    let p = j.i$d(o);
                    let q = this.yb._inner[m];
                    let r = this.yc._inner[m];
                    let s = q;
                    let t = r;
                    let u = p;
                    let v = 1;
                    while (m < b.b0.d) {
                        o = j.i$b._inner[m + 1];
                        u = j.i$d(o);
                        if (u - p > l) {
                            break;
                        }
                        m++;
                        s += this.yb._inner[m];
                        t += this.yc._inner[m];
                        v++;
                    }
                    s /= v;
                    t /= v;
                    let w = s - t * this.yf;
                    let x = s;
                    let y = s + t * this.yf;
                    let z = NaN;
                    if (!isNaN_(p)) {
                        z = this.ub.ej(p, h);
                    }
                    n = [z, w, x, y];
                }
                let aa = Math.max(1, l);
                if (!isNaN_(n[0]) && m * aa >= this.x7) {
                    if (this.ub != null && typeCast(ISortingAxis_$type, this.ub) !== null) {
                        n[0] = n[0] + k;
                    }
                    else {
                        n[0] = f.ej(n[0], h) + k;
                    }
                    n[1] = g.ej(n[1], i);
                    n[2] = g.ej(n[2], i);
                    n[3] = g.ej(n[3], i);
                    a.f.add(n);
                }
            }
        }
        nk(a, b) {
            super.nk(a, b);
            let c = b;
            if (c != null) {
                c.b7();
            }
        }
        ia(a, b, c) {
            if (this.yb == null) {
                return super.ia(a, b, c);
            }
            let d = this.getEffectiveViewport1(this.cg);
            let e = this.v2(this.cg.by, this.cg.bx, d);
            let f = new ScalerParams(0, this.cg.by, this.cg.bx, this.ub.ch, d);
            f.c = this.cv.t6;
            return this.ic(this.yb, a, this.ub, f, e, runOn(this, this.v1), b, c);
        }
        jg(a, b) {
            if (this.yb == null) {
                return super.jg(a, b);
            }
            return this.jh(a, b, this.ub, runOn(this, this.v1), this.yb);
        }
        je(a, b) {
            if (this.yb == null) {
                return super.je(a, b);
            }
            return this.jf(a, b, this.ub, runOn(this, this.v1), this.yb);
        }
        gm(a, b, c) {
            let d = super.gm(a, b, c);
            if (!this.vk(this.vu())) {
                d = false;
            }
            return d;
        }
        xj(a, b) {
            super.xj(a, b);
            let c = typeCast(BollingerBandsOverlayView.$, b);
            let d = this.getEffectiveViewport1(b);
            if (b.b3(a)) {
                c.b7();
                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.b8(e, f, g, h, i, j);
                b.b5(a);
            }
            this.uk.a2(this, this.vj(), runOn(this, this.t3), this.v8(b), this.wa(b));
            let k = false;
            let l = this.uk.b;
            let m = a.f;
            let n = this.c7.count;
            if (l != null) {
                k = true;
            }
            if (k) {
                let o = new ScalerParams(0, b.by, b.bx, this.ub.ch, d);
                this.xg(m, -1, n, this.ub, o, b.q);
            }
            let p = c.cf;
            let q = c.cg;
            let r = c.ch;
            let s = c.ca;
            this.uk.ad(q, true, false, true, true);
            this.uk.an = null;
            this.uk.am = null;
            this.uk.ad(p, true, false, false, true);
            this.uk.ad(r, true, false, false, true);
            this.uk.ad(s, false, true, false, false);
            s._opacity = this.uk.j * this.hg;
        }
        cd() {
            return new BollingerBandsOverlayView(this);
        }
        o2(a) {
            super.o2(a);
            this.ya = typeCast(BollingerBandsOverlayView.$, a);
        }
        wb() {
            return 3;
        }
        h3() {
            return Series.h5(this.yb, null);
        }
    }
    BollingerBandsOverlay.$t = /*@__PURE__*/ markType(BollingerBandsOverlay, 'BollingerBandsOverlay', FinancialOverlay.$);
    BollingerBandsOverlay.$$p = /*@__PURE__*/ markDep(DependencyProperty, PropertyMetadata, BollingerBandsOverlay, 'raisePropertyChanged', ['Multiplier:yf:yj', [1, 2], 'Period:yg:yk', [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.b2.c7.count - 1);
            if (b <= c) {
                let d = this.f.b6.yf;
                let e = 0;
                let f = 0;
                let g = 0;
                for (let h = b; h <= c; ++h) {
                    if (!isNaN_(this.f.b6.yb._inner[h]) && !isNaN_(this.f.b6.yc._inner[h])) {
                        e += this.f.b6.yb._inner[h];
                        f += this.f.b6.yc._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 = /*@__PURE__*/ markType(BollingerBandsBucketCalculator, 'BollingerBandsBucketCalculator', FinancialBucketCalculator.$);
    return BollingerBandsBucketCalculator;
})();
/**
 * @hidden
 */
export let BollingerBandsOverlayView = /*@__PURE__*/ (() => {
    class BollingerBandsOverlayView extends FinancialSeriesView {
        constructor(a) {
            super(a);
            this.b6 = null;
            this.cb = new Polygon();
            this.ci = new Polyline();
            this.cj = new Polyline();
            this.ck = new Polyline();
            this.b9 = new Polygon();
            this.cc = new Polyline();
            this.cd = new Polyline();
            this.ce = new Polyline();
            this.b6 = a;
        }
        au() {
            super.au();
            if (!this.q) {
            }
        }
        b1() {
            return new BollingerBandsBucketCalculator(this);
        }
        b8(a, b, c, d, e, f) {
            for (let g of fromEnum(Flattener.d(a, b, d, this.e.hk))) {
                this.cb.an.add({ $type: Point_$type, x: b(g), y: d(g) });
                this.ci.an.add({ $type: Point_$type, x: b(g), y: d(g) });
            }
            for (let h of fromEnum(Flattener.d(a, b, e, this.e.hk))) {
                this.cj.an.add({ $type: Point_$type, x: b(h), y: e(h) });
            }
            for (let i of fromEnum(Flattener.d(a, c, f, this.e.hk))) {
                this.cb.an.add({ $type: Point_$type, x: c(i), y: f(i) });
                this.ck.an.add({ $type: Point_$type, x: c(i), y: f(i) });
            }
            this.as();
        }
        b7() {
            this.cb.an.clear();
            this.ci.an.clear();
            this.cj.an.clear();
            this.ck.an.clear();
            this.as();
        }
        get ca() {
            return this.cb;
        }
        get cf() {
            return this.ci;
        }
        get cg() {
            return this.cj;
        }
        get ch() {
            return this.ck;
        }
        be() {
            super.be();
            this.b9.an = this.cb.an;
            this.cc.an = this.ci.an;
            this.cd.an = this.cj.an;
            this.ce.an = this.ck.an;
            let a = this.br();
            this.b9._fill = a;
            this.b9._opacity = 1;
            this.cc._stroke = a;
            this.cc.ad = this.e.hl + 3;
            this.cd._stroke = a;
            this.cd.ad = this.e.hl + 3;
            this.ce._stroke = a;
            this.ce.ad = this.e.hl + 3;
        }
        a8(a, b) {
            super.a8(a, b);
            if (a.d) {
                if (b) {
                    a.u(this.b9);
                    a.v(this.cc);
                    a.v(this.cd);
                    a.v(this.ce);
                }
                else {
                    a.u(this.cb);
                    a.v(this.ci);
                    a.v(this.cj);
                    a.v(this.ck);
                }
            }
        }
        an(a) {
            super.an(a);
            this.e.a5.exportPolygonData(a, this.cb, "FillShape", ["Fill"]);
            this.e.a5.exportPolylineData(a, this.ci, "BottomLine", ["Lower", "Main"]);
            this.e.a5.exportPolylineData(a, this.cj, "CentralLine", ["Central"]);
            this.e.a5.exportPolylineData(a, this.ck, "TopLine", ["Upper"]);
        }
    }
    BollingerBandsOverlayView.$t = /*@__PURE__*/ markType(BollingerBandsOverlayView, 'BollingerBandsOverlayView', FinancialSeriesView.$);
    return BollingerBandsOverlayView;
})();
