/*
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 {
        el(a, b, c) {
            return this.ej(a, new ScalerParams(0, b, c, this.ch, this.j8()));
        }
        ek(a, b, c) {
            return this.ej(a, b);
        }
        ej(a, b) {
            if (!b.c.isEmpty) {
                let c = this.n2(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.n2(a, b.e, b.d);
        }
        n2(a, b, c) {
            let d = this.mv;
            let e = this.mw;
            let f = this.mv - this.mw;
            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;
        }
        hm(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.mv;
            let u = this.mw;
            let v = this.mv - this.mw;
            let w = this.mg(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;
                    }
                }
            }
        }
        hn(a, b, c, d) {
            for (let e = b; e < c; e++) {
                a.item(e, this.ej(a.item(e), d));
            }
        }
        eo(a, b, c) {
            return this.em(a, new ScalerParams(0, b, this.kd, this.ch, this.j8()));
        }
        getUnscaledValue3(a, b, c, d) {
            return this.em(a, new ScalerParams(0, b, this.kd, this.ch, this.j8()));
        }
        em(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.n3(c, NumericScaler.ac, b.c);
                return c;
            }
            return this.n3(a, b.e, b.d);
        }
        n3(a, b, c) {
            let d = b.left + (a - c.left) * b.width / c.width;
            if (this.ci) {
                d = 1 - d;
            }
            return this.mw + d * (this.mv - this.mw);
        }
        en(a, b, c) {
            return this.em(a, b);
        }
        ho(a, b, c, d) {
            for (let e = b; e < c; e++) {
                a.item(e, this.em(a.item(e), d));
            }
        }
        constructor() {
            super();
            this.np = dateMaxValue();
            this.no = dateMinValue();
            this.nv = dateMaxValue();
            this.nu = dateMinValue();
            this.nr = dateMaxValue();
            this.nq = dateMinValue();
            this.n5 = null;
            this.n4 = null;
            this.nk = 0;
            this.nm = null;
            this.ni = null;
            this.oe = 0;
            this.nt = dateMinValue();
            this.ns = dateMaxValue();
            this.visibleRangeChanged = null;
            FastItemDateTimeColumn.o();
            this.ni = new TimeAxisLabelFormatCollection();
            let a = this.nj;
            a.collectionChanged = delegateCombine(a.collectionChanged, runOn(this, this.og));
            this.mw = 0;
        }
        hw(a, b, c, d) {
            super.hw(a, b, c, d);
            switch (b) {
                case "FastItemsSource":
                    let e = typeCast(IFastItemsSource_$type, c);
                    if (e != null) {
                        e.deregisterColumn(this.nn);
                        this.nn = null;
                    }
                    if (this.kr != null) {
                        this.nn = TimeAxisBase.mj(this.dateTimeMemberPath, this);
                    }
                    break;
                case "DateTimeMemberPath":
                    if (this.kr != null) {
                        this.kr.deregisterColumn(this.nn);
                        this.nn = TimeAxisBase.mj(this.dateTimeMemberPath, this);
                    }
                    break;
                case "DateTimeColumn":
                    this.c1();
                    this.oe = this.od();
                    break;
                case "MinimumValue":
                case "MaximumValue":
                    this.c1();
                    this.h7();
                    break;
                case "LabellingMode":
                    this.h7();
                    break;
            }
        }
        get actualMinimumValue() {
            return this.np;
        }
        set actualMinimumValue(a) {
            this.np = a;
        }
        get actualMaximumValue() {
            return this.no;
        }
        set actualMaximumValue(a) {
            this.no = a;
        }
        get visibleMinimum() {
            return this.nv;
        }
        set visibleMinimum(a) {
            this.nv = a;
        }
        get visibleMaximum() {
            return this.nu;
        }
        set visibleMaximum(a) {
            this.nu = a;
        }
        get nz() {
            return this.nr;
        }
        set nz(a) {
            this.nr = a;
        }
        get ny() {
            return this.nq;
        }
        set ny(a) {
            this.nq = a;
        }
        oh() {
            if (this.nn == null || this.nn.count == 0 || this.nn.count < this.mw - 1) {
                this.actualMinimumValue = dateMinValue();
                this.actualMaximumValue = dateMaxValue();
                return;
            }
            let a = Math.max(0, Math.min(this.mw, this.nn.count - 1));
            let b = Math.max(0, Math.min(this.mv, this.nn.count - 1));
            let c = +(this.actualMinimumValue) != +(this.nn.item(a)) || +(this.actualMaximumValue) != +(this.nn.item(b));
            if (c) {
                let d = this.actualMinimumValue;
                let e = this.actualMaximumValue;
                this.actualMinimumValue = this.nn.item(a);
                this.actualMaximumValue = this.nn.item(b);
                this.oi();
            }
        }
        oi() {
            if (this.nn == null || this.nn.count == 0 || this.bi == null) {
                let a = +(this.visibleMinimum) != +(this.actualMinimumValue) || +(this.visibleMaximum) != +(this.actualMaximumValue);
                this.visibleMinimum = this.actualMinimumValue;
                this.visibleMaximum = this.actualMaximumValue;
                this.nz = this.visibleMinimum;
                this.ny = this.visibleMaximum;
                if (a && this.visibleRangeChanged != null) {
                    this.visibleRangeChanged(this, EventArgs.empty);
                }
                return;
            }
            let b = this.j7();
            let c = new ScalerParams(1, this.bi.t7, this.bi.t6, this.ch);
            c.c = this.j8();
            let d = this.em(b.left, c);
            let e = this.em(b.right, c);
            let f = truncate(Math.floor(d));
            let g = truncate(Math.ceil(e));
            let h = f >= 0 && f < this.nn.count ? this.nn.item(f) : this.actualMinimumValue;
            let i = g >= 0 && g < this.nn.count ? this.nn.item(g) : this.actualMaximumValue;
            let j = h;
            let k = i;
            if (d > f) {
                let l = f + 1;
                let m = l >= 0 && l < this.nn.count ? this.nn.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.nn.count ? this.nn.item(o) : h;
                let q = dateSubtract(+i, +(timeSpanFromMilliseconds(timeSpanTotalMilliseconds((+i - +p)) * (g - e))));
                k = q;
            }
            this.nz = j;
            this.ny = 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);
            }
        }
        h9(a) {
            let b = TimeXAxis.om(this.actualMinimumValue, false, this.actualMinimumValue, this.nl, this.oc, this.nj, this.fk);
            if (b != this.n5 && this.c4) {
                this.id();
            }
            this.n5 = b;
            super.h9(a);
        }
        get dateTimeMemberPath() {
            return this.n4;
        }
        set dateTimeMemberPath(a) {
            let b = this.dateTimeMemberPath;
            if (a != b) {
                this.n4 = a;
                this.h2("DateTimeMemberPath", b, this.dateTimeMemberPath);
            }
        }
        get nl() {
            return this.nk;
        }
        set nl(a) {
            let b = this.nl;
            if (a != b) {
                this.nk = a;
                this.h2("LabellingMode", enumGetBox(TimeAxisLabellingMode_$type, b), enumGetBox(TimeAxisLabellingMode_$type, this.nl));
            }
        }
        get_ck() {
            return true;
        }
        get nn() {
            return this.nm;
        }
        set nn(a) {
            if (this.nm != a) {
                let b = this.nm;
                this.nm = a;
                this.h2("DateTimeColumn", b, this.nm);
            }
        }
        iq(a, b) {
            super.iq(a, b);
            this.oi();
        }
        getWindowRectForSelection(a, b) {
            if (this.nn == null) {
                return Rect.empty;
            }
            let c = a > b;
            let d = c ? b : a;
            let e = c ? a : b;
            let f = -1;
            while (++f < this.nn.count && this.nn.item(f) < d) {
            }
            let g = this.nn.count;
            while (--g >= 0 && this.nn.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.tv);
            let i = this.ej(f, h);
            let j = this.ej(g + 1, h);
            return new Rect(0, i, NaN, Math.max(0, j - i), NaN);
        }
        get nj() {
            return this.ni;
        }
        og(a, b) {
            this.h8(false);
        }
        hy(a) {
            super.hy(a);
            this.dateTimeMemberPath = a;
        }
        fz(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.fz(a);
            }
            let b = dateMinValue();
            let c = false;
            if (this.mx != null) {
                if (typeCast(Date_$type, this.mx) !== null) {
                    b = this.mx;
                    c = true;
                }
            }
            let d = TimeXAxis.oh(a, c, b, this.nl, this.oc, this.nj, this.fk);
            return d == null ? super.fz(a) : d;
        }
        gd(a) {
            let b = 0;
            if (this.categoryMode == 0) {
                b = truncate(Math.round(a));
            }
            else {
                b = truncate(Math.floor(a));
            }
            if (b < 0 || b > this.lg - 1) {
                return super.gd(a);
            }
            let c = this.nn.item(b);
            let d = this.fz(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.lg - 1) {
                b = this.lg - 1;
            }
            if (this.nn == null) {
                return dateMinValue();
            }
            let c = this.nn.item(b);
            return c;
        }
        get of() {
            return +(this.visibleMaximum) - +(this.visibleMinimum);
        }
        get oc() {
            return +(this.ny) - +(this.nz);
        }
        mi() {
            return this.nn;
        }
        ip(a, b) {
            super.ip(a, b);
            this.oi();
        }
        mp(a, b) {
            let c = this.categoryMode == 0 ? 0 : 0.5;
            return super.mp(a + c, b);
        }
        mo(a, b, c, d) {
            return b;
        }
        od() {
            if (this.nn == null || this.nn.count < 2) {
                return (0);
            }
            return TimeXAxis.os((a) => this.nn.item(a), this.nn.count);
        }
        itemLabelFormat() {
            return TimeXAxis.oo(this.oe);
        }
        get minimumValue() {
            return this.nt;
        }
        set minimumValue(a) {
            let b = this.minimumValue;
            if (+a != +b) {
                this.nt = a;
                this.hw(this, "MinimumValue", b, this.minimumValue);
            }
        }
        get maximumValue() {
            return this.ns;
        }
        set maximumValue(a) {
            let b = this.maximumValue;
            if (+a != +b) {
                this.ns = a;
                this.hw(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.kr == null || this.nn == null) {
                return false;
            }
            let a = this.mw;
            if (+(this.minimumValue) != +(dateMinValue())) {
                let b = -1;
                while (b++ <= this.nn.count) {
                    if (this.nn.item(b) >= this.minimumValue) {
                        break;
                    }
                }
                a = b;
            }
            let c = this.categoryMode == 0 ? this.kr.count - 1 : this.kr.count;
            if (+(this.maximumValue) != +(dateMaxValue())) {
                let d = this.kr.count;
                while (d-- > 0) {
                    if (this.nn.item(d) <= this.maximumValue) {
                        break;
                    }
                }
                c = d;
            }
            if (a != this.mw || c != this.mv) {
                let e = new AxisRangeChangedEventArgs(0, this.mw, a, this.mv, c);
                this.mw = a;
                this.mv = c;
                this.h3(e);
                this.oh();
                this.lt();
                return true;
            }
            return false;
        }
        l2(a) {
            super.l2(a);
            this.c2(true);
        }
        get isVertical() {
            return this.cp;
        }
        j$d(a, b) {
            return this.em.apply(this, arguments);
        }
        j$c(a, b) {
            return this.ej.apply(this, arguments);
        }
        j$e(a, b, c, d, e) {
            this.hm.apply(this, arguments);
        }
        j$f(a, b, c, d) {
            this.hn.apply(this, arguments);
        }
        j$g(a, b, c, d) {
            this.ho.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;
})();
