/*
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 { Series } from "./Series";
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";
/**
 * @hidden
 */
var PriceChannelOverlay = /** @class */ (function (_super) {
    tslib_1.__extends(PriceChannelOverlay, _super);
    function PriceChannelOverlay() {
        var _this = _super.call(this) || this;
        _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);
        return _this;
    }
    PriceChannelOverlay.prototype.is = function (a, b, c, d) {
        switch (b) {
            case "HighColumn":
            case "LowColumn":
                this.pb = false;
                break;
        }
        _super.prototype.is.call(this, a, b, c, d);
        switch (b) {
            case PriceChannelOverlay.$$p[0]:
                this.pb = false;
                this.i6(false);
                break;
        }
    };
    PriceChannelOverlay.prototype.dv = function (a, b, c) {
        var d = _super.prototype.dv.call(this, a, b, c);
        if (this.mr == null || this.mt == null) {
            d = false;
        }
        return d;
    };
    PriceChannelOverlay.prototype.pc = function () {
        this.pi.clear();
        this.ph.clear();
        var a = truncate(MathUtil.b(this.pj, 0, this.bz.count));
        var b = Math.min(this.mr.count, this.mt.count);
        var c = this.nl(this.mr);
        var d = this.nl(this.mt);
        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.pi.add(g);
            this.ph.add(h);
        }
        return true;
    };
    PriceChannelOverlay.prototype.os = function (a, b) {
        _super.prototype.os.call(this, a, b);
        var c = b.bs;
        var d = b.br;
        var e = this.getEffectiveViewport1(b);
        var f = new ScalerParams(0, c, d, this.l1.b8, e);
        var g = new ScalerParams(0, c, d, this.mm.b8, e);
        g.b = this.ee();
        var h = this.l1;
        var i = this.mm;
        a.f.clear();
        a.m.clear();
        a.p.clear();
        var j = 0;
        var 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();
        var l = this.l1.dw(2, f) - this.l1.dw(1, f);
        for (var m = b.bu.c; m <= b.bu.d; ++m) {
            var n = void 0;
            if (k == null) {
                n = b.bu.getBucket(m);
            }
            else {
                var o = k.i$b._inner[m];
                var p = k.i$d(o);
                var q = this.pi._inner[m];
                var r = this.ph._inner[m];
                var s = q;
                var t = r;
                var 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)) {
                    var v = NaN;
                    if (!isNaN_(p)) {
                        v = this.l1.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.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);
            }
        }
    };
    PriceChannelOverlay.prototype.hp = function (a, b) {
        _super.prototype.hp.call(this, a, b);
        var c = b;
        if (c != null) {
            c.b1();
        }
    };
    PriceChannelOverlay.prototype.ou = function (a, b) {
        _super.prototype.ou.call(this, a, b);
        var c = typeCast(PriceChannelOverlayView.$, b);
        if (c == null) {
            return;
        }
        var d = this.getEffectiveViewport1(b);
        if (b.bx(a)) {
            c.b1();
            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.b2(e_1, 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));
        var j = false;
        var k = this.l9.c;
        var l = a.f;
        var m = this.bz.count;
        if (k != null) {
            j = true;
        }
        if (j) {
            var n = new ScalerParams(0, b.bs, b.br, this.l1.b8, d);
            this.or(l, -1, m, this.l1, n, b.n);
        }
        var o = c.b8;
        var p = c.b9;
        var 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;
    };
    PriceChannelOverlay.prototype.bf = function () {
        return new PriceChannelOverlayView(this);
    };
    PriceChannelOverlay.prototype.im = function (a) {
        _super.prototype.im.call(this, a);
        this.pg = typeCast(PriceChannelOverlayView.$, a);
    };
    PriceChannelOverlay.prototype.ek = function (a, b, c) {
        if (this.bs == null) {
            return NaN;
        }
        var d = this.getEffectiveViewport1(this.bi);
        var e = new ScalerParams(0, this.bs.mq, this.bi.br, this.l1.b8, d);
        var 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);
    };
    PriceChannelOverlay.prototype.e9 = function (a, b) {
        if (this.pi == null || this.ph == null) {
            return _super.prototype.e9.call(this, a, b);
        }
        return this.fa(a, b, this.l1, runOn(this, this.nr), new RangeValueList(this.pi, this.ph));
    };
    PriceChannelOverlay.prototype.fb = function (a, b) {
        if (this.pi == null || this.ph == null) {
            return _super.prototype.fb.call(this, a, b);
        }
        return this.fc(a, b, this.l1, runOn(this, this.nr), new RangeValueList(this.pi, this.ph));
    };
    PriceChannelOverlay.prototype.ee = function () {
        return Series.ef(this.ph, null);
    };
    PriceChannelOverlay.$t = markType(PriceChannelOverlay, 'PriceChannelOverlay', FinancialOverlay.$);
    return PriceChannelOverlay;
}(FinancialOverlay));
export { PriceChannelOverlay };
Type.dep(DependencyProperty, PropertyMetadata, PriceChannelOverlay, 'raisePropertyChanged', ['Period:pj:pl', [1, 14]]);
/**
 * @hidden
 */
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.bw.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.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.$);
    return PriceChannelBucketCalculator;
}(FinancialBucketCalculator));
export { PriceChannelBucketCalculator };
/**
 * @hidden
 */
var PriceChannelOverlayView = /** @class */ (function (_super) {
    tslib_1.__extends(PriceChannelOverlayView, _super);
    function PriceChannelOverlayView(a) {
        var _this = _super.call(this, a) || this;
        _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;
        return _this;
    }
    Object.defineProperty(PriceChannelOverlayView.prototype, "b4", {
        get: function () {
            return this.b5;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(PriceChannelOverlayView.prototype, "b8", {
        get: function () {
            return this.ca;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(PriceChannelOverlayView.prototype, "b9", {
        get: function () {
            return this.cb;
        },
        enumerable: true,
        configurable: true
    });
    PriceChannelOverlayView.prototype.ap = function () {
        _super.prototype.ap.call(this);
        if (!this.n) {
            this.e.bj = 1;
        }
    };
    PriceChannelOverlayView.prototype.bv = function () {
        return new PriceChannelBucketCalculator(this);
    };
    PriceChannelOverlayView.prototype.b1 = function () {
        this.b5.aj.clear();
        this.ca.aj.clear();
        this.cb.aj.clear();
        this.an();
    };
    PriceChannelOverlayView.prototype.b2 = function (a, b, c, d, e) {
        var e_2, _a, e_3, _b;
        try {
            for (var _c = tslib_1.__values(fromEnum(Flattener.d(a, b, d, this.e.er))), _d = _c.next(); !_d.done; _d = _c.next()) {
                var f = _d.value;
                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) });
            }
        }
        catch (e_2_1) { e_2 = { error: e_2_1 }; }
        finally {
            try {
                if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
            }
            finally { if (e_2) throw e_2.error; }
        }
        try {
            for (var _e = tslib_1.__values(fromEnum(Flattener.d(a, c, e, this.e.er))), _f = _e.next(); !_f.done; _f = _e.next()) {
                var g = _f.value;
                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) });
            }
        }
        catch (e_3_1) { e_3 = { error: e_3_1 }; }
        finally {
            try {
                if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
            }
            finally { if (e_3) throw e_3.error; }
        }
        this.an();
    };
    PriceChannelOverlayView.prototype.a9 = function () {
        _super.prototype.a9.call(this);
        this.b3.aj = this.b5.aj;
        this.b6.aj = this.ca.aj;
        this.b7.aj = this.cb.aj;
        var 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;
    };
    PriceChannelOverlayView.prototype.a3 = function (a, b) {
        _super.prototype.a3.call(this, 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);
            }
        }
    };
    PriceChannelOverlayView.prototype.ai = function (a) {
        _super.prototype.ai.call(this, 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.$);
    return PriceChannelOverlayView;
}(FinancialSeriesView));
export { PriceChannelOverlayView };
