/*
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-angular-core/ES5/FastItemDateTimeColumn";
import { CategoryAxisBase } from "./CategoryAxisBase";
import { ISortingAxis_$type } from "./ISortingAxis";
import { IDateTimeAxis_$type } from "./IDateTimeAxis";
import { DependencyProperty } from "igniteui-angular-core/ES5/DependencyProperty";
import { List$1 } from "igniteui-angular-core/ES5/List$1";
import { Type, typeCast, Number_$type, Date_$type, fromEnum, runOn, delegateRemove, delegateCombine, markType, TypeRegistrar } from "igniteui-angular-core/ES5/type";
import { ScalerParams } from "./ScalerParams";
import { Rect } from "igniteui-angular-core/ES5/Rect";
import { NumericScaler } from "./NumericScaler";
import { SeriesViewer } from "./SeriesViewer";
import { SortedListView$1 } from "./SortedListView$1";
import { ArrayExtension } from "igniteui-angular-core/ES5/ArrayExtension";
import { AxisRangeChangedEventArgs } from "./AxisRangeChangedEventArgs";
import { IFastItemsSource_$type } from "igniteui-angular-core/ES5/IFastItemsSource";
import { HorizontalAxisLabelPanel } from "./HorizontalAxisLabelPanel";
import { MathUtil } from "igniteui-angular-core/ES5/MathUtil";
import { PropertyMetadata } from "igniteui-angular-core/ES5/PropertyMetadata";
import { truncate, isNaN_ } from "igniteui-angular-core/ES5/number";
import { dateMinValue, dateSubtract, dateAdd, dateMaxValue, dateFromTicks } from "igniteui-angular-core/ES5/date";
/**
 * @hidden
 */
var TimeAxisBase = /** @class */ (function (_super) {
    tslib_1.__extends(TimeAxisBase, _super);
    function TimeAxisBase() {
        var _this = _super.call(this) || this;
        _this.kv = null;
        _this.k4 = null;
        _this.kz = false;
        _this.k7 = dateMaxValue();
        _this.k6 = dateMinValue();
        FastItemDateTimeColumn.o();
        return _this;
    }
    TimeAxisBase.prototype.dv = function (a, b) {
        if (!b.c.isEmpty) {
            var c = this.lc(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.lc(a, b.e, b.d, b.c);
    };
    TimeAxisBase.prototype.e3 = function (a, b) {
        var c = this.fu(b);
        if (c == this.dateTimeMemberPath) {
            return this.e4(a, b, this.dateTimeMemberPath);
        }
        return _super.prototype.e3.call(this, a, b);
    };
    TimeAxisBase.prototype.lc = 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.ld = 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.dy = 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.ls();
    };
    TimeAxisBase.prototype.ls = function () {
        var a = this.bf != null ? this.bf.mq : Rect.empty;
        var b = !a.isEmpty ? this.i0 : Rect.empty;
        var c = (new Date());
        var d = (new Date());
        var e = false;
        if (!a.isEmpty && !b.isEmpty && this.kw != null) {
            var f = typeCast(FastItemDateTimeColumn.$, this.kw);
            if (f != null) {
                if (this.k5 == null) {
                    this.lt();
                }
            }
            else {
                this.k5 = null;
            }
            if (this.kw.count > 0) {
                e = true;
                var g = this.k5 == null ? 0 : this.k5._inner[0];
                var h = this.k5 == null ? this.kw.count - 1 : this.k5._inner[this.kw.count - 1];
                c = this.kw.item(g);
                d = this.kw.item(h);
                this.k0 = false;
                if (this.categoryMode == 2) {
                    var i = this.lq(c, d);
                    if (+c != +(dateMinValue())) {
                        c = dateSubtract(+c, +i);
                    }
                    d = dateAdd(+d, +i);
                }
            }
        }
        if (this.k3()) {
            e = true;
            c = this.minimumValue;
        }
        if (this.k2()) {
            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.lx();
        }
    };
    TimeAxisBase.prototype.lx = function () {
    };
    Object.defineProperty(TimeAxisBase.prototype, "kw", {
        get: function () {
            return this.kv;
        },
        set: function (a) {
            if (this.kv != a) {
                var b = this.kv;
                this.kv = a;
                this.gz("DateTimeColumn", b, this.kv);
            }
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(TimeAxisBase.prototype, "k5", {
        get: function () {
            return this.k4;
        },
        set: function (a) {
            this.k4 = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(TimeAxisBase.prototype, "i$b", {
        get: function () {
            if (this.k5 == null) {
                var a = typeCast(FastItemDateTimeColumn.$, this.kw);
                if (a != null) {
                    this.lt();
                }
                else {
                    this.k5 = null;
                }
            }
            return this.k5;
        },
        enumerable: true,
        configurable: true
    });
    TimeAxisBase.prototype.lt = function () {
        if (this.k1) {
            this.k5 = new List$1(Number_$type, 0);
            var a = this.k5;
            var b = this.kw.count;
            for (var c = 0; c < b; c++) {
                a.add(c);
            }
        }
        else {
            var d = typeCast(FastItemDateTimeColumn.$, this.kw);
            this.k5 = d.h();
        }
    };
    TimeAxisBase.prototype.i$e = function (a, b, c, d) {
        return this.lf(a, b, c, d);
    };
    TimeAxisBase.prototype.lf = 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.dy(e.left, f);
            g = this.dy(e.right, f);
        }
        else {
            g = this.dy(e.left, f);
            h = this.dy(e.right, f);
        }
        var i = Math.max(dateMinValue().getTime(), Math.min(dateMaxValue().getTime(), truncate(g)));
        return this.le(dateFromTicks(i));
    };
    TimeAxisBase.prototype.le = function (a) {
        if (this.kw == null || this.k5.count != this.kw.count) {
            return -1;
        }
        for (var b = 0; b < this.k5.count; b++) {
            var c = this.kw.item(this.k5._inner[b]);
            if (c >= a) {
                return Math.max(b - 1, 0);
            }
        }
        return this.k5.count - 1;
    };
    TimeAxisBase.prototype.i$g = function (a, b, c, d) {
        return this.li(a, b, c, d);
    };
    TimeAxisBase.prototype.li = 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.dy(e.left, f);
            g = this.dy(e.right, f);
        }
        else {
            g = this.dy(e.left, f);
            h = this.dy(e.right, f);
        }
        var i = Math.max(dateMinValue().getTime(), Math.min(dateMaxValue().getTime(), truncate(h)));
        return this.lh(dateFromTicks(i));
    };
    TimeAxisBase.prototype.lh = function (a) {
        if (this.kw == null || this.k5.count != this.kw.count) {
            return -1;
        }
        for (var b = this.k5.count - 1; b >= 0; b--) {
            var c = this.k5._inner[b];
            var d = this.kw.item(c);
            if (d < a) {
                return b == this.k5.count - 1 ? b : b + 1;
            }
        }
        return 0;
    };
    TimeAxisBase.prototype.i$d = function (a) {
        if (this.kw == null) {
            return NaN;
        }
        var b = this.kv.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.kw, b.i$b);
        var ticks_ = a;
        var d = (new Date(ticks_));
        var e = this.lg(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.iv();
        if (e == b.i$b.count && this.bf != null && this.dv(a, new ScalerParams(0, this.bf.mq, this.i0, this.b8, i)) < (this.i0.right + 2)) {
            return e - 1;
        }
        if (e == -1 && this.bf != null && Math.abs(this.dv(a, new ScalerParams(0, this.bf.mq, this.i0, this.b8, i)) - this.i0.left) < 2) {
            return 0;
        }
        return -1;
    };
    TimeAxisBase.prototype.lg = function (a, b) {
        var c = this;
        if (this.kw == 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.kw == null || b.i$b.count != this.kw.count) {
            return -1;
        }
        var c = new SortedListView$1(Date_$type, this.kw, b.i$b);
        var ticks_ = a;
        var d = (new Date(ticks_));
        var e = this.lg(d, c);
        var f = this.iv();
        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.dv(a, new ScalerParams(0, this.bf.mq, this.i0, this.b8, f)) < (this.i0.right + 2)) {
            return e - 1;
        }
        if (e == -1 && this.bf != null && Math.abs(this.dv(a, new ScalerParams(0, this.bf.mq, this.i0, this.b8, f)) - this.i0.left) < 2) {
            return 0;
        }
        return -1;
    };
    TimeAxisBase.prototype.i$i = function () {
        this.k5 = null;
        this.g4();
    };
    TimeAxisBase.prototype.get_k0 = function () {
        return this.kz;
    };
    TimeAxisBase.prototype.set_k0 = function (a) {
        this.kz = a;
    };
    Object.defineProperty(TimeAxisBase.prototype, "k0", {
        get: function () {
            return this.get_k0();
        },
        set: function (a) {
            this.set_k0(a);
        },
        enumerable: true,
        configurable: true
    });
    TimeAxisBase.prototype.gv = function (a) {
        _super.prototype.gv.call(this, a);
        this.dateTimeMemberPath = a;
    };
    TimeAxisBase.prototype.gy = function (a) {
        _super.prototype.gy.call(this, a);
        this.minimumValue = a;
    };
    TimeAxisBase.prototype.gx = function (a) {
        _super.prototype.gy.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.ls();
        var c = !this.k3() ? this.actualMinimumValue.getTime() : this.minimumValue.getTime();
        var d = !this.k2() ? this.actualMaximumValue.getTime() : this.maximumValue.getTime();
        var e = new AxisRangeChangedEventArgs(0, a, c, b, d);
        this.g0(e);
        return true;
    };
    TimeAxisBase.prototype.k3 = function () {
        if (+(this.minimumValue) == +(dateMinValue())) {
            return false;
        }
        return (this.minimumValue != null);
    };
    TimeAxisBase.prototype.k2 = function () {
        if (+(this.maximumValue) == +(dateMaxValue())) {
            return false;
        }
        return (this.maximumValue != null);
    };
    Object.defineProperty(TimeAxisBase.prototype, "actualMinimumValue", {
        get: function () {
            return this.k7;
        },
        set: function (a) {
            var b = +(this.k7) != +a;
            if (b) {
                var c = this.k7;
                this.k7 = a;
                this.gz("ActualMinimumValue", c, a);
            }
        },
        enumerable: true,
        configurable: true
    });
    TimeAxisBase.prototype.e8 = function () {
        return this.actualMinimumValue;
    };
    Object.defineProperty(TimeAxisBase.prototype, "actualMaximumValue", {
        get: function () {
            return this.k6;
        },
        set: function (a) {
            var b = +(this.k6) != +a;
            if (b) {
                var c = this.k6;
                this.k6 = a;
                this.gz("ActualMaximumValue", c, a);
            }
        },
        enumerable: true,
        configurable: true
    });
    TimeAxisBase.prototype.e7 = function () {
        return this.maximumValue;
    };
    TimeAxisBase.prototype.lu = function (a) {
        var e_1, _a;
        this.g6(a);
        if (this.je == null) {
            return;
        }
        try {
            for (var _b = tslib_1.__values(fromEnum(this.ct())), _c = _b.next(); !_c.done; _c = _b.next()) {
                var b = _c.value;
                b.jc(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.gt = function (a, b, c, d) {
        _super.prototype.gt.call(this, a, b, c, d);
        switch (b) {
            case "FastItemsSource":
                var e = typeCast(IFastItemsSource_$type, c);
                if (e != null) {
                    e.deregisterColumn(this.kw);
                    this.kw = null;
                    e.event = delegateRemove(e.event, runOn(this, this.lr));
                }
                var f = typeCast(IFastItemsSource_$type, d);
                if (f != null) {
                    this.k5 = null;
                    this.kw = this.kx(this.dateTimeMemberPath);
                    f.event = delegateCombine(f.event, runOn(this, this.lr));
                }
                this.lu(false);
                break;
            case TimeAxisBase.$$p[0]:
                if (this.je != null) {
                    this.je.deregisterColumn(this.kw);
                    this.kw = this.kx(this.dateTimeMemberPath);
                    this.k5 = null;
                }
                break;
            case TimeAxisBase.$$p[3]:
                this.ck();
                this.lu(false);
                break;
            case TimeAxisBase.$$p[2]:
                this.ck();
                this.lu(false);
                break;
            case "DateTimeColumn":
                this.ls();
                break;
        }
    };
    TimeAxisBase.prototype.lr = function (a, b) {
        this.k5 = null;
    };
    TimeAxisBase.prototype.kx = function (a) {
        return TimeAxisBase.ky(a, this);
    };
    TimeAxisBase.ky = function (a, b) {
        if (a == null) {
            return b.je.registerColumnDateTime(null, null, false);
        }
        var c = null;
        var d = SeriesViewer.bb(a, b.coercionMethods);
        c = d.a;
        return b.je.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.g6 = function (a) {
        _super.prototype.g6.call(this, a);
        var b = this.a2.ay();
        var c = this.a2.a1();
        var d = this.a2.az();
        var e = this.a2.a0();
        var f = this.a2.e();
        var g = this.a2.f();
        var h = this.a2.g();
        this.hd();
        this.gh(b);
        this.gh(c);
        this.gh(d);
        this.gh(e);
        this.cu.clear();
        this.cv.clear();
        this.jl.clear();
        this.at.ac = this;
        var i = this.bf != null ? this.bf.mq : Rect.empty;
        var j = !i.isEmpty ? this.i0 : 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.ls();
    };
    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.jx == 0) {
            return 0;
        }
        var d = this.iy(b, c);
        return d.width / (this.jx * a.width);
    };
    TimeAxisBase.prototype.getGroupCenter = function (a, b, c, d) {
        return this.getCategorySize(b, c, d) * 0.5;
    };
    TimeAxisBase.prototype.jq = function (a, b, c) {
        var d = !isNaN_(this.jn) ? MathUtil.b(this.jn, 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.lq = 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.dy.apply(this, arguments);
    };
    TimeAxisBase.prototype.j$b = function (a, b) {
        return this.dv.apply(this, arguments);
    };
    TimeAxisBase.prototype.j$d = function (a, b, c, d, e) {
        this.gl.apply(this, arguments);
    };
    TimeAxisBase.prototype.j$e = function (a, b, c, d) {
        this.gm.apply(this, arguments);
    };
    TimeAxisBase.prototype.j$f = function (a, b, c, d) {
        this.gn.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, 'gz', ['DateTimeMemberPath::ly', [2, null], 'IsDataPreSorted:k1:lz', [0, false], 'MaximumValue::l0', [3, null], 'MinimumValue::l1', [3, null]]);
//# sourceMappingURL=TimeAxisBase.js.map