/*
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-react-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-react-core";
import { ScalerParams } from "./ScalerParams";
import { NumericScaler } from "./NumericScaler";
import { EventArgs, runOn, delegateCombine, typeCast, enumGetBox, Date_$type, markType } from "igniteui-react-core";
import { IFastItemsSource_$type } from "igniteui-react-core";
import { TimeAxisBase } from "./TimeAxisBase";
import { TimeXAxis } from "./TimeXAxis";
import { AxisRangeChangedEventArgs } from "./AxisRangeChangedEventArgs";
import { truncate } from "igniteui-react-core";
import { dateMaxValue, dateMinValue, dateAdd, dateSubtract } from "igniteui-react-core";
import { timeSpanFromMilliseconds, timeSpanTotalMilliseconds } from "igniteui-react-core";
/**
 * @hidden
 */
export let OrdinalTimeXAxis = /*@__PURE__*/ (() => {
    class OrdinalTimeXAxis extends CategoryXAxis {
        em(a, b, c) {
            return this.ek(a, new ScalerParams(0, b, c, this.ch, this.j9()));
        }
        el(a, b, c) {
            return this.ek(a, b);
        }
        ek(a, b) {
            if (!b.c.isEmpty) {
                let c = this.n4(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.n4(a, b.e, b.d);
        }
        n4(a, b, c) {
            let d = this.mx;
            let e = this.my;
            let f = this.mx - this.my;
            let g = f >= 1 ? (a - e) / (d - e) : f == 0 ? 0.5 : NaN;
            if (this.ci) {
                g = 1 - g;
            }
            return c.left + c.width * (g - b.left) / b.width;
        }
        hn(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.mx;
            let u = this.my;
            let v = this.mx - this.my;
            let w = this.mi(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;
                    }
                }
            }
        }
        ho(a, b, c, d) {
            for (let e = b; e < c; e++) {
                a.item(e, this.ek(a.item(e), d));
            }
        }
        ep(a, b, c) {
            return this.en(a, new ScalerParams(0, b, this.ke, this.ch, this.j9()));
        }
        getUnscaledValue3(a, b, c, d) {
            return this.en(a, new ScalerParams(0, b, this.ke, this.ch, this.j9()));
        }
        en(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.n5(c, NumericScaler.ac, b.c);
                return c;
            }
            return this.n5(a, b.e, b.d);
        }
        n5(a, b, c) {
            let d = b.left + (a - c.left) * b.width / c.width;
            if (this.ci) {
                d = 1 - d;
            }
            return this.my + d * (this.mx - this.my);
        }
        eo(a, b, c) {
            return this.en(a, b);
        }
        hp(a, b, c, d) {
            for (let e = b; e < c; e++) {
                a.item(e, this.en(a.item(e), d));
            }
        }
        constructor() {
            super();
            this.nr = dateMaxValue();
            this.nq = dateMinValue();
            this.nx = dateMaxValue();
            this.nw = dateMinValue();
            this.nt = dateMaxValue();
            this.ns = dateMinValue();
            this.n7 = null;
            this.n6 = null;
            this.nm = 0;
            this.no = null;
            this.nk = null;
            this.og = 0;
            this.nv = dateMinValue();
            this.nu = dateMaxValue();
            this.visibleRangeChanged = null;
            FastItemDateTimeColumn.o();
            this.nk = new TimeAxisLabelFormatCollection();
            let a = this.nl;
            a.collectionChanged = delegateCombine(a.collectionChanged, runOn(this, this.oi));
            this.my = 0;
        }
        hx(a, b, c, d) {
            super.hx(a, b, c, d);
            switch (b) {
                case "FastItemsSource":
                    let e = typeCast(IFastItemsSource_$type, c);
                    if (e != null) {
                        e.deregisterColumn(this.np);
                        this.np = null;
                    }
                    if (this.ks != null) {
                        this.np = TimeAxisBase.ml(this.dateTimeMemberPath, this);
                    }
                    break;
                case "DateTimeMemberPath":
                    if (this.ks != null) {
                        this.ks.deregisterColumn(this.np);
                        this.np = TimeAxisBase.ml(this.dateTimeMemberPath, this);
                    }
                    break;
                case "DateTimeColumn":
                    this.c1();
                    this.og = this.of();
                    break;
                case "MinimumValue":
                case "MaximumValue":
                    this.c1();
                    this.h8();
                    break;
                case "LabellingMode":
                    this.h8();
                    break;
            }
        }
        get actualMinimumValue() {
            return this.nr;
        }
        set actualMinimumValue(a) {
            this.nr = a;
        }
        get actualMaximumValue() {
            return this.nq;
        }
        set actualMaximumValue(a) {
            this.nq = a;
        }
        get visibleMinimum() {
            return this.nx;
        }
        set visibleMinimum(a) {
            this.nx = a;
        }
        get visibleMaximum() {
            return this.nw;
        }
        set visibleMaximum(a) {
            this.nw = a;
        }
        get n1() {
            return this.nt;
        }
        set n1(a) {
            this.nt = a;
        }
        get n0() {
            return this.ns;
        }
        set n0(a) {
            this.ns = a;
        }
        oj() {
            if (this.np == null || this.np.count == 0 || this.np.count < this.my - 1) {
                this.actualMinimumValue = dateMinValue();
                this.actualMaximumValue = dateMaxValue();
                return;
            }
            let a = Math.max(0, Math.min(this.my, this.np.count - 1));
            let b = Math.max(0, Math.min(this.mx, this.np.count - 1));
            let c = +(this.actualMinimumValue) != +(this.np.item(a)) || +(this.actualMaximumValue) != +(this.np.item(b));
            if (c) {
                let d = this.actualMinimumValue;
                let e = this.actualMaximumValue;
                this.actualMinimumValue = this.np.item(a);
                this.actualMaximumValue = this.np.item(b);
                this.ok();
            }
        }
        ok() {
            if (this.np == null || this.np.count == 0 || this.bi == null) {
                let a = +(this.visibleMinimum) != +(this.actualMinimumValue) || +(this.visibleMaximum) != +(this.actualMaximumValue);
                this.visibleMinimum = this.actualMinimumValue;
                this.visibleMaximum = this.actualMaximumValue;
                this.n1 = this.visibleMinimum;
                this.n0 = this.visibleMaximum;
                if (a && this.visibleRangeChanged != null) {
                    this.visibleRangeChanged(this, EventArgs.empty);
                }
                return;
            }
            let b = this.j8();
            let c = new ScalerParams(1, this.bi.v1, this.bi.v0, this.ch);
            c.c = this.j9();
            let d = this.en(b.left, c);
            let e = this.en(b.right, c);
            let f = truncate(Math.floor(d));
            let g = truncate(Math.ceil(e));
            let h = f >= 0 && f < this.np.count ? this.np.item(f) : this.actualMinimumValue;
            let i = g >= 0 && g < this.np.count ? this.np.item(g) : this.actualMaximumValue;
            let j = h;
            let k = i;
            if (d > f) {
                let l = f + 1;
                let m = l >= 0 && l < this.np.count ? this.np.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.np.count ? this.np.item(o) : h;
                let q = dateSubtract(+i, +(timeSpanFromMilliseconds(timeSpanTotalMilliseconds((+i - +p)) * (g - e))));
                k = q;
            }
            this.n1 = j;
            this.n0 = 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);
            }
        }
        ia(a) {
            let b = TimeXAxis.oq(this.actualMinimumValue, false, this.actualMinimumValue, this.nn, this.oe, this.nl, this.fl);
            if (b != this.n7 && this.c4) {
                this.ie();
            }
            this.n7 = b;
            super.ia(a);
        }
        get dateTimeMemberPath() {
            return this.n6;
        }
        set dateTimeMemberPath(a) {
            let b = this.dateTimeMemberPath;
            if (a != b) {
                this.n6 = a;
                this.h3("DateTimeMemberPath", b, this.dateTimeMemberPath);
            }
        }
        get nn() {
            return this.nm;
        }
        set nn(a) {
            let b = this.nn;
            if (a != b) {
                this.nm = a;
                this.h3("LabellingMode", enumGetBox(TimeAxisLabellingMode_$type, b), enumGetBox(TimeAxisLabellingMode_$type, this.nn));
            }
        }
        get_ck() {
            return true;
        }
        get np() {
            return this.no;
        }
        set np(a) {
            if (this.no != a) {
                let b = this.no;
                this.no = a;
                this.h3("DateTimeColumn", b, this.no);
            }
        }
        ir(a, b) {
            super.ir(a, b);
            this.ok();
        }
        getWindowRectForSelection(a, b) {
            if (this.np == null) {
                return Rect.empty;
            }
            let c = a > b;
            let d = c ? b : a;
            let e = c ? a : b;
            let f = -1;
            while (++f < this.np.count && this.np.item(f) < d) {
            }
            let g = this.np.count;
            while (--g >= 0 && this.np.item(g) > e) {
            }
            let h = new ScalerParams(0, new Rect(0, 0, 0, 1, 1), new Rect(0, 0, 0, 1, 1), this.ch, this.bi.vp);
            let i = this.ek(f, h);
            let j = this.ek(g + 1, h);
            return new Rect(0, i, NaN, Math.max(0, j - i), NaN);
        }
        get nl() {
            return this.nk;
        }
        oi(a, b) {
            this.h9(false);
        }
        hz(a) {
            super.hz(a);
            this.dateTimeMemberPath = a;
        }
        f0(a) {
            if (this.au != null) {
                return this.au(a);
            }
            if (this.ai != null) {
                return this.ai.i(a);
            }
            if (!(typeCast(Date_$type, a) !== null)) {
                return super.f0(a);
            }
            let b = dateMinValue();
            let c = false;
            if (this.mz != null) {
                if (typeCast(Date_$type, this.mz) !== null) {
                    b = this.mz;
                    c = true;
                }
            }
            let d = TimeXAxis.ol(a, c, b, this.nn, this.oe, this.nl, this.fl);
            return d == null ? super.f0(a) : d;
        }
        ge(a) {
            let b = 0;
            if (this.categoryMode == 0) {
                b = truncate(Math.round(a));
            }
            else {
                b = truncate(Math.floor(a));
            }
            if (b < 0 || b > this.lh - 1) {
                return super.ge(a);
            }
            let c = this.np.item(b);
            let d = this.f0(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.lh - 1) {
                b = this.lh - 1;
            }
            if (this.np == null) {
                return dateMinValue();
            }
            let c = this.np.item(b);
            return c;
        }
        get oh() {
            return +(this.visibleMaximum) - +(this.visibleMinimum);
        }
        get oe() {
            return +(this.n0) - +(this.n1);
        }
        mk() {
            return this.np;
        }
        iq(a, b) {
            super.iq(a, b);
            this.ok();
        }
        mr(a, b) {
            let c = this.categoryMode == 0 ? 0 : 0.5;
            return super.mr(a + c, b);
        }
        mq(a, b, c, d) {
            return b;
        }
        of() {
            if (this.np == null || this.np.count < 2) {
                return (0);
            }
            return TimeXAxis.ow((a) => this.np.item(a), this.np.count);
        }
        itemLabelFormat() {
            return TimeXAxis.os(this.og);
        }
        get minimumValue() {
            return this.nv;
        }
        set minimumValue(a) {
            let b = this.minimumValue;
            if (+a != +b) {
                this.nv = a;
                this.hx(this, "MinimumValue", b, this.minimumValue);
            }
        }
        get maximumValue() {
            return this.nu;
        }
        set maximumValue(a) {
            let b = this.maximumValue;
            if (+a != +b) {
                this.nu = a;
                this.hx(this, "MaximumValue", b, this.maximumValue);
            }
        }
        c3() {
            if (+(this.minimumValue) == +(dateMaxValue()) || +(this.maximumValue) == +(dateMinValue())) {
                return super.c3();
            }
            if (this.cq && this.c4) {
                this.bc = this.bb();
            }
            if (this.ks == null || this.np == null) {
                return false;
            }
            let a = this.my;
            if (+(this.minimumValue) != +(dateMinValue())) {
                let b = -1;
                while (b++ <= this.np.count) {
                    if (this.np.item(b) >= this.minimumValue) {
                        break;
                    }
                }
                a = b;
            }
            let c = this.categoryMode == 0 ? this.ks.count - 1 : this.ks.count;
            if (+(this.maximumValue) != +(dateMaxValue())) {
                let d = this.ks.count;
                while (d-- > 0) {
                    if (this.np.item(d) <= this.maximumValue) {
                        break;
                    }
                }
                c = d;
            }
            if (a != this.my || c != this.mx) {
                let e = new AxisRangeChangedEventArgs(0, this.my, a, this.mx, c);
                this.my = a;
                this.mx = c;
                this.h4(e);
                this.oj();
                this.lv();
                return true;
            }
            return false;
        }
        l4(a) {
            super.l4(a);
            this.c2(true);
        }
        get isVertical() {
            return this.cp;
        }
        j$d(a, b) {
            return this.en.apply(this, arguments);
        }
        j$c(a, b) {
            return this.ek.apply(this, arguments);
        }
        j$e(a, b, c, d, e) {
            this.hn.apply(this, arguments);
        }
        j$f(a, b, c, d) {
            this.ho.apply(this, arguments);
        }
        j$g(a, b, c, d) {
            this.hp.apply(this, arguments);
        }
        get j$b() {
            return this.ch;
        }
        j$a() {
            return this.ag.apply(this, arguments);
        }
    }
    OrdinalTimeXAxis.$t = /*@__PURE__*/ markType(OrdinalTimeXAxis, 'OrdinalTimeXAxis', CategoryXAxis.$, [ITimeRangeSelectorAxis_$type, IOrdinalAxis_$type]);
    return OrdinalTimeXAxis;
})();
