/*
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";
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 { MathUtil } from "igniteui-angular-core";
import { PropertyMetadata } from "igniteui-angular-core";
import { truncate, isNaN_ } from "igniteui-angular-core";
import { dateMinValue, dateSubtract, dateAdd, dateMaxValue, dateFromTicks } from "igniteui-angular-core";
/**
 * @hidden
 */
var TimeAxisBase = /** @class */ (function (_super) {
    tslib_1.__extends(TimeAxisBase, _super);
    function TimeAxisBase() {
        var _this = _super.call(this) || this;
        _this.kw = null;
        _this.k5 = null;
        _this.k0 = false;
        _this.k8 = dateMaxValue();
        _this.k7 = dateMinValue();
        FastItemDateTimeColumn.o();
        return _this;
    }
    TimeAxisBase.prototype.dw = function (a, b) {
        if (!b.c.isEmpty) {
            var c = this.ld(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.ld(a, b.e, b.d, b.c);
    };
    TimeAxisBase.prototype.e4 = function (a, b) {
        var c = this.fv(b);
        if (c == this.dateTimeMemberPath) {
            return this.e5(a, b, this.dateTimeMemberPath);
        }
        return _super.prototype.e4.call(this, a, b);
    };
    TimeAxisBase.prototype.ld = 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.le = 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.dz = 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.b8) {
            e = 1 - e;
        }
        return truncate(Math.floor(this.actualMinimumValue.getTime() + e * (this.actualMaximumValue.getTime() - this.actualMinimumValue.getTime())));
    };
    TimeAxisBase.prototype.i$h = function () {
        this.lt();
    };
    TimeAxisBase.prototype.lt = function () {
        var a = this.bf != null ? this.bf.mq : Rect.empty;
        var b = !a.isEmpty ? this.i1 : Rect.empty;
        var c = (new Date());
        var d = (new Date());
        var e = false;
        if (!a.isEmpty && !b.isEmpty && this.kx != null) {
            var f = typeCast(FastItemDateTimeColumn.$, this.kx);
            if (f != null) {
                if (this.k6 == null) {
                    this.lu();
                }
            }
            else {
                this.k6 = null;
            }
            if (this.kx.count > 0) {
                e = true;
                var g = this.k6 == null ? 0 : this.k6._inner[0];
                var h = this.k6 == null ? this.kx.count - 1 : this.k6._inner[this.kx.count - 1];
                c = this.kx.item(g);
                d = this.kx.item(h);
                this.k1 = false;
                if (this.categoryMode == 2) {
                    var i = this.lr(c, d);
                    if (+c != +(dateMinValue())) {
                        c = dateSubtract(+c, +i);
                    }
                    d = dateAdd(+d, +i);
                }
            }
        }
        if (this.k4()) {
            e = true;
            c = this.minimumValue;
        }
        if (this.k3()) {
            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.ly();
        }
    };
    TimeAxisBase.prototype.ly = function () {
    };
    Object.defineProperty(TimeAxisBase.prototype, "kx", {
        get: function () {
            return this.kw;
        },
        set: function (a) {
            if (this.kw != a) {
                var b = this.kw;
                this.kw = a;
                this.g0("DateTimeColumn", b, this.kw);
            }
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(TimeAxisBase.prototype, "k6", {
        get: function () {
            return this.k5;
        },
        set: function (a) {
            this.k5 = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(TimeAxisBase.prototype, "i$b", {
        get: function () {
            if (this.k6 == null) {
                var a = typeCast(FastItemDateTimeColumn.$, this.kx);
                if (a != null) {
                    this.lu();
                }
                else {
                    this.k6 = null;
                }
            }
            return this.k6;
        },
        enumerable: true,
        configurable: true
    });
    TimeAxisBase.prototype.lu = function () {
        if (this.k2) {
            this.k6 = new List$1(Number_$type, 0);
            var a = this.k6;
            var b = this.kx.count;
            for (var c = 0; c < b; c++) {
                a.add(c);
            }
        }
        else {
            var d = typeCast(FastItemDateTimeColumn.$, this.kx);
            this.k6 = d.h();
        }
    };
    TimeAxisBase.prototype.i$e = function (a, b, c, d) {
        return this.lg(a, b, c, d);
    };
    TimeAxisBase.prototype.lg = 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.dz(e.left, f);
            g = this.dz(e.right, f);
        }
        else {
            g = this.dz(e.left, f);
            h = this.dz(e.right, f);
        }
        var i = Math.max(dateMinValue().getTime(), Math.min(dateMaxValue().getTime(), truncate(g)));
        return this.lf(dateFromTicks(i));
    };
    TimeAxisBase.prototype.lf = function (a) {
        if (this.kx == null || this.k6.count != this.kx.count) {
            return -1;
        }
        for (var b = 0; b < this.k6.count; b++) {
            var c = this.kx.item(this.k6._inner[b]);
            if (c >= a) {
                return Math.max(b - 1, 0);
            }
        }
        return this.k6.count - 1;
    };
    TimeAxisBase.prototype.i$g = function (a, b, c, d) {
        return this.lj(a, b, c, d);
    };
    TimeAxisBase.prototype.lj = 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.dz(e.left, f);
            g = this.dz(e.right, f);
        }
        else {
            g = this.dz(e.left, f);
            h = this.dz(e.right, f);
        }
        var i = Math.max(dateMinValue().getTime(), Math.min(dateMaxValue().getTime(), truncate(h)));
        return this.li(dateFromTicks(i));
    };
    TimeAxisBase.prototype.li = function (a) {
        if (this.kx == null || this.k6.count != this.kx.count) {
            return -1;
        }
        for (var b = this.k6.count - 1; b >= 0; b--) {
            var c = this.k6._inner[b];
            var d = this.kx.item(c);
            if (d < a) {
                return b == this.k6.count - 1 ? b : b + 1;
            }
        }
        return 0;
    };
    TimeAxisBase.prototype.i$d = function (a) {
        if (this.kx == null) {
            return NaN;
        }
        var b = this.kw.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.kx, b.i$b);
        var ticks_ = a;
        var d = (new Date(ticks_));
        var e = this.lh(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.iw();
        if (e == b.i$b.count && this.bf != null && this.dw(a, new ScalerParams(0, this.bf.mq, this.i1, this.b8, i)) < (this.i1.right + 2)) {
            return e - 1;
        }
        if (e == -1 && this.bf != null && Math.abs(this.dw(a, new ScalerParams(0, this.bf.mq, this.i1, this.b8, i)) - this.i1.left) < 2) {
            return 0;
        }
        return -1;
    };
    TimeAxisBase.prototype.lh = function (a, b) {
        var c = this;
        if (this.kx == 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.kx == null || b.i$b.count != this.kx.count) {
            return -1;
        }
        var c = new SortedListView$1(Date_$type, this.kx, b.i$b);
        var ticks_ = a;
        var d = (new Date(ticks_));
        var e = this.lh(d, c);
        var f = this.iw();
        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.dw(a, new ScalerParams(0, this.bf.mq, this.i1, this.b8, f)) < (this.i1.right + 2)) {
            return e - 1;
        }
        if (e == -1 && this.bf != null && Math.abs(this.dw(a, new ScalerParams(0, this.bf.mq, this.i1, this.b8, f)) - this.i1.left) < 2) {
            return 0;
        }
        return -1;
    };
    TimeAxisBase.prototype.i$i = function () {
        this.k6 = null;
        this.g5();
    };
    TimeAxisBase.prototype.get_k1 = function () {
        return this.k0;
    };
    TimeAxisBase.prototype.set_k1 = function (a) {
        this.k0 = a;
    };
    Object.defineProperty(TimeAxisBase.prototype, "k1", {
        get: function () {
            return this.get_k1();
        },
        set: function (a) {
            this.set_k1(a);
        },
        enumerable: true,
        configurable: true
    });
    TimeAxisBase.prototype.gw = function (a) {
        _super.prototype.gw.call(this, a);
        this.dateTimeMemberPath = a;
    };
    TimeAxisBase.prototype.gz = function (a) {
        _super.prototype.gz.call(this, a);
        this.minimumValue = a;
    };
    TimeAxisBase.prototype.gy = function (a) {
        _super.prototype.gz.call(this, a);
        this.maximumValue = a;
    };
    TimeAxisBase.prototype.cn = function () {
        if (this.cg && this.co) {
            this.a9 = this.a8();
        }
        var a = this.actualMinimumValue.getTime();
        var b = this.actualMaximumValue.getTime();
        this.lt();
        var c = !this.k4() ? this.actualMinimumValue.getTime() : this.minimumValue.getTime();
        var d = !this.k3() ? this.actualMaximumValue.getTime() : this.maximumValue.getTime();
        var e = new AxisRangeChangedEventArgs(0, a, c, b, d);
        this.g1(e);
        return true;
    };
    TimeAxisBase.prototype.k4 = function () {
        if (+(this.minimumValue) == +(dateMinValue())) {
            return false;
        }
        return (this.minimumValue != null);
    };
    TimeAxisBase.prototype.k3 = function () {
        if (+(this.maximumValue) == +(dateMaxValue())) {
            return false;
        }
        return (this.maximumValue != null);
    };
    Object.defineProperty(TimeAxisBase.prototype, "actualMinimumValue", {
        get: function () {
            return this.k8;
        },
        set: function (a) {
            var b = +(this.k8) != +a;
            if (b) {
                var c = this.k8;
                this.k8 = a;
                this.g0("ActualMinimumValue", c, a);
            }
        },
        enumerable: true,
        configurable: true
    });
    TimeAxisBase.prototype.e9 = function () {
        return this.actualMinimumValue;
    };
    Object.defineProperty(TimeAxisBase.prototype, "actualMaximumValue", {
        get: function () {
            return this.k7;
        },
        set: function (a) {
            var b = +(this.k7) != +a;
            if (b) {
                var c = this.k7;
                this.k7 = a;
                this.g0("ActualMaximumValue", c, a);
            }
        },
        enumerable: true,
        configurable: true
    });
    TimeAxisBase.prototype.e8 = function () {
        return this.maximumValue;
    };
    TimeAxisBase.prototype.lv = function (a) {
        var e_1, _a;
        this.g7(a);
        if (this.jf == null) {
            return;
        }
        try {
            for (var _b = tslib_1.__values(fromEnum(this.cu())), _c = _b.next(); !_c.done; _c = _b.next()) {
                var b = _c.value;
                b.jd(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.gu = function (a, b, c, d) {
        _super.prototype.gu.call(this, a, b, c, d);
        switch (b) {
            case "FastItemsSource":
                var e = typeCast(IFastItemsSource_$type, c);
                if (e != null) {
                    e.deregisterColumn(this.kx);
                    this.kx = null;
                    e.event = delegateRemove(e.event, runOn(this, this.ls));
                }
                var f = typeCast(IFastItemsSource_$type, d);
                if (f != null) {
                    this.k6 = null;
                    this.kx = this.ky(this.dateTimeMemberPath);
                    f.event = delegateCombine(f.event, runOn(this, this.ls));
                }
                this.lv(false);
                break;
            case TimeAxisBase.$$p[0]:
                if (this.jf != null) {
                    this.jf.deregisterColumn(this.kx);
                    this.kx = this.ky(this.dateTimeMemberPath);
                    this.k6 = null;
                }
                break;
            case TimeAxisBase.$$p[3]:
                this.cl();
                this.lv(false);
                break;
            case TimeAxisBase.$$p[2]:
                this.cl();
                this.lv(false);
                break;
            case "DateTimeColumn":
                this.lt();
                break;
        }
    };
    TimeAxisBase.prototype.ls = function (a, b) {
        this.k6 = null;
    };
    TimeAxisBase.prototype.ky = function (a) {
        return TimeAxisBase.kz(a, this);
    };
    TimeAxisBase.kz = function (a, b) {
        if (a == null) {
            return b.jf.registerColumnDateTime(null, null, false);
        }
        var c = null;
        var d = SeriesViewer.bb(a, b.coercionMethods);
        c = d.a;
        return b.jf.registerColumnDateTime(a, c, b.expectFunctions);
    };
    TimeAxisBase.prototype.as = function () {
        if (this.cp) {
            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.g7 = function (a) {
        _super.prototype.g7.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.he();
        this.gi(b);
        this.gi(c);
        this.gi(d);
        this.gi(e);
        this.cv.clear();
        this.cw.clear();
        this.jm.clear();
        this.at.ac = this;
        var i = this.bf != null ? this.bf.mq : Rect.empty;
        var j = !i.isEmpty ? this.i1 : 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.lt();
    };
    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.jy == 0) {
            return 0;
        }
        var d = this.iz(b, c);
        return d.width / (this.jy * a.width);
    };
    TimeAxisBase.prototype.getGroupCenter = function (a, b, c, d) {
        return this.getCategorySize(b, c, d) * 0.5;
    };
    TimeAxisBase.prototype.jr = function (a, b, c) {
        var d = !isNaN_(this.jo) ? MathUtil.b(this.jo, 0, 1) : 0;
        var e = 1 - 0.5 * d;
        var f = this.getCategorySize(a, b, c) * e;
        return f;
    };
    TimeAxisBase.prototype.get_cd = function () {
        return true;
    };
    TimeAxisBase.prototype.lr = function (a, b) {
        return (0);
    };
    TimeAxisBase.prototype.preferredCategoryMode = function (a) {
        return this.b3 ? 0 : 1;
    };
    Object.defineProperty(TimeAxisBase.prototype, "isVertical", {
        get: function () {
            return this.cf;
        },
        enumerable: true,
        configurable: true
    });
    TimeAxisBase.prototype.j$c = function (a, b) {
        return this.dz.apply(this, arguments);
    };
    TimeAxisBase.prototype.j$b = function (a, b) {
        return this.dw.apply(this, arguments);
    };
    TimeAxisBase.prototype.j$d = function (a, b, c, d, e) {
        this.gm.apply(this, arguments);
    };
    TimeAxisBase.prototype.j$e = function (a, b, c, d) {
        this.gn.apply(this, arguments);
    };
    TimeAxisBase.prototype.j$f = function (a, b, c, d) {
        this.go.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]);
    TimeAxisBase.$$p = markDep(DependencyProperty, PropertyMetadata, TimeAxisBase, 'g0', ['DateTimeMemberPath::lz', [2, null], 'IsDataPreSorted:k2:l0', [0, false], 'MaximumValue::l1', [3, null], 'MinimumValue::l2', [3, null]]);
    return TimeAxisBase;
}(CategoryAxisBase));
export { TimeAxisBase };
//# sourceMappingURL=TimeAxisBase.js.map