/*
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 { Series } from "./Series";
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";
/**
 * @hidden
 */
export class PriceChannelOverlay extends FinancialOverlay {
    constructor() {
        super();
        this.pi = new List$1(Number_$type, 0);
        this.ph = new List$1(Number_$type, 0);
        this.ab = PriceChannelOverlay.$;
        this.l4 = new CategoryFrame(3);
        this.l6 = new CategoryFrame(3);
        this.l3 = new CategoryFrame(3);
    }
    is(a, b, c, d) {
        switch (b) {
            case "HighColumn":
            case "LowColumn":
                this.pb = false;
                break;
        }
        super.is(a, b, c, d);
        switch (b) {
            case PriceChannelOverlay.$$p[0]:
                this.pb = false;
                this.i6(false);
                break;
        }
    }
    dv(a, b, c) {
        let d = super.dv(a, b, c);
        if (this.mr == null || this.mt == null) {
            d = false;
        }
        return d;
    }
    pc() {
        this.pi.clear();
        this.ph.clear();
        let a = truncate(MathUtil.b(this.pj, 0, this.bz.count));
        let b = Math.min(this.mr.count, this.mt.count);
        let c = this.nl(this.mr);
        let d = this.nl(this.mt);
        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.pi.add(g);
            this.ph.add(h);
        }
        return true;
    }
    os(a, b) {
        super.os(a, b);
        let c = b.bs;
        let d = b.br;
        let e = this.getEffectiveViewport1(b);
        let f = new ScalerParams(0, c, d, this.l1.b8, e);
        let g = new ScalerParams(0, c, d, this.mm.b8, e);
        g.b = this.ee();
        let h = this.l1;
        let i = this.mm;
        a.f.clear();
        a.m.clear();
        a.p.clear();
        let j = 0;
        let k = typeCast(ISortingAxis_$type, this.l1);
        if (k != null && k.i$b.count != this.bz.count) {
            return;
        }
        j = this.ns(c, d, e);
        if (!this.pb) {
            this.pb = this.pc();
        }
        g.b = this.ee();
        let l = this.l1.dw(2, f) - this.l1.dw(1, f);
        for (let m = b.bu.c; m <= b.bu.d; ++m) {
            let n;
            if (k == null) {
                n = b.bu.getBucket(m);
            }
            else {
                let o = k.i$b._inner[m];
                let p = k.i$d(o);
                let q = this.pi._inner[m];
                let r = this.ph._inner[m];
                let s = q;
                let t = r;
                let u = p;
                while (m < b.bu.d) {
                    o = k.i$b._inner[m + 1];
                    u = k.i$d(o);
                    if (u - p > l) {
                        break;
                    }
                    m++;
                    s = Math.max(q, this.pi._inner[m]);
                    t = Math.min(r, this.ph._inner[m]);
                }
                if (!isInfinity(t) && !isInfinity(s)) {
                    let v = NaN;
                    if (!isNaN_(p)) {
                        v = this.l1.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.pd) {
                if (this.l1 != null && typeCast(ISortingAxis_$type, this.l1) !== 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);
            }
        }
    }
    hp(a, b) {
        super.hp(a, b);
        let c = b;
        if (c != null) {
            c.b1();
        }
    }
    ou(a, b) {
        super.ou(a, b);
        let c = typeCast(PriceChannelOverlayView.$, b);
        if (c == null) {
            return;
        }
        let d = this.getEffectiveViewport1(b);
        if (b.bx(a)) {
            c.b1();
            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.b2(e, f, g, h, i);
            b.bz(a);
        }
        this.l9.x(this, this.m9(), this.l1, runOn(this, this.lt), this.ny(b), this.n0(b));
        let j = false;
        let k = this.l9.c;
        let l = a.f;
        let m = this.bz.count;
        if (k != null) {
            j = true;
        }
        if (j) {
            let n = new ScalerParams(0, b.bs, b.br, this.l1.b8, d);
            this.or(l, -1, m, this.l1, n, b.n);
        }
        let o = c.b8;
        let p = c.b9;
        let q = c.b4;
        this.l9.ae(o, true, false, true, true);
        this.l9.ae(p, true, false, true, true);
        this.l9.ae(q, false, true, false, false);
        q._opacity = this.l9.i * this.d3;
    }
    bf() {
        return new PriceChannelOverlayView(this);
    }
    im(a) {
        super.im(a);
        this.pg = typeCast(PriceChannelOverlayView.$, a);
    }
    ek(a, b, c) {
        if (this.bs == null) {
            return NaN;
        }
        let d = this.getEffectiveViewport1(this.bi);
        let e = new ScalerParams(0, this.bs.mq, this.bi.br, this.l1.b8, d);
        let f = this.ns(this.bs.mq, this.bi.br, d);
        return this.em(new RangeValueList(this.pi, this.ph), a, this.l1, e, f, runOn(this, this.nr), b, c);
    }
    e9(a, b) {
        if (this.pi == null || this.ph == null) {
            return super.e9(a, b);
        }
        return this.fa(a, b, this.l1, runOn(this, this.nr), new RangeValueList(this.pi, this.ph));
    }
    fb(a, b) {
        if (this.pi == null || this.ph == null) {
            return super.fb(a, b);
        }
        return this.fc(a, b, this.l1, runOn(this, this.nr), new RangeValueList(this.pi, this.ph));
    }
    ee() {
        return Series.ef(this.ph, null);
    }
}
PriceChannelOverlay.$t = markType(PriceChannelOverlay, 'PriceChannelOverlay', FinancialOverlay.$);
Type.dep(DependencyProperty, PropertyMetadata, PriceChannelOverlay, 'raisePropertyChanged', ['Period:pj:pl', [1, 14]]);
/**
 * @hidden
 */
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.bw.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.b0.pi._inner[g]) && !isNaN_(this.f.b0.ph._inner[g])) {
                    d = Math.max(d, this.f.b0.pi._inner[g]);
                    e = Math.min(e, this.f.b0.ph._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.$);
/**
 * @hidden
 */
export class PriceChannelOverlayView extends FinancialSeriesView {
    constructor(a) {
        super(a);
        this.b5 = new Polygon();
        this.ca = new Polyline();
        this.cb = new Polyline();
        this.b0 = null;
        this.b3 = new Polygon();
        this.b6 = new Polyline();
        this.b7 = new Polyline();
        this.b0 = a;
    }
    get b4() {
        return this.b5;
    }
    get b8() {
        return this.ca;
    }
    get b9() {
        return this.cb;
    }
    ap() {
        super.ap();
        if (!this.n) {
            this.e.bj = 1;
        }
    }
    bv() {
        return new PriceChannelBucketCalculator(this);
    }
    b1() {
        this.b5.aj.clear();
        this.ca.aj.clear();
        this.cb.aj.clear();
        this.an();
    }
    b2(a, b, c, d, e) {
        for (let f of fromEnum(Flattener.d(a, b, d, this.e.er))) {
            this.b5.aj.add({ $type: Point_$type, x: b(f), y: d(f) });
            this.ca.aj.add({ $type: Point_$type, x: b(f), y: d(f) });
        }
        for (let g of fromEnum(Flattener.d(a, c, e, this.e.er))) {
            this.b5.aj.add({ $type: Point_$type, x: c(g), y: e(g) });
            this.cb.aj.add({ $type: Point_$type, x: c(g), y: e(g) });
        }
        this.an();
    }
    a9() {
        super.a9();
        this.b3.aj = this.b5.aj;
        this.b6.aj = this.ca.aj;
        this.b7.aj = this.cb.aj;
        let a = this.bl();
        this.b3._fill = a;
        this.b3._opacity = 1;
        this.b6._stroke = a;
        this.b6.ac = this.e.ev + 3;
        this.b7._stroke = a;
        this.b7.ac = this.e.ev + 3;
    }
    a3(a, b) {
        super.a3(a, b);
        if (a.d) {
            if (b) {
                a.t(this.b3);
                a.u(this.b6);
                a.u(this.b7);
            }
            else {
                a.t(this.b5);
                a.u(this.ca);
                a.u(this.cb);
            }
        }
    }
    ai(a) {
        super.ai(a);
        this.e.a2.exportPolygonData(a, this.b5, "fill", ["Fill"]);
        this.e.a2.exportPolylineData(a, this.ca, "bottom", ["Lower", "Main"]);
        this.e.a2.exportPolylineData(a, this.cb, "top", ["Upper"]);
    }
}
PriceChannelOverlayView.$t = markType(PriceChannelOverlayView, 'PriceChannelOverlayView', FinancialSeriesView.$);
