/*
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 { 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.mi = null;
            this.mr = null;
            this.mm = false;
            this.mu = dateMaxValue();
            this.mt = dateMinValue();
            FastItemDateTimeColumn.o();
        }
        ag() {
            return [this.actualMinimumValue.getTime(), this.actualMaximumValue.getTime()];
        }
        ek(a, b) {
            if (!b.c.isEmpty) {
                let c = this.mz(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.mz(a, b.e, b.d, b.c);
        }
        fy(a, b) {
            let c = this.gt(b);
            if (c == TimeAxisBase.$$p[0]) {
                return this.fz(a, b, this.dateTimeMemberPath);
            }
            return super.fy(a, b);
        }
        mz(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.ch) {
                e = 1 - e;
                f = -f;
            }
            return c.left + c.width * (e - b.left) / b.width - f;
        }
        m0(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.ch) {
                d = 1 - d;
                e = -e;
            }
            return c.left + c.width * (d - b.left) / b.width - e;
        }
        en(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.ch) {
                e = 1 - e;
            }
            return truncate(Math.floor(this.actualMinimumValue.getTime() + e * (this.actualMaximumValue.getTime() - this.actualMinimumValue.getTime())));
        }
        i$h() {
            this.nf();
        }
        nf() {
            let a = this.bi != null ? this.bi.vl : Rect.empty;
            let b = !a.isEmpty ? this.ke : Rect.empty;
            let c = (new Date());
            let d = (new Date());
            let e = false;
            if (!a.isEmpty && !b.isEmpty && this.mj != null) {
                let f = typeCast(FastItemDateTimeColumn.$, this.mj);
                if (f != null) {
                    if (this.ms == null) {
                        this.ng();
                    }
                }
                else {
                    this.ms = null;
                }
                if (this.mj.count > 0) {
                    e = true;
                    let g = this.ms == null ? 0 : this.ms._inner[0];
                    let h = this.ms == null ? this.mj.count - 1 : this.ms._inner[this.mj.count - 1];
                    c = this.mj.item(g);
                    d = this.mj.item(h);
                    this.mn = false;
                    if (this.categoryMode == 2) {
                        let i = this.nd(c, d);
                        if (+c != +(dateMinValue())) {
                            c = dateSubtract(+c, +i);
                        }
                        d = dateAdd(+d, +i);
                    }
                }
            }
            if (this.mq()) {
                e = true;
                c = this.minimumValue;
            }
            if (this.mp()) {
                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.nk();
            }
        }
        nk() {
        }
        get mj() {
            return this.mi;
        }
        set mj(a) {
            if (this.mi != a) {
                let b = this.mi;
                this.mi = a;
                this.h3("DateTimeColumn", b, this.mi);
            }
        }
        get ms() {
            return this.mr;
        }
        set ms(a) {
            this.mr = a;
        }
        get i$b() {
            if (this.ms == null) {
                let a = typeCast(FastItemDateTimeColumn.$, this.mj);
                if (a != null) {
                    this.ng();
                }
                else {
                    this.ms = null;
                }
            }
            return this.ms;
        }
        ng() {
            if (this.mo) {
                this.ms = new List$1(Number_$type, 0);
                let a = this.ms;
                let b = this.mj.count;
                for (let c = 0; c < b; c++) {
                    a.add(c);
                }
            }
            else {
                let d = typeCast(FastItemDateTimeColumn.$, this.mj);
                this.ms = d.h();
            }
        }
        i$e(a, b, c, d) {
            return this.m2(a, b, c, d);
        }
        m2(a, b, c, d) {
            if (b.isEmpty) {
                return -1;
            }
            let e = d;
            let f = new ScalerParams(0, a, b, this.ch, c);
            let g, h;
            if (this.ch) {
                h = this.en(e.left, f);
                g = this.en(e.right, f);
            }
            else {
                g = this.en(e.left, f);
                h = this.en(e.right, f);
            }
            let i = Math.max(dateMinValue().getTime(), Math.min(dateMaxValue().getTime(), truncate(g)));
            return this.m1(dateFromTicks(i));
        }
        m1(a) {
            if (this.mj == null || this.ms.count != this.mj.count) {
                return -1;
            }
            let b = new SortedListView$1(Date_$type, this.mj, this.ms);
            let c = this.m3(a, b);
            if (c >= 0 && c < this.ms.count) {
                let d = this.mj.item(this.ms._inner[c]);
                if (d > a) {
                    c--;
                    if (c < 0) {
                        c = 0;
                    }
                }
                return c;
            }
            return this.ms.count - 1;
        }
        i$g(a, b, c, d) {
            return this.m5(a, b, c, d);
        }
        m5(a, b, c, d) {
            if (b.isEmpty) {
                return -1;
            }
            let e = d;
            let f = new ScalerParams(0, a, b, this.ch, c);
            let g, h;
            if (this.ch) {
                h = this.en(e.left, f);
                g = this.en(e.right, f);
            }
            else {
                g = this.en(e.left, f);
                h = this.en(e.right, f);
            }
            let i = Math.max(dateMinValue().getTime(), Math.min(dateMaxValue().getTime(), truncate(h)));
            return this.m4(dateFromTicks(i));
        }
        m4(a) {
            if (this.mj == null || this.ms.count != this.mj.count) {
                return -1;
            }
            let b = new SortedListView$1(Date_$type, this.mj, this.ms);
            let c = this.m3(a, b);
            if (c >= this.ms.count) {
                c = this.ms.count - 1;
            }
            if (c >= 0 && c < this.ms.count) {
                let d = this.mj.item(this.ms._inner[c]);
                if (d < a) {
                    c++;
                    if (c > this.ms.count - 1) {
                        c = this.ms.count - 1;
                    }
                }
                return c;
            }
            return 0;
        }
        i$d(a) {
            if (this.mj == null) {
                return NaN;
            }
            let b = this.mi.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.mj, b.i$b);
            let ticks_ = a;
            let d = (new Date(ticks_));
            let e = this.m3(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.j9();
            if (e == b.i$b.count && this.bi != null && this.ek(a, new ScalerParams(0, this.bi.vl, this.ke, this.ch, i)) < (this.ke.right + 2)) {
                return e - 1;
            }
            if (e == -1 && this.bi != null && Math.abs(this.ek(a, new ScalerParams(0, this.bi.vl, this.ke, this.ch, i)) - this.ke.left) < 2) {
                return 0;
            }
            return -1;
        }
        m3(a, b) {
            let c = this;
            if (this.mj == 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.mj == null || b.i$b.count != this.mj.count) {
                return -1;
            }
            let c = new SortedListView$1(Date_$type, this.mj, b.i$b);
            let ticks_ = a;
            let d = (new Date(ticks_));
            let e = this.m3(d, c);
            let f = this.j9();
            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.bi != null && this.ek(a, new ScalerParams(0, this.bi.vl, this.ke, this.ch, f)) < (this.ke.right + 2)) {
                return e - 1;
            }
            if (e == -1 && this.bi != null && Math.abs(this.ek(a, new ScalerParams(0, this.bi.vl, this.ke, this.ch, f)) - this.ke.left) < 2) {
                return 0;
            }
            return -1;
        }
        i$i() {
            this.ms = null;
            this.h8();
        }
        get_mn() {
            return this.mm;
        }
        set_mn(a) {
            this.mm = a;
        }
        get mn() {
            return this.get_mn();
        }
        set mn(a) {
            this.set_mn(a);
        }
        hz(a) {
            super.hz(a);
            this.dateTimeMemberPath = a;
        }
        h2(a) {
            super.h2(a);
            this.minimumValue = a;
        }
        h1(a) {
            super.h2(a);
            this.maximumValue = a;
        }
        c3() {
            if (this.cq && this.c4) {
                this.bc = this.bb();
            }
            let a = this.actualMinimumValue.getTime();
            let b = this.actualMaximumValue.getTime();
            this.nf();
            let c = !this.mq() ? this.actualMinimumValue.getTime() : this.minimumValue.getTime();
            let d = !this.mp() ? this.actualMaximumValue.getTime() : this.maximumValue.getTime();
            let e = new AxisRangeChangedEventArgs(0, a, c, b, d);
            this.h4(e);
            return true;
        }
        mq() {
            if (+(this.minimumValue) == +(dateMinValue())) {
                return false;
            }
            return (this.minimumValue != null);
        }
        mp() {
            if (+(this.maximumValue) == +(dateMaxValue())) {
                return false;
            }
            return (this.maximumValue != null);
        }
        get actualMinimumValue() {
            return this.mu;
        }
        set actualMinimumValue(a) {
            let b = +(this.mu) != +a;
            if (b) {
                let c = this.mu;
                this.mu = a;
                this.h3("ActualMinimumValue", c, a);
            }
        }
        f3() {
            return this.actualMinimumValue;
        }
        get actualMaximumValue() {
            return this.mt;
        }
        set actualMaximumValue(a) {
            let b = +(this.mt) != +a;
            if (b) {
                let c = this.mt;
                this.mt = a;
                this.h3("ActualMaximumValue", c, a);
            }
        }
        f2() {
            return this.maximumValue;
        }
        nh(a) {
            this.ia(a);
            if (this.ks == null) {
                return;
            }
            for (let b of fromEnum(this.db())) {
                b.rm(a);
            }
        }
        hx(a, b, c, d) {
            super.hx(a, b, c, d);
            switch (b) {
                case "FastItemsSource":
                    let e = typeCast(IFastItemsSource_$type, c);
                    if (e != null) {
                        e.deregisterColumn(this.mj);
                        this.mj = null;
                        e.event = delegateRemove(e.event, runOn(this, this.ne));
                    }
                    let f = typeCast(IFastItemsSource_$type, d);
                    if (f != null) {
                        this.ms = null;
                        this.mj = this.mk(this.dateTimeMemberPath);
                        f.event = delegateCombine(f.event, runOn(this, this.ne));
                    }
                    this.nh(false);
                    break;
                case TimeAxisBase.$$p[0]:
                    if (this.ks != null) {
                        this.ks.deregisterColumn(this.mj);
                        this.mj = this.mk(this.dateTimeMemberPath);
                        this.ms = null;
                    }
                    break;
                case TimeAxisBase.$$p[3]:
                    this.c1();
                    this.nh(false);
                    break;
                case TimeAxisBase.$$p[2]:
                    this.c1();
                    this.nh(false);
                    break;
                case "DateTimeColumn":
                    this.nf();
                    break;
            }
        }
        ne(a, b) {
            this.ms = null;
        }
        mk(a) {
            return TimeAxisBase.ml(a, this);
        }
        static ml(a, b) {
            if (a == null) {
                return b.ks.registerColumnDateTime(null, null, false);
            }
            let c = null;
            let d = SeriesViewer.cu(a, b.coercionMethods);
            c = d.a;
            return b.ks.registerColumnDateTime(a, c, b.expectFunctions);
        }
        av() {
            if (this.c6) {
                return (TypeRegistrar.create('HorizontalSmartAxisLabelPanel'));
            }
            else {
                return new HorizontalAxisLabelPanel();
            }
        }
        get_a2() {
            return 0;
        }
        get a2() {
            return this.get_a2();
        }
        ia(a) {
            super.ia(a);
            let b = this.a5.a4();
            let c = this.a5.a7();
            let d = this.a5.a5();
            let e = this.a5.a6();
            let f = this.a5.e();
            let g = this.a5.f();
            let h = this.a5.g();
            this.im();
            this.hj(b);
            this.hj(c);
            this.hj(d);
            this.hj(e);
            this.dc.clear();
            this.dd.clear();
            this.kz.clear();
            this.aw.ad = this;
            let i = this.bi != null ? this.bi.vl : Rect.empty;
            let j = !i.isEmpty ? this.ke : Rect.empty;
            this.aw.c5 = i;
            this.aw.c4 = j;
            if (i.isEmpty || j.isEmpty) {
                this.br.count = 0;
                this.bq.count = 0;
                this.bp.count = 0;
            }
            if (this.br.count == 0 && this.a5.l()) {
                this.aw.ab.clear();
            }
            if (this.az != null) {
                this.az.registerAxis(this);
            }
            this.nf();
        }
        get_ce() {
            return true;
        }
        startCursor() {
        }
        resetCursor() {
        }
        StartTimeCursor() {
            this.startCursor();
        }
        ResetTimeCursor() {
            this.resetCursor();
        }
        getCategorySize(a, b, c) {
            if (this.lg == 0) {
                return 0;
            }
            let d = this.kc(b, c);
            return d.width / (this.lg * a.width);
        }
        getGroupCenter(a, b, c, d) {
            return this.getCategorySize(b, c, d) * 0.5;
        }
        k5(a, b, c) {
            let d = this.lc(a, b, c);
            let e = 1 - 0.5 * d;
            let f = this.getCategorySize(a, b, c) * e;
            return f;
        }
        get_cn() {
            return true;
        }
        nd(a, b) {
            return (0);
        }
        preferredCategoryMode(a) {
            return this.cc ? 0 : 1;
        }
        get isVertical() {
            return this.cp;
        }
        j$d(a, b) {
            return this.en.apply(this, arguments);
        }
        j$c(a, b) {
            return this.ek.apply(this, arguments);
        }
        j$e(a, b, c, d, e) {
            this.hn.apply(this, arguments);
        }
        j$f(a, b, c, d) {
            this.ho.apply(this, arguments);
        }
        j$g(a, b, c, d) {
            this.hp.apply(this, arguments);
        }
        get j$b() {
            return this.ch;
        }
        j$a() {
            return this.ag.apply(this, arguments);
        }
        get i$a() {
            return this.ce;
        }
    }
    TimeAxisBase.$t = /*@__PURE__*/ markType(TimeAxisBase, 'TimeAxisBase', CategoryAxisBase.$, [ISortingAxis_$type, IDateTimeAxis_$type]);
    TimeAxisBase.$$p = /*@__PURE__*/ markDep(DependencyProperty, PropertyMetadata, TimeAxisBase, 'h3', ['DateTimeMemberPath::nl', [2, null], 'IsDataPreSorted:mo:nm', [0, false], 'MaximumValue::nn', [3, null], 'MinimumValue::no', [3, null]]);
    return TimeAxisBase;
})();
