/*
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 { FastItemDateTimeColumn } from "igniteui-react-core/ES2015/FastItemDateTimeColumn";
import { CategoryAxisBase } from "./CategoryAxisBase";
import { ISortingAxis_$type } from "./ISortingAxis";
import { IDateTimeAxis_$type } from "./IDateTimeAxis";
import { DependencyProperty } from "igniteui-react-core/ES2015/DependencyProperty";
import { List$1 } from "igniteui-react-core/ES2015/List$1";
import { ScalerParams } from "./ScalerParams";
import { Rect } from "igniteui-react-core/ES2015/Rect";
import { NumericScaler } from "./NumericScaler";
import { SeriesViewer } from "./SeriesViewer";
import { Type, typeCast, Number_$type, Date_$type, fromEnum, runOn, delegateRemove, delegateCombine, markType, TypeRegistrar } from "igniteui-react-core/ES2015/type";
import { AxisRangeChangedEventArgs } from "./AxisRangeChangedEventArgs";
import { SortedListView$1 } from "./SortedListView$1";
import { ArrayExtension } from "igniteui-react-core/ES2015/ArrayExtension";
import { IFastItemsSource_$type } from "igniteui-react-core/ES2015/IFastItemsSource";
import { HorizontalAxisLabelPanel } from "./HorizontalAxisLabelPanel";
import { MathUtil } from "igniteui-react-core/ES2015/MathUtil";
import { PropertyMetadata } from "igniteui-react-core/ES2015/PropertyMetadata";
import { truncate, isNaN_ } from "igniteui-react-core/ES2015/number";
import { dateMinValue, dateSubtract, dateAdd, dateMaxValue } from "igniteui-react-core/ES2015/date";
export class TimeAxisBase extends CategoryAxisBase {
    constructor() {
        super();
        this.kp = null;
        this.ky = null;
        this.kt = false;
        this.k1 = dateMaxValue();
        this.k0 = dateMinValue();
        this.actualRangeChanged = null;
        FastItemDateTimeColumn.m();
    }
    dt(a, b) {
        if (!b.c.isEmpty) {
            let c = this.k6(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.k6(a, b.e, b.d, b.c);
    }
    k6(a, b, c, d) {
        let e;
        if (+(this.actualMaximumValue) == +(this.actualMinimumValue)) {
            e = -1;
        }
        else {
            e = (a - this.actualMinimumValue.getTime()) / (this.actualMaximumValue.getTime() - this.actualMinimumValue.getTime());
        }
        let f = 0;
        if (this.categoryMode == 2) {
            f = this.getGroupCenter(0, b, c, d);
        }
        if (this.b8) {
            e = 1 - e;
            f = -f;
        }
        return c.left + c.width * (e - b.left) / b.width - f;
    }
    k7(a, b, c) {
        let d;
        if (+(this.actualMaximumValue) == +(this.actualMinimumValue)) {
            d = -1;
        }
        else {
            d = (a - this.actualMinimumValue.getTime()) / (this.actualMaximumValue.getTime() - this.actualMinimumValue.getTime());
        }
        let e = 0;
        if (this.b8) {
            d = 1 - d;
            e = -e;
        }
        return c.left + c.width * (d - b.left) / b.width - e;
    }
    dw(a, b) {
        if (!b.c.isEmpty) {
            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) / this.width;
            f = (f * b.d.width) + b.d.left;
            f = this.getUnscaledValue3(f, NumericScaler.ac, b.c, this.categoryMode);
            return f;
        }
        return this.getUnscaledValue3(a, b.e, b.d, this.categoryMode);
    }
    getUnscaledValue3(a, b, c, d) {
        let e = b.left + b.width * (a - c.left) / c.width;
        if (this.b8) {
            e = 1 - e;
        }
        return truncate(Math.floor(this.actualMinimumValue.getTime() + e * (this.actualMaximumValue.getTime() - this.actualMinimumValue.getTime())));
    }
    i$h() {
        this.li();
    }
    li() {
        let a = this.bf != null ? this.bf.mk : Rect.empty;
        let b = !a.isEmpty ? this.it : Rect.empty;
        let c = (new Date());
        let d = (new Date());
        let e = false;
        if (!a.isEmpty && !b.isEmpty && this.kq != null) {
            let f = typeCast(FastItemDateTimeColumn.$, this.kq);
            if (f != null) {
                if (this.kz == null) {
                    this.lj();
                }
            }
            else {
                this.kz = null;
            }
            if (this.kq.count > 0) {
                e = true;
                let g = this.kz == null ? 0 : this.kz._inner[0];
                let h = this.kz == null ? this.kq.count - 1 : this.kz._inner[this.kq.count - 1];
                c = this.kq.item(g);
                d = this.kq.item(h);
                this.ku = false;
                if (this.categoryMode == 2) {
                    let i = this.lg(c, d);
                    if (+c != +(dateMinValue())) {
                        c = dateSubtract(+c, +i);
                    }
                    d = dateAdd(+d, +i);
                }
            }
        }
        if (this.kx()) {
            e = true;
            c = this.minimumValue;
        }
        if (this.kw()) {
            e = true;
            d = this.maximumValue;
        }
        let j = +(this.actualMinimumValue) != +c || +(this.actualMaximumValue) != +d;
        if (e && j) {
            let k = this.actualMinimumValue;
            let l = this.actualMaximumValue;
            this.actualMinimumValue = c;
            this.actualMaximumValue = d;
            this.ln();
            if (this.actualRangeChanged != null) {
                this.actualRangeChanged(this, new AxisRangeChangedEventArgs(0, k.getTime(), this.actualMinimumValue.getTime(), l.getTime(), this.actualMaximumValue.getTime()));
            }
        }
    }
    ln() {
    }
    get kq() {
        return this.kp;
    }
    set kq(a) {
        if (this.kp != a) {
            let b = this.kp;
            this.kp = a;
            this.gt("DateTimeColumn", b, this.kp);
        }
    }
    get kz() {
        return this.ky;
    }
    set kz(a) {
        this.ky = a;
    }
    get i$b() {
        if (this.kz == null) {
            let a = typeCast(FastItemDateTimeColumn.$, this.kq);
            if (a != null) {
                this.lj();
            }
            else {
                this.kz = null;
            }
        }
        return this.kz;
    }
    lj() {
        if (this.kv) {
            this.kz = new List$1(Number_$type, 0);
            let a = this.kz;
            let b = this.kq.count;
            for (let c = 0; c < b; c++) {
                a.add(c);
            }
        }
        else {
            let d = typeCast(FastItemDateTimeColumn.$, this.kq);
            this.kz = d.f();
        }
    }
    i$e(a, b, c, d) {
        let e = d;
        let f = new ScalerParams(0, a, b, this.b8, c);
        let g, h;
        if (this.b8) {
            h = this.dw(e.left, f);
            g = this.dw(e.right, f);
        }
        else {
            g = this.dw(e.left, f);
            h = this.dw(e.right, f);
        }
        let i = 0;
        for (let j = 0; j < this.kz.count; j++) {
            if (this.kq == null) {
                break;
            }
            let k = this.kq.item(this.kz._inner[j]);
            if (k.getTime() >= g) {
                break;
            }
            i = j;
        }
        return i;
    }
    i$g(a, b, c, d) {
        let e = d;
        let f = new ScalerParams(0, a, b, this.b8, c);
        let g, h;
        if (this.b8) {
            h = this.dw(e.left, f);
            g = this.dw(e.right, f);
        }
        else {
            g = this.dw(e.left, f);
            h = this.dw(e.right, f);
        }
        let i = this.kz.count - 1;
        let j = i;
        for (let k = i; k >= 0; k--) {
            if (this.kq == null || this.kz.count <= k) {
                break;
            }
            let l = this.kz._inner[k];
            if (l >= this.kq.count) {
                break;
            }
            let m = this.kq.item(l);
            if (m.getTime() < h) {
                break;
            }
            j = Math.min(k + 1, this.kz.count - 1);
        }
        return j;
    }
    i$d(a) {
        if (this.kq == null) {
            return NaN;
        }
        let b = this.kp.item(a);
        let c = b.getTime();
        let d = c;
        return d;
    }
    i$c(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.k8(d, c);
        if (e >= 0 && e < b.i$b.count && e - 1 >= 0 && e - 1 < b.i$b.count) {
            let diff1_ = d.getTime() - c.item(e - 1).getTime();
            let diff2_ = c.item(e).getTime() - d.getTime();
            let f = e - 1;
            let g = e;
            if (f < 0 && g >= 0) {
                return g;
            }
            if (g > b.i$b.count - 1 && f < b.i$b.count) {
                return f;
            }
            if (f < 0 && g < 0) {
                return -1;
            }
            if (f > b.i$b.count - 1 && g > b.i$b.count - 1) {
                return -1;
            }
            let h = diff1_ / (diff1_ + diff2_);
            if (isNaN_(h)) {
                h = 0;
            }
            return f + h;
        }
        if (e >= 0 && e < b.i$b.count) {
            return e;
        }
        let i = this.io();
        if (e == b.i$b.count && this.bf != null && this.dt(a, new ScalerParams(0, this.bf.mk, this.it, this.b8, i)) < (this.it.right + 2)) {
            return e - 1;
        }
        if (e == -1 && this.bf != null && Math.abs(this.dt(a, new ScalerParams(0, this.bf.mk, this.it, this.b8, i)) - this.it.left) < 2) {
            return 0;
        }
        return -1;
    }
    k8(a, b) {
        let c = this;
        if (this.kq == null || c.i$b == null) {
            return -1;
        }
        let d = -1;
        let e = ArrayExtension.binarySearch$11(Date_$type, b, (f) => {
            if (a < f) {
                return -1;
            }
            if (a > f) {
                return 1;
            }
            return 0;
        });
        if (e >= 0) {
            d = e;
        }
        else {
            d = ~e;
        }
        while (d >= 0 && d < b.count && d - 1 >= 0 && +(b.item(d)) == +(b.item(d - 1))) {
            d--;
        }
        return d;
    }
    i$f(a) {
        let b = this;
        if (b.i$b == null || this.kq == null || b.i$b.count != this.kq.count) {
            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.k8(d, c);
        let f = this.io();
        if (e >= 0 && e < b.i$b.count && e - 1 >= 0 && e - 1 < b.i$b.count) {
            let g = +d - +(c.item(e - 1));
            let h = +(c.item(e)) - +d;
            if (g < h) {
                e = e - 1;
            }
        }
        if (e >= 0 && e < b.i$b.count) {
            return b.i$b._inner[e];
        }
        if (e == b.i$b.count && this.bf != null && this.dt(a, new ScalerParams(0, this.bf.mk, this.it, this.b8, f)) < (this.it.right + 2)) {
            return e - 1;
        }
        if (e == -1 && this.bf != null && Math.abs(this.dt(a, new ScalerParams(0, this.bf.mk, this.it, this.b8, f)) - this.it.left) < 2) {
            return 0;
        }
        return -1;
    }
    i$i() {
        this.kz = null;
        this.gy();
    }
    get_ku() {
        return this.kt;
    }
    set_ku(a) {
        this.kt = a;
    }
    get ku() {
        return this.get_ku();
    }
    set ku(a) {
        this.set_ku(a);
    }
    gp(a) {
        super.gp(a);
        this.dateTimeMemberPath = a;
    }
    gs(a) {
        super.gs(a);
        this.minimumValue = a;
    }
    gr(a) {
        super.gs(a);
        this.maximumValue = a;
    }
    cm() {
        if (this.cf && this.cn) {
            this.a9 = this.a8();
        }
        let a = this.actualMinimumValue.getTime();
        let b = this.actualMaximumValue.getTime();
        this.li();
        let c = !this.kx() ? this.actualMinimumValue.getTime() : this.minimumValue.getTime();
        let d = !this.kw() ? this.actualMaximumValue.getTime() : this.maximumValue.getTime();
        let e = new AxisRangeChangedEventArgs(0, a, c, b, d);
        this.gu(e);
        return true;
    }
    kx() {
        return (this.minimumValue != null);
    }
    kw() {
        return (this.maximumValue != null);
    }
    get actualMinimumValue() {
        return this.k1;
    }
    set actualMinimumValue(a) {
        let b = +(this.k1) != +a;
        if (b) {
            let c = this.k1;
            this.k1 = a;
            this.gt("ActualMinimumValue", c, a);
        }
    }
    e3() {
        return this.actualMinimumValue;
    }
    get actualMaximumValue() {
        return this.k0;
    }
    set actualMaximumValue(a) {
        let b = +(this.k0) != +a;
        if (b) {
            let c = this.k0;
            this.k0 = a;
            this.gt("ActualMaximumValue", c, a);
        }
    }
    e2() {
        return this.maximumValue;
    }
    lk(a) {
        this.g0(a);
        if (this.i7 == null) {
            return;
        }
        for (let b of fromEnum(this.cr())) {
            b.i1(a);
        }
    }
    gn(a, b, c, d) {
        super.gn(a, b, c, d);
        switch (b) {
            case "FastItemsSource":
                let e = typeCast(IFastItemsSource_$type, c);
                if (e != null) {
                    e.deregisterColumn(this.kq);
                    this.kq = null;
                    e.event = delegateRemove(e.event, runOn(this, this.lh));
                }
                let f = typeCast(IFastItemsSource_$type, d);
                if (f != null) {
                    this.kz = null;
                    this.kq = this.kr(this.dateTimeMemberPath);
                    f.event = delegateCombine(f.event, runOn(this, this.lh));
                }
                this.lk(false);
                break;
            case TimeAxisBase.$$p[0]:
                if (this.i7 != null) {
                    this.i7.deregisterColumn(this.kq);
                    this.kq = this.kr(this.dateTimeMemberPath);
                    this.kz = null;
                }
                break;
            case TimeAxisBase.$$p[3]:
                this.ck();
                this.lk(false);
                break;
            case TimeAxisBase.$$p[2]:
                this.ck();
                this.lk(false);
                break;
            case "DateTimeColumn":
                this.li();
                break;
        }
    }
    lh(a, b) {
        this.kz = null;
    }
    kr(a) {
        return TimeAxisBase.ks(a, this);
    }
    static ks(a, b) {
        if (a == null) {
            return b.i7.registerColumnDateTime(null, null, false);
        }
        let c = null;
        let d = SeriesViewer.bb(a, b.coercionMethods);
        c = d.a;
        return b.i7.registerColumnDateTime(a, c, b.expectFunctions);
    }
    as() {
        if (this.co) {
            return (TypeRegistrar.create('HorizontalSmartAxisLabelPanel'));
        }
        else {
            return new HorizontalAxisLabelPanel();
        }
    }
    get_az() {
        return 0;
    }
    get az() {
        return this.get_az();
    }
    g0(a) {
        super.g0(a);
        let b = this.a2.ax();
        let c = this.a2.a0();
        let d = this.a2.ay();
        let e = this.a2.az();
        let f = this.a2.e();
        let g = this.a2.f();
        let h = this.a2.g();
        this.g7();
        this.gb(b);
        this.gb(c);
        this.gb(d);
        this.gb(e);
        this.cs.clear();
        this.ct.clear();
        this.je.clear();
        this.at.ac = this;
        let i = this.bf != null ? this.bf.mk : Rect.empty;
        let j = !i.isEmpty ? this.it : Rect.empty;
        this.at.cp = i;
        this.at.co = j;
        if (i.isEmpty || j.isEmpty) {
            this.bo.count = 0;
            this.bn.count = 0;
            this.bm.count = 0;
        }
        if (this.bo.count == 0) {
            this.at.ab.clear();
        }
        if (this.aw != null) {
            this.aw.registerAxis(this);
        }
        this.li();
    }
    get_b5() {
        return true;
    }
    get b5() {
        return this.get_b5();
    }
    startCursor() {
    }
    resetCursor() {
    }
    StartTimeCursor() {
        this.startCursor();
    }
    ResetTimeCursor() {
        this.resetCursor();
    }
    getCategorySize(a, b, c) {
        if (this.jq == 0) {
            return 0;
        }
        let d = this.ir(b, c);
        return d.width / (this.jq * a.width);
    }
    getGroupCenter(a, b, c, d) {
        return this.getCategorySize(b, c, d) * 0.5;
    }
    jj(a, b, c) {
        let d = !isNaN_(this.jg) ? MathUtil.b(this.jg, 0, 1) : 0;
        let e = 1 - 0.5 * d;
        let f = this.getCategorySize(a, b, c) * e;
        return f;
    }
    get_cc() {
        return true;
    }
    get cc() {
        return this.get_cc();
    }
    lg(a, b) {
        return (0);
    }
    preferredCategoryMode(a) {
        return this.b3 ? 0 : 1;
    }
    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.gf.apply(this, arguments);
    }
    j$e(a, b, c, d) {
        this.gg.apply(this, arguments);
    }
    j$f(a, b, c, d) {
        this.gh.apply(this, arguments);
    }
    get j$a() {
        return this.b8;
    }
    get i$a() {
        return this.b5;
    }
}
TimeAxisBase.$t = markType(TimeAxisBase, 'TimeAxisBase', CategoryAxisBase.$, [ISortingAxis_$type, IDateTimeAxis_$type]);
Type.dep(DependencyProperty, PropertyMetadata, TimeAxisBase, 'gt', ['DateTimeMemberPath::lo', [2, null], 'IsDataPreSorted:kv:lp', [0, false], 'MaximumValue::lq', [3, null], 'MinimumValue::lr', [3, null]]);
/*<BeginStatics Name="Infragistics.Controls.Charts.TimeAxisBase" />*/
/*<EndStatics Name="Infragistics.Controls.Charts.TimeAxisBase" />*/
