/*
THIS INFRAGISTICS ULTIMATE SOFTWARE LICENSE  AGREEMENT ("AGREEMENT") LOCATED HERE:
https://www.infragistics.com/legal/license/igultimate-la
https://www.infragistics.com/legal/license/igultimate-eula
GOVERNS THE LICENSING, INSTALLATION AND USE OF INFRAGISTICS SOFTWARE. BY DOWNLOADING AND/OR INSTALLING AND USING INFRAGISTICS SOFTWARE:  you are indicating that you have read and understand this Agreement, and agree to be legally bound by it on behalf of the yourself and your company.
*/
import { FastItemDateTimeColumn } from "igniteui-angular-core";
import { CategoryXAxis } from "./CategoryXAxis";
import { ITimeRangeSelectorAxis_$type } from "./ITimeRangeSelectorAxis";
import { IOrdinalAxis_$type } from "./IOrdinalAxis";
import { TimeAxisLabellingMode_$type } from "./TimeAxisLabellingMode";
import { TimeAxisLabelFormatCollection } from "./TimeAxisLabelFormatCollection";
import { Rect } from "igniteui-angular-core";
import { ScalerParams } from "./ScalerParams";
import { NumericScaler } from "./NumericScaler";
import { EventArgs, runOn, delegateCombine, typeCast, enumGetBox, Date_$type, markType } from "igniteui-angular-core";
import { IFastItemsSource_$type } from "igniteui-angular-core";
import { TimeAxisBase } from "./TimeAxisBase";
import { TimeXAxis } from "./TimeXAxis";
import { AxisRangeChangedEventArgs } from "./AxisRangeChangedEventArgs";
import { truncate } from "igniteui-angular-core";
import { dateMaxValue, dateMinValue, dateAdd, dateSubtract } from "igniteui-angular-core";
import { timeSpanFromMilliseconds, timeSpanTotalMilliseconds } from "igniteui-angular-core";
/**
 * @hidden
 */
export let OrdinalTimeXAxis = /*@__PURE__*/ (() => {
    class OrdinalTimeXAxis extends CategoryXAxis {
        constructor() {
            super();
            this.nc = dateMaxValue();
            this.nb = dateMinValue();
            this.ni = dateMaxValue();
            this.nh = dateMinValue();
            this.ne = dateMaxValue();
            this.nd = dateMinValue();
            this.ns = null;
            this.nr = null;
            this.m7 = 0;
            this.m9 = null;
            this.m5 = null;
            this.n1 = 0;
            this.ng = dateMinValue();
            this.nf = dateMaxValue();
            this.visibleRangeChanged = null;
            FastItemDateTimeColumn.o();
            this.m5 = new TimeAxisLabelFormatCollection();
            let a = this.m6;
            a.collectionChanged = delegateCombine(a.collectionChanged, runOn(this, this.n3));
            this.mv = 0;
        }
        ek(a, b, c) {
            return this.ei(a, new ScalerParams(0, b, c, this.cg, this.j7()));
        }
        ej(a, b, c) {
            return this.ei(a, b);
        }
        ei(a, b) {
            if (!b.c.isEmpty) {
                let c = this.np(a, NumericScaler.ac, b.c);
                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.np(a, b.e, b.d);
        }
        np(a, b, c) {
            let d = this.mu;
            let e = this.mv;
            let f = this.mu - this.mv;
            let g = f >= 1 ? (a - e) / (d - e) : f == 0 ? 0.5 : NaN;
            if (this.ch) {
                g = 1 - g;
            }
            return c.left + c.width * (g - b.left) / b.width;
        }
        hl(a, b, c, d, e) {
            let f;
            let g = e.e;
            let h = e.d;
            let i = e.c;
            let j = e.a;
            let k = !i.isEmpty;
            let l = i.left;
            let m = i.width;
            let n = g.left;
            let o = g.width;
            let p = h.left;
            let q = h.width;
            let r = 0;
            let s = 1;
            let t = this.mu;
            let u = this.mv;
            let v = this.mu - this.mv;
            let w = this.mf(a);
            let x = w != null;
            for (let y = 0; y < b.count; y++) {
                let z = b.item(y);
                for (let aa = c; aa < d; aa++) {
                    f = x ? w[aa] : a.item(aa);
                    if (k) {
                        let ab = v >= 1 ? (f[z] - u) / (t - u) : v == 0 ? 0.5 : NaN;
                        if (j) {
                            ab = 1 - ab;
                        }
                        ab = l + m * (ab - r) / s;
                        let ac = (ab - p) / q;
                        ac = p + q * (ac - n) / o;
                        f[z] = ac;
                    }
                    else {
                        let ad = v >= 1 ? (f[z] - u) / (t - u) : v == 0 ? 0.5 : NaN;
                        if (j) {
                            ad = 1 - ad;
                        }
                        ad = p + q * (ad - n) / o;
                        f[z] = ad;
                    }
                }
            }
        }
        hm(a, b, c, d) {
            for (let e = b; e < c; e++) {
                a.item(e, this.ei(a.item(e), d));
            }
        }
        en(a, b, c) {
            return this.el(a, new ScalerParams(0, b, this.kc, this.cg, this.j7()));
        }
        getUnscaledValue3(a, b, c, d) {
            return this.el(a, new ScalerParams(0, b, this.kc, this.cg, this.j7()));
        }
        el(a, b) {
            if (!b.c.isEmpty) {
                let c = b.e.left + b.e.width * (a - b.d.left) / b.d.width;
                c = (c * b.d.width) + b.d.left;
                c = this.nq(c, NumericScaler.ac, b.c);
                return c;
            }
            return this.nq(a, b.e, b.d);
        }
        nq(a, b, c) {
            let d = b.left + (a - c.left) * b.width / c.width;
            if (this.ch) {
                d = 1 - d;
            }
            return this.mv + d * (this.mu - this.mv);
        }
        em(a, b, c) {
            return this.el(a, b);
        }
        hn(a, b, c, d) {
            for (let e = b; e < c; e++) {
                a.item(e, this.el(a.item(e), d));
            }
        }
        hv(a, b, c, d) {
            super.hv(a, b, c, d);
            switch (b) {
                case "FastItemsSource":
                    let e = typeCast(IFastItemsSource_$type, c);
                    if (e != null) {
                        e.deregisterColumn(this.na);
                        this.na = null;
                    }
                    if (this.kq != null) {
                        this.na = TimeAxisBase.mi(this.dateTimeMemberPath, this);
                    }
                    break;
                case "DateTimeMemberPath":
                    if (this.kq != null) {
                        this.kq.deregisterColumn(this.na);
                        this.na = TimeAxisBase.mi(this.dateTimeMemberPath, this);
                    }
                    break;
                case "DateTimeColumn":
                    this.c0();
                    this.n1 = this.n0();
                    break;
                case "MinimumValue":
                case "MaximumValue":
                    this.c0();
                    this.h6();
                    break;
                case "LabellingMode":
                    this.h6();
                    break;
            }
        }
        get actualMinimumValue() {
            return this.nc;
        }
        set actualMinimumValue(a) {
            this.nc = a;
        }
        get actualMaximumValue() {
            return this.nb;
        }
        set actualMaximumValue(a) {
            this.nb = a;
        }
        get visibleMinimum() {
            return this.ni;
        }
        set visibleMinimum(a) {
            this.ni = a;
        }
        get visibleMaximum() {
            return this.nh;
        }
        set visibleMaximum(a) {
            this.nh = a;
        }
        get nm() {
            return this.ne;
        }
        set nm(a) {
            this.ne = a;
        }
        get nl() {
            return this.nd;
        }
        set nl(a) {
            this.nd = a;
        }
        n4() {
            if (this.na == null || this.na.count == 0 || this.na.count < this.mv - 1) {
                this.actualMinimumValue = dateMinValue();
                this.actualMaximumValue = dateMaxValue();
                return;
            }
            let a = Math.max(0, Math.min(this.mv, this.na.count - 1));
            let b = Math.max(0, Math.min(this.mu, this.na.count - 1));
            let c = +(this.actualMinimumValue) != +(this.na.item(a)) || +(this.actualMaximumValue) != +(this.na.item(b));
            if (c) {
                let d = this.actualMinimumValue;
                let e = this.actualMaximumValue;
                this.actualMinimumValue = this.na.item(a);
                this.actualMaximumValue = this.na.item(b);
                this.n5();
            }
        }
        n5() {
            if (this.na == null || this.na.count == 0 || this.bh == null) {
                let a = +(this.visibleMinimum) != +(this.actualMinimumValue) || +(this.visibleMaximum) != +(this.actualMaximumValue);
                this.visibleMinimum = this.actualMinimumValue;
                this.visibleMaximum = this.actualMaximumValue;
                this.nm = this.visibleMinimum;
                this.nl = this.visibleMaximum;
                if (a && this.visibleRangeChanged != null) {
                    this.visibleRangeChanged(this, EventArgs.empty);
                }
                return;
            }
            let b = this.j6();
            let c = new ScalerParams(1, this.bh.sv, this.bh.su, this.cg);
            c.c = this.j7();
            let d = this.el(b.left, c);
            let e = this.el(b.right, c);
            let f = truncate(Math.floor(d));
            let g = truncate(Math.ceil(e));
            let h = f >= 0 && f < this.na.count ? this.na.item(f) : this.actualMinimumValue;
            let i = g >= 0 && g < this.na.count ? this.na.item(g) : this.actualMaximumValue;
            let j = h;
            let k = i;
            if (d > f) {
                let l = f + 1;
                let m = l >= 0 && l < this.na.count ? this.na.item(l) : i;
                let n = dateAdd(+h, +(timeSpanFromMilliseconds(timeSpanTotalMilliseconds((+m - +h)) * (d - f))));
                j = n;
            }
            if (e < g) {
                let o = g - 1;
                let p = o >= 0 && o < this.na.count ? this.na.item(o) : h;
                let q = dateSubtract(+i, +(timeSpanFromMilliseconds(timeSpanTotalMilliseconds((+i - +p)) * (g - e))));
                k = q;
            }
            this.nm = j;
            this.nl = k;
            let r = +(this.visibleMinimum) != +h || +(this.visibleMaximum) != +i;
            this.visibleMinimum = h;
            this.visibleMaximum = i;
            if (r && this.visibleRangeChanged != null) {
                this.visibleRangeChanged(this, EventArgs.empty);
            }
        }
        h8(a) {
            let b = TimeXAxis.ol(this.actualMinimumValue, false, this.actualMinimumValue, this.m8, this.nz, this.m6, this.fj);
            if (b != this.ns && this.c3) {
                this.ic();
            }
            this.ns = b;
            super.h8(a);
        }
        get dateTimeMemberPath() {
            return this.nr;
        }
        set dateTimeMemberPath(a) {
            let b = this.dateTimeMemberPath;
            if (a != b) {
                this.nr = a;
                this.h1("DateTimeMemberPath", b, this.dateTimeMemberPath);
            }
        }
        get m8() {
            return this.m7;
        }
        set m8(a) {
            let b = this.m8;
            if (a != b) {
                this.m7 = a;
                this.h1("LabellingMode", enumGetBox(TimeAxisLabellingMode_$type, b), enumGetBox(TimeAxisLabellingMode_$type, this.m8));
            }
        }
        get_cj() {
            return true;
        }
        get na() {
            return this.m9;
        }
        set na(a) {
            if (this.m9 != a) {
                let b = this.m9;
                this.m9 = a;
                this.h1("DateTimeColumn", b, this.m9);
            }
        }
        ip(a, b) {
            super.ip(a, b);
            this.n5();
        }
        getWindowRectForSelection(a, b) {
            if (this.na == null) {
                return Rect.empty;
            }
            let c = a > b;
            let d = c ? b : a;
            let e = c ? a : b;
            let f = -1;
            while (++f < this.na.count && this.na.item(f) < d) {
            }
            let g = this.na.count;
            while (--g >= 0 && this.na.item(g) > e) {
            }
            let h = new ScalerParams(0, new Rect(0, 0, 0, 1, 1), new Rect(0, 0, 0, 1, 1), this.cg, this.bh.sj);
            let i = this.ei(f, h);
            let j = this.ei(g + 1, h);
            return new Rect(0, i, NaN, Math.max(0, j - i), NaN);
        }
        get m6() {
            return this.m5;
        }
        n3(a, b) {
            this.h7(false);
        }
        hx(a) {
            super.hx(a);
            this.dateTimeMemberPath = a;
        }
        fy(a) {
            if (this.at != null) {
                return this.at(a);
            }
            if (this.ah != null) {
                return this.ah.i(a);
            }
            if (!(typeCast(Date_$type, a) !== null)) {
                return super.fy(a);
            }
            let b = dateMinValue();
            let c = false;
            if (this.mw != null) {
                if (typeCast(Date_$type, this.mw) !== null) {
                    b = this.mw;
                    c = true;
                }
            }
            let d = TimeXAxis.og(a, c, b, this.m8, this.nz, this.m6, this.fj);
            return d == null ? super.fy(a) : d;
        }
        gc(a) {
            let b = 0;
            if (this.categoryMode == 0) {
                b = truncate(Math.round(a));
            }
            else {
                b = truncate(Math.floor(a));
            }
            if (b < 0 || b > this.lf - 1) {
                return super.gc(a);
            }
            let c = this.na.item(b);
            let d = this.fy(c);
            if (d == null) {
                return null;
            }
            return d.toString();
        }
        getDateTime(a) {
            let b = truncate(Math.round(a));
            if (b < 0) {
                b = 0;
            }
            if (b > this.lf - 1) {
                b = this.lf - 1;
            }
            if (this.na == null) {
                return dateMinValue();
            }
            let c = this.na.item(b);
            return c;
        }
        get n2() {
            return +(this.visibleMaximum) - +(this.visibleMinimum);
        }
        get nz() {
            return +(this.nl) - +(this.nm);
        }
        mh() {
            return this.na;
        }
        io(a, b) {
            super.io(a, b);
            this.n5();
        }
        mo(a, b) {
            let c = this.categoryMode == 0 ? 0 : 0.5;
            return super.mo(a + c, b);
        }
        mn(a, b, c, d) {
            return b;
        }
        n0() {
            if (this.na == null || this.na.count < 2) {
                return (0);
            }
            return TimeXAxis.or((a) => this.na.item(a), this.na.count);
        }
        itemLabelFormat() {
            return TimeXAxis.on(this.n1);
        }
        get minimumValue() {
            return this.ng;
        }
        set minimumValue(a) {
            let b = this.minimumValue;
            if (+a != +b) {
                this.ng = a;
                this.hv(this, "MinimumValue", b, this.minimumValue);
            }
        }
        get maximumValue() {
            return this.nf;
        }
        set maximumValue(a) {
            let b = this.maximumValue;
            if (+a != +b) {
                this.nf = a;
                this.hv(this, "MaximumValue", b, this.maximumValue);
            }
        }
        c2() {
            if (+(this.minimumValue) == +(dateMaxValue()) || +(this.maximumValue) == +(dateMinValue())) {
                return super.c2();
            }
            if (this.cp && this.c3) {
                this.bb = this.ba();
            }
            if (this.kq == null || this.na == null) {
                return false;
            }
            let a = this.mv;
            if (+(this.minimumValue) != +(dateMinValue())) {
                let b = -1;
                while (b++ <= this.na.count) {
                    if (this.na.item(b) >= this.minimumValue) {
                        break;
                    }
                }
                a = b;
            }
            let c = this.categoryMode == 0 ? this.kq.count - 1 : this.kq.count;
            if (+(this.maximumValue) != +(dateMaxValue())) {
                let d = this.kq.count;
                while (d-- > 0) {
                    if (this.na.item(d) <= this.maximumValue) {
                        break;
                    }
                }
                c = d;
            }
            if (a != this.mv || c != this.mu) {
                let e = new AxisRangeChangedEventArgs(0, this.mv, a, this.mu, c);
                this.mv = a;
                this.mu = c;
                this.h2(e);
                this.n4();
                this.ls();
                return true;
            }
            return false;
        }
        l1(a) {
            super.l1(a);
            this.c1(true);
        }
        get isVertical() {
            return this.co;
        }
        j$c(a, b) {
            return this.el.apply(this, arguments);
        }
        j$b(a, b) {
            return this.ei.apply(this, arguments);
        }
        j$d(a, b, c, d, e) {
            this.hl.apply(this, arguments);
        }
        j$e(a, b, c, d) {
            this.hm.apply(this, arguments);
        }
        j$f(a, b, c, d) {
            this.hn.apply(this, arguments);
        }
        get j$a() {
            return this.cg;
        }
    }
    OrdinalTimeXAxis.$t = markType(OrdinalTimeXAxis, 'OrdinalTimeXAxis', CategoryXAxis.$, [ITimeRangeSelectorAxis_$type, IOrdinalAxis_$type]);
    return OrdinalTimeXAxis;
})();
