/*
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 { __extends, __values } from "tslib";
import { FinancialOverlay } from "./FinancialOverlay";
import { DependencyProperty } from "igniteui-angular-core";
import { List$1 } from "igniteui-angular-core";
import { Number_$type, typeCast, runOn, markType, markDep, Point_$type, fromEnum } from "igniteui-angular-core";
import { CategoryFrame } from "./CategoryFrame";
import { Series } from "./Series";
import { MathUtil } from "igniteui-angular-core";
import { FinancialSeriesView } from "./FinancialSeriesView";
import { ScalerParams } from "./ScalerParams";
import { ISortingAxis_$type } from "./ISortingAxis";
import { FinancialBucketCalculator } from "./FinancialBucketCalculator";
import { Polyline } from "igniteui-angular-core";
import { Polygon } from "igniteui-angular-core";
import { RangeValueList } from "./RangeValueList";
import { PropertyMetadata } from "igniteui-angular-core";
import { truncate, isNaN_, isInfinity } from "igniteui-angular-core";
import { Flattener } from "igniteui-angular-core";
/**
 * @hidden
 */
var PriceChannelOverlay = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(PriceChannelOverlay, _super);
    function PriceChannelOverlay() {
        var _this = _super.call(this) || this;
        _this.t1 = new List$1(Number_$type, 0);
        _this.t0 = new List$1(Number_$type, 0);
        _this.ab = PriceChannelOverlay.$;
        _this.qo = new CategoryFrame(3);
        _this.qq = new CategoryFrame(3);
        _this.qm = new CategoryFrame(3);
        return _this;
    }
    PriceChannelOverlay.prototype.l9 = function (a, b, c, d) {
        switch (b) {
            case "HighColumn":
            case "LowColumn":
                this.tu = false;
                break;
        }
        _super.prototype.l9.call(this, a, b, c, d);
        switch (b) {
            case PriceChannelOverlay.$$p[0]:
                this.tu = false;
                this.mn(false);
                break;
        }
    };
    PriceChannelOverlay.prototype.fd = function (a, b, c) {
        var d = _super.prototype.fd.call(this, a, b, c);
        if (this.re == null || this.rf == null) {
            d = false;
        }
        return d;
    };
    PriceChannelOverlay.prototype.tv = function () {
        this.t1.clear();
        this.t0.clear();
        var a = truncate(MathUtil.d(this.t2, 0, this.cl.count));
        var b = Math.min(this.re.count, this.rf.count);
        var c = this.r4(this.re);
        var d = this.r4(this.rf);
        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.t1.add(g);
            this.t0.add(h);
        }
        return true;
    };
    PriceChannelOverlay.prototype.tb = function (a, b) {
        _super.prototype.tb.call(this, a, b);
        var c = b.bv;
        var d = b.bu;
        var e = this.getEffectiveViewport1(b);
        var f = new ScalerParams(0, c, d, this.qk.cg, e);
        var g = new ScalerParams(0, c, d, this.q6.cg, e);
        g.b = this.gg();
        var h = this.qk;
        var i = this.q6;
        a.f.clear();
        a.m.clear();
        a.p.clear();
        var j = 0;
        var k = typeCast(ISortingAxis_$type, this.qk);
        if (k != null && k.i$b.count != this.cl.count) {
            return;
        }
        j = this.sb(c, d, e);
        if (!this.tu) {
            this.tu = this.tv();
        }
        g.b = this.gg();
        var l = this.qk.el(2, f) - this.qk.el(1, f);
        for (var m = b.bx.c; m <= b.bx.d; ++m) {
            var n = void 0;
            if (k == null) {
                n = b.bx.getBucket(m);
            }
            else {
                var o = k.i$b._inner[m];
                var p = k.i$d(o);
                var q = this.t1._inner[m];
                var r = this.t0._inner[m];
                var s = q;
                var t = r;
                var u = p;
                while (m < b.bx.d) {
                    o = k.i$b._inner[m + 1];
                    u = k.i$d(o);
                    if (u - p > l) {
                        break;
                    }
                    m++;
                    s = Math.max(q, this.t1._inner[m]);
                    t = Math.min(r, this.t0._inner[m]);
                }
                if (!isInfinity(t) && !isInfinity(s)) {
                    var v = NaN;
                    if (!isNaN_(p)) {
                        v = this.qk.ei(p, f);
                    }
                    n = [v, t, s];
                }
                else {
                    n = [NaN, NaN, NaN];
                }
            }
            var w = Math.max(1, l);
            if (!isNaN_(n[0]) && m * w >= this.tw) {
                if (this.qk != null && typeCast(ISortingAxis_$type, this.qk) !== null) {
                    n[0] = n[0] + j;
                }
                else {
                    n[0] = h.ei(n[0], f) + j;
                }
                n[1] = i.ei(n[1], g);
                n[2] = i.ei(n[2], g);
                a.f.add(n);
            }
        }
    };
    PriceChannelOverlay.prototype.k2 = function (a, b) {
        _super.prototype.k2.call(this, a, b);
        var c = b;
        if (c != null) {
            c.b4();
        }
    };
    PriceChannelOverlay.prototype.td = function (a, b) {
        _super.prototype.td.call(this, a, b);
        var c = typeCast(PriceChannelOverlayView.$, b);
        if (c == null) {
            return;
        }
        var d = this.getEffectiveViewport1(b);
        if (b.b0(a)) {
            c.b4();
            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.b5(e_1, f, g, h, i);
            b.b2(a);
        }
        this.qt.a2(this, this.rs(), runOn(this, this.qc), this.sh(b), this.sj(b));
        var j = false;
        var k = this.qt.b;
        var l = a.f;
        var m = this.cl.count;
        if (k != null) {
            j = true;
        }
        if (j) {
            var n = new ScalerParams(0, b.bv, b.bu, this.qk.cg, d);
            this.ta(l, -1, m, this.qk, n, b.q);
        }
        var o = c.cb;
        var p = c.cc;
        var q = c.b7;
        this.qt.ad(o, true, false, true, true);
        this.qt.ad(p, true, false, true, true);
        this.qt.ad(q, false, true, false, false);
        q._opacity = this.qt.j * this.fu;
    };
    PriceChannelOverlay.prototype.by = function () {
        return new PriceChannelOverlayView(this);
    };
    PriceChannelOverlay.prototype.l4 = function (a) {
        _super.prototype.l4.call(this, a);
        this.tz = typeCast(PriceChannelOverlayView.$, a);
    };
    PriceChannelOverlay.prototype.gn = function (a, b, c) {
        if (this.ce == null) {
            return NaN;
        }
        var d = this.getEffectiveViewport1(this.b1);
        var e = new ScalerParams(0, this.ce.ta, this.b1.bu, this.qk.cg, d);
        var f = this.sb(this.ce.ta, this.b1.bu, d);
        return this.gp(new RangeValueList(this.t1, this.t0), a, this.qk, e, f, runOn(this, this.sa), b, c);
    };
    PriceChannelOverlay.prototype.hm = function (a, b) {
        if (this.t1 == null || this.t0 == null) {
            return _super.prototype.hm.call(this, a, b);
        }
        return this.hn(a, b, this.qk, runOn(this, this.sa), new RangeValueList(this.t1, this.t0));
    };
    PriceChannelOverlay.prototype.ho = function (a, b) {
        if (this.t1 == null || this.t0 == null) {
            return _super.prototype.ho.call(this, a, b);
        }
        return this.hp(a, b, this.qk, runOn(this, this.sa), new RangeValueList(this.t1, this.t0));
    };
    PriceChannelOverlay.prototype.gg = function () {
        return Series.gi(this.t0, null);
    };
    PriceChannelOverlay.$t = markType(PriceChannelOverlay, 'PriceChannelOverlay', FinancialOverlay.$);
    PriceChannelOverlay.$$p = markDep(DependencyProperty, PropertyMetadata, PriceChannelOverlay, 'raisePropertyChanged', ['Period:t2:t4', [1, 14]]);
    return PriceChannelOverlay;
}(FinancialOverlay));
export { PriceChannelOverlay };
/**
 * @hidden
 */
var PriceChannelBucketCalculator = /** @class */ /*@__PURE__*/ (function (_super) {
    __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.bz.cl.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.b3.t1._inner[g]) && !isNaN_(this.f.b3.t0._inner[g])) {
                    d = Math.max(d, this.f.b3.t1._inner[g]);
                    e = Math.min(e, this.f.b3.t0._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 */ /*@__PURE__*/ (function (_super) {
    __extends(PriceChannelOverlayView, _super);
    function PriceChannelOverlayView(a) {
        var _this = _super.call(this, a) || this;
        _this.b8 = new Polygon();
        _this.cd = new Polyline();
        _this.ce = new Polyline();
        _this.b3 = null;
        _this.b6 = new Polygon();
        _this.b9 = new Polyline();
        _this.ca = new Polyline();
        _this.b3 = a;
        return _this;
    }
    Object.defineProperty(PriceChannelOverlayView.prototype, "b7", {
        get: function () {
            return this.b8;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(PriceChannelOverlayView.prototype, "cb", {
        get: function () {
            return this.cd;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(PriceChannelOverlayView.prototype, "cc", {
        get: function () {
            return this.ce;
        },
        enumerable: false,
        configurable: true
    });
    PriceChannelOverlayView.prototype.as = function () {
        _super.prototype.as.call(this);
        if (!this.q) {
        }
    };
    PriceChannelOverlayView.prototype.by = function () {
        return new PriceChannelBucketCalculator(this);
    };
    PriceChannelOverlayView.prototype.b4 = function () {
        this.b8.an.clear();
        this.cd.an.clear();
        this.ce.an.clear();
        this.aq();
    };
    PriceChannelOverlayView.prototype.b5 = function (a, b, c, d, e) {
        var e_2, _a, e_3, _b;
        try {
            for (var _c = __values(fromEnum(Flattener.d(a, b, d, this.e.fx))), _d = _c.next(); !_d.done; _d = _c.next()) {
                var f = _d.value;
                this.b8.an.add({ $type: Point_$type, x: b(f), y: d(f) });
                this.cd.an.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 = __values(fromEnum(Flattener.d(a, c, e, this.e.fx))), _f = _e.next(); !_f.done; _f = _e.next()) {
                var g = _f.value;
                this.b8.an.add({ $type: Point_$type, x: c(g), y: e(g) });
                this.ce.an.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.aq();
    };
    PriceChannelOverlayView.prototype.bc = function () {
        _super.prototype.bc.call(this);
        this.b6.an = this.b8.an;
        this.b9.an = this.cd.an;
        this.ca.an = this.ce.an;
        var a = this.bo();
        this.b6._fill = a;
        this.b6._opacity = 1;
        this.b9._stroke = a;
        this.b9.ad = this.e.fy + 3;
        this.ca._stroke = a;
        this.ca.ad = this.e.fy + 3;
    };
    PriceChannelOverlayView.prototype.a6 = function (a, b) {
        _super.prototype.a6.call(this, a, b);
        if (a.d) {
            if (b) {
                a.u(this.b6);
                a.v(this.b9);
                a.v(this.ca);
            }
            else {
                a.u(this.b8);
                a.v(this.cd);
                a.v(this.ce);
            }
        }
    };
    PriceChannelOverlayView.prototype.al = function (a) {
        _super.prototype.al.call(this, a);
        this.e.a9.exportPolygonData(a, this.b8, "fill", ["Fill"]);
        this.e.a9.exportPolylineData(a, this.cd, "bottom", ["Lower", "Main"]);
        this.e.a9.exportPolylineData(a, this.ce, "top", ["Upper"]);
    };
    PriceChannelOverlayView.$t = markType(PriceChannelOverlayView, 'PriceChannelOverlayView', FinancialSeriesView.$);
    return PriceChannelOverlayView;
}(FinancialSeriesView));
export { PriceChannelOverlayView };
