/*
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-angular-core";
import { CategoryAxisBase } from "./CategoryAxisBase";
import { ISortingAxis_$type } from "./ISortingAxis";
import { IDateTimeAxis_$type } from "./IDateTimeAxis";
import { DependencyProperty } from "igniteui-angular-core";
import { List$1 } from "igniteui-angular-core";
import { typeCast, Number_$type, Date_$type, fromEnum, runOn, delegateRemove, delegateCombine, markType, markDep, TypeRegistrar } from "igniteui-angular-core";
import { ScalerParams } from "./ScalerParams";
import { Rect } from "igniteui-angular-core";
import { NumericScaler } from "./NumericScaler";
import { SeriesViewer } from "./SeriesViewer";
import { SortedListView$1 } from "./SortedListView$1";
import { ArrayExtension } from "igniteui-angular-core";
import { AxisRangeChangedEventArgs } from "./AxisRangeChangedEventArgs";
import { IFastItemsSource_$type } from "igniteui-angular-core";
import { HorizontalAxisLabelPanel } from "./HorizontalAxisLabelPanel";
import { MathUtil } from "igniteui-angular-core";
import { PropertyMetadata } from "igniteui-angular-core";
import { truncate, isNaN_ } from "igniteui-angular-core";
import { dateMinValue, dateSubtract, dateAdd, dateMaxValue, dateFromTicks } from "igniteui-angular-core";
/**
 * @hidden
 */
export class TimeAxisBase extends CategoryAxisBase {
    constructor() {
        super();
        this.kw = null;
        this.k5 = null;
        this.k0 = false;
        this.k8 = dateMaxValue();
        this.k7 = dateMinValue();
        FastItemDateTimeColumn.o();
    }
    dw(a, b) {
        if (!b.c.isEmpty) {
            let c = this.ld(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.ld(a, b.e, b.d, b.c);
    }
    e4(a, b) {
        let c = this.fv(b);
        if (c == this.dateTimeMemberPath) {
            return this.e5(a, b, this.dateTimeMemberPath);
        }
        return super.e4(a, b);
    }
    ld(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;
    }
    le(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;
    }
    dz(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) / b.d.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.lt();
    }
    lt() {
        let a = this.bf != null ? this.bf.mq : Rect.empty;
        let b = !a.isEmpty ? this.i1 : Rect.empty;
        let c = (new Date());
        let d = (new Date());
        let e = false;
        if (!a.isEmpty && !b.isEmpty && this.kx != null) {
            let f = typeCast(FastItemDateTimeColumn.$, this.kx);
            if (f != null) {
                if (this.k6 == null) {
                    this.lu();
                }
            }
            else {
                this.k6 = null;
            }
            if (this.kx.count > 0) {
                e = true;
                let g = this.k6 == null ? 0 : this.k6._inner[0];
                let h = this.k6 == null ? this.kx.count - 1 : this.k6._inner[this.kx.count - 1];
                c = this.kx.item(g);
                d = this.kx.item(h);
                this.k1 = false;
                if (this.categoryMode == 2) {
                    let i = this.lr(c, d);
                    if (+c != +(dateMinValue())) {
                        c = dateSubtract(+c, +i);
                    }
                    d = dateAdd(+d, +i);
                }
            }
        }
        if (this.k4()) {
            e = true;
            c = this.minimumValue;
        }
        if (this.k3()) {
            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.ly();
        }
    }
    ly() {
    }
    get kx() {
        return this.kw;
    }
    set kx(a) {
        if (this.kw != a) {
            let b = this.kw;
            this.kw = a;
            this.g0("DateTimeColumn", b, this.kw);
        }
    }
    get k6() {
        return this.k5;
    }
    set k6(a) {
        this.k5 = a;
    }
    get i$b() {
        if (this.k6 == null) {
            let a = typeCast(FastItemDateTimeColumn.$, this.kx);
            if (a != null) {
                this.lu();
            }
            else {
                this.k6 = null;
            }
        }
        return this.k6;
    }
    lu() {
        if (this.k2) {
            this.k6 = new List$1(Number_$type, 0);
            let a = this.k6;
            let b = this.kx.count;
            for (let c = 0; c < b; c++) {
                a.add(c);
            }
        }
        else {
            let d = typeCast(FastItemDateTimeColumn.$, this.kx);
            this.k6 = d.h();
        }
    }
    i$e(a, b, c, d) {
        return this.lg(a, b, c, d);
    }
    lg(a, b, c, d) {
        if (b.isEmpty) {
            return -1;
        }
        let e = d;
        let f = new ScalerParams(0, a, b, this.b8, c);
        let g, h;
        if (this.b8) {
            h = this.dz(e.left, f);
            g = this.dz(e.right, f);
        }
        else {
            g = this.dz(e.left, f);
            h = this.dz(e.right, f);
        }
        let i = Math.max(dateMinValue().getTime(), Math.min(dateMaxValue().getTime(), truncate(g)));
        return this.lf(dateFromTicks(i));
    }
    lf(a) {
        if (this.kx == null || this.k6.count != this.kx.count) {
            return -1;
        }
        for (let b = 0; b < this.k6.count; b++) {
            let c = this.kx.item(this.k6._inner[b]);
            if (c >= a) {
                return Math.max(b - 1, 0);
            }
        }
        return this.k6.count - 1;
    }
    i$g(a, b, c, d) {
        return this.lj(a, b, c, d);
    }
    lj(a, b, c, d) {
        if (b.isEmpty) {
            return -1;
        }
        let e = d;
        let f = new ScalerParams(0, a, b, this.b8, c);
        let g, h;
        if (this.b8) {
            h = this.dz(e.left, f);
            g = this.dz(e.right, f);
        }
        else {
            g = this.dz(e.left, f);
            h = this.dz(e.right, f);
        }
        let i = Math.max(dateMinValue().getTime(), Math.min(dateMaxValue().getTime(), truncate(h)));
        return this.li(dateFromTicks(i));
    }
    li(a) {
        if (this.kx == null || this.k6.count != this.kx.count) {
            return -1;
        }
        for (let b = this.k6.count - 1; b >= 0; b--) {
            let c = this.k6._inner[b];
            let d = this.kx.item(c);
            if (d < a) {
                return b == this.k6.count - 1 ? b : b + 1;
            }
        }
        return 0;
    }
    i$d(a) {
        if (this.kx == null) {
            return NaN;
        }
        let b = this.kw.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.kx, b.i$b);
        let ticks_ = a;
        let d = (new Date(ticks_));
        let e = this.lh(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.iw();
        if (e == b.i$b.count && this.bf != null && this.dw(a, new ScalerParams(0, this.bf.mq, this.i1, this.b8, i)) < (this.i1.right + 2)) {
            return e - 1;
        }
        if (e == -1 && this.bf != null && Math.abs(this.dw(a, new ScalerParams(0, this.bf.mq, this.i1, this.b8, i)) - this.i1.left) < 2) {
            return 0;
        }
        return -1;
    }
    lh(a, b) {
        let c = this;
        if (this.kx == 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.kx == null || b.i$b.count != this.kx.count) {
            return -1;
        }
        let c = new SortedListView$1(Date_$type, this.kx, b.i$b);
        let ticks_ = a;
        let d = (new Date(ticks_));
        let e = this.lh(d, c);
        let f = this.iw();
        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.dw(a, new ScalerParams(0, this.bf.mq, this.i1, this.b8, f)) < (this.i1.right + 2)) {
            return e - 1;
        }
        if (e == -1 && this.bf != null && Math.abs(this.dw(a, new ScalerParams(0, this.bf.mq, this.i1, this.b8, f)) - this.i1.left) < 2) {
            return 0;
        }
        return -1;
    }
    i$i() {
        this.k6 = null;
        this.g5();
    }
    get_k1() {
        return this.k0;
    }
    set_k1(a) {
        this.k0 = a;
    }
    get k1() {
        return this.get_k1();
    }
    set k1(a) {
        this.set_k1(a);
    }
    gw(a) {
        super.gw(a);
        this.dateTimeMemberPath = a;
    }
    gz(a) {
        super.gz(a);
        this.minimumValue = a;
    }
    gy(a) {
        super.gz(a);
        this.maximumValue = a;
    }
    cn() {
        if (this.cg && this.co) {
            this.a9 = this.a8();
        }
        let a = this.actualMinimumValue.getTime();
        let b = this.actualMaximumValue.getTime();
        this.lt();
        let c = !this.k4() ? this.actualMinimumValue.getTime() : this.minimumValue.getTime();
        let d = !this.k3() ? this.actualMaximumValue.getTime() : this.maximumValue.getTime();
        let e = new AxisRangeChangedEventArgs(0, a, c, b, d);
        this.g1(e);
        return true;
    }
    k4() {
        if (+(this.minimumValue) == +(dateMinValue())) {
            return false;
        }
        return (this.minimumValue != null);
    }
    k3() {
        if (+(this.maximumValue) == +(dateMaxValue())) {
            return false;
        }
        return (this.maximumValue != null);
    }
    get actualMinimumValue() {
        return this.k8;
    }
    set actualMinimumValue(a) {
        let b = +(this.k8) != +a;
        if (b) {
            let c = this.k8;
            this.k8 = a;
            this.g0("ActualMinimumValue", c, a);
        }
    }
    e9() {
        return this.actualMinimumValue;
    }
    get actualMaximumValue() {
        return this.k7;
    }
    set actualMaximumValue(a) {
        let b = +(this.k7) != +a;
        if (b) {
            let c = this.k7;
            this.k7 = a;
            this.g0("ActualMaximumValue", c, a);
        }
    }
    e8() {
        return this.maximumValue;
    }
    lv(a) {
        this.g7(a);
        if (this.jf == null) {
            return;
        }
        for (let b of fromEnum(this.cu())) {
            b.jd(a);
        }
    }
    gu(a, b, c, d) {
        super.gu(a, b, c, d);
        switch (b) {
            case "FastItemsSource":
                let e = typeCast(IFastItemsSource_$type, c);
                if (e != null) {
                    e.deregisterColumn(this.kx);
                    this.kx = null;
                    e.event = delegateRemove(e.event, runOn(this, this.ls));
                }
                let f = typeCast(IFastItemsSource_$type, d);
                if (f != null) {
                    this.k6 = null;
                    this.kx = this.ky(this.dateTimeMemberPath);
                    f.event = delegateCombine(f.event, runOn(this, this.ls));
                }
                this.lv(false);
                break;
            case TimeAxisBase.$$p[0]:
                if (this.jf != null) {
                    this.jf.deregisterColumn(this.kx);
                    this.kx = this.ky(this.dateTimeMemberPath);
                    this.k6 = null;
                }
                break;
            case TimeAxisBase.$$p[3]:
                this.cl();
                this.lv(false);
                break;
            case TimeAxisBase.$$p[2]:
                this.cl();
                this.lv(false);
                break;
            case "DateTimeColumn":
                this.lt();
                break;
        }
    }
    ls(a, b) {
        this.k6 = null;
    }
    ky(a) {
        return TimeAxisBase.kz(a, this);
    }
    static kz(a, b) {
        if (a == null) {
            return b.jf.registerColumnDateTime(null, null, false);
        }
        let c = null;
        let d = SeriesViewer.bb(a, b.coercionMethods);
        c = d.a;
        return b.jf.registerColumnDateTime(a, c, b.expectFunctions);
    }
    as() {
        if (this.cp) {
            return (TypeRegistrar.create('HorizontalSmartAxisLabelPanel'));
        }
        else {
            return new HorizontalAxisLabelPanel();
        }
    }
    get_az() {
        return 0;
    }
    get az() {
        return this.get_az();
    }
    g7(a) {
        super.g7(a);
        let b = this.a2.ay();
        let c = this.a2.a1();
        let d = this.a2.az();
        let e = this.a2.a0();
        let f = this.a2.e();
        let g = this.a2.f();
        let h = this.a2.g();
        this.he();
        this.gi(b);
        this.gi(c);
        this.gi(d);
        this.gi(e);
        this.cv.clear();
        this.cw.clear();
        this.jm.clear();
        this.at.ac = this;
        let i = this.bf != null ? this.bf.mq : Rect.empty;
        let j = !i.isEmpty ? this.i1 : 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.lt();
    }
    get_b5() {
        return true;
    }
    startCursor() {
    }
    resetCursor() {
    }
    StartTimeCursor() {
        this.startCursor();
    }
    ResetTimeCursor() {
        this.resetCursor();
    }
    getCategorySize(a, b, c) {
        if (this.jy == 0) {
            return 0;
        }
        let d = this.iz(b, c);
        return d.width / (this.jy * a.width);
    }
    getGroupCenter(a, b, c, d) {
        return this.getCategorySize(b, c, d) * 0.5;
    }
    jr(a, b, c) {
        let d = !isNaN_(this.jo) ? MathUtil.b(this.jo, 0, 1) : 0;
        let e = 1 - 0.5 * d;
        let f = this.getCategorySize(a, b, c) * e;
        return f;
    }
    get_cd() {
        return true;
    }
    lr(a, b) {
        return (0);
    }
    preferredCategoryMode(a) {
        return this.b3 ? 0 : 1;
    }
    get isVertical() {
        return this.cf;
    }
    j$c(a, b) {
        return this.dz.apply(this, arguments);
    }
    j$b(a, b) {
        return this.dw.apply(this, arguments);
    }
    j$d(a, b, c, d, e) {
        this.gm.apply(this, arguments);
    }
    j$e(a, b, c, d) {
        this.gn.apply(this, arguments);
    }
    j$f(a, b, c, d) {
        this.go.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]);
TimeAxisBase.$$p = markDep(DependencyProperty, PropertyMetadata, TimeAxisBase, 'g0', ['DateTimeMemberPath::lz', [2, null], 'IsDataPreSorted:k2:l0', [0, false], 'MaximumValue::l1', [3, null], 'MinimumValue::l2', [3, null]]);
//# sourceMappingURL=TimeAxisBase.js.map