/*
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.ml = null;
            this.mu = null;
            this.mp = false;
            this.mx = dateMaxValue();
            this.mw = dateMinValue();
            FastItemDateTimeColumn.o();
        }
        ag() {
            return [this.actualMinimumValue.getTime(), this.actualMaximumValue.getTime()];
        }
        el(a, b) {
            if (!b.c.isEmpty) {
                let c = this.m2(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.m2(a, b.e, b.d, b.c);
        }
        fz(a, b) {
            let c = this.gu(b);
            if (c == TimeAxisBase.$$p[0]) {
                return this.f0(a, b, this.dateTimeMemberPath);
            }
            return super.fz(a, b);
        }
        m2(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;
        }
        m3(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;
        }
        eo(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.ni();
        }
        ni() {
            let a = this.bi != null ? this.bi.v4 : Rect.empty;
            let b = !a.isEmpty ? this.kh : Rect.empty;
            let c = (new Date());
            let d = (new Date());
            let e = false;
            if (!a.isEmpty && !b.isEmpty && this.mm != null) {
                let f = typeCast(FastItemDateTimeColumn.$, this.mm);
                if (f != null) {
                    if (this.mv == null) {
                        this.nj();
                    }
                }
                else {
                    this.mv = null;
                }
                if (this.mm.count > 0) {
                    e = true;
                    let g = this.mv == null ? 0 : this.mv._inner[0];
                    let h = this.mv == null ? this.mm.count - 1 : this.mv._inner[this.mm.count - 1];
                    c = this.mm.item(g);
                    d = this.mm.item(h);
                    this.mq = false;
                    if (this.categoryMode == 2) {
                        let i = this.ng(c, d);
                        if (+c != +(dateMinValue())) {
                            c = dateSubtract(+c, +i);
                        }
                        d = dateAdd(+d, +i);
                    }
                }
            }
            if (this.mt()) {
                e = true;
                c = this.minimumValue;
            }
            if (this.ms()) {
                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.nn();
            }
        }
        nn() {
        }
        get mm() {
            return this.ml;
        }
        set mm(a) {
            if (this.ml != a) {
                let b = this.ml;
                this.ml = a;
                this.h5("DateTimeColumn", b, this.ml);
            }
        }
        get mv() {
            return this.mu;
        }
        set mv(a) {
            this.mu = a;
        }
        get i$b() {
            if (this.mv == null) {
                let a = typeCast(FastItemDateTimeColumn.$, this.mm);
                if (a != null) {
                    this.nj();
                }
                else {
                    this.mv = null;
                }
            }
            return this.mv;
        }
        nj() {
            if (this.mr) {
                this.mv = new List$1(Number_$type, 0);
                let a = this.mv;
                let b = this.mm.count;
                for (let c = 0; c < b; c++) {
                    a.add(c);
                }
            }
            else {
                let d = typeCast(FastItemDateTimeColumn.$, this.mm);
                this.mv = d.h();
            }
        }
        i$e(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.eo(e.left, f);
                g = this.eo(e.right, f);
            }
            else {
                g = this.eo(e.left, f);
                h = this.eo(e.right, f);
            }
            let i = Math.max(dateMinValue().getTime(), Math.min(dateMaxValue().getTime(), truncate(g)));
            return this.m4(dateFromTicks(i));
        }
        m4(a) {
            if (this.mm == null || this.mv.count != this.mm.count) {
                return -1;
            }
            let b = new SortedListView$1(Date_$type, this.mm, this.mv);
            let c = this.m6(a, b);
            if (c >= 0 && c < this.mv.count) {
                let d = this.mm.item(this.mv._inner[c]);
                if (d > a) {
                    c--;
                    if (c < 0) {
                        c = 0;
                    }
                }
                return c;
            }
            return this.mv.count - 1;
        }
        i$g(a, b, c, d) {
            return this.m8(a, b, c, d);
        }
        m8(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.eo(e.left, f);
                g = this.eo(e.right, f);
            }
            else {
                g = this.eo(e.left, f);
                h = this.eo(e.right, f);
            }
            let i = Math.max(dateMinValue().getTime(), Math.min(dateMaxValue().getTime(), truncate(h)));
            return this.m7(dateFromTicks(i));
        }
        m7(a) {
            if (this.mm == null || this.mv.count != this.mm.count) {
                return -1;
            }
            let b = new SortedListView$1(Date_$type, this.mm, this.mv);
            let c = this.m6(a, b);
            if (c >= this.mv.count) {
                c = this.mv.count - 1;
            }
            if (c >= 0 && c < this.mv.count) {
                let d = this.mm.item(this.mv._inner[c]);
                if (d < a) {
                    c++;
                    if (c > this.mv.count - 1) {
                        c = this.mv.count - 1;
                    }
                }
                return c;
            }
            return 0;
        }
        i$d(a) {
            if (this.mm == null) {
                return NaN;
            }
            let b = this.ml.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.mm, b.i$b);
            let ticks_ = a;
            let d = (new Date(ticks_));
            let e = this.m6(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.kc();
            if (e == b.i$b.count && this.bi != null && this.el(a, new ScalerParams(0, this.bi.v4, this.kh, this.ch, i)) < (this.kh.right + 2)) {
                return e - 1;
            }
            if (e == -1 && this.bi != null && Math.abs(this.el(a, new ScalerParams(0, this.bi.v4, this.kh, this.ch, i)) - this.kh.left) < 2) {
                return 0;
            }
            return -1;
        }
        m6(a, b) {
            let c = this;
            if (this.mm == 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.mm == null || b.i$b.count != this.mm.count) {
                return -1;
            }
            let c = new SortedListView$1(Date_$type, this.mm, b.i$b);
            let ticks_ = a;
            let d = (new Date(ticks_));
            let e = this.m6(d, c);
            let f = this.kc();
            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.el(a, new ScalerParams(0, this.bi.v4, this.kh, this.ch, f)) < (this.kh.right + 2)) {
                return e - 1;
            }
            if (e == -1 && this.bi != null && Math.abs(this.el(a, new ScalerParams(0, this.bi.v4, this.kh, this.ch, f)) - this.kh.left) < 2) {
                return 0;
            }
            return -1;
        }
        i$i() {
            this.mv = null;
            this.ia();
        }
        get_mq() {
            return this.mp;
        }
        set_mq(a) {
            this.mp = a;
        }
        get mq() {
            return this.get_mq();
        }
        set mq(a) {
            this.set_mq(a);
        }
        h1(a) {
            super.h1(a);
            this.dateTimeMemberPath = a;
        }
        h4(a) {
            super.h4(a);
            this.minimumValue = a;
        }
        h3(a) {
            super.h4(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.ni();
            let c = !this.mt() ? this.actualMinimumValue.getTime() : this.minimumValue.getTime();
            let d = !this.ms() ? this.actualMaximumValue.getTime() : this.maximumValue.getTime();
            let e = new AxisRangeChangedEventArgs(0, a, c, b, d);
            this.h6(e);
            return true;
        }
        mt() {
            if (+(this.minimumValue) == +(dateMinValue())) {
                return false;
            }
            return (this.minimumValue != null);
        }
        ms() {
            if (+(this.maximumValue) == +(dateMaxValue())) {
                return false;
            }
            return (this.maximumValue != null);
        }
        get actualMinimumValue() {
            return this.mx;
        }
        set actualMinimumValue(a) {
            let b = +(this.mx) != +a;
            if (b) {
                let c = this.mx;
                this.mx = a;
                this.h5("ActualMinimumValue", c, a);
            }
        }
        f4() {
            return this.actualMinimumValue;
        }
        get actualMaximumValue() {
            return this.mw;
        }
        set actualMaximumValue(a) {
            let b = +(this.mw) != +a;
            if (b) {
                let c = this.mw;
                this.mw = a;
                this.h5("ActualMaximumValue", c, a);
            }
        }
        f3() {
            return this.maximumValue;
        }
        nk(a) {
            this.ic(a);
            if (this.kv == null) {
                return;
            }
            for (let b of fromEnum(this.db())) {
                b.rp(a);
            }
        }
        hz(a, b, c, d) {
            super.hz(a, b, c, d);
            switch (b) {
                case "FastItemsSource":
                    let e = typeCast(IFastItemsSource_$type, c);
                    if (e != null) {
                        e.deregisterColumn(this.mm);
                        this.mm = null;
                        e.event = delegateRemove(e.event, runOn(this, this.nh));
                    }
                    let f = typeCast(IFastItemsSource_$type, d);
                    if (f != null) {
                        this.mv = null;
                        this.mm = this.mn(this.dateTimeMemberPath);
                        f.event = delegateCombine(f.event, runOn(this, this.nh));
                    }
                    this.nk(false);
                    break;
                case TimeAxisBase.$$p[0]:
                    if (this.kv != null) {
                        this.kv.deregisterColumn(this.mm);
                        this.mm = this.mn(this.dateTimeMemberPath);
                        this.mv = null;
                    }
                    break;
                case TimeAxisBase.$$p[3]:
                    this.c1();
                    this.nk(false);
                    break;
                case TimeAxisBase.$$p[2]:
                    this.c1();
                    this.nk(false);
                    break;
                case "DateTimeColumn":
                    this.ni();
                    break;
            }
        }
        nh(a, b) {
            this.mv = null;
        }
        mn(a) {
            return TimeAxisBase.mo(a, this);
        }
        static mo(a, b) {
            if (a == null) {
                return b.kv.registerColumnDateTime(null, null, false);
            }
            let c = null;
            let d = SeriesViewer.cu(a, b.coercionMethods);
            c = d.a;
            return b.kv.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();
        }
        ic(a) {
            super.ic(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.ip();
            this.hk(b);
            this.hk(c);
            this.hk(d);
            this.hk(e);
            this.dd.clear();
            this.de.clear();
            this.k2.clear();
            this.aw.ad = this;
            let i = this.bi != null ? this.bi.v4 : Rect.empty;
            let j = !i.isEmpty ? this.kh : 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.ni();
        }
        get_ce() {
            return true;
        }
        startCursor() {
        }
        resetCursor() {
        }
        StartTimeCursor() {
            this.startCursor();
        }
        ResetTimeCursor() {
            this.resetCursor();
        }
        getCategorySize(a, b, c) {
            if (this.lj == 0) {
                return 0;
            }
            let d = this.kf(b, c);
            return d.width / (this.lj * a.width);
        }
        getGroupCenter(a, b, c, d) {
            return this.getCategorySize(b, c, d) * 0.5;
        }
        k8(a, b, c) {
            let d = this.lf(a, b, c);
            let e = 1 - 0.5 * d;
            let f = this.getCategorySize(a, b, c) * e;
            return f;
        }
        get_cn() {
            return true;
        }
        ng(a, b) {
            return (0);
        }
        preferredCategoryMode(a) {
            return this.cc ? 0 : 1;
        }
        get isVertical() {
            return this.cp;
        }
        j$d(a, b) {
            return this.eo.apply(this, arguments);
        }
        j$c(a, b) {
            return this.el.apply(this, arguments);
        }
        j$e(a, b, c, d, e) {
            this.hp.apply(this, arguments);
        }
        j$f(a, b, c, d) {
            this.hq.apply(this, arguments);
        }
        j$g(a, b, c, d) {
            this.hr.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, 'h5', ['DateTimeMemberPath::no', [2, null], 'IsDataPreSorted:mr:np', [0, false], 'MaximumValue::nq', [3, null], 'MinimumValue::nr', [3, null]]);
    return TimeAxisBase;
})();
