/*
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/ES5/FastItemDateTimeColumn";
import { CategoryAxisBase } from "./CategoryAxisBase";
import { ISortingAxis_$type } from "./ISortingAxis";
import { IDateTimeAxis_$type } from "./IDateTimeAxis";
import { DependencyProperty } from "igniteui-react-core/ES5/DependencyProperty";
import { List$1 } from "igniteui-react-core/ES5/List$1";
import { Type, typeCast, Number_$type, Date_$type, fromEnum, runOn, delegateRemove, delegateCombine, markType, TypeRegistrar } from "igniteui-react-core/ES5/type";
import { ScalerParams } from "./ScalerParams";
import { Rect } from "igniteui-react-core/ES5/Rect";
import { NumericScaler } from "./NumericScaler";
import { SeriesViewer } from "./SeriesViewer";
import { SortedListView$1 } from "./SortedListView$1";
import { ArrayExtension } from "igniteui-react-core/ES5/ArrayExtension";
import { AxisRangeChangedEventArgs } from "./AxisRangeChangedEventArgs";
import { IFastItemsSource_$type } from "igniteui-react-core/ES5/IFastItemsSource";
import { HorizontalAxisLabelPanel } from "./HorizontalAxisLabelPanel";
import { MathUtil } from "igniteui-react-core/ES5/MathUtil";
import { PropertyMetadata } from "igniteui-react-core/ES5/PropertyMetadata";
import { truncate, isNaN_ } from "igniteui-react-core/ES5/number";
import { dateMinValue, dateSubtract, dateAdd, dateMaxValue, dateFromTicks } from "igniteui-react-core/ES5/date";
/**
 * @hidden
 */
var TimeAxisBase = /** @class */ (function (_super) {
    tslib_1.__extends(TimeAxisBase, _super);
    function TimeAxisBase() {
        var _this = _super.call(this) || this;
        _this.kp = null;
        _this.ky = null;
        _this.kt = false;
        _this.k1 = dateMaxValue();
        _this.k0 = dateMinValue();
        FastItemDateTimeColumn.m();
        return _this;
    }
    TimeAxisBase.prototype.dt = function (a, b) {
        if (!b.c.isEmpty) {
            var c = this.k6(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.k6(a, b.e, b.d, b.c);
    };
    TimeAxisBase.prototype.k6 = 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.b8) {
            e = 1 - e;
            f = -f;
        }
        return c.left + c.width * (e - b.left) / b.width - f;
    };
    TimeAxisBase.prototype.k7 = 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.b8) {
            d = 1 - d;
            e = -e;
        }
        return c.left + c.width * (d - b.left) / b.width - e;
    };
    TimeAxisBase.prototype.dw = 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) / this.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.b8) {
            e = 1 - e;
        }
        return truncate(Math.floor(this.actualMinimumValue.getTime() + e * (this.actualMaximumValue.getTime() - this.actualMinimumValue.getTime())));
    };
    TimeAxisBase.prototype.i$h = function () {
        this.lm();
    };
    TimeAxisBase.prototype.lm = function () {
        var a = this.bf != null ? this.bf.mq : Rect.empty;
        var b = !a.isEmpty ? this.iu : Rect.empty;
        var c = (new Date());
        var d = (new Date());
        var e = false;
        if (!a.isEmpty && !b.isEmpty && this.kq != null) {
            var f = typeCast(FastItemDateTimeColumn.$, this.kq);
            if (f != null) {
                if (this.kz == null) {
                    this.ln();
                }
            }
            else {
                this.kz = null;
            }
            if (this.kq.count > 0) {
                e = true;
                var g = this.kz == null ? 0 : this.kz._inner[0];
                var h = this.kz == null ? this.kq.count - 1 : this.kz._inner[this.kq.count - 1];
                c = this.kq.item(g);
                d = this.kq.item(h);
                this.ku = false;
                if (this.categoryMode == 2) {
                    var i = this.lk(c, d);
                    if (+c != +(dateMinValue())) {
                        c = dateSubtract(+c, +i);
                    }
                    d = dateAdd(+d, +i);
                }
            }
        }
        if (this.kx()) {
            e = true;
            c = this.minimumValue;
        }
        if (this.kw()) {
            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.lr();
        }
    };
    TimeAxisBase.prototype.lr = function () {
    };
    Object.defineProperty(TimeAxisBase.prototype, "kq", {
        get: function () {
            return this.kp;
        },
        set: function (a) {
            if (this.kp != a) {
                var b = this.kp;
                this.kp = a;
                this.gu("DateTimeColumn", b, this.kp);
            }
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(TimeAxisBase.prototype, "kz", {
        get: function () {
            return this.ky;
        },
        set: function (a) {
            this.ky = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(TimeAxisBase.prototype, "i$b", {
        get: function () {
            if (this.kz == null) {
                var a = typeCast(FastItemDateTimeColumn.$, this.kq);
                if (a != null) {
                    this.ln();
                }
                else {
                    this.kz = null;
                }
            }
            return this.kz;
        },
        enumerable: true,
        configurable: true
    });
    TimeAxisBase.prototype.ln = function () {
        if (this.kv) {
            this.kz = new List$1(Number_$type, 0);
            var a = this.kz;
            var b = this.kq.count;
            for (var c = 0; c < b; c++) {
                a.add(c);
            }
        }
        else {
            var d = typeCast(FastItemDateTimeColumn.$, this.kq);
            this.kz = d.f();
        }
    };
    TimeAxisBase.prototype.i$e = function (a, b, c, d) {
        return this.k9(a, b, c, d);
    };
    TimeAxisBase.prototype.k9 = function (a, b, c, d) {
        if (b.isEmpty) {
            return -1;
        }
        var e = d;
        var f = new ScalerParams(0, a, b, this.b8, c);
        var g, h;
        if (this.b8) {
            h = this.dw(e.left, f);
            g = this.dw(e.right, f);
        }
        else {
            g = this.dw(e.left, f);
            h = this.dw(e.right, f);
        }
        var i = Math.max(dateMinValue().getTime(), Math.min(dateMaxValue().getTime(), truncate(g)));
        return this.k8(dateFromTicks(i));
    };
    TimeAxisBase.prototype.k8 = function (a) {
        if (this.kq == null || this.kz.count != this.kq.count) {
            return -1;
        }
        for (var b = 0; b < this.kz.count; b++) {
            var c = this.kq.item(this.kz._inner[b]);
            if (c >= a) {
                return Math.max(b - 1, 0);
            }
        }
        return this.kz.count - 1;
    };
    TimeAxisBase.prototype.i$g = function (a, b, c, d) {
        return this.lc(a, b, c, d);
    };
    TimeAxisBase.prototype.lc = function (a, b, c, d) {
        if (b.isEmpty) {
            return -1;
        }
        var e = d;
        var f = new ScalerParams(0, a, b, this.b8, c);
        var g, h;
        if (this.b8) {
            h = this.dw(e.left, f);
            g = this.dw(e.right, f);
        }
        else {
            g = this.dw(e.left, f);
            h = this.dw(e.right, f);
        }
        var i = Math.max(dateMinValue().getTime(), Math.min(dateMaxValue().getTime(), truncate(h)));
        return this.lb(dateFromTicks(i));
    };
    TimeAxisBase.prototype.lb = function (a) {
        if (this.kq == null || this.kz.count != this.kq.count) {
            return -1;
        }
        for (var b = this.kz.count - 1; b >= 0; b--) {
            var c = this.kz._inner[b];
            var d = this.kq.item(c);
            if (d < a) {
                return b == this.kz.count - 1 ? b : b + 1;
            }
        }
        return 0;
    };
    TimeAxisBase.prototype.i$d = function (a) {
        if (this.kq == null) {
            return NaN;
        }
        var b = this.kp.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.kq, b.i$b);
        var ticks_ = a;
        var d = (new Date(ticks_));
        var e = this.la(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.ip();
        if (e == b.i$b.count && this.bf != null && this.dt(a, new ScalerParams(0, this.bf.mq, this.iu, this.b8, i)) < (this.iu.right + 2)) {
            return e - 1;
        }
        if (e == -1 && this.bf != null && Math.abs(this.dt(a, new ScalerParams(0, this.bf.mq, this.iu, this.b8, i)) - this.iu.left) < 2) {
            return 0;
        }
        return -1;
    };
    TimeAxisBase.prototype.la = function (a, b) {
        var c = this;
        if (this.kq == 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.kq == null || b.i$b.count != this.kq.count) {
            return -1;
        }
        var c = new SortedListView$1(Date_$type, this.kq, b.i$b);
        var ticks_ = a;
        var d = (new Date(ticks_));
        var e = this.la(d, c);
        var f = this.ip();
        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.bf != null && this.dt(a, new ScalerParams(0, this.bf.mq, this.iu, this.b8, f)) < (this.iu.right + 2)) {
            return e - 1;
        }
        if (e == -1 && this.bf != null && Math.abs(this.dt(a, new ScalerParams(0, this.bf.mq, this.iu, this.b8, f)) - this.iu.left) < 2) {
            return 0;
        }
        return -1;
    };
    TimeAxisBase.prototype.i$i = function () {
        this.kz = null;
        this.gz();
    };
    TimeAxisBase.prototype.get_ku = function () {
        return this.kt;
    };
    TimeAxisBase.prototype.set_ku = function (a) {
        this.kt = a;
    };
    Object.defineProperty(TimeAxisBase.prototype, "ku", {
        get: function () {
            return this.get_ku();
        },
        set: function (a) {
            this.set_ku(a);
        },
        enumerable: true,
        configurable: true
    });
    TimeAxisBase.prototype.gq = function (a) {
        _super.prototype.gq.call(this, a);
        this.dateTimeMemberPath = a;
    };
    TimeAxisBase.prototype.gt = function (a) {
        _super.prototype.gt.call(this, a);
        this.minimumValue = a;
    };
    TimeAxisBase.prototype.gs = function (a) {
        _super.prototype.gt.call(this, a);
        this.maximumValue = a;
    };
    TimeAxisBase.prototype.cm = function () {
        if (this.cf && this.cn) {
            this.a9 = this.a8();
        }
        var a = this.actualMinimumValue.getTime();
        var b = this.actualMaximumValue.getTime();
        this.lm();
        var c = !this.kx() ? this.actualMinimumValue.getTime() : this.minimumValue.getTime();
        var d = !this.kw() ? this.actualMaximumValue.getTime() : this.maximumValue.getTime();
        var e = new AxisRangeChangedEventArgs(0, a, c, b, d);
        this.gv(e);
        return true;
    };
    TimeAxisBase.prototype.kx = function () {
        if (+(this.minimumValue) == +(dateMinValue())) {
            return false;
        }
        return (this.minimumValue != null);
    };
    TimeAxisBase.prototype.kw = function () {
        if (+(this.maximumValue) == +(dateMaxValue())) {
            return false;
        }
        return (this.maximumValue != null);
    };
    Object.defineProperty(TimeAxisBase.prototype, "actualMinimumValue", {
        get: function () {
            return this.k1;
        },
        set: function (a) {
            var b = +(this.k1) != +a;
            if (b) {
                var c = this.k1;
                this.k1 = a;
                this.gu("ActualMinimumValue", c, a);
            }
        },
        enumerable: true,
        configurable: true
    });
    TimeAxisBase.prototype.e4 = function () {
        return this.actualMinimumValue;
    };
    Object.defineProperty(TimeAxisBase.prototype, "actualMaximumValue", {
        get: function () {
            return this.k0;
        },
        set: function (a) {
            var b = +(this.k0) != +a;
            if (b) {
                var c = this.k0;
                this.k0 = a;
                this.gu("ActualMaximumValue", c, a);
            }
        },
        enumerable: true,
        configurable: true
    });
    TimeAxisBase.prototype.e3 = function () {
        return this.maximumValue;
    };
    TimeAxisBase.prototype.lo = function (a) {
        var e_1, _a;
        this.g1(a);
        if (this.i8 == null) {
            return;
        }
        try {
            for (var _b = tslib_1.__values(fromEnum(this.cr())), _c = _b.next(); !_c.done; _c = _b.next()) {
                var b = _c.value;
                b.i6(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.go = function (a, b, c, d) {
        _super.prototype.go.call(this, a, b, c, d);
        switch (b) {
            case "FastItemsSource":
                var e = typeCast(IFastItemsSource_$type, c);
                if (e != null) {
                    e.deregisterColumn(this.kq);
                    this.kq = null;
                    e.event = delegateRemove(e.event, runOn(this, this.ll));
                }
                var f = typeCast(IFastItemsSource_$type, d);
                if (f != null) {
                    this.kz = null;
                    this.kq = this.kr(this.dateTimeMemberPath);
                    f.event = delegateCombine(f.event, runOn(this, this.ll));
                }
                this.lo(false);
                break;
            case TimeAxisBase.$$p[0]:
                if (this.i8 != null) {
                    this.i8.deregisterColumn(this.kq);
                    this.kq = this.kr(this.dateTimeMemberPath);
                    this.kz = null;
                }
                break;
            case TimeAxisBase.$$p[3]:
                this.ck();
                this.lo(false);
                break;
            case TimeAxisBase.$$p[2]:
                this.ck();
                this.lo(false);
                break;
            case "DateTimeColumn":
                this.lm();
                break;
        }
    };
    TimeAxisBase.prototype.ll = function (a, b) {
        this.kz = null;
    };
    TimeAxisBase.prototype.kr = function (a) {
        return TimeAxisBase.ks(a, this);
    };
    TimeAxisBase.ks = function (a, b) {
        if (a == null) {
            return b.i8.registerColumnDateTime(null, null, false);
        }
        var c = null;
        var d = SeriesViewer.bb(a, b.coercionMethods);
        c = d.a;
        return b.i8.registerColumnDateTime(a, c, b.expectFunctions);
    };
    TimeAxisBase.prototype.as = function () {
        if (this.co) {
            return (TypeRegistrar.create('HorizontalSmartAxisLabelPanel'));
        }
        else {
            return new HorizontalAxisLabelPanel();
        }
    };
    TimeAxisBase.prototype.get_az = function () {
        return 0;
    };
    Object.defineProperty(TimeAxisBase.prototype, "az", {
        get: function () {
            return this.get_az();
        },
        enumerable: true,
        configurable: true
    });
    TimeAxisBase.prototype.g1 = function (a) {
        _super.prototype.g1.call(this, a);
        var b = this.a2.ax();
        var c = this.a2.a0();
        var d = this.a2.ay();
        var e = this.a2.az();
        var f = this.a2.e();
        var g = this.a2.f();
        var h = this.a2.g();
        this.g8();
        this.gc(b);
        this.gc(c);
        this.gc(d);
        this.gc(e);
        this.cs.clear();
        this.ct.clear();
        this.jf.clear();
        this.at.ac = this;
        var i = this.bf != null ? this.bf.mq : Rect.empty;
        var j = !i.isEmpty ? this.iu : Rect.empty;
        this.at.cp = i;
        this.at.co = j;
        if (i.isEmpty || j.isEmpty) {
            this.bo.count = 0;
            this.bn.count = 0;
            this.bm.count = 0;
        }
        if (this.bo.count == 0) {
            this.at.ab.clear();
        }
        if (this.aw != null) {
            this.aw.registerAxis(this);
        }
        this.lm();
    };
    TimeAxisBase.prototype.get_b5 = 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.jr == 0) {
            return 0;
        }
        var d = this.is(b, c);
        return d.width / (this.jr * a.width);
    };
    TimeAxisBase.prototype.getGroupCenter = function (a, b, c, d) {
        return this.getCategorySize(b, c, d) * 0.5;
    };
    TimeAxisBase.prototype.jk = function (a, b, c) {
        var d = !isNaN_(this.jh) ? MathUtil.b(this.jh, 0, 1) : 0;
        var e = 1 - 0.5 * d;
        var f = this.getCategorySize(a, b, c) * e;
        return f;
    };
    TimeAxisBase.prototype.get_cc = function () {
        return true;
    };
    TimeAxisBase.prototype.lk = function (a, b) {
        return (0);
    };
    TimeAxisBase.prototype.preferredCategoryMode = function (a) {
        return this.b3 ? 0 : 1;
    };
    Object.defineProperty(TimeAxisBase.prototype, "isVertical", {
        get: function () {
            return this.ce;
        },
        enumerable: true,
        configurable: true
    });
    TimeAxisBase.prototype.j$c = function (a, b) {
        return this.dw.apply(this, arguments);
    };
    TimeAxisBase.prototype.j$b = function (a, b) {
        return this.dt.apply(this, arguments);
    };
    TimeAxisBase.prototype.j$d = function (a, b, c, d, e) {
        this.gg.apply(this, arguments);
    };
    TimeAxisBase.prototype.j$e = function (a, b, c, d) {
        this.gh.apply(this, arguments);
    };
    TimeAxisBase.prototype.j$f = function (a, b, c, d) {
        this.gi.apply(this, arguments);
    };
    Object.defineProperty(TimeAxisBase.prototype, "j$a", {
        get: function () {
            return this.b8;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(TimeAxisBase.prototype, "i$a", {
        get: function () {
            return this.b5;
        },
        enumerable: true,
        configurable: true
    });
    TimeAxisBase.$t = markType(TimeAxisBase, 'TimeAxisBase', CategoryAxisBase.$, [ISortingAxis_$type, IDateTimeAxis_$type]);
    return TimeAxisBase;
}(CategoryAxisBase));
export { TimeAxisBase };
Type.dep(DependencyProperty, PropertyMetadata, TimeAxisBase, 'gu', ['DateTimeMemberPath::ls', [2, null], 'IsDataPreSorted:kv:lt', [0, false], 'MaximumValue::lu', [3, null], 'MinimumValue::lv', [3, null]]);
