/*
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 * as tslib_1 from "tslib";
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 { MathUtil } from "igniteui-react-core";
import { PropertyMetadata } from "igniteui-react-core";
import { truncate, isNaN_ } from "igniteui-react-core";
import { dateMinValue, dateSubtract, dateAdd, dateMaxValue, dateFromTicks } from "igniteui-react-core";
/**
 * @hidden
 */
var TimeAxisBase = /** @class */ /*@__PURE__*/ (function (_super) {
    tslib_1.__extends(TimeAxisBase, _super);
    function TimeAxisBase() {
        var _this = _super.call(this) || this;
        _this.k5 = null;
        _this.le = null;
        _this.k9 = false;
        _this.lh = dateMaxValue();
        _this.lg = dateMinValue();
        FastItemDateTimeColumn.o();
        return _this;
    }
    TimeAxisBase.prototype.dz = function (a, b) {
        if (!b.c.isEmpty) {
            var c = this.lm(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.lm(a, b.e, b.d, b.c);
    };
    TimeAxisBase.prototype.e7 = function (a, b) {
        var c = this.f1(b);
        if (c == this.dateTimeMemberPath) {
            return this.e8(a, b, this.dateTimeMemberPath);
        }
        return _super.prototype.e7.call(this, a, b);
    };
    TimeAxisBase.prototype.lm = function (a, b, c, d) {
        var e;
        if (+(this.actualMaximumValue) == +(this.actualMinimumValue)) {
            e = -1;
        }
        else {
            e = (a - this.actualMinimumValue.getTime()) / (this.actualMaximumValue.getTime() - this.actualMinimumValue.getTime());
        }
        var f = 0;
        if (this.categoryMode == 2) {
            f = this.getGroupCenter(0, b, c, d);
        }
        if (this.ca) {
            e = 1 - e;
            f = -f;
        }
        return c.left + c.width * (e - b.left) / b.width - f;
    };
    TimeAxisBase.prototype.ln = function (a, b, c) {
        var d;
        if (+(this.actualMaximumValue) == +(this.actualMinimumValue)) {
            d = -1;
        }
        else {
            d = (a - this.actualMinimumValue.getTime()) / (this.actualMaximumValue.getTime() - this.actualMinimumValue.getTime());
        }
        var e = 0;
        if (this.ca) {
            d = 1 - d;
            e = -e;
        }
        return c.left + c.width * (d - b.left) / b.width - e;
    };
    TimeAxisBase.prototype.d2 = function (a, b) {
        if (!b.c.isEmpty) {
            var c = b.d.left;
            var d = b.d.width;
            if (this.categoryMode != 0) {
                var e = this.getCategorySize(b.e, b.d, b.c);
                c += e / 2;
                d -= e;
            }
            var 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);
    };
    TimeAxisBase.prototype.getUnscaledValue3 = function (a, b, c, d) {
        var e = b.left + b.width * (a - c.left) / c.width;
        if (this.ca) {
            e = 1 - e;
        }
        return truncate(Math.floor(this.actualMinimumValue.getTime() + e * (this.actualMaximumValue.getTime() - this.actualMinimumValue.getTime())));
    };
    TimeAxisBase.prototype.i$h = function () {
        this.l2();
    };
    TimeAxisBase.prototype.l2 = function () {
        var a = this.bh != null ? this.bh.os : Rect.empty;
        var b = !a.isEmpty ? this.ja : Rect.empty;
        var c = (new Date());
        var d = (new Date());
        var e = false;
        if (!a.isEmpty && !b.isEmpty && this.k6 != null) {
            var f = typeCast(FastItemDateTimeColumn.$, this.k6);
            if (f != null) {
                if (this.lf == null) {
                    this.l3();
                }
            }
            else {
                this.lf = null;
            }
            if (this.k6.count > 0) {
                e = true;
                var g = this.lf == null ? 0 : this.lf._inner[0];
                var h = this.lf == null ? this.k6.count - 1 : this.lf._inner[this.k6.count - 1];
                c = this.k6.item(g);
                d = this.k6.item(h);
                this.la = false;
                if (this.categoryMode == 2) {
                    var i = this.l0(c, d);
                    if (+c != +(dateMinValue())) {
                        c = dateSubtract(+c, +i);
                    }
                    d = dateAdd(+d, +i);
                }
            }
        }
        if (this.ld()) {
            e = true;
            c = this.minimumValue;
        }
        if (this.lc()) {
            e = true;
            d = this.maximumValue;
        }
        var j = +(this.actualMinimumValue) != +c || +(this.actualMaximumValue) != +d;
        if (e && j) {
            var k = this.actualMinimumValue;
            var l = this.actualMaximumValue;
            this.actualMinimumValue = c;
            this.actualMaximumValue = d;
            this.l7();
        }
    };
    TimeAxisBase.prototype.l7 = function () {
    };
    Object.defineProperty(TimeAxisBase.prototype, "k6", {
        get: function () {
            return this.k5;
        },
        set: function (a) {
            if (this.k5 != a) {
                var b = this.k5;
                this.k5 = a;
                this.g6("DateTimeColumn", b, this.k5);
            }
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(TimeAxisBase.prototype, "lf", {
        get: function () {
            return this.le;
        },
        set: function (a) {
            this.le = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(TimeAxisBase.prototype, "i$b", {
        get: function () {
            if (this.lf == null) {
                var a = typeCast(FastItemDateTimeColumn.$, this.k6);
                if (a != null) {
                    this.l3();
                }
                else {
                    this.lf = null;
                }
            }
            return this.lf;
        },
        enumerable: true,
        configurable: true
    });
    TimeAxisBase.prototype.l3 = function () {
        if (this.lb) {
            this.lf = new List$1(Number_$type, 0);
            var a = this.lf;
            var b = this.k6.count;
            for (var c = 0; c < b; c++) {
                a.add(c);
            }
        }
        else {
            var d = typeCast(FastItemDateTimeColumn.$, this.k6);
            this.lf = d.h();
        }
    };
    TimeAxisBase.prototype.i$e = function (a, b, c, d) {
        return this.lp(a, b, c, d);
    };
    TimeAxisBase.prototype.lp = function (a, b, c, d) {
        if (b.isEmpty) {
            return -1;
        }
        var e = d;
        var f = new ScalerParams(0, a, b, this.ca, c);
        var g, h;
        if (this.ca) {
            h = this.d2(e.left, f);
            g = this.d2(e.right, f);
        }
        else {
            g = this.d2(e.left, f);
            h = this.d2(e.right, f);
        }
        var i = Math.max(dateMinValue().getTime(), Math.min(dateMaxValue().getTime(), truncate(g)));
        return this.lo(dateFromTicks(i));
    };
    TimeAxisBase.prototype.lo = function (a) {
        if (this.k6 == null || this.lf.count != this.k6.count) {
            return -1;
        }
        var b = new SortedListView$1(Date_$type, this.k6, this.lf);
        var c = this.lq(a, b);
        if (c >= 0 && c < this.lf.count) {
            var d = this.k6.item(this.lf._inner[c]);
            if (d > a) {
                c--;
                if (c < 0) {
                    c = 0;
                }
            }
            return c;
        }
        return this.lf.count - 1;
    };
    TimeAxisBase.prototype.i$g = function (a, b, c, d) {
        return this.ls(a, b, c, d);
    };
    TimeAxisBase.prototype.ls = function (a, b, c, d) {
        if (b.isEmpty) {
            return -1;
        }
        var e = d;
        var f = new ScalerParams(0, a, b, this.ca, c);
        var g, h;
        if (this.ca) {
            h = this.d2(e.left, f);
            g = this.d2(e.right, f);
        }
        else {
            g = this.d2(e.left, f);
            h = this.d2(e.right, f);
        }
        var i = Math.max(dateMinValue().getTime(), Math.min(dateMaxValue().getTime(), truncate(h)));
        return this.lr(dateFromTicks(i));
    };
    TimeAxisBase.prototype.lr = function (a) {
        if (this.k6 == null || this.lf.count != this.k6.count) {
            return -1;
        }
        var b = new SortedListView$1(Date_$type, this.k6, this.lf);
        var c = this.lq(a, b);
        if (c >= this.lf.count) {
            c = this.lf.count - 1;
        }
        if (c >= 0 && c < this.lf.count) {
            var d = this.k6.item(this.lf._inner[c]);
            if (d < a) {
                c++;
                if (c > this.lf.count - 1) {
                    c = this.lf.count - 1;
                }
            }
            return c;
        }
        return 0;
    };
    TimeAxisBase.prototype.i$d = function (a) {
        if (this.k6 == null) {
            return NaN;
        }
        var b = this.k5.item(a);
        var c = b.getTime();
        var d = c;
        return d;
    };
    TimeAxisBase.prototype.i$c = function (a) {
        var b = this;
        if (b.i$b == null) {
            return -1;
        }
        var c = new SortedListView$1(Date_$type, this.k6, b.i$b);
        var ticks_ = a;
        var d = (new Date(ticks_));
        var e = this.lq(d, c);
        if (e >= 0 && e < b.i$b.count && e - 1 >= 0 && e - 1 < b.i$b.count) {
            var diff1_ = d.getTime() - c.item(e - 1).getTime();
            var diff2_ = c.item(e).getTime() - d.getTime();
            var f = e - 1;
            var 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;
            }
            var h = diff1_ / (diff1_ + diff2_);
            if (isNaN_(h)) {
                h = 0;
            }
            return f + h;
        }
        if (e >= 0 && e < b.i$b.count) {
            return e;
        }
        var i = this.i5();
        if (e == b.i$b.count && this.bh != null && this.dz(a, new ScalerParams(0, this.bh.os, this.ja, this.ca, i)) < (this.ja.right + 2)) {
            return e - 1;
        }
        if (e == -1 && this.bh != null && Math.abs(this.dz(a, new ScalerParams(0, this.bh.os, this.ja, this.ca, i)) - this.ja.left) < 2) {
            return 0;
        }
        return -1;
    };
    TimeAxisBase.prototype.lq = function (a, b) {
        var c = this;
        if (this.k6 == null || c.i$b == null) {
            return -1;
        }
        var d = -1;
        var e = ArrayExtension.binarySearch$11(Date_$type, b, function (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;
    };
    TimeAxisBase.prototype.i$f = function (a) {
        var b = this;
        if (b.i$b == null || this.k6 == null || b.i$b.count != this.k6.count) {
            return -1;
        }
        var c = new SortedListView$1(Date_$type, this.k6, b.i$b);
        var ticks_ = a;
        var d = (new Date(ticks_));
        var e = this.lq(d, c);
        var f = this.i5();
        if (e >= 0 && e < b.i$b.count && e - 1 >= 0 && e - 1 < b.i$b.count) {
            var g = +d - +(c.item(e - 1));
            var 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.dz(a, new ScalerParams(0, this.bh.os, this.ja, this.ca, f)) < (this.ja.right + 2)) {
            return e - 1;
        }
        if (e == -1 && this.bh != null && Math.abs(this.dz(a, new ScalerParams(0, this.bh.os, this.ja, this.ca, f)) - this.ja.left) < 2) {
            return 0;
        }
        return -1;
    };
    TimeAxisBase.prototype.i$i = function () {
        this.lf = null;
        this.hb();
    };
    TimeAxisBase.prototype.get_la = function () {
        return this.k9;
    };
    TimeAxisBase.prototype.set_la = function (a) {
        this.k9 = a;
    };
    Object.defineProperty(TimeAxisBase.prototype, "la", {
        get: function () {
            return this.get_la();
        },
        set: function (a) {
            this.set_la(a);
        },
        enumerable: true,
        configurable: true
    });
    TimeAxisBase.prototype.g2 = function (a) {
        _super.prototype.g2.call(this, a);
        this.dateTimeMemberPath = a;
    };
    TimeAxisBase.prototype.g5 = function (a) {
        _super.prototype.g5.call(this, a);
        this.minimumValue = a;
    };
    TimeAxisBase.prototype.g4 = function (a) {
        _super.prototype.g5.call(this, a);
        this.maximumValue = a;
    };
    TimeAxisBase.prototype.cp = function () {
        if (this.ci && this.cq) {
            this.bb = this.ba();
        }
        var a = this.actualMinimumValue.getTime();
        var b = this.actualMaximumValue.getTime();
        this.l2();
        var c = !this.ld() ? this.actualMinimumValue.getTime() : this.minimumValue.getTime();
        var d = !this.lc() ? this.actualMaximumValue.getTime() : this.maximumValue.getTime();
        var e = new AxisRangeChangedEventArgs(0, a, c, b, d);
        this.g7(e);
        return true;
    };
    TimeAxisBase.prototype.ld = function () {
        if (+(this.minimumValue) == +(dateMinValue())) {
            return false;
        }
        return (this.minimumValue != null);
    };
    TimeAxisBase.prototype.lc = function () {
        if (+(this.maximumValue) == +(dateMaxValue())) {
            return false;
        }
        return (this.maximumValue != null);
    };
    Object.defineProperty(TimeAxisBase.prototype, "actualMinimumValue", {
        get: function () {
            return this.lh;
        },
        set: function (a) {
            var b = +(this.lh) != +a;
            if (b) {
                var c = this.lh;
                this.lh = a;
                this.g6("ActualMinimumValue", c, a);
            }
        },
        enumerable: true,
        configurable: true
    });
    TimeAxisBase.prototype.fc = function () {
        return this.actualMinimumValue;
    };
    Object.defineProperty(TimeAxisBase.prototype, "actualMaximumValue", {
        get: function () {
            return this.lg;
        },
        set: function (a) {
            var b = +(this.lg) != +a;
            if (b) {
                var c = this.lg;
                this.lg = a;
                this.g6("ActualMaximumValue", c, a);
            }
        },
        enumerable: true,
        configurable: true
    });
    TimeAxisBase.prototype.fb = function () {
        return this.maximumValue;
    };
    TimeAxisBase.prototype.l4 = function (a) {
        var e_1, _a;
        this.hd(a);
        if (this.jo == null) {
            return;
        }
        try {
            for (var _b = tslib_1.__values(fromEnum(this.cw())), _c = _b.next(); !_c.done; _c = _b.next()) {
                var b = _c.value;
                b.k3(a);
            }
        }
        catch (e_1_1) {
            e_1 = { error: e_1_1 };
        }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return))
                    _a.call(_b);
            }
            finally {
                if (e_1)
                    throw e_1.error;
            }
        }
    };
    TimeAxisBase.prototype.g0 = function (a, b, c, d) {
        _super.prototype.g0.call(this, a, b, c, d);
        switch (b) {
            case "FastItemsSource":
                var e = typeCast(IFastItemsSource_$type, c);
                if (e != null) {
                    e.deregisterColumn(this.k6);
                    this.k6 = null;
                    e.event = delegateRemove(e.event, runOn(this, this.l1));
                }
                var f = typeCast(IFastItemsSource_$type, d);
                if (f != null) {
                    this.lf = null;
                    this.k6 = this.k7(this.dateTimeMemberPath);
                    f.event = delegateCombine(f.event, runOn(this, this.l1));
                }
                this.l4(false);
                break;
            case TimeAxisBase.$$p[0]:
                if (this.jo != null) {
                    this.jo.deregisterColumn(this.k6);
                    this.k6 = this.k7(this.dateTimeMemberPath);
                    this.lf = null;
                }
                break;
            case TimeAxisBase.$$p[3]:
                this.cn();
                this.l4(false);
                break;
            case TimeAxisBase.$$p[2]:
                this.cn();
                this.l4(false);
                break;
            case "DateTimeColumn":
                this.l2();
                break;
        }
    };
    TimeAxisBase.prototype.l1 = function (a, b) {
        this.lf = null;
    };
    TimeAxisBase.prototype.k7 = function (a) {
        return TimeAxisBase.k8(a, this);
    };
    TimeAxisBase.k8 = function (a, b) {
        if (a == null) {
            return b.jo.registerColumnDateTime(null, null, false);
        }
        var c = null;
        var d = SeriesViewer.bu(a, b.coercionMethods);
        c = d.a;
        return b.jo.registerColumnDateTime(a, c, b.expectFunctions);
    };
    TimeAxisBase.prototype.au = function () {
        if (this.cr) {
            return (TypeRegistrar.create('HorizontalSmartAxisLabelPanel'));
        }
        else {
            return new HorizontalAxisLabelPanel();
        }
    };
    TimeAxisBase.prototype.get_a1 = function () {
        return 0;
    };
    Object.defineProperty(TimeAxisBase.prototype, "a1", {
        get: function () {
            return this.get_a1();
        },
        enumerable: true,
        configurable: true
    });
    TimeAxisBase.prototype.hd = function (a) {
        _super.prototype.hd.call(this, a);
        var b = this.a4.ay();
        var c = this.a4.a1();
        var d = this.a4.az();
        var e = this.a4.a0();
        var f = this.a4.e();
        var g = this.a4.f();
        var h = this.a4.g();
        this.hl();
        this.go(b);
        this.go(c);
        this.go(d);
        this.go(e);
        this.cx.clear();
        this.cy.clear();
        this.jv.clear();
        this.av.ac = this;
        var i = this.bh != null ? this.bh.os : Rect.empty;
        var j = !i.isEmpty ? this.ja : Rect.empty;
        this.av.cq = i;
        this.av.cp = 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.l2();
    };
    TimeAxisBase.prototype.get_b7 = function () {
        return true;
    };
    TimeAxisBase.prototype.startCursor = function () {
    };
    TimeAxisBase.prototype.resetCursor = function () {
    };
    TimeAxisBase.prototype.StartTimeCursor = function () {
        this.startCursor();
    };
    TimeAxisBase.prototype.ResetTimeCursor = function () {
        this.resetCursor();
    };
    TimeAxisBase.prototype.getCategorySize = function (a, b, c) {
        if (this.j8 == 0) {
            return 0;
        }
        var d = this.i8(b, c);
        return d.width / (this.j8 * a.width);
    };
    TimeAxisBase.prototype.getGroupCenter = function (a, b, c, d) {
        return this.getCategorySize(b, c, d) * 0.5;
    };
    TimeAxisBase.prototype.j0 = function (a, b, c) {
        var d = !isNaN_(this.jx) ? MathUtil.b(this.jx, 0, 1) : 0;
        var e = 1 - 0.5 * d;
        var f = this.getCategorySize(a, b, c) * e;
        return f;
    };
    TimeAxisBase.prototype.get_cf = function () {
        return true;
    };
    TimeAxisBase.prototype.l0 = function (a, b) {
        return (0);
    };
    TimeAxisBase.prototype.preferredCategoryMode = function (a) {
        return this.b5 ? 0 : 1;
    };
    Object.defineProperty(TimeAxisBase.prototype, "isVertical", {
        get: function () {
            return this.ch;
        },
        enumerable: true,
        configurable: true
    });
    TimeAxisBase.prototype.j$c = function (a, b) {
        return this.d2.apply(this, arguments);
    };
    TimeAxisBase.prototype.j$b = function (a, b) {
        return this.dz.apply(this, arguments);
    };
    TimeAxisBase.prototype.j$d = function (a, b, c, d, e) {
        this.gs.apply(this, arguments);
    };
    TimeAxisBase.prototype.j$e = function (a, b, c, d) {
        this.gt.apply(this, arguments);
    };
    TimeAxisBase.prototype.j$f = function (a, b, c, d) {
        this.gu.apply(this, arguments);
    };
    Object.defineProperty(TimeAxisBase.prototype, "j$a", {
        get: function () {
            return this.ca;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(TimeAxisBase.prototype, "i$a", {
        get: function () {
            return this.b7;
        },
        enumerable: true,
        configurable: true
    });
    TimeAxisBase.$t = markType(TimeAxisBase, 'TimeAxisBase', CategoryAxisBase.$, [ISortingAxis_$type, IDateTimeAxis_$type]);
    TimeAxisBase.$$p = markDep(DependencyProperty, PropertyMetadata, TimeAxisBase, 'g6', ['DateTimeMemberPath::l8', [2, null], 'IsDataPreSorted:lb:l9', [0, false], 'MaximumValue::ma', [3, null], 'MinimumValue::mb', [3, null]]);
    return TimeAxisBase;
}(CategoryAxisBase));
export { TimeAxisBase };
//# sourceMappingURL=TimeAxisBase.js.map
