/*
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.mg = null;
            this.mp = null;
            this.mk = false;
            this.ms = dateMaxValue();
            this.mr = dateMinValue();
            FastItemDateTimeColumn.o();
        }
        ag() {
            return [this.actualMinimumValue.getTime(), this.actualMaximumValue.getTime()];
        }
        ej(a, b) {
            if (!b.c.isEmpty) {
                let c = this.mx(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.mx(a, b.e, b.d, b.c);
        }
        fx(a, b) {
            let c = this.gs(b);
            if (c == TimeAxisBase.$$p[0]) {
                return this.fy(a, b, this.dateTimeMemberPath);
            }
            return super.fx(a, b);
        }
        mx(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;
        }
        my(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;
        }
        em(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.nd();
        }
        nd() {
            let a = this.bi != null ? this.bi.tn : Rect.empty;
            let b = !a.isEmpty ? this.kd : Rect.empty;
            let c = (new Date());
            let d = (new Date());
            let e = false;
            if (!a.isEmpty && !b.isEmpty && this.mh != null) {
                let f = typeCast(FastItemDateTimeColumn.$, this.mh);
                if (f != null) {
                    if (this.mq == null) {
                        this.ne();
                    }
                }
                else {
                    this.mq = null;
                }
                if (this.mh.count > 0) {
                    e = true;
                    let g = this.mq == null ? 0 : this.mq._inner[0];
                    let h = this.mq == null ? this.mh.count - 1 : this.mq._inner[this.mh.count - 1];
                    c = this.mh.item(g);
                    d = this.mh.item(h);
                    this.ml = false;
                    if (this.categoryMode == 2) {
                        let i = this.nb(c, d);
                        if (+c != +(dateMinValue())) {
                            c = dateSubtract(+c, +i);
                        }
                        d = dateAdd(+d, +i);
                    }
                }
            }
            if (this.mo()) {
                e = true;
                c = this.minimumValue;
            }
            if (this.mn()) {
                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.ni();
            }
        }
        ni() {
        }
        get mh() {
            return this.mg;
        }
        set mh(a) {
            if (this.mg != a) {
                let b = this.mg;
                this.mg = a;
                this.h2("DateTimeColumn", b, this.mg);
            }
        }
        get mq() {
            return this.mp;
        }
        set mq(a) {
            this.mp = a;
        }
        get i$b() {
            if (this.mq == null) {
                let a = typeCast(FastItemDateTimeColumn.$, this.mh);
                if (a != null) {
                    this.ne();
                }
                else {
                    this.mq = null;
                }
            }
            return this.mq;
        }
        ne() {
            if (this.mm) {
                this.mq = new List$1(Number_$type, 0);
                let a = this.mq;
                let b = this.mh.count;
                for (let c = 0; c < b; c++) {
                    a.add(c);
                }
            }
            else {
                let d = typeCast(FastItemDateTimeColumn.$, this.mh);
                this.mq = d.h();
            }
        }
        i$e(a, b, c, d) {
            return this.m0(a, b, c, d);
        }
        m0(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.em(e.left, f);
                g = this.em(e.right, f);
            }
            else {
                g = this.em(e.left, f);
                h = this.em(e.right, f);
            }
            let i = Math.max(dateMinValue().getTime(), Math.min(dateMaxValue().getTime(), truncate(g)));
            return this.mz(dateFromTicks(i));
        }
        mz(a) {
            if (this.mh == null || this.mq.count != this.mh.count) {
                return -1;
            }
            let b = new SortedListView$1(Date_$type, this.mh, this.mq);
            let c = this.m1(a, b);
            if (c >= 0 && c < this.mq.count) {
                let d = this.mh.item(this.mq._inner[c]);
                if (d > a) {
                    c--;
                    if (c < 0) {
                        c = 0;
                    }
                }
                return c;
            }
            return this.mq.count - 1;
        }
        i$g(a, b, c, d) {
            return this.m3(a, b, c, d);
        }
        m3(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.em(e.left, f);
                g = this.em(e.right, f);
            }
            else {
                g = this.em(e.left, f);
                h = this.em(e.right, f);
            }
            let i = Math.max(dateMinValue().getTime(), Math.min(dateMaxValue().getTime(), truncate(h)));
            return this.m2(dateFromTicks(i));
        }
        m2(a) {
            if (this.mh == null || this.mq.count != this.mh.count) {
                return -1;
            }
            let b = new SortedListView$1(Date_$type, this.mh, this.mq);
            let c = this.m1(a, b);
            if (c >= this.mq.count) {
                c = this.mq.count - 1;
            }
            if (c >= 0 && c < this.mq.count) {
                let d = this.mh.item(this.mq._inner[c]);
                if (d < a) {
                    c++;
                    if (c > this.mq.count - 1) {
                        c = this.mq.count - 1;
                    }
                }
                return c;
            }
            return 0;
        }
        i$d(a) {
            if (this.mh == null) {
                return NaN;
            }
            let b = this.mg.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.mh, b.i$b);
            let ticks_ = a;
            let d = (new Date(ticks_));
            let e = this.m1(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.j8();
            if (e == b.i$b.count && this.bi != null && this.ej(a, new ScalerParams(0, this.bi.tn, this.kd, this.ch, i)) < (this.kd.right + 2)) {
                return e - 1;
            }
            if (e == -1 && this.bi != null && Math.abs(this.ej(a, new ScalerParams(0, this.bi.tn, this.kd, this.ch, i)) - this.kd.left) < 2) {
                return 0;
            }
            return -1;
        }
        m1(a, b) {
            let c = this;
            if (this.mh == 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.mh == null || b.i$b.count != this.mh.count) {
                return -1;
            }
            let c = new SortedListView$1(Date_$type, this.mh, b.i$b);
            let ticks_ = a;
            let d = (new Date(ticks_));
            let e = this.m1(d, c);
            let f = this.j8();
            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.ej(a, new ScalerParams(0, this.bi.tn, this.kd, this.ch, f)) < (this.kd.right + 2)) {
                return e - 1;
            }
            if (e == -1 && this.bi != null && Math.abs(this.ej(a, new ScalerParams(0, this.bi.tn, this.kd, this.ch, f)) - this.kd.left) < 2) {
                return 0;
            }
            return -1;
        }
        i$i() {
            this.mq = null;
            this.h7();
        }
        get_ml() {
            return this.mk;
        }
        set_ml(a) {
            this.mk = a;
        }
        get ml() {
            return this.get_ml();
        }
        set ml(a) {
            this.set_ml(a);
        }
        hy(a) {
            super.hy(a);
            this.dateTimeMemberPath = a;
        }
        h1(a) {
            super.h1(a);
            this.minimumValue = a;
        }
        h0(a) {
            super.h1(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.nd();
            let c = !this.mo() ? this.actualMinimumValue.getTime() : this.minimumValue.getTime();
            let d = !this.mn() ? this.actualMaximumValue.getTime() : this.maximumValue.getTime();
            let e = new AxisRangeChangedEventArgs(0, a, c, b, d);
            this.h3(e);
            return true;
        }
        mo() {
            if (+(this.minimumValue) == +(dateMinValue())) {
                return false;
            }
            return (this.minimumValue != null);
        }
        mn() {
            if (+(this.maximumValue) == +(dateMaxValue())) {
                return false;
            }
            return (this.maximumValue != null);
        }
        get actualMinimumValue() {
            return this.ms;
        }
        set actualMinimumValue(a) {
            let b = +(this.ms) != +a;
            if (b) {
                let c = this.ms;
                this.ms = a;
                this.h2("ActualMinimumValue", c, a);
            }
        }
        f2() {
            return this.actualMinimumValue;
        }
        get actualMaximumValue() {
            return this.mr;
        }
        set actualMaximumValue(a) {
            let b = +(this.mr) != +a;
            if (b) {
                let c = this.mr;
                this.mr = a;
                this.h2("ActualMaximumValue", c, a);
            }
        }
        f1() {
            return this.maximumValue;
        }
        nf(a) {
            this.h9(a);
            if (this.kr == null) {
                return;
            }
            for (let b of fromEnum(this.db())) {
                b.pp(a);
            }
        }
        hw(a, b, c, d) {
            super.hw(a, b, c, d);
            switch (b) {
                case "FastItemsSource":
                    let e = typeCast(IFastItemsSource_$type, c);
                    if (e != null) {
                        e.deregisterColumn(this.mh);
                        this.mh = null;
                        e.event = delegateRemove(e.event, runOn(this, this.nc));
                    }
                    let f = typeCast(IFastItemsSource_$type, d);
                    if (f != null) {
                        this.mq = null;
                        this.mh = this.mi(this.dateTimeMemberPath);
                        f.event = delegateCombine(f.event, runOn(this, this.nc));
                    }
                    this.nf(false);
                    break;
                case TimeAxisBase.$$p[0]:
                    if (this.kr != null) {
                        this.kr.deregisterColumn(this.mh);
                        this.mh = this.mi(this.dateTimeMemberPath);
                        this.mq = null;
                    }
                    break;
                case TimeAxisBase.$$p[3]:
                    this.c1();
                    this.nf(false);
                    break;
                case TimeAxisBase.$$p[2]:
                    this.c1();
                    this.nf(false);
                    break;
                case "DateTimeColumn":
                    this.nd();
                    break;
            }
        }
        nc(a, b) {
            this.mq = null;
        }
        mi(a) {
            return TimeAxisBase.mj(a, this);
        }
        static mj(a, b) {
            if (a == null) {
                return b.kr.registerColumnDateTime(null, null, false);
            }
            let c = null;
            let d = SeriesViewer.ch(a, b.coercionMethods);
            c = d.a;
            return b.kr.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();
        }
        h9(a) {
            super.h9(a);
            let b = this.a5.a1();
            let c = this.a5.a4();
            let d = this.a5.a2();
            let e = this.a5.a3();
            let f = this.a5.e();
            let g = this.a5.f();
            let h = this.a5.g();
            this.il();
            this.hi(b);
            this.hi(c);
            this.hi(d);
            this.hi(e);
            this.dc.clear();
            this.dd.clear();
            this.ky.clear();
            this.aw.ad = this;
            let i = this.bi != null ? this.bi.tn : Rect.empty;
            let j = !i.isEmpty ? this.kd : 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.aw.ab.clear();
            }
            if (this.az != null) {
                this.az.registerAxis(this);
            }
            this.nd();
        }
        get_ce() {
            return true;
        }
        startCursor() {
        }
        resetCursor() {
        }
        StartTimeCursor() {
            this.startCursor();
        }
        ResetTimeCursor() {
            this.resetCursor();
        }
        getCategorySize(a, b, c) {
            if (this.lf == 0) {
                return 0;
            }
            let d = this.kb(b, c);
            return d.width / (this.lf * a.width);
        }
        getGroupCenter(a, b, c, d) {
            return this.getCategorySize(b, c, d) * 0.5;
        }
        k4(a, b, c) {
            let d = this.lb(a, b, c);
            let e = 1 - 0.5 * d;
            let f = this.getCategorySize(a, b, c) * e;
            return f;
        }
        get_cn() {
            return true;
        }
        nb(a, b) {
            return (0);
        }
        preferredCategoryMode(a) {
            return this.cc ? 0 : 1;
        }
        get isVertical() {
            return this.cp;
        }
        j$d(a, b) {
            return this.em.apply(this, arguments);
        }
        j$c(a, b) {
            return this.ej.apply(this, arguments);
        }
        j$e(a, b, c, d, e) {
            this.hm.apply(this, arguments);
        }
        j$f(a, b, c, d) {
            this.hn.apply(this, arguments);
        }
        j$g(a, b, c, d) {
            this.ho.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, 'h2', ['DateTimeMemberPath::nj', [2, null], 'IsDataPreSorted:mm:nk', [0, false], 'MaximumValue::nl', [3, null], 'MinimumValue::nm', [3, null]]);
    return TimeAxisBase;
})();
