/*
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 { PropertyMetadata } from "igniteui-angular-core";
import { truncate, isNaN_ } from "igniteui-angular-core";
import { dateMinValue, dateSubtract, dateAdd, dateMaxValue, dateFromTicks } from "igniteui-angular-core";
/**
 * @hidden
 */
export let TimeAxisBase = /*@__PURE__*/ (() => {
    class TimeAxisBase extends CategoryAxisBase {
        constructor() {
            super();
            this.p7 = null;
            this.qg = null;
            this.qb = false;
            this.qj = dateMaxValue();
            this.qi = dateMinValue();
            FastItemDateTimeColumn.o();
        }
        ag() {
            return [this.actualMinimumValue.getTime(), this.actualMaximumValue.getTime()];
        }
        ft(a, b) {
            if (!b.c.isEmpty) {
                let c = this.qo(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.qo(a, b.e, b.d, b.c);
        }
        hd(a, b) {
            let c = this.ix(b);
            if (c == TimeAxisBase.$$p[0]) {
                return this.he(a, b, this.dateTimeMemberPath);
            }
            return super.hd(a, b);
        }
        qo(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.df) {
                e = 1 - e;
                f = -f;
            }
            return c.left + c.width * (e - b.left) / b.width - f;
        }
        qp(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.df) {
                d = 1 - d;
                e = -e;
            }
            return c.left + c.width * (d - b.left) / b.width - e;
        }
        fw(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.df) {
                e = 1 - e;
            }
            return truncate(Math.floor(this.actualMinimumValue.getTime() + e * (this.actualMaximumValue.getTime() - this.actualMinimumValue.getTime())));
        }
        i$h() {
            this.q4();
        }
        q4() {
            let a = this.b0 != null ? this.b0.xm : Rect.empty;
            let b = !a.isEmpty ? this.n1 : Rect.empty;
            let c = (new Date());
            let d = (new Date());
            let e = false;
            if (!a.isEmpty && !b.isEmpty && this.p8 != null) {
                let f = typeCast(FastItemDateTimeColumn.$, this.p8);
                if (f != null) {
                    if (this.qh == null) {
                        this.q5();
                    }
                }
                else {
                    this.qh = null;
                }
                if (this.p8.count > 0) {
                    e = true;
                    let g = this.qh == null ? 0 : this.qh._inner[0];
                    let h = this.qh == null ? this.p8.count - 1 : this.qh._inner[this.p8.count - 1];
                    c = this.p8.item(g);
                    d = this.p8.item(h);
                    this.qc = false;
                    if (this.categoryMode == 2) {
                        let i = this.q2(c, d);
                        if (+c != +(dateMinValue())) {
                            c = dateSubtract(+c, +i);
                        }
                        d = dateAdd(+d, +i);
                    }
                }
            }
            if (this.qf()) {
                e = true;
                c = this.minimumValue;
            }
            if (this.qe()) {
                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.q9();
            }
        }
        q9() {
        }
        get p8() {
            return this.p7;
        }
        set p8(a) {
            if (this.p7 != a) {
                let b = this.p7;
                this.p7 = a;
                this.kx("DateTimeColumn", b, this.p7);
            }
        }
        get qh() {
            return this.qg;
        }
        set qh(a) {
            this.qg = a;
        }
        get i$b() {
            if (this.qh == null) {
                let a = typeCast(FastItemDateTimeColumn.$, this.p8);
                if (a != null) {
                    this.q5();
                }
                else {
                    this.qh = null;
                }
            }
            return this.qh;
        }
        q5() {
            if (this.qd) {
                this.qh = new List$1(Number_$type, 0);
                let a = this.qh;
                let b = this.p8.count;
                for (let c = 0; c < b; c++) {
                    a.add(c);
                }
            }
            else {
                let d = typeCast(FastItemDateTimeColumn.$, this.p8);
                this.qh = d.h();
            }
        }
        i$e(a, b, c, d) {
            return this.qr(a, b, c, d);
        }
        qr(a, b, c, d) {
            if (b.isEmpty) {
                return -1;
            }
            let e = d;
            let f = new ScalerParams(0, a, b, this.df, c);
            let g, h;
            if (this.df) {
                h = this.fw(e.left, f);
                g = this.fw(e.right, f);
            }
            else {
                g = this.fw(e.left, f);
                h = this.fw(e.right, f);
            }
            let i = Math.max(dateMinValue().getTime(), Math.min(dateMaxValue().getTime(), truncate(g)));
            return this.qq(dateFromTicks(i));
        }
        qq(a) {
            if (this.p8 == null || this.qh.count != this.p8.count) {
                return -1;
            }
            let b = new SortedListView$1(Date_$type, this.p8, this.qh);
            let c = this.qs(a, b);
            if (c >= 0 && c < this.qh.count) {
                let d = this.p8.item(this.qh._inner[c]);
                if (d > a) {
                    c--;
                    if (c < 0) {
                        c = 0;
                    }
                }
                return c;
            }
            return this.qh.count - 1;
        }
        i$g(a, b, c, d) {
            return this.qu(a, b, c, d);
        }
        qu(a, b, c, d) {
            if (b.isEmpty) {
                return -1;
            }
            let e = d;
            let f = new ScalerParams(0, a, b, this.df, c);
            let g, h;
            if (this.df) {
                h = this.fw(e.left, f);
                g = this.fw(e.right, f);
            }
            else {
                g = this.fw(e.left, f);
                h = this.fw(e.right, f);
            }
            let i = Math.max(dateMinValue().getTime(), Math.min(dateMaxValue().getTime(), truncate(h)));
            return this.qt(dateFromTicks(i));
        }
        qt(a) {
            if (this.p8 == null || this.qh.count != this.p8.count) {
                return -1;
            }
            let b = new SortedListView$1(Date_$type, this.p8, this.qh);
            let c = this.qs(a, b);
            if (c >= this.qh.count) {
                c = this.qh.count - 1;
            }
            if (c >= 0 && c < this.qh.count) {
                let d = this.p8.item(this.qh._inner[c]);
                if (d < a) {
                    c++;
                    if (c > this.qh.count - 1) {
                        c = this.qh.count - 1;
                    }
                }
                return c;
            }
            return 0;
        }
        i$d(a) {
            if (this.p8 == null) {
                return NaN;
            }
            let b = this.p7.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.p8, b.i$b);
            let ticks_ = a;
            let d = (new Date(ticks_));
            let e = this.qs(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.nw();
            if (e == b.i$b.count && this.b0 != null && this.ft(a, new ScalerParams(0, this.b0.xm, this.n1, this.df, i)) < (this.n1.right + 2)) {
                return e - 1;
            }
            if (e == -1 && this.b0 != null && Math.abs(this.ft(a, new ScalerParams(0, this.b0.xm, this.n1, this.df, i)) - this.n1.left) < 2) {
                return 0;
            }
            return -1;
        }
        qs(a, b) {
            let c = this;
            if (this.p8 == 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.p8 == null || b.i$b.count != this.p8.count) {
                return -1;
            }
            let c = new SortedListView$1(Date_$type, this.p8, b.i$b);
            let ticks_ = a;
            let d = (new Date(ticks_));
            let e = this.qs(d, c);
            let f = this.nw();
            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.b0 != null && this.ft(a, new ScalerParams(0, this.b0.xm, this.n1, this.df, f)) < (this.n1.right + 2)) {
                return e - 1;
            }
            if (e == -1 && this.b0 != null && Math.abs(this.ft(a, new ScalerParams(0, this.b0.xm, this.n1, this.df, f)) - this.n1.left) < 2) {
                return 0;
            }
            return -1;
        }
        i$i() {
            this.qh = null;
            this.k2();
        }
        get_qc() {
            return this.qb;
        }
        set_qc(a) {
            this.qb = a;
        }
        get qc() {
            return this.get_qc();
        }
        set qc(a) {
            this.set_qc(a);
        }
        kh(a) {
            super.kh(a);
            this.dateTimeMemberPath = a;
        }
        kk(a) {
            super.kk(a);
            this.minimumValue = a;
        }
        kj(a) {
            super.kk(a);
            this.maximumValue = a;
        }
        d4() {
            if (this.dr && this.d5) {
                this.bu = this.bt();
            }
            let a = this.actualMinimumValue.getTime();
            let b = this.actualMaximumValue.getTime();
            this.q4();
            let c = !this.qf() ? this.actualMinimumValue.getTime() : this.minimumValue.getTime();
            let d = !this.qe() ? this.actualMaximumValue.getTime() : this.maximumValue.getTime();
            let e = new AxisRangeChangedEventArgs(0, a, c, b, d);
            this.ky(e);
            return true;
        }
        qf() {
            if (+(this.minimumValue) == +(dateMinValue())) {
                return false;
            }
            return (this.minimumValue != null);
        }
        qe() {
            if (+(this.maximumValue) == +(dateMaxValue())) {
                return false;
            }
            return (this.maximumValue != null);
        }
        get actualMinimumValue() {
            return this.qj;
        }
        set actualMinimumValue(a) {
            let b = +(this.qj) != +a;
            if (b) {
                let c = this.qj;
                this.qj = a;
                this.kx("ActualMinimumValue", c, a);
            }
        }
        hi() {
            return this.actualMinimumValue;
        }
        get actualMaximumValue() {
            return this.qi;
        }
        set actualMaximumValue(a) {
            let b = +(this.qi) != +a;
            if (b) {
                let c = this.qi;
                this.qi = a;
                this.kx("ActualMaximumValue", c, a);
            }
        }
        hh() {
            return this.maximumValue;
        }
        q6(a) {
            this.k4(a);
            if (this.oh == null) {
                return;
            }
            for (let b of fromEnum(this.ed())) {
                b.ry(a);
            }
        }
        kf(a, b, c, d) {
            super.kf(a, b, c, d);
            switch (b) {
                case "FastItemsSource":
                    let e = typeCast(IFastItemsSource_$type, c);
                    if (e != null) {
                        e.deregisterColumn(this.p8);
                        this.p8 = null;
                        e.event = delegateRemove(e.event, runOn(this, this.q3));
                    }
                    let f = typeCast(IFastItemsSource_$type, d);
                    if (f != null) {
                        this.qh = null;
                        this.p8 = this.p9(this.dateTimeMemberPath);
                        f.event = delegateCombine(f.event, runOn(this, this.q3));
                    }
                    this.q6(false);
                    break;
                case TimeAxisBase.$$p[0]:
                    if (this.oh != null) {
                        this.oh.deregisterColumn(this.p8);
                        this.p8 = this.p9(this.dateTimeMemberPath);
                        this.qh = null;
                    }
                    break;
                case TimeAxisBase.$$p[3]:
                    this.d2();
                    this.q6(false);
                    break;
                case TimeAxisBase.$$p[2]:
                    this.d2();
                    this.q6(false);
                    break;
                case "DateTimeColumn":
                    this.q4();
                    break;
            }
        }
        q3(a, b) {
            this.qh = null;
        }
        p9(a) {
            return TimeAxisBase.qa(a, this);
        }
        static qa(a, b) {
            if (a == null) {
                return b.oh.registerColumnDateTime(null, null, false);
            }
            let c = null;
            let d = SeriesViewer.c7(a, b.coercionMethods);
            c = d.a;
            return b.oh.registerColumnDateTime(a, c, b.expectFunctions);
        }
        az() {
            if (this.d7) {
                return (TypeRegistrar.create('HorizontalSmartAxisLabelPanel'));
            }
            else {
                return new HorizontalAxisLabelPanel();
            }
        }
        get_bk() {
            return 0;
        }
        get bk() {
            return this.get_bk();
        }
        k4(a) {
            super.k4(a);
            let b = this.bn.a4();
            let c = this.bn.a7();
            let d = this.bn.a5();
            let e = this.bn.a6();
            let f = this.bn.e();
            let g = this.bn.f();
            let h = this.bn.g();
            this.lf();
            this.jp(b);
            this.jp(c);
            this.jp(d);
            this.jp(e);
            this.ef.clear();
            this.eg.clear();
            this.oo.clear();
            this.a0.ad = this;
            let i = this.b0 != null ? this.b0.xm : Rect.empty;
            let j = !i.isEmpty ? this.n1 : Rect.empty;
            this.a0.c8 = i;
            this.a0.c7 = j;
            if (i.isEmpty || j.isEmpty) {
                this.b9.count = 0;
                this.b8.count = 0;
                this.b7.count = 0;
            }
            if (this.b9.count == 0 && this.bn.l()) {
                this.a0.ab.clear();
            }
            if (this.a3 != null) {
                this.a3.registerAxis(this);
            }
            this.q4();
        }
        get_dc() {
            return true;
        }
        startCursor() {
        }
        resetCursor() {
        }
        StartTimeCursor() {
            this.startCursor();
        }
        ResetTimeCursor() {
            this.resetCursor();
        }
        getCategorySize(a, b, c) {
            if (this.o5 == 0) {
                return 0;
            }
            let d = this.nz(b, c);
            return d.width / (this.o5 * a.width);
        }
        getGroupCenter(a, b, c, d) {
            return this.getCategorySize(b, c, d) * 0.5;
        }
        ou(a, b, c) {
            let d = this.o1(a, b, c);
            let e = 1 - 0.5 * d;
            let f = this.getCategorySize(a, b, c) * e;
            return f;
        }
        get_dm() {
            return true;
        }
        q2(a, b) {
            return (0);
        }
        preferredCategoryMode(a) {
            return this.c9 ? 0 : 1;
        }
        hc(a) {
            let b = new ScalerParams(1, this.b0.x5, this.b0.x4, this.df);
            let c = this.fw(a, b);
            return dateFromTicks(truncate(c));
        }
        get isVertical() {
            return this.dq;
        }
        j$d(a, b) {
            return this.fw.apply(this, arguments);
        }
        j$c(a, b) {
            return this.ft.apply(this, arguments);
        }
        j$e(a, b, c, d, e) {
            this.j5.apply(this, arguments);
        }
        j$f(a, b, c, d) {
            this.j6.apply(this, arguments);
        }
        j$g(a, b, c, d) {
            this.j7.apply(this, arguments);
        }
        get j$b() {
            return this.df;
        }
        j$a() {
            return this.ag.apply(this, arguments);
        }
        get i$a() {
            return this.dc;
        }
    }
    TimeAxisBase.$t = markType(TimeAxisBase, 'TimeAxisBase', CategoryAxisBase.$, [ISortingAxis_$type, IDateTimeAxis_$type]);
    TimeAxisBase.$$p = markDep(DependencyProperty, PropertyMetadata, TimeAxisBase, 'kx', ['DateTimeMemberPath::ra', [2, null], 'IsDataPreSorted:qd:rb', [0, false], 'MaximumValue::rc', [3, null], 'MinimumValue::rd', [3, null]]);
    return TimeAxisBase;
})();
