/*
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 { TimeAxisBase } from "./TimeAxisBase";
import { ITimeRangeSelectorAxis_$type } from "./ITimeRangeSelectorAxis";
import { TimeAxisBreakCollection } from "./TimeAxisBreakCollection";
import { TimeAxisLabelFormatCollection } from "./TimeAxisLabelFormatCollection";
import { OrderedDictionary$2 } from "./OrderedDictionary$2";
import { TimeAxisBreak } from "./TimeAxisBreak";
import { List$1 } from "igniteui-react-core/ES2015/List$1";
import { TimeAxisInterval } from "./TimeAxisInterval";
import { TimeAxisIntervalCollection } from "./TimeAxisIntervalCollection";
import { EventArgs, runOn, delegateCombine, Number_$type, fromEnum, fromEn, typeCast, Date_$type, markType } from "igniteui-react-core/ES2015/type";
import { ScalerParams } from "./ScalerParams";
import { KeyValuePair$2 } from "igniteui-react-core/ES2015/KeyValuePair$2";
import { Rect } from "igniteui-react-core/ES2015/Rect";
import { NumericYAxis } from "./NumericYAxis";
import { LabelPosition } from "./LabelPosition";
import { Extensions } from "igniteui-react-core/ES2015/Extensions";
import { SortedListView$1 } from "./SortedListView$1";
import { Dictionary$2 } from "igniteui-react-core/ES2015/Dictionary$2";
import { truncate, intDivide, isNaN_ } from "igniteui-react-core/ES2015/number";
import { dateFromTicks, dateMaxValue, dateAdd, dateSubtract, dateAddMonths, dateFromValues, dateGetMonth, dateGetTimeOfDay, dateAddDays, dateAddHours, dateMinValue } from "igniteui-react-core/ES2015/date";
import { timeSpanTicks, timeSpanInit3, timeSpanInit1, timeSpanFromMilliseconds, timeSpanFromSeconds, timeSpanFromMinutes, timeSpanFromHours, timeSpanFromDays, timeSpanFromTicks, timeSpanTotalMilliseconds } from "igniteui-react-core/ES2015/timespan";
import { dateToStringFormat } from "igniteui-react-core/ES2015/dateExtended";
/**
 * @hidden
 */
export class TimeXAxis extends TimeAxisBase {
    constructor() {
        super();
        this.mf = 0;
        this.lz = null;
        this.l5 = null;
        this._omission = 0;
        this.lx = null;
        this.l3 = null;
        this.mr = 0;
        this.mb = dateMaxValue();
        this.ma = dateMinValue();
        this.mp = 0;
        this.visibleRangeChanged = null;
        this.ab = TimeXAxis.$;
        this.omission = (0);
        this.mf = -2147483648;
        this.lz = new TimeAxisBreakCollection();
        let a = this.breaks;
        a.collectionChanged = delegateCombine(a.collectionChanged, runOn(this, this.mu));
        this.l3 = new TimeAxisIntervalCollection();
        let b = this.intervals;
        b.collectionChanged = delegateCombine(b.collectionChanged, runOn(this, this.mv));
        this.l5 = new TimeAxisLabelFormatCollection();
        let c = this.labelFormats;
        c.collectionChanged = delegateCombine(c.collectionChanged, runOn(this, this.mw));
        this.mr = (0);
    }
    dt(a, b) {
        let c = new KeyValuePair$2(TimeAxisBreak.$, Number_$type);
        let d = this.mf != -2147483648 ? this.mf : 0;
        let e = this.lx != null ? this.lx.count : 0;
        while (d < e) {
            let f = this.lx.item1(d);
            if (f.d.getTime() <= a && f.c.getTime() >= a) {
                return NaN;
            }
            if (f.c.getTime() <= a) {
                c = new KeyValuePair$2(TimeAxisBreak.$, Number_$type, 1, f, this.lx.item(f));
                d++;
            }
            else {
                break;
            }
        }
        if (d > 0) {
            d -= 1;
        }
        if (this.mf != -2147483648) {
            this.mf = d;
        }
        let g;
        let h;
        if (c.key != null) {
            g = c.value;
            h = c.key.c;
        }
        else {
            g = 0;
            h = this.actualMinimumValue;
        }
        let fromPrecedingBreak_ = +(dateFromTicks(truncate(a))) - +h;
        let i = (fromPrecedingBreak_ / ((this.actualMaximumValue.getTime() - this.actualMinimumValue.getTime()) - this.omission));
        let j = g + i;
        j -= b.e.left;
        j /= b.e.width;
        let k = b.d.left;
        let l = b.d.right;
        let m = b.d.width;
        if (this.categoryMode != 0) {
            let n = this.getCategorySize(b.e, b.d, b.c);
            k += n / 2;
            l -= n / 2;
            m -= n;
        }
        if (this.b8) {
            return l - m * j;
        }
        return k + m * j;
    }
    gh(a, b, c, d) {
        this.mf = 0;
        for (let e = b; e < c; e++) {
            a.item(e, this.dt(a.item(e), d));
        }
        this.mf = -2147483648;
    }
    startCursor() {
        this.mf = 0;
    }
    resetCursor() {
        this.mf = -2147483648;
    }
    dw(a, b) {
        let c = b.d.left;
        let d = b.d.width;
        if (this.categoryMode != 0) {
            let e = this.getCategorySize(b.e, b.d, b.c);
            c += e / 2;
            d -= e;
        }
        let f = b.e.left + b.e.width * (a - c) / d;
        if (this.b8) {
            f = 1 - f;
        }
        let g = new KeyValuePair$2(TimeAxisBreak.$, Number_$type);
        if (this.lx != null) {
            for (let h of fromEnum(this.lx)) {
                if (h.value >= f && h.value <= f) {
                    return h.key.d.getTime();
                }
                if (h.value < f && h.value > g.value) {
                    g = h;
                }
            }
        }
        let precedingBreakEnd_;
        let i;
        if (g.key != null) {
            i = g.value;
            precedingBreakEnd_ = g.key.c;
        }
        else {
            i = 0;
            precedingBreakEnd_ = this.actualMinimumValue;
        }
        let portionFromPrecedingBreak_ = f - i;
        let actualRange_ = +(this.actualMaximumValue) - +(this.actualMinimumValue) - this.omission;
        return (precedingBreakEnd_.getTime() + portionFromPrecedingBreak_ * actualRange_);
    }
    get breaks() {
        return this.lz;
    }
    get labelFormats() {
        return this.l5;
    }
    go(a, b, c, d) {
        super.go(a, b, c, d);
        switch (b) {
            case "ActualMaximumValue":
                this.mt();
                break;
            case "DateTimeColumn":
                this.mp = this.mn();
                break;
        }
    }
    hc(a, b) {
        super.hc(a, b);
        this.ms();
        this.lr();
    }
    hb(a, b) {
        super.hb(a, b);
        this.ms();
        this.lr();
    }
    cm() {
        let a = super.cm();
        this.ms();
        return a;
    }
    get omission() {
        return this._omission;
    }
    set omission(a) {
        this._omission = a;
    }
    mt() {
        let a = this.l8(this.breaks);
        let b = (0);
        for (let c of fromEnum(a)) {
            b += (+(c.c) - +(c.d));
        }
        this.omission = b;
    }
    l8(a) {
        let b = new List$1(TimeAxisBreak.$, 0);
        if (a == null || +(this.actualMaximumValue) == +(dateMaxValue())) {
            return b;
        }
        for (let c of fromEn(a)) {
            if (c.c >= this.actualMinimumValue && c.d <= this.actualMaximumValue) {
                b.add(c);
            }
            if (c.i != (0)) {
                let d = +(c.c) - +(c.d);
                let e = dateAdd(+(c.d), +(c.i));
                while (e <= this.actualMaximumValue) {
                    let f = this.mc(e, d);
                    let g = ((() => {
                        let $ret = new TimeAxisBreak();
                        $ret.d = e;
                        $ret.c = f;
                        return $ret;
                    })());
                    if (g.c >= this.actualMinimumValue && g.d <= this.actualMaximumValue) {
                        b.add(g);
                    }
                    if (dateSubtract(+(dateMaxValue()), +(c.i)) < g.d) {
                        break;
                    }
                    e = this.mc(g.d, c.i);
                }
            }
        }
        return b;
    }
    mc(a, b) {
        let c = dateFromTicks(a.getTime());
        c.setMilliseconds(c.getMilliseconds() + b);
        return c;
    }
    ms() {
        this.lx = new OrderedDictionary$2(TimeAxisBreak.$, Number_$type);
        if (+(this.actualMaximumValue) == +(dateMaxValue()) || this.breaks == null || this.breaks.count == 0 || this.bf == null) {
            return;
        }
        let a = new List$1(TimeAxisBreak.$, 1, this.l8(this.breaks));
        a.aa((b, c) => b.d < c.d ? -1 : b.d > c.d ? 1 : 0);
        let b = this.bf.mq;
        let c = new ScalerParams(0, this.bf.mq, this.bf.m3, this.b8, this.bf.mu);
        let d = +(this.actualMaximumValue) - +(this.actualMinimumValue) - this.omission;
        let e = 0;
        for (let f of fromEnum(a)) {
            let g = timeSpanTicks((+(f.d) - +(this.actualMinimumValue))) / timeSpanTicks(d);
            let h = timeSpanTicks((+(f.c) - +(this.actualMinimumValue))) / timeSpanTicks(d);
            if (h < 0 || g - e > 1) {
                continue;
            }
            this.lx.addItem(f, g - e);
            e += (h - g);
        }
    }
    fe(a) {
        if (isNaN_(a)) {
            return null;
        }
        let ticks_ = truncate(a);
        let b = (new Date(ticks_));
        let c = this.e1(b);
        if (c == null) {
            return null;
        }
        return c.toString();
    }
    e1(a) {
        if (!(typeCast(Date_$type, a) !== null)) {
            return super.e1(a);
        }
        let b = TimeXAxis.mg(a, this.mr, this.labelFormats, this.eo);
        return b == null ? super.e1(a) : b;
    }
    static mg(a, b, c, d) {
        let e = null;
        if (b == (0)) {
            return null;
        }
        let f;
        if (c == null || c.count == 0) {
            f = TimeXAxis.mj(b);
        }
        else {
            for (let g of fromEnum(c)) {
                if (g.f > b) {
                    continue;
                }
                if (e == null || g.f > e.f) {
                    e = g;
                }
            }
            if (e == null) {
                return null;
            }
            f = e.b;
        }
        return dateToStringFormat(a, f, d);
    }
    get_ku() {
        return super.get_ku() || (this.intervals != null && this.intervals.count > 0);
    }
    set_ku(a) {
        super.set_ku(a);
    }
    get intervals() {
        return this.l3;
    }
    mi() {
        return TimeXAxis.mj(this.mr);
    }
    static mj(a) {
        a = Math.abs(a);
        if (a > timeSpanInit3(1825, 0, 0, 0)) {
            return "yyyy";
        }
        if (a > timeSpanInit3(365, 0, 0, 0)) {
            return "MMM yyyy";
        }
        if (a > timeSpanInit3(4, 0, 0, 0)) {
            return "MMM dd";
        }
        if (a > timeSpanInit3(1, 0, 0, 0)) {
            return "MMM dd hh:mmtt";
        }
        if (a > timeSpanInit1(0, 10, 0)) {
            return "hh:mm";
        }
        return "hh:mm:ss";
    }
    static ly() {
        if (TimeXAxis.lw == null) {
            TimeXAxis.lw = ((() => {
                let $ret = new OrderedDictionary$2(Number_$type, TimeAxisInterval.$);
                $ret.addItem((0), ((() => {
                    let $ret = new TimeAxisInterval();
                    $ret.b = 0;
                    $ret.f = 1;
                    return $ret;
                })()));
                $ret.addItem(timeSpanFromMilliseconds(1), ((() => {
                    let $ret = new TimeAxisInterval();
                    $ret.b = 1;
                    $ret.f = 1;
                    return $ret;
                })()));
                $ret.addItem(timeSpanFromSeconds(1), ((() => {
                    let $ret = new TimeAxisInterval();
                    $ret.b = 2;
                    $ret.f = 1;
                    return $ret;
                })()));
                $ret.addItem(timeSpanFromSeconds(30), ((() => {
                    let $ret = new TimeAxisInterval();
                    $ret.b = 2;
                    $ret.f = 30;
                    return $ret;
                })()));
                $ret.addItem(timeSpanFromMinutes(2), ((() => {
                    let $ret = new TimeAxisInterval();
                    $ret.b = 3;
                    $ret.f = 2;
                    return $ret;
                })()));
                $ret.addItem(timeSpanFromMinutes(5), ((() => {
                    let $ret = new TimeAxisInterval();
                    $ret.b = 3;
                    $ret.f = 5;
                    return $ret;
                })()));
                $ret.addItem(timeSpanFromHours(1), ((() => {
                    let $ret = new TimeAxisInterval();
                    $ret.b = 4;
                    $ret.f = 1;
                    return $ret;
                })()));
                $ret.addItem(timeSpanFromDays(1), ((() => {
                    let $ret = new TimeAxisInterval();
                    $ret.b = 5;
                    $ret.f = 1;
                    return $ret;
                })()));
                $ret.addItem(timeSpanFromDays(2), ((() => {
                    let $ret = new TimeAxisInterval();
                    $ret.b = 5;
                    $ret.f = 2;
                    return $ret;
                })()));
                $ret.addItem(timeSpanFromDays(5), ((() => {
                    let $ret = new TimeAxisInterval();
                    $ret.b = 5;
                    $ret.f = 5;
                    return $ret;
                })()));
                $ret.addItem(timeSpanFromDays(10), ((() => {
                    let $ret = new TimeAxisInterval();
                    $ret.b = 5;
                    $ret.f = 10;
                    return $ret;
                })()));
                $ret.addItem(timeSpanFromDays(30), ((() => {
                    let $ret = new TimeAxisInterval();
                    $ret.b = 7;
                    $ret.f = 1;
                    return $ret;
                })()));
                $ret.addItem(timeSpanFromDays(60), ((() => {
                    let $ret = new TimeAxisInterval();
                    $ret.b = 7;
                    $ret.f = 3;
                    return $ret;
                })()));
                $ret.addItem(timeSpanFromDays(365), ((() => {
                    let $ret = new TimeAxisInterval();
                    $ret.b = 8;
                    $ret.f = 1;
                    return $ret;
                })()));
                $ret.addItem(timeSpanFromDays(365 * 10), ((() => {
                    let $ret = new TimeAxisInterval();
                    $ret.b = 8;
                    $ret.f = 10;
                    return $ret;
                })()));
                return $ret;
            })());
        }
        return TimeXAxis.lw;
    }
    l1() {
        if (this.cn && (this.aw == null || this.aw.angle == 0)) {
            let a = this.d5();
            let b = this.iu.width;
            let c = truncate(Math.floor(b / a));
            let d = Number.MAX_VALUE;
            let e = c == 0 ? d : timeSpanFromTicks((intDivide(timeSpanTicks(this.mr), c)));
            let f = null;
            for (let g of fromEnum(TimeXAxis.ly())) {
                f = g.value;
                if (g.key > e) {
                    break;
                }
            }
            return f;
        }
        let h = this.mr;
        if (h == (0)) {
            return ((() => {
                let $ret = new TimeAxisInterval();
                $ret.b = 8;
                $ret.f = 1;
                return $ret;
            })());
        }
        if (h >= timeSpanFromDays(365 * 100)) {
            return ((() => {
                let $ret = new TimeAxisInterval();
                $ret.b = 8;
                $ret.f = 10;
                return $ret;
            })());
        }
        if (h >= timeSpanFromDays(365 * 5)) {
            return ((() => {
                let $ret = new TimeAxisInterval();
                $ret.b = 8;
                $ret.f = 1;
                return $ret;
            })());
        }
        if (h >= timeSpanFromDays(365 * 2)) {
            return ((() => {
                let $ret = new TimeAxisInterval();
                $ret.b = 7;
                $ret.f = 3;
                return $ret;
            })());
        }
        if (h >= timeSpanFromDays(150)) {
            return ((() => {
                let $ret = new TimeAxisInterval();
                $ret.b = 7;
                $ret.f = 1;
                return $ret;
            })());
        }
        if (h >= timeSpanFromDays(60)) {
            return ((() => {
                let $ret = new TimeAxisInterval();
                $ret.b = 5;
                $ret.f = 10;
                return $ret;
            })());
        }
        if (h >= timeSpanFromDays(30)) {
            return ((() => {
                let $ret = new TimeAxisInterval();
                $ret.b = 5;
                $ret.f = 5;
                return $ret;
            })());
        }
        if (h >= timeSpanFromDays(20)) {
            return ((() => {
                let $ret = new TimeAxisInterval();
                $ret.b = 5;
                $ret.f = 2;
                return $ret;
            })());
        }
        if (h >= timeSpanFromDays(4)) {
            return ((() => {
                let $ret = new TimeAxisInterval();
                $ret.b = 5;
                $ret.f = 1;
                return $ret;
            })());
        }
        if (h >= timeSpanFromDays(1)) {
            return ((() => {
                let $ret = new TimeAxisInterval();
                $ret.b = 4;
                $ret.f = 12;
                return $ret;
            })());
        }
        if (h >= timeSpanFromHours(6)) {
            return ((() => {
                let $ret = new TimeAxisInterval();
                $ret.b = 4;
                $ret.f = 1;
                return $ret;
            })());
        }
        if (h >= timeSpanFromMinutes(30)) {
            return ((() => {
                let $ret = new TimeAxisInterval();
                $ret.b = 3;
                $ret.f = 5;
                return $ret;
            })());
        }
        if (h >= timeSpanFromMinutes(10)) {
            return ((() => {
                let $ret = new TimeAxisInterval();
                $ret.b = 3;
                $ret.f = 2;
                return $ret;
            })());
        }
        return ((() => {
            let $ret = new TimeAxisInterval();
            $ret.b = 2;
            $ret.f = 30;
            return $ret;
        })());
    }
    l2() {
        if (this.intervals == null || this.intervals.count == 0) {
            return this.l1();
        }
        if (this.mr == (0)) {
            return ((() => {
                let $ret = new TimeAxisInterval();
                $ret.b = 8;
                $ret.f = 1;
                return $ret;
            })());
        }
        let a = null;
        for (let b of fromEnum(this.intervals)) {
            if (b.k > this.mr) {
                continue;
            }
            if (a == null || b.k > a.k) {
                a = b;
            }
        }
        return a == null ? this.l1() : a;
    }
    g1(a) {
        super.g1(a);
        let b = this.bf != null ? this.bf.mq : Rect.empty;
        let c = !b.isEmpty ? this.iu : Rect.empty;
        let d = this.ip();
        let e = this.io();
        let f = new ScalerParams(0, b, c, this.b8, d);
        if (this.kq == null && !this.kw() && !this.kx()) {
            return;
        }
        if (!b.isEmpty && !c.isEmpty) {
            let g = c.bottom;
            let h = g - c.top;
            if (this.ag != null) {
                let i = typeCast(NumericYAxis.$, this.ag);
                if (i != null) {
                    let j = this.g4(g, h, b, c, d, c.top, c.bottom);
                    g = j.p0;
                    h = j.p1;
                }
            }
            if (isNaN_(g)) {
                g = 0;
            }
            this.gk(this.a2.ax(), g, c, this.a2.e(), true);
            this.at.a8 = h;
            let k = truncate(Math.ceil(e.right));
            let l = truncate(Math.floor(e.left));
            let m = this.a2.a0();
            let n = this.a2.f();
            let o = this.a2.ay();
            this.lr();
            let p = new List$1(Date_$type, 1, this.l7());
            let q = new List$1(Number_$type, 0);
            for (let r = 0; r < p.count; r++) {
                q.add(p._inner[r].getTime());
            }
            this.gh(q, 0, q.count, f);
            for (let s = 0; s < p.count; s++) {
                let t = p._inner[s];
                let u = q._inner[s];
                let v = truncate(Math.round(u));
                if (v <= k) {
                    if (s % 2 == 0) {
                        let w = s < q.count - 1 ? q._inner[s + 1] : e.right;
                        this.ha(m, u, w, c);
                    }
                    this.g9(o, u, c, n, false);
                    this.jf.add(u);
                }
                if (v >= l && v <= k) {
                    let x = this.e1(t);
                    if (x != null) {
                        this.cs.add1(x);
                        this.ct.add(new LabelPosition(u));
                    }
                }
            }
        }
        if ((this.aw == null || this.aw.visibility == 0) && this.ag != null) {
            if (this.aw != null && (this.aw.location == 4 || this.aw.location == 5)) {
                this.bf.ht();
            }
        }
        this.at.a1 = this.cs;
        this.at.a2 = this.ct;
        this.kf();
    }
    l7() {
        let a = this.bf != null ? this.bf.mq : Rect.empty;
        let b = !a.isEmpty ? this.iu : Rect.empty;
        let c = this.ip();
        let d = this.io();
        let e = new ScalerParams(0, a, b, this.b8, c);
        let f = this.l2();
        let g = this.visibleMinimum;
        if (!this.kx() && 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);
            }
        }
        let h = new List$1(Date_$type, 0);
        let i = 0;
        let j = 10000;
        if (f.f == 0) {
            return h;
        }
        while (g <= this.visibleMaximum && i++ < j) {
            let k = this.l9(g);
            let l = g;
            if (k.key != null) {
                l = f.d(k.key.c);
            }
            h.add(l);
            while (g <= l) {
                g = f.c(g);
            }
        }
        return h;
    }
    l9(a) {
        if (this.lx == null) {
            return new KeyValuePair$2(TimeAxisBreak.$, Number_$type);
        }
        for (let b of fromEnum(this.lx)) {
            if (b.key.d <= a && b.key.c >= a) {
                return b;
            }
        }
        return new KeyValuePair$2(TimeAxisBreak.$, Number_$type);
    }
    get visibleMinimum() {
        return this.mb;
    }
    set visibleMinimum(a) {
        this.mb = a;
    }
    get visibleMaximum() {
        return this.ma;
    }
    set visibleMaximum(a) {
        this.ma = a;
    }
    lr() {
        super.lr();
        if (this.bf == null || !Extensions.b(this.bf.m3)) {
            return;
        }
        let a = new ScalerParams(0, this.bf.mq, this.bf.m3, this.b8, this.bf.mu);
        let b = this.bf.m3.left;
        let c = this.bf.m3.right;
        if (this.categoryMode != 0) {
            let d = this.getCategorySize(a.e, a.d, a.c);
            b += d / 2;
            c -= d / 2;
        }
        let e = truncate(this.dw(b, a));
        let f = truncate(this.dw(c, a));
        if (e < 0 || f < 0) {
            this.mr = (0);
            return;
        }
        e = Math.min(dateMaxValue().getTime(), e);
        f = Math.min(dateMaxValue().getTime(), f);
        let g = dateFromTicks(Math.min(e, f));
        let h = dateFromTicks(Math.max(e, f));
        let i = +(this.visibleMinimum) != +g || +(this.visibleMaximum) != +h;
        this.visibleMinimum = g;
        this.visibleMaximum = h;
        if (i && this.visibleRangeChanged != null) {
            this.visibleRangeChanged(this, EventArgs.empty);
        }
        this.mr = +(this.visibleMaximum) - +(this.visibleMinimum);
    }
    jm(a, b, c, d, e) {
        return 0;
    }
    i$f(a) {
        let b = this;
        if (b.i$b == null) {
            return -1;
        }
        let c = new SortedListView$1(Date_$type, this.kq, b.i$b);
        let ticks_ = a;
        let d = (new Date(ticks_));
        let e = this.la(d, c);
        e = Math.max(0, Math.min(b.i$b.count - 1, e));
        let f = c.item(e);
        let g;
        let h;
        if (f < d) {
            g = e;
            h = g + 1;
        }
        else {
            h = e;
            g = e - 1;
        }
        if (g < 0) {
            return h;
        }
        if (h >= b.i$b.count) {
            return g;
        }
        let i = c.item(g);
        let j = c.item(h);
        let k = new ScalerParams(0, this.bf.m4, this.iu, this.b8, this.bf.mu);
        this.startCursor();
        let l = this.dt(i.getTime(), k);
        let m = this.dt(a, k);
        let n = this.dt(j.getTime(), k);
        this.resetCursor();
        if (i < this.visibleMinimum && j <= this.visibleMaximum) {
            return this.kz._inner[h];
        }
        if (j > this.visibleMaximum && i >= this.visibleMinimum) {
            return this.kz._inner[g];
        }
        if (Math.abs(l - m) < Math.abs(n - m)) {
            return this.kz._inner[g];
        }
        else {
            return this.kz._inner[h];
        }
    }
    mu(a, b) {
        this.mt();
        this.ms();
        if (this.at != null) {
            this.at.ak = this.breaks.count > 0;
        }
        this.g0(false);
        for (let c of fromEnum(this.cr())) {
            c.i6(false);
        }
    }
    mw(a, b) {
        this.g0(false);
    }
    mv(a, b) {
        this.g0(false);
    }
    get_b3() {
        return false;
    }
    getWindowRectForSelection(a, b) {
        let c = +(this.actualMaximumValue) - +(this.actualMinimumValue);
        let d = timeSpanTotalMilliseconds((+a - +(this.actualMinimumValue))) / timeSpanTotalMilliseconds(c);
        let 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);
    }
    getCategorySize(a, b, c) {
        if (this.mr != (0)) {
            let d = timeSpanFromTicks(truncate((timeSpanTicks(this.mr) * (a.width / (this.bf != null ? this.bf.mq.width : 1)))));
            let e = timeSpanTicks(d) / timeSpanTicks(this.mp);
            let f = b.width / 4;
            return Math.min(f, b.width / (e + 1));
        }
        return super.getCategorySize(a, b, c);
    }
    static mo(a, b) {
        let c = Math.min(5, b);
        let d = a(0);
        let e = new Dictionary$2(Number_$type, Number_$type, 0);
        let f = 0;
        let g = (0);
        for (let h = 0; h < c; h++) {
            let i = a(h);
            let 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;
    }
    mn() {
        if (this.kq == null || this.kq.count < 2 || this.kz == null) {
            return (0);
        }
        return TimeXAxis.mo((a) => this.kq.item(this.kz._inner[a]), this.kq.count);
    }
    ln() {
        super.ln();
        this.mp = this.mn();
    }
    static ml(a) {
        if (a >= timeSpanFromDays(1)) {
            return "D";
        }
        return "g";
    }
    itemLabelFormat() {
        return TimeXAxis.ml(this.mp);
    }
    k9(a, b, c, d) {
        return this.k8(this.visibleMinimum);
    }
    lc(a, b, c, d) {
        return this.lb(this.visibleMaximum);
    }
    get isVertical() {
        return this.ce;
    }
    j$c(a, b) {
        return this.dw.apply(this, arguments);
    }
    j$b(a, b) {
        return this.dt.apply(this, arguments);
    }
    j$d(a, b, c, d, e) {
        this.gg.apply(this, arguments);
    }
    j$e(a, b, c, d) {
        this.gh.apply(this, arguments);
    }
    j$f(a, b, c, d) {
        this.gi.apply(this, arguments);
    }
    get j$a() {
        return this.b8;
    }
    get i$a() {
        return this.b5;
    }
}
TimeXAxis.$t = markType(TimeXAxis, 'TimeXAxis', TimeAxisBase.$, [ITimeRangeSelectorAxis_$type]);
TimeXAxis.lw = null;
