/*
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";
import { CategoryAxisBase } from "./CategoryAxisBase";
import { ISortingAxis_$type } from "./ISortingAxis";
import { IDateTimeAxis_$type } from "./IDateTimeAxis";
import { DependencyProperty } from "igniteui-react-core";
import { List$1 } from "igniteui-react-core";
import { typeCast, Number_$type, Date_$type, fromEnum, runOn, delegateRemove, delegateCombine, markType, markDep, TypeRegistrar } from "igniteui-react-core";
import { ScalerParams } from "./ScalerParams";
import { Rect } from "igniteui-react-core";
import { NumericScaler } from "./NumericScaler";
import { SeriesViewer } from "./SeriesViewer";
import { SortedListView$1 } from "./SortedListView$1";
import { ArrayExtension } from "igniteui-react-core";
import { AxisRangeChangedEventArgs } from "./AxisRangeChangedEventArgs";
import { IFastItemsSource_$type } from "igniteui-react-core";
import { HorizontalAxisLabelPanel } from "./HorizontalAxisLabelPanel";
import { MathUtil } from "igniteui-react-core";
import { PropertyMetadata } from "igniteui-react-core";
import { truncate, isNaN_ } from "igniteui-react-core";
import { dateMinValue, dateSubtract, dateAdd, dateMaxValue, dateFromTicks } from "igniteui-react-core";
/**
 * @hidden
 */
export let TimeAxisBase = /*@__PURE__*/ (() => {
    class TimeAxisBase extends CategoryAxisBase {
        constructor() {
            super();
            this.k4 = null;
            this.ld = null;
            this.k8 = false;
            this.lg = dateMaxValue();
            this.lf = dateMinValue();
            FastItemDateTimeColumn.o();
        }
        dy(a, b) {
            if (!b.c.isEmpty) {
                let c = this.ll(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.ll(a, b.e, b.d, b.c);
        }
        e6(a, b) {
            let c = this.f0(b);
            if (c == this.dateTimeMemberPath) {
                return this.e7(a, b, this.dateTimeMemberPath);
            }
            return super.e6(a, b);
        }
        ll(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.ca) {
                e = 1 - e;
                f = -f;
            }
            return c.left + c.width * (e - b.left) / b.width - f;
        }
        lm(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.ca) {
                d = 1 - d;
                e = -e;
            }
            return c.left + c.width * (d - b.left) / b.width - e;
        }
        d1(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.ca) {
                e = 1 - e;
            }
            return truncate(Math.floor(this.actualMinimumValue.getTime() + e * (this.actualMaximumValue.getTime() - this.actualMinimumValue.getTime())));
        }
        i$h() {
            this.l1();
        }
        l1() {
            let a = this.bh != null ? this.bh.n2 : Rect.empty;
            let b = !a.isEmpty ? this.i9 : Rect.empty;
            let c = (new Date());
            let d = (new Date());
            let e = false;
            if (!a.isEmpty && !b.isEmpty && this.k5 != null) {
                let f = typeCast(FastItemDateTimeColumn.$, this.k5);
                if (f != null) {
                    if (this.le == null) {
                        this.l2();
                    }
                }
                else {
                    this.le = null;
                }
                if (this.k5.count > 0) {
                    e = true;
                    let g = this.le == null ? 0 : this.le._inner[0];
                    let h = this.le == null ? this.k5.count - 1 : this.le._inner[this.k5.count - 1];
                    c = this.k5.item(g);
                    d = this.k5.item(h);
                    this.k9 = false;
                    if (this.categoryMode == 2) {
                        let i = this.lz(c, d);
                        if (+c != +(dateMinValue())) {
                            c = dateSubtract(+c, +i);
                        }
                        d = dateAdd(+d, +i);
                    }
                }
            }
            if (this.lc()) {
                e = true;
                c = this.minimumValue;
            }
            if (this.lb()) {
                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.l6();
            }
        }
        l6() {
        }
        get k5() {
            return this.k4;
        }
        set k5(a) {
            if (this.k4 != a) {
                let b = this.k4;
                this.k4 = a;
                this.g5("DateTimeColumn", b, this.k4);
            }
        }
        get le() {
            return this.ld;
        }
        set le(a) {
            this.ld = a;
        }
        get i$b() {
            if (this.le == null) {
                let a = typeCast(FastItemDateTimeColumn.$, this.k5);
                if (a != null) {
                    this.l2();
                }
                else {
                    this.le = null;
                }
            }
            return this.le;
        }
        l2() {
            if (this.la) {
                this.le = new List$1(Number_$type, 0);
                let a = this.le;
                let b = this.k5.count;
                for (let c = 0; c < b; c++) {
                    a.add(c);
                }
            }
            else {
                let d = typeCast(FastItemDateTimeColumn.$, this.k5);
                this.le = d.h();
            }
        }
        i$e(a, b, c, d) {
            return this.lo(a, b, c, d);
        }
        lo(a, b, c, d) {
            if (b.isEmpty) {
                return -1;
            }
            let e = d;
            let f = new ScalerParams(0, a, b, this.ca, c);
            let g, h;
            if (this.ca) {
                h = this.d1(e.left, f);
                g = this.d1(e.right, f);
            }
            else {
                g = this.d1(e.left, f);
                h = this.d1(e.right, f);
            }
            let i = Math.max(dateMinValue().getTime(), Math.min(dateMaxValue().getTime(), truncate(g)));
            return this.ln(dateFromTicks(i));
        }
        ln(a) {
            if (this.k5 == null || this.le.count != this.k5.count) {
                return -1;
            }
            let b = new SortedListView$1(Date_$type, this.k5, this.le);
            let c = this.lp(a, b);
            if (c >= 0 && c < this.le.count) {
                let d = this.k5.item(this.le._inner[c]);
                if (d > a) {
                    c--;
                    if (c < 0) {
                        c = 0;
                    }
                }
                return c;
            }
            return this.le.count - 1;
        }
        i$g(a, b, c, d) {
            return this.lr(a, b, c, d);
        }
        lr(a, b, c, d) {
            if (b.isEmpty) {
                return -1;
            }
            let e = d;
            let f = new ScalerParams(0, a, b, this.ca, c);
            let g, h;
            if (this.ca) {
                h = this.d1(e.left, f);
                g = this.d1(e.right, f);
            }
            else {
                g = this.d1(e.left, f);
                h = this.d1(e.right, f);
            }
            let i = Math.max(dateMinValue().getTime(), Math.min(dateMaxValue().getTime(), truncate(h)));
            return this.lq(dateFromTicks(i));
        }
        lq(a) {
            if (this.k5 == null || this.le.count != this.k5.count) {
                return -1;
            }
            let b = new SortedListView$1(Date_$type, this.k5, this.le);
            let c = this.lp(a, b);
            if (c >= this.le.count) {
                c = this.le.count - 1;
            }
            if (c >= 0 && c < this.le.count) {
                let d = this.k5.item(this.le._inner[c]);
                if (d < a) {
                    c++;
                    if (c > this.le.count - 1) {
                        c = this.le.count - 1;
                    }
                }
                return c;
            }
            return 0;
        }
        i$d(a) {
            if (this.k5 == null) {
                return NaN;
            }
            let b = this.k4.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.k5, b.i$b);
            let ticks_ = a;
            let d = (new Date(ticks_));
            let e = this.lp(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.i4();
            if (e == b.i$b.count && this.bh != null && this.dy(a, new ScalerParams(0, this.bh.n2, this.i9, this.ca, i)) < (this.i9.right + 2)) {
                return e - 1;
            }
            if (e == -1 && this.bh != null && Math.abs(this.dy(a, new ScalerParams(0, this.bh.n2, this.i9, this.ca, i)) - this.i9.left) < 2) {
                return 0;
            }
            return -1;
        }
        lp(a, b) {
            let c = this;
            if (this.k5 == 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.k5 == null || b.i$b.count != this.k5.count) {
                return -1;
            }
            let c = new SortedListView$1(Date_$type, this.k5, b.i$b);
            let ticks_ = a;
            let d = (new Date(ticks_));
            let e = this.lp(d, c);
            let f = this.i4();
            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.bh != null && this.dy(a, new ScalerParams(0, this.bh.n2, this.i9, this.ca, f)) < (this.i9.right + 2)) {
                return e - 1;
            }
            if (e == -1 && this.bh != null && Math.abs(this.dy(a, new ScalerParams(0, this.bh.n2, this.i9, this.ca, f)) - this.i9.left) < 2) {
                return 0;
            }
            return -1;
        }
        i$i() {
            this.le = null;
            this.ha();
        }
        get_k9() {
            return this.k8;
        }
        set_k9(a) {
            this.k8 = a;
        }
        get k9() {
            return this.get_k9();
        }
        set k9(a) {
            this.set_k9(a);
        }
        g1(a) {
            super.g1(a);
            this.dateTimeMemberPath = a;
        }
        g4(a) {
            super.g4(a);
            this.minimumValue = a;
        }
        g3(a) {
            super.g4(a);
            this.maximumValue = a;
        }
        cp() {
            if (this.ci && this.cq) {
                this.bb = this.ba();
            }
            let a = this.actualMinimumValue.getTime();
            let b = this.actualMaximumValue.getTime();
            this.l1();
            let c = !this.lc() ? this.actualMinimumValue.getTime() : this.minimumValue.getTime();
            let d = !this.lb() ? this.actualMaximumValue.getTime() : this.maximumValue.getTime();
            let e = new AxisRangeChangedEventArgs(0, a, c, b, d);
            this.g6(e);
            return true;
        }
        lc() {
            if (+(this.minimumValue) == +(dateMinValue())) {
                return false;
            }
            return (this.minimumValue != null);
        }
        lb() {
            if (+(this.maximumValue) == +(dateMaxValue())) {
                return false;
            }
            return (this.maximumValue != null);
        }
        get actualMinimumValue() {
            return this.lg;
        }
        set actualMinimumValue(a) {
            let b = +(this.lg) != +a;
            if (b) {
                let c = this.lg;
                this.lg = a;
                this.g5("ActualMinimumValue", c, a);
            }
        }
        fb() {
            return this.actualMinimumValue;
        }
        get actualMaximumValue() {
            return this.lf;
        }
        set actualMaximumValue(a) {
            let b = +(this.lf) != +a;
            if (b) {
                let c = this.lf;
                this.lf = a;
                this.g5("ActualMaximumValue", c, a);
            }
        }
        fa() {
            return this.maximumValue;
        }
        l3(a) {
            this.hc(a);
            if (this.jn == null) {
                return;
            }
            for (let b of fromEnum(this.cw())) {
                b.jl(a);
            }
        }
        gz(a, b, c, d) {
            super.gz(a, b, c, d);
            switch (b) {
                case "FastItemsSource":
                    let e = typeCast(IFastItemsSource_$type, c);
                    if (e != null) {
                        e.deregisterColumn(this.k5);
                        this.k5 = null;
                        e.event = delegateRemove(e.event, runOn(this, this.l0));
                    }
                    let f = typeCast(IFastItemsSource_$type, d);
                    if (f != null) {
                        this.le = null;
                        this.k5 = this.k6(this.dateTimeMemberPath);
                        f.event = delegateCombine(f.event, runOn(this, this.l0));
                    }
                    this.l3(false);
                    break;
                case TimeAxisBase.$$p[0]:
                    if (this.jn != null) {
                        this.jn.deregisterColumn(this.k5);
                        this.k5 = this.k6(this.dateTimeMemberPath);
                        this.le = null;
                    }
                    break;
                case TimeAxisBase.$$p[3]:
                    this.cn();
                    this.l3(false);
                    break;
                case TimeAxisBase.$$p[2]:
                    this.cn();
                    this.l3(false);
                    break;
                case "DateTimeColumn":
                    this.l1();
                    break;
            }
        }
        l0(a, b) {
            this.le = null;
        }
        k6(a) {
            return TimeAxisBase.k7(a, this);
        }
        static k7(a, b) {
            if (a == null) {
                return b.jn.registerColumnDateTime(null, null, false);
            }
            let c = null;
            let d = SeriesViewer.bn(a, b.coercionMethods);
            c = d.a;
            return b.jn.registerColumnDateTime(a, c, b.expectFunctions);
        }
        au() {
            if (this.cr) {
                return (TypeRegistrar.create('HorizontalSmartAxisLabelPanel'));
            }
            else {
                return new HorizontalAxisLabelPanel();
            }
        }
        get_a1() {
            return 0;
        }
        get a1() {
            return this.get_a1();
        }
        hc(a) {
            super.hc(a);
            let b = this.a4.ay();
            let c = this.a4.a1();
            let d = this.a4.az();
            let e = this.a4.a0();
            let f = this.a4.e();
            let g = this.a4.f();
            let h = this.a4.g();
            this.hk();
            this.gn(b);
            this.gn(c);
            this.gn(d);
            this.gn(e);
            this.cx.clear();
            this.cy.clear();
            this.ju.clear();
            this.av.ac = this;
            let i = this.bh != null ? this.bh.n2 : Rect.empty;
            let j = !i.isEmpty ? this.i9 : Rect.empty;
            this.av.cq = i;
            this.av.cp = j;
            if (i.isEmpty || j.isEmpty) {
                this.bq.count = 0;
                this.bp.count = 0;
                this.bo.count = 0;
            }
            if (this.bq.count == 0) {
                this.av.ab.clear();
            }
            if (this.ay != null) {
                this.ay.registerAxis(this);
            }
            this.l1();
        }
        get_b7() {
            return true;
        }
        startCursor() {
        }
        resetCursor() {
        }
        StartTimeCursor() {
            this.startCursor();
        }
        ResetTimeCursor() {
            this.resetCursor();
        }
        getCategorySize(a, b, c) {
            if (this.j7 == 0) {
                return 0;
            }
            let d = this.i7(b, c);
            return d.width / (this.j7 * a.width);
        }
        getGroupCenter(a, b, c, d) {
            return this.getCategorySize(b, c, d) * 0.5;
        }
        jz(a, b, c) {
            let d = !isNaN_(this.jw) ? MathUtil.b(this.jw, 0, 1) : 0;
            let e = 1 - 0.5 * d;
            let f = this.getCategorySize(a, b, c) * e;
            return f;
        }
        get_cf() {
            return true;
        }
        lz(a, b) {
            return (0);
        }
        preferredCategoryMode(a) {
            return this.b5 ? 0 : 1;
        }
        get isVertical() {
            return this.ch;
        }
        j$c(a, b) {
            return this.d1.apply(this, arguments);
        }
        j$b(a, b) {
            return this.dy.apply(this, arguments);
        }
        j$d(a, b, c, d, e) {
            this.gr.apply(this, arguments);
        }
        j$e(a, b, c, d) {
            this.gs.apply(this, arguments);
        }
        j$f(a, b, c, d) {
            this.gt.apply(this, arguments);
        }
        get j$a() {
            return this.ca;
        }
        get i$a() {
            return this.b7;
        }
    }
    TimeAxisBase.$t = /*@__PURE__*/ markType(TimeAxisBase, 'TimeAxisBase', CategoryAxisBase.$, [ISortingAxis_$type, IDateTimeAxis_$type]);
    TimeAxisBase.$$p = /*@__PURE__*/ markDep(DependencyProperty, PropertyMetadata, TimeAxisBase, 'g5', ['DateTimeMemberPath::l7', [2, null], 'IsDataPreSorted:la:l8', [0, false], 'MaximumValue::l9', [3, null], 'MinimumValue::ma', [3, null]]);
    return TimeAxisBase;
})();
//# sourceMappingURL=TimeAxisBase.js.map
