/*
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, Point_$type, fromEnum } from "igniteui-react-core/ES2015/type";
import { CategoryFrame } from "./CategoryFrame";
import { MathUtil } from "igniteui-react-core/ES2015/MathUtil";
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 { RangeValueList } from "./RangeValueList";
import { PropertyMetadata } from "igniteui-react-core/ES2015/PropertyMetadata";
import { truncate, isNaN_, isInfinity } from "igniteui-react-core/ES2015/number";
import { Flattener } from "igniteui-react-core/ES2015/Flattener";
export class PriceChannelOverlay extends FinancialOverlay {
    constructor() {
        super();
        this.pd = new List$1(Number_$type, 0);
        this.pc = new List$1(Number_$type, 0);
        this.ab = PriceChannelOverlay.$;
        this.lz = new CategoryFrame(3);
        this.l1 = new CategoryFrame(3);
        this.ly = new CategoryFrame(3);
    }
    io(a, b, c, d) {
        switch (b) {
            case "HighColumn":
            case "LowColumn":
                this.o6 = false;
                break;
        }
        super.io(a, b, c, d);
        switch (b) {
            case PriceChannelOverlay.$$p[0]:
                this.o6 = false;
                this.i1(false);
                break;
        }
    }
    dt(a, b, c) {
        let d = super.dt(a, b, c);
        if (this.mk == null || this.mm == null) {
            d = false;
        }
        return d;
    }
    o7() {
        this.pd.clear();
        this.pc.clear();
        let a = truncate(MathUtil.b(this.pe, 0, this.bz.count));
        let b = Math.min(this.mk.count, this.mm.count);
        let c = this.ne(this.mk);
        let d = this.ne(this.mm);
        for (let e = 0; e < b; e++) {
            let f = Math.min(a, e);
            let g = -1.7976931348623157E+308;
            let h = 1.7976931348623157E+308;
            for (let i = 0; i < f; i++) {
                if (!isNaN_(c.item(e - i))) {
                    g = Math.max(g, c.item(e - i));
                }
                if (!isNaN_(d.item(e - i))) {
                    h = Math.min(h, d.item(e - i));
                }
            }
            if (e == 0) {
                h = d.item(0);
                g = c.item(0);
            }
            this.pd.add(g);
            this.pc.add(h);
        }
        return true;
    }
    ol(a, b) {
        super.ol(a, b);
        let c = b.bq;
        let d = b.bp;
        let e = this.getEffectiveViewport1(b);
        let f = new ScalerParams(0, c, d, this.lw.b8, e);
        let g = new ScalerParams(0, c, d, this.mg.b8, e);
        let h = this.lw;
        let i = this.mg;
        a.f.clear();
        a.m.clear();
        a.p.clear();
        let j = 0;
        let k = typeCast(ISortingAxis_$type, this.lw);
        if (k != null && k.i$b.count != this.bz.count) {
            return;
        }
        j = this.nl(c, d, e);
        if (!this.o6) {
            this.o6 = this.o7();
        }
        g.b = this.ec();
        let l = this.lw.dw(2, f) - this.lw.dw(1, f);
        for (let m = b.bs.c; m <= b.bs.d; ++m) {
            let n;
            if (k == null) {
                n = b.bs.getBucket(m);
            }
            else {
                let o = k.i$b._inner[m];
                let p = k.i$d(o);
                let q = this.pd._inner[m];
                let r = this.pc._inner[m];
                let s = q;
                let t = r;
                let u = p;
                while (m < b.bs.d) {
                    o = k.i$b._inner[m + 1];
                    u = k.i$d(o);
                    if (u - p > l) {
                        break;
                    }
                    m++;
                    s = Math.max(q, this.pd._inner[m]);
                    t = Math.min(r, this.pc._inner[m]);
                }
                if (!isInfinity(t) && !isInfinity(s)) {
                    let v = NaN;
                    if (!isNaN_(p)) {
                        v = this.lw.dt(p, f);
                    }
                    n = [v, t, s];
                }
                else {
                    n = [NaN, NaN, NaN];
                }
            }
            let w = Math.max(1, l);
            if (!isNaN_(n[0]) && m * w >= this.o8) {
                if (this.lw != null && typeCast(ISortingAxis_$type, this.lw) !== null) {
                    n[0] = n[0] + j;
                }
                else {
                    n[0] = h.dt(n[0], f) + j;
                }
                n[1] = i.dt(n[1], g);
                n[2] = i.dt(n[2], g);
                a.f.add(n);
            }
        }
    }
    hl(a, b) {
        super.hl(a, b);
        let c = b;
        if (c != null) {
            c.bz();
        }
    }
    on(a, b) {
        super.on(a, b);
        let c = typeCast(PriceChannelOverlayView.$, b);
        if (c == null) {
            return;
        }
        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[e - 1 - j][2];
            c.b0(e, f, g, h, i);
            b.bx(a);
        }
        this.l4.x(this, this.m2(), this.lw, runOn(this, this.lo), this.nr(b), this.nt(b));
        let j = false;
        let k = this.l4.c;
        let l = a.f;
        let m = this.bz.count;
        if (k != null) {
            j = true;
        }
        if (j) {
            let n = new ScalerParams(0, b.bq, b.bp, this.lw.b8, d);
            this.ok(l, -1, m, this.lw, n, b.n);
        }
        let o = c.b6;
        let p = c.b7;
        let q = c.b2;
        this.l4.ae(o, true, false, true, true);
        this.l4.ae(p, true, false, true, true);
        this.l4.ae(q, false, true, false, false);
        q._opacity = this.l4.i * this.d1;
    }
    bf() {
        return new PriceChannelOverlayView(this);
    }
    ii(a) {
        super.ii(a);
        this.pb = typeCast(PriceChannelOverlayView.$, a);
    }
    eh(a, b, c) {
        if (this.bs == null) {
            return NaN;
        }
        let d = this.getEffectiveViewport1(this.bi);
        let e = new ScalerParams(0, this.bs.mk, this.bi.bp, this.lw.b8, d);
        let f = this.nl(this.bs.mk, this.bi.bp, d);
        return this.ej(new RangeValueList(this.pd, this.pc), a, this.lw, e, f, runOn(this, this.nk), b, c);
    }
    e6(a, b) {
        if (this.pd == null || this.pc == null) {
            return super.e6(a, b);
        }
        return this.e7(a, b, this.lw, runOn(this, this.nk), new RangeValueList(this.pd, this.pc));
    }
    e8(a, b) {
        if (this.pd == null || this.pc == null) {
            return super.e8(a, b);
        }
        return this.e9(a, b, this.lw, runOn(this, this.nk), new RangeValueList(this.pd, this.pc));
    }
    ec() {
        return this.pc == null || this.pc.count == 0 ? NaN : this.pc._inner[0];
    }
}
PriceChannelOverlay.$t = markType(PriceChannelOverlay, 'PriceChannelOverlay', FinancialOverlay.$);
Type.dep(DependencyProperty, PropertyMetadata, PriceChannelOverlay, 'raisePropertyChanged', ['Period:pe:pg', [1, 14]]);
export class PriceChannelBucketCalculator 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 = -1.7976931348623157E+308;
            let e = 1.7976931348623157E+308;
            let f = 0;
            for (let g = b; g <= c; ++g) {
                if (!isNaN_(this.f.by.pd._inner[g]) && !isNaN_(this.f.by.pc._inner[g])) {
                    d = Math.max(d, this.f.by.pd._inner[g]);
                    e = Math.min(e, this.f.by.pc._inner[g]);
                    ++f;
                }
            }
            if (f > 0 && e != 1.7976931348623157E+308 && d != -1.7976931348623157E+308) {
                return [0.5 * (b + c), e, d];
            }
        }
        return [NaN, NaN, NaN];
    }
}
PriceChannelBucketCalculator.$t = markType(PriceChannelBucketCalculator, 'PriceChannelBucketCalculator', FinancialBucketCalculator.$);
export class PriceChannelOverlayView extends FinancialSeriesView {
    constructor(a) {
        super(a);
        this.b3 = new Polygon();
        this.b8 = new Polyline();
        this.b9 = new Polyline();
        this.by = null;
        this.b1 = new Polygon();
        this.b4 = new Polyline();
        this.b5 = new Polyline();
        this.by = a;
    }
    get b2() {
        return this.b3;
    }
    get b6() {
        return this.b8;
    }
    get b7() {
        return this.b9;
    }
    ap() {
        super.ap();
        if (!this.n) {
            this.e.bj = 1;
        }
    }
    bt() {
        return new PriceChannelBucketCalculator(this);
    }
    bz() {
        this.b3.aj.clear();
        this.b8.aj.clear();
        this.b9.aj.clear();
        this.an();
    }
    b0(a, b, c, d, e) {
        for (let f of fromEnum(Flattener.d(a, b, d, this.e.eo))) {
            this.b3.aj.add({ $type: Point_$type, x: b(f), y: d(f) });
            this.b8.aj.add({ $type: Point_$type, x: b(f), y: d(f) });
        }
        for (let g of fromEnum(Flattener.d(a, c, e, this.e.eo))) {
            this.b3.aj.add({ $type: Point_$type, x: c(g), y: e(g) });
            this.b9.aj.add({ $type: Point_$type, x: c(g), y: e(g) });
        }
        this.an();
    }
    a9() {
        super.a9();
        this.b1.aj = this.b3.aj;
        this.b4.aj = this.b8.aj;
        this.b5.aj = this.b9.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;
    }
    a3(a, b) {
        super.a3(a, b);
        if (a.d) {
            if (b) {
                a.t(this.b1);
                a.u(this.b4);
                a.u(this.b5);
            }
            else {
                a.t(this.b3);
                a.u(this.b8);
                a.u(this.b9);
            }
        }
    }
    ai(a) {
        super.ai(a);
        this.e.a2.exportPolygonData(a, this.b3, "fill", ["Fill"]);
        this.e.a2.exportPolylineData(a, this.b8, "bottom", ["Lower", "Main"]);
        this.e.a2.exportPolylineData(a, this.b9, "top", ["Upper"]);
    }
}
PriceChannelOverlayView.$t = markType(PriceChannelOverlayView, 'PriceChannelOverlayView', FinancialSeriesView.$);
/*<BeginStatics Name="Infragistics.Controls.Charts.PriceChannelOverlay" />*/
/*<EndStatics Name="Infragistics.Controls.Charts.PriceChannelOverlay" />*/
