/*
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 { TimeAxisBase } from "./TimeAxisBase";
import { ITimeRangeSelectorAxis_$type } from "./ITimeRangeSelectorAxis";
import { List$1 } from "igniteui-react-core";
import { TimeAxisBreak } from "./TimeAxisBreak";
import { TimeAxisBreakCollection } from "./TimeAxisBreakCollection";
import { TimeAxisLabellingMode_$type } from "./TimeAxisLabellingMode";
import { TimeAxisLabelFormatCollection } from "./TimeAxisLabelFormatCollection";
import { OrderedDictionary$2 } from "./OrderedDictionary$2";
import { TimeAxisInterval } from "./TimeAxisInterval";
import { TimeAxisIntervalCollection } from "./TimeAxisIntervalCollection";
import { EventArgs, runOn, delegateCombine, Number_$type, fromEnum, enumGetBox, fromEn, typeCast, Date_$type, markType } from "igniteui-react-core";
import { ScalerParams } from "./ScalerParams";
import { Rect } from "igniteui-react-core";
import { NumericScaler } from "./NumericScaler";
import { KeyValuePair$2 } from "igniteui-react-core";
import { NumericYAxis } from "./NumericYAxis";
import { LabelPosition } from "./LabelPosition";
import { Extensions } from "igniteui-react-core";
import { SortedListView$1 } from "./SortedListView$1";
import { Dictionary$2 } from "igniteui-react-core";
import { truncate, intDivide, isNaN_ } from "igniteui-react-core";
import { dateFromTicks, dateMaxValue, dateSubtract, dateMinValue, dateGetMonth, dateAddMonths, dateFromValues, dateGetTimeOfDay, dateAddDays, dateAddHours } from "igniteui-react-core";
import { timeSpanTicks, timeSpanInit3, timeSpanInit1, timeSpanFromMilliseconds, timeSpanFromSeconds, timeSpanFromMinutes, timeSpanFromHours, timeSpanFromDays, timeSpanFromTicks, timeSpanTotalMilliseconds } from "igniteui-react-core";
import { dateToStringFormat } from "igniteui-react-core";
/**
 * @hidden
 */
var TimeXAxis = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(TimeXAxis, _super);
    function TimeXAxis() {
        var _this = _super.call(this) || this;
        _this.oi = 0;
        _this.ns = null;
        _this.n1 = 0;
        _this.nz = null;
        _this._omission = 0;
        _this.nq = null;
        _this.nx = null;
        _this.om = null;
        _this.oz = 0;
        _this.oa = dateMaxValue();
        _this.n8 = dateMaxValue();
        _this.n7 = dateMaxValue();
        _this.n9 = dateMinValue();
        _this.nu = null;
        _this.ox = 0;
        _this.visibleRangeChanged = null;
        _this.ab = TimeXAxis.$;
        _this.omission = (0);
        _this.oi = -2147483648;
        _this.ns = new TimeAxisBreakCollection();
        var a = _this.breaks;
        a.collectionChanged = delegateCombine(a.collectionChanged, runOn(_this, _this.o2));
        _this.nx = new TimeAxisIntervalCollection();
        var b = _this.intervals;
        b.collectionChanged = delegateCombine(b.collectionChanged, runOn(_this, _this.o3));
        _this.nz = new TimeAxisLabelFormatCollection();
        var c = _this.labelFormats;
        c.collectionChanged = delegateCombine(c.collectionChanged, runOn(_this, _this.o4));
        _this.oz = (0);
        return _this;
    }
    TimeXAxis.prototype.get_cl = function () {
        return this.breaks != null && this.breaks.count > 0;
    };
    TimeXAxis.prototype.ek = function (a, b) {
        if (!b.c.isEmpty) {
            var c = this.og(a, NumericScaler.ac, b.c, Rect.empty);
            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.og(a, b.e, b.d, b.c);
    };
    TimeXAxis.prototype.og = function (a, b, c, d) {
        var e = new KeyValuePair$2(TimeAxisBreak.$, Number_$type);
        var f = this.oi != -2147483648 ? this.oi : 0;
        var g = this.nq != null ? this.nq.count : 0;
        while (f < g) {
            var h = this.nq.item1(f);
            if (h.d.getTime() <= a && h.c.getTime() >= a) {
                return NaN;
            }
            if (h.c.getTime() <= a) {
                e = new KeyValuePair$2(TimeAxisBreak.$, Number_$type, 1, h, this.nq.item(h));
                f++;
            }
            else {
                break;
            }
        }
        if (f > 0) {
            f -= 1;
        }
        if (this.oi != -2147483648) {
            this.oi = f;
        }
        var i;
        var j;
        if (e.key != null) {
            i = e.value;
            j = e.key.c;
        }
        else {
            i = 0;
            j = this.actualMinimumValue;
        }
        var fromPrecedingBreak_ = +(dateFromTicks(truncate(a))) - +j;
        var k = (fromPrecedingBreak_ / ((this.actualMaximumValue.getTime() - this.actualMinimumValue.getTime()) - this.omission));
        var l = i + k;
        l -= b.left;
        l /= b.width;
        var m = c.left;
        var n = c.right;
        var o = c.width;
        if (this.categoryMode != 0) {
            var p = this.getCategorySize(b, c, d);
            m += p / 2;
            n -= p / 2;
            o -= p;
        }
        if (this.ch) {
            return n - o * l;
        }
        return m + o * l;
    };
    TimeXAxis.prototype.ho = function (a, b, c, d) {
        this.oi = 0;
        for (var e = b; e < c; e++) {
            a.item(e, this.ek(a.item(e), d));
        }
        this.oi = -2147483648;
    };
    TimeXAxis.prototype.startCursor = function () {
        this.oi = 0;
    };
    TimeXAxis.prototype.resetCursor = function () {
        this.oi = -2147483648;
    };
    TimeXAxis.prototype.en = 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.getUnscaledValue3(c, NumericScaler.ac, b.c, this.categoryMode);
            return c;
        }
        return this.oh(a, b.e, b.d, b.c);
    };
    TimeXAxis.prototype.oh = function (a, b, c, d) {
        var e_1, _a;
        var e = c.left;
        var f = c.width;
        if (this.categoryMode != 0) {
            var g = this.getCategorySize(b, c, d);
            e += g / 2;
            f -= g;
        }
        var h = b.left + b.width * (a - e) / f;
        if (this.ch) {
            h = 1 - h;
        }
        var i = new KeyValuePair$2(TimeAxisBreak.$, Number_$type);
        if (this.nq != null) {
            try {
                for (var _b = __values(fromEnum(this.nq)), _c = _b.next(); !_c.done; _c = _b.next()) {
                    var j = _c.value;
                    if (j.value >= h && j.value <= h) {
                        return j.key.d.getTime();
                    }
                    if (j.value < h && j.value > i.value) {
                        i = j;
                    }
                }
            }
            catch (e_1_1) {
                e_1 = { error: e_1_1 };
            }
            finally {
                try {
                    if (_c && !_c.done && (_a = _b.return))
                        _a.call(_b);
                }
                finally {
                    if (e_1)
                        throw e_1.error;
                }
            }
        }
        var precedingBreakEnd_;
        var k;
        if (i.key != null) {
            k = i.value;
            precedingBreakEnd_ = i.key.c;
        }
        else {
            k = 0;
            precedingBreakEnd_ = this.actualMinimumValue;
        }
        var portionFromPrecedingBreak_ = h - k;
        var actualRange_ = +(this.actualMaximumValue) - +(this.actualMinimumValue) - this.omission;
        return (precedingBreakEnd_.getTime() + portionFromPrecedingBreak_ * actualRange_);
    };
    Object.defineProperty(TimeXAxis.prototype, "breaks", {
        get: function () {
            return this.ns;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(TimeXAxis.prototype, "n2", {
        get: function () {
            return this.n1;
        },
        set: function (a) {
            var b = this.n2;
            if (a != b) {
                this.n1 = a;
                this.h3("LabellingMode", enumGetBox(TimeAxisLabellingMode_$type, b), enumGetBox(TimeAxisLabellingMode_$type, this.n2));
            }
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(TimeXAxis.prototype, "labelFormats", {
        get: function () {
            return this.nz;
        },
        enumerable: false,
        configurable: true
    });
    TimeXAxis.prototype.hx = function (a, b, c, d) {
        _super.prototype.hx.call(this, a, b, c, d);
        switch (b) {
            case "ActualMaximumValue":
                this.o1();
                break;
            case "DateTimeColumn":
                this.ox = this.ov();
                break;
            case "LabellingMode":
                this.h9(false);
                break;
        }
    };
    TimeXAxis.prototype.ir = function (a, b) {
        _super.prototype.ir.call(this, a, b);
        this.o0();
        this.nk();
    };
    TimeXAxis.prototype.iq = function (a, b) {
        _super.prototype.iq.call(this, a, b);
        this.o0();
        this.nk();
    };
    TimeXAxis.prototype.c3 = function () {
        var a = _super.prototype.c3.call(this);
        this.o0();
        return a;
    };
    Object.defineProperty(TimeXAxis.prototype, "omission", {
        get: function () {
            return this._omission;
        },
        set: function (a) {
            this._omission = a;
        },
        enumerable: false,
        configurable: true
    });
    TimeXAxis.prototype.o1 = function () {
        var e_2, _a;
        var a = this.n5(this.breaks);
        var b = (0);
        try {
            for (var _b = __values(fromEnum(a)), _c = _b.next(); !_c.done; _c = _b.next()) {
                var c = _c.value;
                b += (+(c.c) - +(c.d));
            }
        }
        catch (e_2_1) {
            e_2 = { error: e_2_1 };
        }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return))
                    _a.call(_b);
            }
            finally {
                if (e_2)
                    throw e_2.error;
            }
        }
        this.omission = b;
    };
    TimeXAxis.prototype.n5 = function (a) {
        var e_3, _a;
        var b = new List$1(TimeAxisBreak.$, 0);
        if (a == null || +(this.actualMaximumValue) == +(dateMaxValue())) {
            return b;
        }
        var _loop_1 = function (c) {
            if (c.c >= this_1.actualMinimumValue && c.d <= this_1.actualMaximumValue) {
                b.add(c);
            }
            if (c.i != (0)) {
                var d = +(c.c) - +(c.d);
                var e_4 = this_1.ob(c.d, c.i);
                var _loop_2 = function () {
                    var f = this_1.ob(e_4, d);
                    var g = ((function () {
                        var $ret = new TimeAxisBreak();
                        $ret.d = e_4;
                        $ret.c = f;
                        return $ret;
                    })());
                    if (g.c >= this_1.actualMinimumValue && g.d <= this_1.actualMaximumValue) {
                        b.add(g);
                    }
                    if (dateSubtract(+(dateMaxValue()), +(c.i)) < g.d) {
                        return "break";
                    }
                    e_4 = this_1.ob(g.d, c.i);
                };
                while (e_4 <= this_1.actualMaximumValue) {
                    var state_1 = _loop_2();
                    if (state_1 === "break")
                        break;
                }
            }
        };
        var this_1 = this;
        try {
            for (var _b = __values(fromEn(a)), _c = _b.next(); !_c.done; _c = _b.next()) {
                var c = _c.value;
                _loop_1(c);
            }
        }
        catch (e_3_1) {
            e_3 = { error: e_3_1 };
        }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return))
                    _a.call(_b);
            }
            finally {
                if (e_3)
                    throw e_3.error;
            }
        }
        return b;
    };
    TimeXAxis.prototype.ob = function (a, b) {
        var c = dateFromTicks(a.getTime());
        c.setMilliseconds(c.getMilliseconds() + b);
        return c;
    };
    TimeXAxis.prototype.o0 = function () {
        var e_5, _a;
        this.nq = new OrderedDictionary$2(TimeAxisBreak.$, Number_$type);
        if (+(this.actualMaximumValue) == +(dateMaxValue()) || this.breaks == null || this.breaks.count == 0 || this.bi == null) {
            return;
        }
        var a = new List$1(TimeAxisBreak.$, 1, this.n5(this.breaks));
        a.aa(function (b, c) { return b.d < c.d ? -1 : b.d > c.d ? 1 : 0; });
        var b = this.bi.vl;
        var c = new ScalerParams(0, this.bi.vl, this.bi.v0, this.ch, this.bi.vp);
        var d = +(this.actualMaximumValue) - +(this.actualMinimumValue) - this.omission;
        var e = 0;
        try {
            for (var _b = __values(fromEnum(a)), _c = _b.next(); !_c.done; _c = _b.next()) {
                var f = _c.value;
                var g = timeSpanTicks((+(f.d) - +(this.actualMinimumValue))) / timeSpanTicks(d);
                var h = timeSpanTicks((+(f.c) - +(this.actualMinimumValue))) / timeSpanTicks(d);
                if (h < 0 || g - e > 1) {
                    continue;
                }
                this.nq.addItem(f, g - e);
                e += (h - g);
            }
        }
        catch (e_5_1) {
            e_5 = { error: e_5_1 };
        }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return))
                    _a.call(_b);
            }
            finally {
                if (e_5)
                    throw e_5.error;
            }
        }
    };
    TimeXAxis.prototype.ge = function (a) {
        if (isNaN_(a)) {
            return null;
        }
        var ticks_ = truncate(a);
        var b = (new Date(ticks_));
        var c = this.f0(b);
        if (c == null) {
            return null;
        }
        return c.toString();
    };
    TimeXAxis.prototype.f0 = function (a) {
        if (!(typeCast(Date_$type, a) !== null)) {
            return _super.prototype.f0.call(this, a);
        }
        var b = dateMinValue();
        var c = false;
        if (this.om != null) {
            if (typeCast(Date_$type, this.om) !== null) {
                b = this.om;
                c = true;
            }
        }
        var d = TimeXAxis.ol(a, c, b, this.n2, this.oz, this.labelFormats, this.fl);
        return d == null ? _super.prototype.f0.call(this, a) : d;
    };
    TimeXAxis.ol = function (a, b, c, d, e, f, g) {
        var h = TimeXAxis.oq(a, b, c, d, e, f, g);
        return dateToStringFormat(a, h, g);
    };
    TimeXAxis.oq = function (a, b, c, d, e, f, g) {
        var e_6, _a;
        var h = 0;
        if (b) {
            h = TimeXAxis.n3(a, c);
        }
        var i = null;
        var j;
        if (f == null || f.count == 0) {
            j = TimeXAxis.op(e, h, d);
        }
        else {
            var k = Number.MAX_VALUE;
            var l = null;
            try {
                for (var _b = __values(fromEnum(f)), _c = _b.next(); !_c.done; _c = _b.next()) {
                    var m = _c.value;
                    if (m.o < k) {
                        k = m.o;
                        l = m;
                    }
                    if (m.o > e) {
                        continue;
                    }
                    if (i == null || m.o > i.o) {
                        i = m;
                    }
                }
            }
            catch (e_6_1) {
                e_6 = { error: e_6_1 };
            }
            finally {
                try {
                    if (_c && !_c.done && (_a = _b.return))
                        _a.call(_b);
                }
                finally {
                    if (e_6)
                        throw e_6.error;
                }
            }
            if (i == null) {
                i = l;
            }
            if (i == null) {
                return null;
            }
            switch (h) {
                case 3:
                    if (i.h != null) {
                        j = i.h;
                    }
                    else if (i.j != null) {
                        j = i.j;
                    }
                    else if (i.l != null) {
                        j = i.l;
                    }
                    else {
                        j = i.e;
                    }
                    break;
                case 2:
                    if (i.j != null) {
                        j = i.j;
                    }
                    else if (i.l != null) {
                        j = i.l;
                    }
                    else {
                        j = i.e;
                    }
                    break;
                case 1:
                    if (i.l != null) {
                        j = i.l;
                    }
                    else {
                        j = i.e;
                    }
                    break;
                default:
                    j = i.e;
                    break;
            }
        }
        return j;
    };
    TimeXAxis.n3 = function (a, b) {
        if (a.getFullYear() == b.getFullYear() && dateGetMonth(a) == dateGetMonth(b) && a.getDate() == b.getDate()) {
            return 3;
        }
        else if (a.getFullYear() == b.getFullYear() && dateGetMonth(a) == dateGetMonth(b)) {
            return 2;
        }
        else if (a.getFullYear() == b.getFullYear()) {
            return 1;
        }
        return 0;
    };
    TimeXAxis.prototype.get_mn = function () {
        return _super.prototype.get_mn.call(this) || (this.intervals != null && this.intervals.count > 0);
    };
    TimeXAxis.prototype.set_mn = function (a) {
        _super.prototype.set_mn.call(this, a);
    };
    Object.defineProperty(TimeXAxis.prototype, "intervals", {
        get: function () {
            return this.nx;
        },
        enumerable: false,
        configurable: true
    });
    TimeXAxis.prototype.oo = function (a, b) {
        return TimeXAxis.op(this.oz, a, b);
    };
    TimeXAxis.op = function (a, b, c) {
        if (c != 2) {
            b = 0;
        }
        a = Math.abs(a);
        if (a > timeSpanInit3(1825, 0, 0, 0)) {
            return "yyyy";
        }
        if (a > timeSpanInit3(365, 0, 0, 0)) {
            switch (b) {
                case 3:
                case 2:
                case 1: return "MMM";
            }
            return "MMM yyyy";
        }
        ;
        if (a > timeSpanInit3(4, 0, 0, 0)) {
            switch (b) {
                case 3:
                case 2: return "dd";
            }
            return "MMM dd";
        }
        ;
        if (a > timeSpanInit3(1, 0, 0, 0)) {
            switch (b) {
                case 3: return "hh:mmtt";
                case 2: return "dd hh:mmtt";
            }
            return "MMM dd hh:mmtt";
        }
        ;
        if (a > timeSpanInit1(0, 10, 0)) {
            return "hh:mm";
        }
        return "hh:mm:ss";
    };
    TimeXAxis.nr = function () {
        if (TimeXAxis.np == null) {
            TimeXAxis.np = ((function () {
                var $ret = new OrderedDictionary$2(Number_$type, TimeAxisInterval.$);
                $ret.addItem((0), ((function () {
                    var $ret = new TimeAxisInterval();
                    $ret.b = 0;
                    $ret.f = 1;
                    return $ret;
                })()));
                $ret.addItem(timeSpanFromMilliseconds(1), ((function () {
                    var $ret = new TimeAxisInterval();
                    $ret.b = 1;
                    $ret.f = 1;
                    return $ret;
                })()));
                $ret.addItem(timeSpanFromSeconds(1), ((function () {
                    var $ret = new TimeAxisInterval();
                    $ret.b = 2;
                    $ret.f = 1;
                    return $ret;
                })()));
                $ret.addItem(timeSpanFromSeconds(30), ((function () {
                    var $ret = new TimeAxisInterval();
                    $ret.b = 2;
                    $ret.f = 30;
                    return $ret;
                })()));
                $ret.addItem(timeSpanFromMinutes(2), ((function () {
                    var $ret = new TimeAxisInterval();
                    $ret.b = 3;
                    $ret.f = 2;
                    return $ret;
                })()));
                $ret.addItem(timeSpanFromMinutes(5), ((function () {
                    var $ret = new TimeAxisInterval();
                    $ret.b = 3;
                    $ret.f = 5;
                    return $ret;
                })()));
                $ret.addItem(timeSpanFromHours(1), ((function () {
                    var $ret = new TimeAxisInterval();
                    $ret.b = 4;
                    $ret.f = 1;
                    return $ret;
                })()));
                $ret.addItem(timeSpanFromDays(1), ((function () {
                    var $ret = new TimeAxisInterval();
                    $ret.b = 5;
                    $ret.f = 1;
                    return $ret;
                })()));
                $ret.addItem(timeSpanFromDays(2), ((function () {
                    var $ret = new TimeAxisInterval();
                    $ret.b = 5;
                    $ret.f = 2;
                    return $ret;
                })()));
                $ret.addItem(timeSpanFromDays(5), ((function () {
                    var $ret = new TimeAxisInterval();
                    $ret.b = 5;
                    $ret.f = 5;
                    return $ret;
                })()));
                $ret.addItem(timeSpanFromDays(10), ((function () {
                    var $ret = new TimeAxisInterval();
                    $ret.b = 5;
                    $ret.f = 10;
                    return $ret;
                })()));
                $ret.addItem(timeSpanFromDays(30), ((function () {
                    var $ret = new TimeAxisInterval();
                    $ret.b = 7;
                    $ret.f = 1;
                    return $ret;
                })()));
                $ret.addItem(timeSpanFromDays(60), ((function () {
                    var $ret = new TimeAxisInterval();
                    $ret.b = 7;
                    $ret.f = 3;
                    return $ret;
                })()));
                $ret.addItem(timeSpanFromDays(365), ((function () {
                    var $ret = new TimeAxisInterval();
                    $ret.b = 8;
                    $ret.f = 1;
                    return $ret;
                })()));
                $ret.addItem(timeSpanFromDays(365 * 10), ((function () {
                    var $ret = new TimeAxisInterval();
                    $ret.b = 8;
                    $ret.f = 10;
                    return $ret;
                })()));
                return $ret;
            })());
        }
        return TimeXAxis.np;
    };
    TimeXAxis.prototype.nv = function () {
        var e_7, _a;
        if (this.c4 && (this.az == null || this.az.angle == 0)) {
            var a = this.ey();
            var b = this.ke.width;
            var c = truncate(Math.floor(b / a));
            var d = Number.MAX_VALUE;
            var e = c == 0 ? d : timeSpanFromTicks((intDivide(timeSpanTicks(this.oz), c)));
            var f = null;
            try {
                for (var _b = __values(fromEnum(TimeXAxis.nr())), _c = _b.next(); !_c.done; _c = _b.next()) {
                    var g = _c.value;
                    f = g.value;
                    if (g.key > e) {
                        break;
                    }
                }
            }
            catch (e_7_1) {
                e_7 = { error: e_7_1 };
            }
            finally {
                try {
                    if (_c && !_c.done && (_a = _b.return))
                        _a.call(_b);
                }
                finally {
                    if (e_7)
                        throw e_7.error;
                }
            }
            var h = this.nu;
            if (h != f) {
                this.ie();
            }
            this.nu = f;
            return f;
        }
        var i = this.oz;
        if (i == (0)) {
            return ((function () {
                var $ret = new TimeAxisInterval();
                $ret.b = 8;
                $ret.f = 1;
                return $ret;
            })());
        }
        if (i >= timeSpanFromDays(365 * 100)) {
            return ((function () {
                var $ret = new TimeAxisInterval();
                $ret.b = 8;
                $ret.f = 10;
                return $ret;
            })());
        }
        if (i >= timeSpanFromDays(365 * 5)) {
            return ((function () {
                var $ret = new TimeAxisInterval();
                $ret.b = 8;
                $ret.f = 1;
                return $ret;
            })());
        }
        if (i >= timeSpanFromDays(365 * 2)) {
            return ((function () {
                var $ret = new TimeAxisInterval();
                $ret.b = 7;
                $ret.f = 3;
                return $ret;
            })());
        }
        if (i >= timeSpanFromDays(150)) {
            return ((function () {
                var $ret = new TimeAxisInterval();
                $ret.b = 7;
                $ret.f = 1;
                return $ret;
            })());
        }
        if (i >= timeSpanFromDays(60)) {
            return ((function () {
                var $ret = new TimeAxisInterval();
                $ret.b = 5;
                $ret.f = 10;
                return $ret;
            })());
        }
        if (i >= timeSpanFromDays(30)) {
            return ((function () {
                var $ret = new TimeAxisInterval();
                $ret.b = 5;
                $ret.f = 5;
                return $ret;
            })());
        }
        if (i >= timeSpanFromDays(20)) {
            return ((function () {
                var $ret = new TimeAxisInterval();
                $ret.b = 5;
                $ret.f = 2;
                return $ret;
            })());
        }
        if (i >= timeSpanFromDays(4)) {
            return ((function () {
                var $ret = new TimeAxisInterval();
                $ret.b = 5;
                $ret.f = 1;
                return $ret;
            })());
        }
        if (i >= timeSpanFromDays(1)) {
            return ((function () {
                var $ret = new TimeAxisInterval();
                $ret.b = 4;
                $ret.f = 12;
                return $ret;
            })());
        }
        if (i >= timeSpanFromHours(6)) {
            return ((function () {
                var $ret = new TimeAxisInterval();
                $ret.b = 4;
                $ret.f = 1;
                return $ret;
            })());
        }
        if (i >= timeSpanFromMinutes(30)) {
            return ((function () {
                var $ret = new TimeAxisInterval();
                $ret.b = 3;
                $ret.f = 5;
                return $ret;
            })());
        }
        if (i >= timeSpanFromMinutes(10)) {
            return ((function () {
                var $ret = new TimeAxisInterval();
                $ret.b = 3;
                $ret.f = 2;
                return $ret;
            })());
        }
        return ((function () {
            var $ret = new TimeAxisInterval();
            $ret.b = 2;
            $ret.f = 30;
            return $ret;
        })());
    };
    TimeXAxis.prototype.nw = function () {
        var e_8, _a;
        if (this.intervals == null || this.intervals.count == 0) {
            return this.nv();
        }
        if (this.oz == (0)) {
            return ((function () {
                var $ret = new TimeAxisInterval();
                $ret.b = 8;
                $ret.f = 1;
                return $ret;
            })());
        }
        var a = null;
        try {
            for (var _b = __values(fromEnum(this.intervals)), _c = _b.next(); !_c.done; _c = _b.next()) {
                var b = _c.value;
                if (b.k > this.oz) {
                    continue;
                }
                if (a == null || b.k > a.k) {
                    a = b;
                }
            }
        }
        catch (e_8_1) {
            e_8 = { error: e_8_1 };
        }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return))
                    _a.call(_b);
            }
            finally {
                if (e_8)
                    throw e_8.error;
            }
        }
        return a == null ? this.nv() : a;
    };
    TimeXAxis.prototype.ia = function (a) {
        _super.prototype.ia.call(this, a);
        var b = this.bi != null ? this.bi.vl : Rect.empty;
        var c = !b.isEmpty ? this.ke : Rect.empty;
        var d = this.j9();
        var e = this.j8();
        var f = new ScalerParams(0, b, c, this.ch, d);
        if (this.mj == null && !this.mp() && !this.mq()) {
            return;
        }
        if (!b.isEmpty && !c.isEmpty) {
            var g = c.bottom;
            var h = g - c.top;
            if (this.aj != null) {
                var i = typeCast(NumericYAxis.$, this.aj);
                if (i != null) {
                    var j = this.ig(g, h, b, c, d, c.top, c.bottom);
                    g = j.p0;
                    h = j.p1;
                }
            }
            if (isNaN_(g)) {
                g = 0;
            }
            this.hr(this.a5.a4(), g, c, this.a5.e(), true);
            this.aw.bg = h;
            var k = truncate(Math.ceil(e.right));
            var l = truncate(Math.floor(e.left));
            var m = this.a5.a7();
            var n = this.a5.f();
            var o = this.a5.a5();
            this.nk();
            var p = new List$1(Date_$type, 1, this.n4());
            var q = new List$1(Number_$type, 0);
            for (var r = 0; r < p.count; r++) {
                q.add(p._inner[r].getTime());
            }
            this.ho(q, 0, q.count, f);
            this.om = null;
            for (var s = 0; s < p.count; s++) {
                var t = p._inner[s];
                var u = q._inner[s];
                var v = truncate(Math.round(u));
                if (v <= k) {
                    if (s % 2 == 0) {
                        var w = s < q.count - 1 ? q._inner[s + 1] : e.right;
                        this.ip(m, u, w, c);
                    }
                    this.io(o, u, c, n, false);
                    this.kz.add(u);
                }
                if (v >= l && v <= k) {
                    var x = this.f0(t);
                    this.om = t;
                    if (x != null) {
                        this.dc.add1(x);
                        this.dd.add(new LabelPosition(u));
                    }
                }
            }
            this.om = null;
        }
        if ((this.az == null || this.az.visibility == 0) && this.aj != null) {
            if (this.az != null && (this.az.location == 4 || this.az.location == 5)) {
                this.bi.nr();
            }
        }
        this.aw.a9 = this.dc;
        this.aw.ba = this.dd;
        this.aw.cl();
        this.l6();
    };
    TimeXAxis.prototype.n4 = function () {
        var a = this.bi != null ? this.bi.vl : Rect.empty;
        var b = !a.isEmpty ? this.ke : Rect.empty;
        var c = this.j9();
        var d = this.j8();
        var e = new ScalerParams(0, a, b, this.ch, c);
        var f = this.nw();
        var g = this.od;
        if (!this.mq() && f != null) {
            if (f.b == 7 && g.getDate() != 1) {
                g = dateAddMonths(g, 1);
                g = dateFromValues(g.getFullYear(), dateGetMonth(g), 1, 0, 0, 0, 0);
            }
            else if (f.b == 5 && dateGetTimeOfDay(g) != (0)) {
                g = dateAddDays(g, 1);
                g = dateFromValues(g.getFullYear(), dateGetMonth(g), g.getDate(), 0, 0, 0, 0);
            }
            else if (f.b == 4 && g.getMinutes() != 0) {
                g = dateAddHours(g, 1);
                g = dateFromValues(g.getFullYear(), dateGetMonth(g), g.getDate(), g.getHours(), 0, 0, 0);
            }
        }
        var h = new List$1(Date_$type, 0);
        var i = 0;
        var j = 10000;
        if (f.f == 0) {
            return h;
        }
        while (g <= this.visibleMaximum && i++ < j) {
            var k = this.n6(g);
            var l = g;
            if (k.key != null) {
                l = f.d(k.key.c);
            }
            h.add(l);
            while (g <= l) {
                g = f.c(g);
            }
        }
        return h;
    };
    TimeXAxis.prototype.n6 = function (a) {
        var e_9, _a;
        if (this.nq == null) {
            return new KeyValuePair$2(TimeAxisBreak.$, Number_$type);
        }
        try {
            for (var _b = __values(fromEnum(this.nq)), _c = _b.next(); !_c.done; _c = _b.next()) {
                var b = _c.value;
                if (b.key.d <= a && b.key.c >= a) {
                    return b;
                }
            }
        }
        catch (e_9_1) {
            e_9 = { error: e_9_1 };
        }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return))
                    _a.call(_b);
            }
            finally {
                if (e_9)
                    throw e_9.error;
            }
        }
        return new KeyValuePair$2(TimeAxisBreak.$, Number_$type);
    };
    Object.defineProperty(TimeXAxis.prototype, "visibleMinimum", {
        get: function () {
            return this.oa;
        },
        set: function (a) {
            this.oa = a;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(TimeXAxis.prototype, "od", {
        get: function () {
            return this.n8;
        },
        set: function (a) {
            this.n8 = a;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(TimeXAxis.prototype, "oc", {
        get: function () {
            return this.n7;
        },
        set: function (a) {
            this.n7 = a;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(TimeXAxis.prototype, "visibleMaximum", {
        get: function () {
            return this.n9;
        },
        set: function (a) {
            this.n9 = a;
        },
        enumerable: false,
        configurable: true
    });
    TimeXAxis.prototype.nk = function () {
        _super.prototype.nk.call(this);
        if (this.bi == null || !Extensions.b(this.bi.v0)) {
            return;
        }
        var a = new ScalerParams(0, this.bi.vl, this.bi.v0, this.ch, this.bi.vp);
        var b = this.j8();
        var c = b.left;
        var d = b.right;
        var e = b.left;
        var f = b.right;
        if (this.categoryMode != 0) {
            var g = this.getCategorySize(a.e, a.d, a.c);
            e += g / 2;
            f -= g / 2;
        }
        var h = truncate(this.en(c, a));
        var i = truncate(this.en(d, a));
        var j = truncate(this.en(e, a));
        var k = truncate(this.en(f, a));
        if (j < 0 || k < 0) {
            this.oz = (0);
            return;
        }
        h = Math.min(dateMaxValue().getTime(), h);
        i = Math.min(dateMaxValue().getTime(), i);
        h = Math.max(dateMinValue().getTime(), h);
        i = Math.max(dateMinValue().getTime(), i);
        j = Math.min(dateMaxValue().getTime(), j);
        k = Math.min(dateMaxValue().getTime(), k);
        var l = dateFromTicks(Math.min(h, i));
        var m = dateFromTicks(Math.max(h, i));
        var n = dateFromTicks(Math.min(j, k));
        var o = dateFromTicks(Math.max(j, k));
        var p = +(this.visibleMinimum) != +n || +(this.visibleMaximum) != +o;
        this.visibleMinimum = n;
        this.visibleMaximum = o;
        this.od = l;
        this.oc = m;
        if (p && this.visibleRangeChanged != null) {
            this.visibleRangeChanged(this, EventArgs.empty);
        }
        this.oz = +(this.visibleMaximum) - +(this.visibleMinimum);
    };
    TimeXAxis.prototype.k7 = function (a, b, c, d, e) {
        return 0;
    };
    TimeXAxis.prototype.i$f = function (a) {
        var b = this;
        if (b.i$b == null || b.i$b.count == 0) {
            return -1;
        }
        var c = new SortedListView$1(Date_$type, this.mj, b.i$b);
        var ticks_ = a;
        var d = (new Date(ticks_));
        var e = this.m3(d, c);
        e = Math.max(0, Math.min(b.i$b.count - 1, e));
        var f = c.item(e);
        var g;
        var h;
        if (f < d) {
            g = e;
            h = g + 1;
        }
        else {
            h = e;
            g = e - 1;
        }
        g = this.ok(g, c);
        h = this.oj(h, c);
        if (g < 0) {
            return h;
        }
        if (h >= b.i$b.count) {
            return g;
        }
        var i = c.item(g);
        var j = c.item(h);
        var k = new ScalerParams(0, this.bi.v1, this.ke, this.ch, this.bi.vp);
        this.startCursor();
        var l = this.ek(i.getTime(), k);
        var m = this.ek(a, k);
        var n = this.ek(j.getTime(), k);
        this.resetCursor();
        if (i < this.od && j <= this.oc) {
            return this.ms._inner[h];
        }
        if (j > this.oc && i >= this.od) {
            return this.ms._inner[g];
        }
        if (Math.abs(l - m) < Math.abs(n - m)) {
            return this.ms._inner[g];
        }
        else {
            return this.ms._inner[h];
        }
    };
    TimeXAxis.prototype.oj = function (a, b) {
        if (a < 0 || a >= b.count - 1) {
            return a;
        }
        var c = b.item(a);
        var d = 0;
        while (d < this.nq.count) {
            var e = this.nq.item1(d);
            if (c >= e.d && c <= e.c) {
                break;
            }
            d++;
        }
        if (d >= this.nq.count) {
            return a;
        }
        var f = a;
        while (f < b.count) {
            if (f < 0 || f >= b.count - 1) {
                return a;
            }
            c = b.item(f);
            var g = this.nq.item1(d);
            if (c >= g.d && c <= g.c) {
                f++;
                continue;
            }
            d++;
            if (d > this.nq.count - 1) {
                break;
            }
            g = this.nq.item1(d);
            if (c >= g.d && c <= g.c) {
                f++;
                continue;
            }
            break;
        }
        if (f < 0 || f > b.count - 1) {
            return a;
        }
        return f;
    };
    TimeXAxis.prototype.ok = function (a, b) {
        if (a < 0 || a >= b.count - 1) {
            return a;
        }
        var c = b.item(a);
        var d = 0;
        while (d < this.nq.count) {
            var e = this.nq.item1(d);
            if (c >= e.d && c <= e.c) {
                break;
            }
            d++;
        }
        if (d >= this.nq.count) {
            return a;
        }
        var f = a;
        while (f >= 0) {
            if (f < 0 || f >= b.count - 1) {
                return a;
            }
            c = b.item(f);
            var g = this.nq.item1(d);
            if (c >= g.d && c <= g.c) {
                f--;
                continue;
            }
            d--;
            if (d < 0) {
                break;
            }
            g = this.nq.item1(d);
            if (c >= g.d && c <= g.c) {
                f--;
                continue;
            }
            break;
        }
        if (f < 0 || f > b.count - 1) {
            return a;
        }
        return f;
    };
    TimeXAxis.prototype.o2 = function (a, b) {
        var e_10, _a;
        this.o1();
        this.o0();
        if (this.aw != null) {
            this.aw.al = this.breaks.count > 0;
        }
        this.h9(false);
        try {
            for (var _b = __values(fromEnum(this.db())), _c = _b.next(); !_c.done; _c = _b.next()) {
                var c = _c.value;
                c.rm(false);
            }
        }
        catch (e_10_1) {
            e_10 = { error: e_10_1 };
        }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return))
                    _a.call(_b);
            }
            finally {
                if (e_10)
                    throw e_10.error;
            }
        }
    };
    TimeXAxis.prototype.o4 = function (a, b) {
        this.h9(false);
    };
    TimeXAxis.prototype.o3 = function (a, b) {
        this.h9(false);
    };
    TimeXAxis.prototype.get_cc = function () {
        return false;
    };
    TimeXAxis.prototype.getWindowRectForSelection = function (a, b) {
        var c = +(this.actualMaximumValue) - +(this.actualMinimumValue);
        var d = timeSpanTotalMilliseconds((+a - +(this.actualMinimumValue))) / timeSpanTotalMilliseconds(c);
        var e = 1 - (timeSpanTotalMilliseconds((+(this.actualMaximumValue) - +b)) / timeSpanTotalMilliseconds(c));
        e = Math.min(1, e);
        return new Rect(0, d, NaN, Math.max(0, e - d), NaN);
    };
    TimeXAxis.prototype.getCategorySize = function (a, b, c) {
        if (this.oz != (0)) {
            var d = timeSpanFromTicks(truncate((timeSpanTicks(this.oz) * (a.width / (this.bi != null ? this.bi.vl.width : 1)))));
            var e = timeSpanTicks(d) / timeSpanTicks(this.ox);
            var f = b.width / 4;
            return Math.min(f, b.width / (e + 1));
        }
        return _super.prototype.getCategorySize.call(this, a, b, c);
    };
    TimeXAxis.ow = function (a, b) {
        var c = Math.min(5, b);
        var d = a(0);
        var e = new Dictionary$2(Number_$type, Number_$type, 0);
        var f = 0;
        var g = (0);
        for (var h = 0; h < c; h++) {
            var i = a(h);
            var j = +i - +d;
            if (!e.containsKey(j)) {
                e.addItem(j, 0);
            }
            e.item(j, e.item(j) + 1);
            if (e.item(j) > f) {
                f = e.item(j);
                g = j;
            }
            d = i;
        }
        return g;
    };
    TimeXAxis.prototype.ov = function () {
        var _this = this;
        if (this.mj == null || this.mj.count < 2 || this.ms == null) {
            return (0);
        }
        return TimeXAxis.ow(function (a) { return _this.mj.item(_this.ms._inner[a]); }, this.mj.count);
    };
    TimeXAxis.prototype.ng = function () {
        _super.prototype.ng.call(this);
        this.ox = this.ov();
    };
    TimeXAxis.os = function (a) {
        if (a >= timeSpanFromDays(1)) {
            return "D";
        }
        return "g";
    };
    TimeXAxis.prototype.itemLabelFormat = function () {
        return TimeXAxis.os(this.ox);
    };
    TimeXAxis.prototype.m2 = function (a, b, c, d) {
        return this.m1(this.visibleMinimum);
    };
    TimeXAxis.prototype.m5 = function (a, b, c, d) {
        return this.m4(this.visibleMaximum);
    };
    Object.defineProperty(TimeXAxis.prototype, "isVertical", {
        get: function () {
            return this.cp;
        },
        enumerable: false,
        configurable: true
    });
    TimeXAxis.prototype.j$d = function (a, b) {
        return this.en.apply(this, arguments);
    };
    TimeXAxis.prototype.j$c = function (a, b) {
        return this.ek.apply(this, arguments);
    };
    TimeXAxis.prototype.j$e = function (a, b, c, d, e) {
        this.hn.apply(this, arguments);
    };
    TimeXAxis.prototype.j$f = function (a, b, c, d) {
        this.ho.apply(this, arguments);
    };
    TimeXAxis.prototype.j$g = function (a, b, c, d) {
        this.hp.apply(this, arguments);
    };
    Object.defineProperty(TimeXAxis.prototype, "j$b", {
        get: function () {
            return this.ch;
        },
        enumerable: false,
        configurable: true
    });
    TimeXAxis.prototype.j$a = function () {
        return this.ag.apply(this, arguments);
    };
    Object.defineProperty(TimeXAxis.prototype, "i$a", {
        get: function () {
            return this.ce;
        },
        enumerable: false,
        configurable: true
    });
    TimeXAxis.$t = markType(TimeXAxis, 'TimeXAxis', TimeAxisBase.$, [ITimeRangeSelectorAxis_$type]);
    TimeXAxis.np = null;
    return TimeXAxis;
}(TimeAxisBase));
export { TimeXAxis };
