/*
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 } from "tslib";
import { FastItemDateTimeColumn } from "igniteui-angular-core";
import { CategoryXAxis } from "./CategoryXAxis";
import { ITimeRangeSelectorAxis_$type } from "./ITimeRangeSelectorAxis";
import { IOrdinalAxis_$type } from "./IOrdinalAxis";
import { TimeAxisLabellingMode_$type } from "./TimeAxisLabellingMode";
import { TimeAxisLabelFormatCollection } from "./TimeAxisLabelFormatCollection";
import { Rect } from "igniteui-angular-core";
import { ScalerParams } from "./ScalerParams";
import { NumericScaler } from "./NumericScaler";
import { EventArgs, runOn, delegateCombine, typeCast, enumGetBox, Date_$type, markType } from "igniteui-angular-core";
import { IFastItemsSource_$type } from "igniteui-angular-core";
import { TimeAxisBase } from "./TimeAxisBase";
import { TimeXAxis } from "./TimeXAxis";
import { AxisRangeChangedEventArgs } from "./AxisRangeChangedEventArgs";
import { truncate } from "igniteui-angular-core";
import { dateMaxValue, dateMinValue, dateAdd, dateSubtract } from "igniteui-angular-core";
import { timeSpanFromMilliseconds, timeSpanTotalMilliseconds } from "igniteui-angular-core";
/**
 * @hidden
 */
var OrdinalTimeXAxis = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(OrdinalTimeXAxis, _super);
    function OrdinalTimeXAxis() {
        var _this = _super.call(this) || this;
        _this.no = dateMaxValue();
        _this.nn = dateMinValue();
        _this.nu = dateMaxValue();
        _this.nt = dateMinValue();
        _this.nq = dateMaxValue();
        _this.np = dateMinValue();
        _this.n4 = null;
        _this.n3 = null;
        _this.nj = 0;
        _this.nl = null;
        _this.nh = null;
        _this.od = 0;
        _this.ns = dateMinValue();
        _this.nr = dateMaxValue();
        _this.visibleRangeChanged = null;
        FastItemDateTimeColumn.o();
        _this.nh = new TimeAxisLabelFormatCollection();
        var a = _this.ni;
        a.collectionChanged = delegateCombine(a.collectionChanged, runOn(_this, _this.of));
        _this.mv = 0;
        return _this;
    }
    OrdinalTimeXAxis.prototype.ek = function (a, b, c) {
        return this.ei(a, new ScalerParams(0, b, c, this.cg, this.j7()));
    };
    OrdinalTimeXAxis.prototype.ej = function (a, b, c) {
        return this.ei(a, b);
    };
    OrdinalTimeXAxis.prototype.ei = function (a, b) {
        if (!b.c.isEmpty) {
            var c = this.n1(a, NumericScaler.ac, b.c);
            c = (c - b.d.left) / b.d.width;
            c = b.d.left + b.d.width * (c - b.e.left) / b.e.width;
            return c;
        }
        return this.n1(a, b.e, b.d);
    };
    OrdinalTimeXAxis.prototype.n1 = function (a, b, c) {
        var d = this.mu;
        var e = this.mv;
        var f = this.mu - this.mv;
        var g = f >= 1 ? (a - e) / (d - e) : f == 0 ? 0.5 : NaN;
        if (this.ch) {
            g = 1 - g;
        }
        return c.left + c.width * (g - b.left) / b.width;
    };
    OrdinalTimeXAxis.prototype.hl = function (a, b, c, d, e) {
        var f;
        var g = e.e;
        var h = e.d;
        var i = e.c;
        var j = e.a;
        var k = !i.isEmpty;
        var l = i.left;
        var m = i.width;
        var n = g.left;
        var o = g.width;
        var p = h.left;
        var q = h.width;
        var r = 0;
        var s = 1;
        var t = this.mu;
        var u = this.mv;
        var v = this.mu - this.mv;
        var w = this.mf(a);
        var x = w != null;
        for (var y = 0; y < b.count; y++) {
            var z = b.item(y);
            for (var aa = c; aa < d; aa++) {
                f = x ? w[aa] : a.item(aa);
                if (k) {
                    var ab = v >= 1 ? (f[z] - u) / (t - u) : v == 0 ? 0.5 : NaN;
                    if (j) {
                        ab = 1 - ab;
                    }
                    ab = l + m * (ab - r) / s;
                    var ac = (ab - p) / q;
                    ac = p + q * (ac - n) / o;
                    f[z] = ac;
                }
                else {
                    var ad = v >= 1 ? (f[z] - u) / (t - u) : v == 0 ? 0.5 : NaN;
                    if (j) {
                        ad = 1 - ad;
                    }
                    ad = p + q * (ad - n) / o;
                    f[z] = ad;
                }
            }
        }
    };
    OrdinalTimeXAxis.prototype.hm = function (a, b, c, d) {
        for (var e = b; e < c; e++) {
            a.item(e, this.ei(a.item(e), d));
        }
    };
    OrdinalTimeXAxis.prototype.en = function (a, b, c) {
        return this.el(a, new ScalerParams(0, b, this.kc, this.cg, this.j7()));
    };
    OrdinalTimeXAxis.prototype.getUnscaledValue3 = function (a, b, c, d) {
        return this.el(a, new ScalerParams(0, b, this.kc, this.cg, this.j7()));
    };
    OrdinalTimeXAxis.prototype.el = function (a, b) {
        if (!b.c.isEmpty) {
            var c = b.e.left + b.e.width * (a - b.d.left) / b.d.width;
            c = (c * b.d.width) + b.d.left;
            c = this.n2(c, NumericScaler.ac, b.c);
            return c;
        }
        return this.n2(a, b.e, b.d);
    };
    OrdinalTimeXAxis.prototype.n2 = function (a, b, c) {
        var d = b.left + (a - c.left) * b.width / c.width;
        if (this.ch) {
            d = 1 - d;
        }
        return this.mv + d * (this.mu - this.mv);
    };
    OrdinalTimeXAxis.prototype.em = function (a, b, c) {
        return this.el(a, b);
    };
    OrdinalTimeXAxis.prototype.hn = function (a, b, c, d) {
        for (var e = b; e < c; e++) {
            a.item(e, this.el(a.item(e), d));
        }
    };
    OrdinalTimeXAxis.prototype.hv = function (a, b, c, d) {
        _super.prototype.hv.call(this, a, b, c, d);
        switch (b) {
            case "FastItemsSource":
                var e = typeCast(IFastItemsSource_$type, c);
                if (e != null) {
                    e.deregisterColumn(this.nm);
                    this.nm = null;
                }
                if (this.kq != null) {
                    this.nm = TimeAxisBase.mi(this.dateTimeMemberPath, this);
                }
                break;
            case "DateTimeMemberPath":
                if (this.kq != null) {
                    this.kq.deregisterColumn(this.nm);
                    this.nm = TimeAxisBase.mi(this.dateTimeMemberPath, this);
                }
                break;
            case "DateTimeColumn":
                this.c0();
                this.od = this.oc();
                break;
            case "MinimumValue":
            case "MaximumValue":
                this.c0();
                this.h6();
                break;
            case "LabellingMode":
                this.h6();
                break;
        }
    };
    Object.defineProperty(OrdinalTimeXAxis.prototype, "actualMinimumValue", {
        get: function () {
            return this.no;
        },
        set: function (a) {
            this.no = a;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(OrdinalTimeXAxis.prototype, "actualMaximumValue", {
        get: function () {
            return this.nn;
        },
        set: function (a) {
            this.nn = a;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(OrdinalTimeXAxis.prototype, "visibleMinimum", {
        get: function () {
            return this.nu;
        },
        set: function (a) {
            this.nu = a;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(OrdinalTimeXAxis.prototype, "visibleMaximum", {
        get: function () {
            return this.nt;
        },
        set: function (a) {
            this.nt = a;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(OrdinalTimeXAxis.prototype, "ny", {
        get: function () {
            return this.nq;
        },
        set: function (a) {
            this.nq = a;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(OrdinalTimeXAxis.prototype, "nx", {
        get: function () {
            return this.np;
        },
        set: function (a) {
            this.np = a;
        },
        enumerable: false,
        configurable: true
    });
    OrdinalTimeXAxis.prototype.og = function () {
        if (this.nm == null || this.nm.count == 0 || this.nm.count < this.mv - 1) {
            this.actualMinimumValue = dateMinValue();
            this.actualMaximumValue = dateMaxValue();
            return;
        }
        var a = Math.max(0, Math.min(this.mv, this.nm.count - 1));
        var b = Math.max(0, Math.min(this.mu, this.nm.count - 1));
        var c = +(this.actualMinimumValue) != +(this.nm.item(a)) || +(this.actualMaximumValue) != +(this.nm.item(b));
        if (c) {
            var d = this.actualMinimumValue;
            var e = this.actualMaximumValue;
            this.actualMinimumValue = this.nm.item(a);
            this.actualMaximumValue = this.nm.item(b);
            this.oh();
        }
    };
    OrdinalTimeXAxis.prototype.oh = function () {
        if (this.nm == null || this.nm.count == 0 || this.bh == null) {
            var a = +(this.visibleMinimum) != +(this.actualMinimumValue) || +(this.visibleMaximum) != +(this.actualMaximumValue);
            this.visibleMinimum = this.actualMinimumValue;
            this.visibleMaximum = this.actualMaximumValue;
            this.ny = this.visibleMinimum;
            this.nx = this.visibleMaximum;
            if (a && this.visibleRangeChanged != null) {
                this.visibleRangeChanged(this, EventArgs.empty);
            }
            return;
        }
        var b = this.j6();
        var c = new ScalerParams(1, this.bh.tp, this.bh.to, this.cg);
        c.c = this.j7();
        var d = this.el(b.left, c);
        var e = this.el(b.right, c);
        var f = truncate(Math.floor(d));
        var g = truncate(Math.ceil(e));
        var h = f >= 0 && f < this.nm.count ? this.nm.item(f) : this.actualMinimumValue;
        var i = g >= 0 && g < this.nm.count ? this.nm.item(g) : this.actualMaximumValue;
        var j = h;
        var k = i;
        if (d > f) {
            var l = f + 1;
            var m = l >= 0 && l < this.nm.count ? this.nm.item(l) : i;
            var n = dateAdd(+h, +(timeSpanFromMilliseconds(timeSpanTotalMilliseconds((+m - +h)) * (d - f))));
            j = n;
        }
        if (e < g) {
            var o = g - 1;
            var p = o >= 0 && o < this.nm.count ? this.nm.item(o) : h;
            var q = dateSubtract(+i, +(timeSpanFromMilliseconds(timeSpanTotalMilliseconds((+i - +p)) * (g - e))));
            k = q;
        }
        this.ny = j;
        this.nx = k;
        var r = +(this.visibleMinimum) != +h || +(this.visibleMaximum) != +i;
        this.visibleMinimum = h;
        this.visibleMaximum = i;
        if (r && this.visibleRangeChanged != null) {
            this.visibleRangeChanged(this, EventArgs.empty);
        }
    };
    OrdinalTimeXAxis.prototype.h8 = function (a) {
        var b = TimeXAxis.ol(this.actualMinimumValue, false, this.actualMinimumValue, this.nk, this.ob, this.ni, this.fj);
        if (b != this.n4 && this.c3) {
            this.ic();
        }
        this.n4 = b;
        _super.prototype.h8.call(this, a);
    };
    Object.defineProperty(OrdinalTimeXAxis.prototype, "dateTimeMemberPath", {
        get: function () {
            return this.n3;
        },
        set: function (a) {
            var b = this.dateTimeMemberPath;
            if (a != b) {
                this.n3 = a;
                this.h1("DateTimeMemberPath", b, this.dateTimeMemberPath);
            }
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(OrdinalTimeXAxis.prototype, "nk", {
        get: function () {
            return this.nj;
        },
        set: function (a) {
            var b = this.nk;
            if (a != b) {
                this.nj = a;
                this.h1("LabellingMode", enumGetBox(TimeAxisLabellingMode_$type, b), enumGetBox(TimeAxisLabellingMode_$type, this.nk));
            }
        },
        enumerable: false,
        configurable: true
    });
    OrdinalTimeXAxis.prototype.get_cj = function () {
        return true;
    };
    Object.defineProperty(OrdinalTimeXAxis.prototype, "nm", {
        get: function () {
            return this.nl;
        },
        set: function (a) {
            if (this.nl != a) {
                var b = this.nl;
                this.nl = a;
                this.h1("DateTimeColumn", b, this.nl);
            }
        },
        enumerable: false,
        configurable: true
    });
    OrdinalTimeXAxis.prototype.ip = function (a, b) {
        _super.prototype.ip.call(this, a, b);
        this.oh();
    };
    OrdinalTimeXAxis.prototype.getWindowRectForSelection = function (a, b) {
        if (this.nm == null) {
            return Rect.empty;
        }
        var c = a > b;
        var d = c ? b : a;
        var e = c ? a : b;
        var f = -1;
        while (++f < this.nm.count && this.nm.item(f) < d) {
        }
        var g = this.nm.count;
        while (--g >= 0 && this.nm.item(g) > e) {
        }
        var h = new ScalerParams(0, new Rect(0, 0, 0, 1, 1), new Rect(0, 0, 0, 1, 1), this.cg, this.bh.td);
        var i = this.ei(f, h);
        var j = this.ei(g + 1, h);
        return new Rect(0, i, NaN, Math.max(0, j - i), NaN);
    };
    Object.defineProperty(OrdinalTimeXAxis.prototype, "ni", {
        get: function () {
            return this.nh;
        },
        enumerable: false,
        configurable: true
    });
    OrdinalTimeXAxis.prototype.of = function (a, b) {
        this.h7(false);
    };
    OrdinalTimeXAxis.prototype.hx = function (a) {
        _super.prototype.hx.call(this, a);
        this.dateTimeMemberPath = a;
    };
    OrdinalTimeXAxis.prototype.fy = function (a) {
        if (this.at != null) {
            return this.at(a);
        }
        if (this.ah != null) {
            return this.ah.i(a);
        }
        if (!(typeCast(Date_$type, a) !== null)) {
            return _super.prototype.fy.call(this, a);
        }
        var b = dateMinValue();
        var c = false;
        if (this.mw != null) {
            if (typeCast(Date_$type, this.mw) !== null) {
                b = this.mw;
                c = true;
            }
        }
        var d = TimeXAxis.og(a, c, b, this.nk, this.ob, this.ni, this.fj);
        return d == null ? _super.prototype.fy.call(this, a) : d;
    };
    OrdinalTimeXAxis.prototype.gc = function (a) {
        var b = 0;
        if (this.categoryMode == 0) {
            b = truncate(Math.round(a));
        }
        else {
            b = truncate(Math.floor(a));
        }
        if (b < 0 || b > this.lf - 1) {
            return _super.prototype.gc.call(this, a);
        }
        var c = this.nm.item(b);
        var d = this.fy(c);
        if (d == null) {
            return null;
        }
        return d.toString();
    };
    OrdinalTimeXAxis.prototype.getDateTime = function (a) {
        var b = truncate(Math.round(a));
        if (b < 0) {
            b = 0;
        }
        if (b > this.lf - 1) {
            b = this.lf - 1;
        }
        if (this.nm == null) {
            return dateMinValue();
        }
        var c = this.nm.item(b);
        return c;
    };
    Object.defineProperty(OrdinalTimeXAxis.prototype, "oe", {
        get: function () {
            return +(this.visibleMaximum) - +(this.visibleMinimum);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(OrdinalTimeXAxis.prototype, "ob", {
        get: function () {
            return +(this.nx) - +(this.ny);
        },
        enumerable: false,
        configurable: true
    });
    OrdinalTimeXAxis.prototype.mh = function () {
        return this.nm;
    };
    OrdinalTimeXAxis.prototype.io = function (a, b) {
        _super.prototype.io.call(this, a, b);
        this.oh();
    };
    OrdinalTimeXAxis.prototype.mo = function (a, b) {
        var c = this.categoryMode == 0 ? 0 : 0.5;
        return _super.prototype.mo.call(this, a + c, b);
    };
    OrdinalTimeXAxis.prototype.mn = function (a, b, c, d) {
        return b;
    };
    OrdinalTimeXAxis.prototype.oc = function () {
        var _this = this;
        if (this.nm == null || this.nm.count < 2) {
            return (0);
        }
        return TimeXAxis.or(function (a) { return _this.nm.item(a); }, this.nm.count);
    };
    OrdinalTimeXAxis.prototype.itemLabelFormat = function () {
        return TimeXAxis.on(this.od);
    };
    Object.defineProperty(OrdinalTimeXAxis.prototype, "minimumValue", {
        get: function () {
            return this.ns;
        },
        set: function (a) {
            var b = this.minimumValue;
            if (+a != +b) {
                this.ns = a;
                this.hv(this, "MinimumValue", b, this.minimumValue);
            }
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(OrdinalTimeXAxis.prototype, "maximumValue", {
        get: function () {
            return this.nr;
        },
        set: function (a) {
            var b = this.maximumValue;
            if (+a != +b) {
                this.nr = a;
                this.hv(this, "MaximumValue", b, this.maximumValue);
            }
        },
        enumerable: false,
        configurable: true
    });
    OrdinalTimeXAxis.prototype.c2 = function () {
        if (+(this.minimumValue) == +(dateMaxValue()) || +(this.maximumValue) == +(dateMinValue())) {
            return _super.prototype.c2.call(this);
        }
        if (this.cp && this.c3) {
            this.bb = this.ba();
        }
        if (this.kq == null || this.nm == null) {
            return false;
        }
        var a = this.mv;
        if (+(this.minimumValue) != +(dateMinValue())) {
            var b = -1;
            while (b++ <= this.nm.count) {
                if (this.nm.item(b) >= this.minimumValue) {
                    break;
                }
            }
            a = b;
        }
        var c = this.categoryMode == 0 ? this.kq.count - 1 : this.kq.count;
        if (+(this.maximumValue) != +(dateMaxValue())) {
            var d = this.kq.count;
            while (d-- > 0) {
                if (this.nm.item(d) <= this.maximumValue) {
                    break;
                }
            }
            c = d;
        }
        if (a != this.mv || c != this.mu) {
            var e = new AxisRangeChangedEventArgs(0, this.mv, a, this.mu, c);
            this.mv = a;
            this.mu = c;
            this.h2(e);
            this.og();
            this.ls();
            return true;
        }
        return false;
    };
    OrdinalTimeXAxis.prototype.l1 = function (a) {
        _super.prototype.l1.call(this, a);
        this.c1(true);
    };
    Object.defineProperty(OrdinalTimeXAxis.prototype, "isVertical", {
        get: function () {
            return this.co;
        },
        enumerable: false,
        configurable: true
    });
    OrdinalTimeXAxis.prototype.j$c = function (a, b) {
        return this.el.apply(this, arguments);
    };
    OrdinalTimeXAxis.prototype.j$b = function (a, b) {
        return this.ei.apply(this, arguments);
    };
    OrdinalTimeXAxis.prototype.j$d = function (a, b, c, d, e) {
        this.hl.apply(this, arguments);
    };
    OrdinalTimeXAxis.prototype.j$e = function (a, b, c, d) {
        this.hm.apply(this, arguments);
    };
    OrdinalTimeXAxis.prototype.j$f = function (a, b, c, d) {
        this.hn.apply(this, arguments);
    };
    Object.defineProperty(OrdinalTimeXAxis.prototype, "j$a", {
        get: function () {
            return this.cg;
        },
        enumerable: false,
        configurable: true
    });
    OrdinalTimeXAxis.$t = markType(OrdinalTimeXAxis, 'OrdinalTimeXAxis', CategoryXAxis.$, [ITimeRangeSelectorAxis_$type, IOrdinalAxis_$type]);
    return OrdinalTimeXAxis;
}(CategoryXAxis));
export { OrdinalTimeXAxis };
