/*
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 * as tslib_1 from "tslib";
import { FinancialOverlay } from "./FinancialOverlay";
import { DependencyProperty } from "igniteui-react-core/ES5/DependencyProperty";
import { List$1 } from "igniteui-react-core/ES5/List$1";
import { Type, Number_$type, typeCast, runOn, markType, Point_$type, fromEnum } from "igniteui-react-core/ES5/type";
import { CategoryFrame } from "./CategoryFrame";
import { MathUtil } from "igniteui-react-core/ES5/MathUtil";
import { FinancialSeriesView } from "./FinancialSeriesView";
import { ScalerParams } from "./ScalerParams";
import { ISortingAxis_$type } from "./ISortingAxis";
import { FinancialBucketCalculator } from "./FinancialBucketCalculator";
import { Polyline } from "igniteui-react-core/ES5/Polyline";
import { Polygon } from "igniteui-react-core/ES5/Polygon";
import { RangeValueList } from "./RangeValueList";
import { PropertyMetadata } from "igniteui-react-core/ES5/PropertyMetadata";
import { truncate, isNaN_, isInfinity } from "igniteui-react-core/ES5/number";
import { Flattener } from "igniteui-react-core/ES5/Flattener";
var PriceChannelOverlay = /** @class */ (function (_super) {
    tslib_1.__extends(PriceChannelOverlay, _super);
    function PriceChannelOverlay() {
        var _this = _super.call(this) || this;
        _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);
        return _this;
    }
    PriceChannelOverlay.prototype.io = function (a, b, c, d) {
        switch (b) {
            case "HighColumn":
            case "LowColumn":
                this.o6 = false;
                break;
        }
        _super.prototype.io.call(this, a, b, c, d);
        switch (b) {
            case PriceChannelOverlay.$$p[0]:
                this.o6 = false;
                this.i1(false);
                break;
        }
    };
    PriceChannelOverlay.prototype.dt = function (a, b, c) {
        var d = _super.prototype.dt.call(this, a, b, c);
        if (this.mk == null || this.mm == null) {
            d = false;
        }
        return d;
    };
    PriceChannelOverlay.prototype.o7 = function () {
        this.pd.clear();
        this.pc.clear();
        var a = truncate(MathUtil.b(this.pe, 0, this.bz.count));
        var b = Math.min(this.mk.count, this.mm.count);
        var c = this.ne(this.mk);
        var d = this.ne(this.mm);
        for (var e = 0; e < b; e++) {
            var f = Math.min(a, e);
            var g = -1.7976931348623157E+308;
            var h = 1.7976931348623157E+308;
            for (var 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;
    };
    PriceChannelOverlay.prototype.ol = function (a, b) {
        _super.prototype.ol.call(this, a, b);
        var c = b.bq;
        var d = b.bp;
        var e = this.getEffectiveViewport1(b);
        var f = new ScalerParams(0, c, d, this.lw.b8, e);
        var g = new ScalerParams(0, c, d, this.mg.b8, e);
        var h = this.lw;
        var i = this.mg;
        a.f.clear();
        a.m.clear();
        a.p.clear();
        var j = 0;
        var 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();
        var l = this.lw.dw(2, f) - this.lw.dw(1, f);
        for (var m = b.bs.c; m <= b.bs.d; ++m) {
            var n = void 0;
            if (k == null) {
                n = b.bs.getBucket(m);
            }
            else {
                var o = k.i$b._inner[m];
                var p = k.i$d(o);
                var q = this.pd._inner[m];
                var r = this.pc._inner[m];
                var s = q;
                var t = r;
                var 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)) {
                    var v = NaN;
                    if (!isNaN_(p)) {
                        v = this.lw.dt(p, f);
                    }
                    n = [v, t, s];
                }
                else {
                    n = [NaN, NaN, NaN];
                }
            }
            var 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);
            }
        }
    };
    PriceChannelOverlay.prototype.hl = function (a, b) {
        _super.prototype.hl.call(this, a, b);
        var c = b;
        if (c != null) {
            c.bz();
        }
    };
    PriceChannelOverlay.prototype.on = function (a, b) {
        _super.prototype.on.call(this, a, b);
        var c = typeCast(PriceChannelOverlayView.$, b);
        if (c == null) {
            return;
        }
        var d = this.getEffectiveViewport1(b);
        if (b.bv(a)) {
            c.bz();
            var e_1 = a.f.count;
            var f = function (g) { return a.f._inner[g][0]; };
            var g = function (h) { return a.f._inner[e_1 - 1 - h][0]; };
            var h = function (i) { return a.f._inner[i][1]; };
            var i = function (j) { return a.f._inner[e_1 - 1 - j][2]; };
            c.b0(e_1, 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));
        var j = false;
        var k = this.l4.c;
        var l = a.f;
        var m = this.bz.count;
        if (k != null) {
            j = true;
        }
        if (j) {
            var n = new ScalerParams(0, b.bq, b.bp, this.lw.b8, d);
            this.ok(l, -1, m, this.lw, n, b.n);
        }
        var o = c.b6;
        var p = c.b7;
        var 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;
    };
    PriceChannelOverlay.prototype.bf = function () {
        return new PriceChannelOverlayView(this);
    };
    PriceChannelOverlay.prototype.ii = function (a) {
        _super.prototype.ii.call(this, a);
        this.pb = typeCast(PriceChannelOverlayView.$, a);
    };
    PriceChannelOverlay.prototype.eh = function (a, b, c) {
        if (this.bs == null) {
            return NaN;
        }
        var d = this.getEffectiveViewport1(this.bi);
        var e = new ScalerParams(0, this.bs.mk, this.bi.bp, this.lw.b8, d);
        var 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);
    };
    PriceChannelOverlay.prototype.e6 = function (a, b) {
        if (this.pd == null || this.pc == null) {
            return _super.prototype.e6.call(this, a, b);
        }
        return this.e7(a, b, this.lw, runOn(this, this.nk), new RangeValueList(this.pd, this.pc));
    };
    PriceChannelOverlay.prototype.e8 = function (a, b) {
        if (this.pd == null || this.pc == null) {
            return _super.prototype.e8.call(this, a, b);
        }
        return this.e9(a, b, this.lw, runOn(this, this.nk), new RangeValueList(this.pd, this.pc));
    };
    PriceChannelOverlay.prototype.ec = function () {
        return this.pc == null || this.pc.count == 0 ? NaN : this.pc._inner[0];
    };
    PriceChannelOverlay.$t = markType(PriceChannelOverlay, 'PriceChannelOverlay', FinancialOverlay.$);
    return PriceChannelOverlay;
}(FinancialOverlay));
export { PriceChannelOverlay };
Type.dep(DependencyProperty, PropertyMetadata, PriceChannelOverlay, 'raisePropertyChanged', ['Period:pe:pg', [1, 14]]);
var PriceChannelBucketCalculator = /** @class */ (function (_super) {
    tslib_1.__extends(PriceChannelBucketCalculator, _super);
    function PriceChannelBucketCalculator(a) {
        var _this = _super.call(this, a) || this;
        _this.f = null;
        _this.f = a;
        return _this;
    }
    PriceChannelBucketCalculator.prototype.getBucket = function (a) {
        var b = a * this.b;
        var c = Math.min(b + this.b - 1, this.a.bu.bz.count - 1);
        if (b <= c) {
            var d = -1.7976931348623157E+308;
            var e = 1.7976931348623157E+308;
            var f = 0;
            for (var 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.$);
    return PriceChannelBucketCalculator;
}(FinancialBucketCalculator));
export { PriceChannelBucketCalculator };
var PriceChannelOverlayView = /** @class */ (function (_super) {
    tslib_1.__extends(PriceChannelOverlayView, _super);
    function PriceChannelOverlayView(a) {
        var _this = _super.call(this, a) || this;
        _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;
        return _this;
    }
    Object.defineProperty(PriceChannelOverlayView.prototype, "b2", {
        get: function () {
            return this.b3;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(PriceChannelOverlayView.prototype, "b6", {
        get: function () {
            return this.b8;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(PriceChannelOverlayView.prototype, "b7", {
        get: function () {
            return this.b9;
        },
        enumerable: true,
        configurable: true
    });
    PriceChannelOverlayView.prototype.ap = function () {
        _super.prototype.ap.call(this);
        if (!this.n) {
            this.e.bj = 1;
        }
    };
    PriceChannelOverlayView.prototype.bt = function () {
        return new PriceChannelBucketCalculator(this);
    };
    PriceChannelOverlayView.prototype.bz = function () {
        this.b3.aj.clear();
        this.b8.aj.clear();
        this.b9.aj.clear();
        this.an();
    };
    PriceChannelOverlayView.prototype.b0 = function (a, b, c, d, e) {
        try {
            for (var _a = tslib_1.__values(fromEnum(Flattener.d(a, b, d, this.e.eo))), _b = _a.next(); !_b.done; _b = _a.next()) {
                var f = _b.value;
                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) });
            }
        }
        catch (e_2_1) { e_2 = { error: e_2_1 }; }
        finally {
            try {
                if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
            }
            finally { if (e_2) throw e_2.error; }
        }
        try {
            for (var _d = tslib_1.__values(fromEnum(Flattener.d(a, c, e, this.e.eo))), _e = _d.next(); !_e.done; _e = _d.next()) {
                var g = _e.value;
                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) });
            }
        }
        catch (e_3_1) { e_3 = { error: e_3_1 }; }
        finally {
            try {
                if (_e && !_e.done && (_f = _d.return)) _f.call(_d);
            }
            finally { if (e_3) throw e_3.error; }
        }
        this.an();
        var e_2, _c, e_3, _f;
    };
    PriceChannelOverlayView.prototype.a9 = function () {
        _super.prototype.a9.call(this);
        this.b1.aj = this.b3.aj;
        this.b4.aj = this.b8.aj;
        this.b5.aj = this.b9.aj;
        var 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;
    };
    PriceChannelOverlayView.prototype.a3 = function (a, b) {
        _super.prototype.a3.call(this, 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);
            }
        }
    };
    PriceChannelOverlayView.prototype.ai = function (a) {
        _super.prototype.ai.call(this, 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.$);
    return PriceChannelOverlayView;
}(FinancialSeriesView));
export { PriceChannelOverlayView };
/*<BeginStatics Name="Infragistics.Controls.Charts.PriceChannelOverlay" />*/
/*<EndStatics Name="Infragistics.Controls.Charts.PriceChannelOverlay" />*/
