/*
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.mf = null;
            this.mo = null;
            this.mj = false;
            this.mr = dateMaxValue();
            this.mq = dateMinValue();
            FastItemDateTimeColumn.o();
        }
        ei(a, b) {
            if (!b.c.isEmpty) {
                let c = this.mw(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.mw(a, b.e, b.d, b.c);
        }
        fw(a, b) {
            let c = this.gr(b);
            if (c == TimeAxisBase.$$p[0]) {
                return this.fx(a, b, this.dateTimeMemberPath);
            }
            return super.fw(a, b);
        }
        mw(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.cg) {
                e = 1 - e;
                f = -f;
            }
            return c.left + c.width * (e - b.left) / b.width - f;
        }
        mx(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.cg) {
                d = 1 - d;
                e = -e;
            }
            return c.left + c.width * (d - b.left) / b.width - e;
        }
        el(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.cg) {
                e = 1 - e;
            }
            return truncate(Math.floor(this.actualMinimumValue.getTime() + e * (this.actualMaximumValue.getTime() - this.actualMinimumValue.getTime())));
        }
        i$h() {
            this.nc();
        }
        nc() {
            let a = this.bh != null ? this.bh.s9 : Rect.empty;
            let b = !a.isEmpty ? this.kc : Rect.empty;
            let c = (new Date());
            let d = (new Date());
            let e = false;
            if (!a.isEmpty && !b.isEmpty && this.mg != null) {
                let f = typeCast(FastItemDateTimeColumn.$, this.mg);
                if (f != null) {
                    if (this.mp == null) {
                        this.nd();
                    }
                }
                else {
                    this.mp = null;
                }
                if (this.mg.count > 0) {
                    e = true;
                    let g = this.mp == null ? 0 : this.mp._inner[0];
                    let h = this.mp == null ? this.mg.count - 1 : this.mp._inner[this.mg.count - 1];
                    c = this.mg.item(g);
                    d = this.mg.item(h);
                    this.mk = false;
                    if (this.categoryMode == 2) {
                        let i = this.na(c, d);
                        if (+c != +(dateMinValue())) {
                            c = dateSubtract(+c, +i);
                        }
                        d = dateAdd(+d, +i);
                    }
                }
            }
            if (this.mn()) {
                e = true;
                c = this.minimumValue;
            }
            if (this.mm()) {
                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.nh();
            }
        }
        nh() {
        }
        get mg() {
            return this.mf;
        }
        set mg(a) {
            if (this.mf != a) {
                let b = this.mf;
                this.mf = a;
                this.h1("DateTimeColumn", b, this.mf);
            }
        }
        get mp() {
            return this.mo;
        }
        set mp(a) {
            this.mo = a;
        }
        get i$b() {
            if (this.mp == null) {
                let a = typeCast(FastItemDateTimeColumn.$, this.mg);
                if (a != null) {
                    this.nd();
                }
                else {
                    this.mp = null;
                }
            }
            return this.mp;
        }
        nd() {
            if (this.ml) {
                this.mp = new List$1(Number_$type, 0);
                let a = this.mp;
                let b = this.mg.count;
                for (let c = 0; c < b; c++) {
                    a.add(c);
                }
            }
            else {
                let d = typeCast(FastItemDateTimeColumn.$, this.mg);
                this.mp = d.h();
            }
        }
        i$e(a, b, c, d) {
            return this.mz(a, b, c, d);
        }
        mz(a, b, c, d) {
            if (b.isEmpty) {
                return -1;
            }
            let e = d;
            let f = new ScalerParams(0, a, b, this.cg, c);
            let g, h;
            if (this.cg) {
                h = this.el(e.left, f);
                g = this.el(e.right, f);
            }
            else {
                g = this.el(e.left, f);
                h = this.el(e.right, f);
            }
            let i = Math.max(dateMinValue().getTime(), Math.min(dateMaxValue().getTime(), truncate(g)));
            return this.my(dateFromTicks(i));
        }
        my(a) {
            if (this.mg == null || this.mp.count != this.mg.count) {
                return -1;
            }
            let b = new SortedListView$1(Date_$type, this.mg, this.mp);
            let c = this.m0(a, b);
            if (c >= 0 && c < this.mp.count) {
                let d = this.mg.item(this.mp._inner[c]);
                if (d > a) {
                    c--;
                    if (c < 0) {
                        c = 0;
                    }
                }
                return c;
            }
            return this.mp.count - 1;
        }
        i$g(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.cg, c);
            let g, h;
            if (this.cg) {
                h = this.el(e.left, f);
                g = this.el(e.right, f);
            }
            else {
                g = this.el(e.left, f);
                h = this.el(e.right, f);
            }
            let i = Math.max(dateMinValue().getTime(), Math.min(dateMaxValue().getTime(), truncate(h)));
            return this.m1(dateFromTicks(i));
        }
        m1(a) {
            if (this.mg == null || this.mp.count != this.mg.count) {
                return -1;
            }
            let b = new SortedListView$1(Date_$type, this.mg, this.mp);
            let c = this.m0(a, b);
            if (c >= this.mp.count) {
                c = this.mp.count - 1;
            }
            if (c >= 0 && c < this.mp.count) {
                let d = this.mg.item(this.mp._inner[c]);
                if (d < a) {
                    c++;
                    if (c > this.mp.count - 1) {
                        c = this.mp.count - 1;
                    }
                }
                return c;
            }
            return 0;
        }
        i$d(a) {
            if (this.mg == null) {
                return NaN;
            }
            let b = this.mf.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.mg, b.i$b);
            let ticks_ = a;
            let d = (new Date(ticks_));
            let e = this.m0(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.j7();
            if (e == b.i$b.count && this.bh != null && this.ei(a, new ScalerParams(0, this.bh.s9, this.kc, this.cg, i)) < (this.kc.right + 2)) {
                return e - 1;
            }
            if (e == -1 && this.bh != null && Math.abs(this.ei(a, new ScalerParams(0, this.bh.s9, this.kc, this.cg, i)) - this.kc.left) < 2) {
                return 0;
            }
            return -1;
        }
        m0(a, b) {
            let c = this;
            if (this.mg == 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.mg == null || b.i$b.count != this.mg.count) {
                return -1;
            }
            let c = new SortedListView$1(Date_$type, this.mg, b.i$b);
            let ticks_ = a;
            let d = (new Date(ticks_));
            let e = this.m0(d, c);
            let f = this.j7();
            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.ei(a, new ScalerParams(0, this.bh.s9, this.kc, this.cg, f)) < (this.kc.right + 2)) {
                return e - 1;
            }
            if (e == -1 && this.bh != null && Math.abs(this.ei(a, new ScalerParams(0, this.bh.s9, this.kc, this.cg, f)) - this.kc.left) < 2) {
                return 0;
            }
            return -1;
        }
        i$i() {
            this.mp = null;
            this.h6();
        }
        get_mk() {
            return this.mj;
        }
        set_mk(a) {
            this.mj = a;
        }
        get mk() {
            return this.get_mk();
        }
        set mk(a) {
            this.set_mk(a);
        }
        hx(a) {
            super.hx(a);
            this.dateTimeMemberPath = a;
        }
        h0(a) {
            super.h0(a);
            this.minimumValue = a;
        }
        hz(a) {
            super.h0(a);
            this.maximumValue = a;
        }
        c2() {
            if (this.cp && this.c3) {
                this.bb = this.ba();
            }
            let a = this.actualMinimumValue.getTime();
            let b = this.actualMaximumValue.getTime();
            this.nc();
            let c = !this.mn() ? this.actualMinimumValue.getTime() : this.minimumValue.getTime();
            let d = !this.mm() ? this.actualMaximumValue.getTime() : this.maximumValue.getTime();
            let e = new AxisRangeChangedEventArgs(0, a, c, b, d);
            this.h2(e);
            return true;
        }
        mn() {
            if (+(this.minimumValue) == +(dateMinValue())) {
                return false;
            }
            return (this.minimumValue != null);
        }
        mm() {
            if (+(this.maximumValue) == +(dateMaxValue())) {
                return false;
            }
            return (this.maximumValue != null);
        }
        get actualMinimumValue() {
            return this.mr;
        }
        set actualMinimumValue(a) {
            let b = +(this.mr) != +a;
            if (b) {
                let c = this.mr;
                this.mr = a;
                this.h1("ActualMinimumValue", c, a);
            }
        }
        f1() {
            return this.actualMinimumValue;
        }
        get actualMaximumValue() {
            return this.mq;
        }
        set actualMaximumValue(a) {
            let b = +(this.mq) != +a;
            if (b) {
                let c = this.mq;
                this.mq = a;
                this.h1("ActualMaximumValue", c, a);
            }
        }
        f0() {
            return this.maximumValue;
        }
        ne(a) {
            this.h8(a);
            if (this.kq == null) {
                return;
            }
            for (let b of fromEnum(this.da())) {
                b.ml(a);
            }
        }
        hv(a, b, c, d) {
            super.hv(a, b, c, d);
            switch (b) {
                case "FastItemsSource":
                    let e = typeCast(IFastItemsSource_$type, c);
                    if (e != null) {
                        e.deregisterColumn(this.mg);
                        this.mg = null;
                        e.event = delegateRemove(e.event, runOn(this, this.nb));
                    }
                    let f = typeCast(IFastItemsSource_$type, d);
                    if (f != null) {
                        this.mp = null;
                        this.mg = this.mh(this.dateTimeMemberPath);
                        f.event = delegateCombine(f.event, runOn(this, this.nb));
                    }
                    this.ne(false);
                    break;
                case TimeAxisBase.$$p[0]:
                    if (this.kq != null) {
                        this.kq.deregisterColumn(this.mg);
                        this.mg = this.mh(this.dateTimeMemberPath);
                        this.mp = null;
                    }
                    break;
                case TimeAxisBase.$$p[3]:
                    this.c0();
                    this.ne(false);
                    break;
                case TimeAxisBase.$$p[2]:
                    this.c0();
                    this.ne(false);
                    break;
                case "DateTimeColumn":
                    this.nc();
                    break;
            }
        }
        nb(a, b) {
            this.mp = null;
        }
        mh(a) {
            return TimeAxisBase.mi(a, this);
        }
        static mi(a, b) {
            if (a == null) {
                return b.kq.registerColumnDateTime(null, null, false);
            }
            let c = null;
            let d = SeriesViewer.cg(a, b.coercionMethods);
            c = d.a;
            return b.kq.registerColumnDateTime(a, c, b.expectFunctions);
        }
        au() {
            if (this.c5) {
                return (TypeRegistrar.create('HorizontalSmartAxisLabelPanel'));
            }
            else {
                return new HorizontalAxisLabelPanel();
            }
        }
        get_a1() {
            return 0;
        }
        get a1() {
            return this.get_a1();
        }
        h8(a) {
            super.h8(a);
            let b = this.a4.a1();
            let c = this.a4.a4();
            let d = this.a4.a2();
            let e = this.a4.a3();
            let f = this.a4.e();
            let g = this.a4.f();
            let h = this.a4.g();
            this.ik();
            this.hh(b);
            this.hh(c);
            this.hh(d);
            this.hh(e);
            this.db.clear();
            this.dc.clear();
            this.kx.clear();
            this.av.ad = this;
            let i = this.bh != null ? this.bh.s9 : Rect.empty;
            let j = !i.isEmpty ? this.kc : Rect.empty;
            this.av.c5 = i;
            this.av.c4 = 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.nc();
        }
        get_cd() {
            return true;
        }
        startCursor() {
        }
        resetCursor() {
        }
        StartTimeCursor() {
            this.startCursor();
        }
        ResetTimeCursor() {
            this.resetCursor();
        }
        getCategorySize(a, b, c) {
            if (this.le == 0) {
                return 0;
            }
            let d = this.ka(b, c);
            return d.width / (this.le * a.width);
        }
        getGroupCenter(a, b, c, d) {
            return this.getCategorySize(b, c, d) * 0.5;
        }
        k3(a, b, c) {
            let d = this.la(a, b, c);
            let e = 1 - 0.5 * d;
            let f = this.getCategorySize(a, b, c) * e;
            return f;
        }
        get_cm() {
            return true;
        }
        na(a, b) {
            return (0);
        }
        preferredCategoryMode(a) {
            return this.cb ? 0 : 1;
        }
        get isVertical() {
            return this.co;
        }
        j$c(a, b) {
            return this.el.apply(this, arguments);
        }
        j$b(a, b) {
            return this.ei.apply(this, arguments);
        }
        j$d(a, b, c, d, e) {
            this.hl.apply(this, arguments);
        }
        j$e(a, b, c, d) {
            this.hm.apply(this, arguments);
        }
        j$f(a, b, c, d) {
            this.hn.apply(this, arguments);
        }
        get j$a() {
            return this.cg;
        }
        get i$a() {
            return this.cd;
        }
    }
    TimeAxisBase.$t = markType(TimeAxisBase, 'TimeAxisBase', CategoryAxisBase.$, [ISortingAxis_$type, IDateTimeAxis_$type]);
    TimeAxisBase.$$p = markDep(DependencyProperty, PropertyMetadata, TimeAxisBase, 'h1', ['DateTimeMemberPath::ni', [2, null], 'IsDataPreSorted:ml:nj', [0, false], 'MaximumValue::nk', [3, null], 'MinimumValue::nl', [3, null]]);
    return TimeAxisBase;
})();
