/*
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.tx = new List$1(Number_$type, 0);
        _this.tw = new List$1(Number_$type, 0);
        _this.ab = PriceChannelOverlay.$;
        _this.qk = new CategoryFrame(3);
        _this.qm = new CategoryFrame(3);
        _this.qi = new CategoryFrame(3);
        return _this;
    }
    PriceChannelOverlay.prototype.l7 = function (a, b, c, d) {
        switch (b) {
            case "HighColumn":
            case "LowColumn":
                this.tq = false;
                break;
        }
        _super.prototype.l7.call(this, a, b, c, d);
        switch (b) {
            case PriceChannelOverlay.$$p[0]:
                this.tq = false;
                this.ml(false);
                break;
        }
    };
    PriceChannelOverlay.prototype.fb = function (a, b, c) {
        var d = _super.prototype.fb.call(this, a, b, c);
        if (this.ra == null || this.rb == null) {
            d = false;
        }
        return d;
    };
    PriceChannelOverlay.prototype.tr = function () {
        this.tx.clear();
        this.tw.clear();
        var a = truncate(MathUtil.d(this.ty, 0, this.ck.count));
        var b = Math.min(this.ra.count, this.rb.count);
        var c = this.r0(this.ra);
        var d = this.r0(this.rb);
        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.tx.add(g);
            this.tw.add(h);
        }
        return true;
    };
    PriceChannelOverlay.prototype.s7 = function (a, b) {
        _super.prototype.s7.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.qg.cg, e);
        var g = new ScalerParams(0, c, d, this.q2.cg, e);
        g.b = this.ge();
        var h = this.qg;
        var i = this.q2;
        a.f.clear();
        a.m.clear();
        a.p.clear();
        var j = 0;
        var k = typeCast(ISortingAxis_$type, this.qg);
        if (k != null && k.i$b.count != this.ck.count) {
            return;
        }
        j = this.r7(c, d, e);
        if (!this.tq) {
            this.tq = this.tr();
        }
        g.b = this.ge();
        var l = this.qg.el(2, f) - this.qg.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.tx._inner[m];
                var r = this.tw._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.tx._inner[m]);
                    t = Math.min(r, this.tw._inner[m]);
                }
                if (!isInfinity(t) && !isInfinity(s)) {
                    var v = NaN;
                    if (!isNaN_(p)) {
                        v = this.qg.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.ts) {
                if (this.qg != null && typeCast(ISortingAxis_$type, this.qg) !== 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.k0 = function (a, b) {
        _super.prototype.k0.call(this, a, b);
        var c = b;
        if (c != null) {
            c.b4();
        }
    };
    PriceChannelOverlay.prototype.s9 = function (a, b) {
        _super.prototype.s9.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.qp.a0(this, this.ro(), runOn(this, this.p8), this.sd(b), this.sf(b));
        var j = false;
        var k = this.qp.b;
        var l = a.f;
        var m = this.ck.count;
        if (k != null) {
            j = true;
        }
        if (j) {
            var n = new ScalerParams(0, b.bv, b.bu, this.qg.cg, d);
            this.s6(l, -1, m, this.qg, n, b.q);
        }
        var o = c.cb;
        var p = c.cc;
        var q = c.b7;
        this.qp.ad(o, true, false, true, true);
        this.qp.ad(p, true, false, true, true);
        this.qp.ad(q, false, true, false, false);
        q._opacity = this.qp.j * this.fs;
    };
    PriceChannelOverlay.prototype.bx = function () {
        return new PriceChannelOverlayView(this);
    };
    PriceChannelOverlay.prototype.l2 = function (a) {
        _super.prototype.l2.call(this, a);
        this.tv = typeCast(PriceChannelOverlayView.$, a);
    };
    PriceChannelOverlay.prototype.gl = function (a, b, c) {
        if (this.cd == null) {
            return NaN;
        }
        var d = this.getEffectiveViewport1(this.b0);
        var e = new ScalerParams(0, this.cd.s9, this.b0.bu, this.qg.cg, d);
        var f = this.r7(this.cd.s9, this.b0.bu, d);
        return this.gn(new RangeValueList(this.tx, this.tw), a, this.qg, e, f, runOn(this, this.r6), b, c);
    };
    PriceChannelOverlay.prototype.hj = function (a, b) {
        if (this.tx == null || this.tw == null) {
            return _super.prototype.hj.call(this, a, b);
        }
        return this.hk(a, b, this.qg, runOn(this, this.r6), new RangeValueList(this.tx, this.tw));
    };
    PriceChannelOverlay.prototype.hl = function (a, b) {
        if (this.tx == null || this.tw == null) {
            return _super.prototype.hl.call(this, a, b);
        }
        return this.hm(a, b, this.qg, runOn(this, this.r6), new RangeValueList(this.tx, this.tw));
    };
    PriceChannelOverlay.prototype.ge = function () {
        return Series.gg(this.tw, null);
    };
    PriceChannelOverlay.$t = markType(PriceChannelOverlay, 'PriceChannelOverlay', FinancialOverlay.$);
    PriceChannelOverlay.$$p = markDep(DependencyProperty, PropertyMetadata, PriceChannelOverlay, 'raisePropertyChanged', ['Period:ty:t0', [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.ck.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.tx._inner[g]) && !isNaN_(this.f.b3.tw._inner[g])) {
                    d = Math.max(d, this.f.b3.tx._inner[g]);
                    e = Math.min(e, this.f.b3.tw._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.ak.clear();
        this.cd.ak.clear();
        this.ce.ak.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.fv))), _d = _c.next(); !_d.done; _d = _c.next()) {
                var f = _d.value;
                this.b8.ak.add({ $type: Point_$type, x: b(f), y: d(f) });
                this.cd.ak.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.fv))), _f = _e.next(); !_f.done; _f = _e.next()) {
                var g = _f.value;
                this.b8.ak.add({ $type: Point_$type, x: c(g), y: e(g) });
                this.ce.ak.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.ak = this.b8.ak;
        this.b9.ak = this.cd.ak;
        this.ca.ak = this.ce.ak;
        var a = this.bo();
        this.b6._fill = a;
        this.b6._opacity = 1;
        this.b9._stroke = a;
        this.b9.ac = this.e.fw + 3;
        this.ca._stroke = a;
        this.ca.ac = this.e.fw + 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.a8.exportPolygonData(a, this.b8, "fill", ["Fill"]);
        this.e.a8.exportPolylineData(a, this.cd, "bottom", ["Lower", "Main"]);
        this.e.a8.exportPolylineData(a, this.ce, "top", ["Upper"]);
    };
    PriceChannelOverlayView.$t = markType(PriceChannelOverlayView, 'PriceChannelOverlayView', FinancialSeriesView.$);
    return PriceChannelOverlayView;
}(FinancialSeriesView));
export { PriceChannelOverlayView };
