/*
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/ES2015/FastItemDateTimeColumn";
import { CategoryXAxis } from "./CategoryXAxis";
import { ITimeRangeSelectorAxis_$type } from "./ITimeRangeSelectorAxis";
import { TimeAxisLabelFormatCollection } from "./TimeAxisLabelFormatCollection";
import { Rect } from "igniteui-react-core/ES2015/Rect";
import { ScalerParams } from "./ScalerParams";
import { EventArgs, runOn, delegateCombine, typeCast, Date_$type, markType } from "igniteui-react-core/ES2015/type";
import { IFastItemsSource_$type } from "igniteui-react-core/ES2015/IFastItemsSource";
import { TimeAxisBase } from "./TimeAxisBase";
import { TimeXAxis } from "./TimeXAxis";
import { AxisRangeChangedEventArgs } from "./AxisRangeChangedEventArgs";
import { truncate } from "igniteui-react-core/ES2015/number";
import { dateMaxValue, dateMinValue } from "igniteui-react-core/ES2015/date";
/**
 * @hidden
 */
export class OrdinalTimeXAxis extends CategoryXAxis {
    constructor() {
        super();
        this.li = dateMaxValue();
        this.lh = dateMinValue();
        this.lm = dateMaxValue();
        this.ll = dateMinValue();
        this.lr = null;
        this.lf = null;
        this.ld = null;
        this.ly = 0;
        this.lk = dateMinValue();
        this.lj = dateMaxValue();
        this.visibleRangeChanged = null;
        FastItemDateTimeColumn.m();
        this.ld = new TimeAxisLabelFormatCollection();
        let a = this.le;
        a.collectionChanged = delegateCombine(a.collectionChanged, runOn(this, this.l0));
        this.k4 = 0;
    }
    dv(a, b, c) {
        return this.dt(a, new ScalerParams(0, b, c, this.b8, this.ip()));
    }
    du(a, b, c) {
        return this.dt(a, b);
    }
    dt(a, b) {
        let c = this.k3;
        let d = this.k4;
        let e = this.k3 - this.k4;
        let f = e >= 1 ? (a - d) / (c - d) : e == 0 ? 0.5 : NaN;
        if (this.b9) {
            f = 1 - f;
        }
        let g = b.d;
        let h = b.e;
        return g.left + g.width * (f - h.left) / h.width;
    }
    gg(a, b, c, d, e) {
        let f = this.kp(a);
        let g = f != null;
        for (let h = 0; h < b.count; h++) {
            for (let i = c; i < d; i++) {
                let j = g ? f[i] : a.item(i);
                j[h] = this.dt(j[h], e);
            }
        }
    }
    gh(a, b, c, d) {
        for (let e = b; e < c; e++) {
            a.item(e, this.dt(a.item(e), d));
        }
    }
    dy(a, b, c) {
        return this.dw(a, new ScalerParams(0, b, this.iu, this.b8, this.ip()));
    }
    getUnscaledValue3(a, b, c, d) {
        return this.dw(a, new ScalerParams(0, b, this.iu, this.b8, this.ip()));
    }
    dw(a, b) {
        let c = b.e;
        let d = b.d;
        let e = c.left + (a - d.left) * c.width / d.width;
        if (this.b9) {
            e = 1 - e;
        }
        return this.k4 + e * (this.k3 - this.k4);
    }
    dx(a, b, c) {
        return this.dw(a, b);
    }
    gi(a, b, c, d) {
        for (let e = b; e < c; e++) {
            a.item(e, this.dw(a.item(e), d));
        }
    }
    go(a, b, c, d) {
        super.go(a, b, c, d);
        switch (b) {
            case "FastItemsSource":
                let e = typeCast(IFastItemsSource_$type, c);
                if (e != null) {
                    e.deregisterColumn(this.lg);
                    this.lg = null;
                }
                if (this.i8 != null) {
                    this.lg = TimeAxisBase.ks(this.dateTimeMemberPath, this);
                }
                break;
            case "DateTimeMemberPath":
                if (this.i8 != null) {
                    this.i8.deregisterColumn(this.lg);
                    this.lg = TimeAxisBase.ks(this.dateTimeMemberPath, this);
                }
                break;
            case "DateTimeColumn":
                this.ck();
                this.ly = this.lx();
                break;
            case "MinimumValue":
            case "MaximumValue":
                this.ck();
                this.gz();
                break;
        }
    }
    get actualMinimumValue() {
        return this.li;
    }
    set actualMinimumValue(a) {
        this.li = a;
    }
    get actualMaximumValue() {
        return this.lh;
    }
    set actualMaximumValue(a) {
        this.lh = a;
    }
    get visibleMinimum() {
        return this.lm;
    }
    set visibleMinimum(a) {
        this.lm = a;
    }
    get visibleMaximum() {
        return this.ll;
    }
    set visibleMaximum(a) {
        this.ll = a;
    }
    l1() {
        if (this.lg == null || this.lg.count == 0 || this.lg.count < this.k4 - 1) {
            this.actualMinimumValue = dateMinValue();
            this.actualMaximumValue = dateMaxValue();
            return;
        }
        let a = Math.max(0, Math.min(this.k4, this.lg.count - 1));
        let b = Math.max(0, Math.min(this.k3, this.lg.count - 1));
        let c = +(this.actualMinimumValue) != +(this.lg.item(a)) || +(this.actualMaximumValue) != +(this.lg.item(b));
        if (c) {
            let d = this.actualMinimumValue;
            let e = this.actualMaximumValue;
            this.actualMinimumValue = this.lg.item(a);
            this.actualMaximumValue = this.lg.item(b);
            this.l2();
        }
    }
    l2() {
        if (this.lg == null || this.lg.count == 0 || this.bf == null) {
            let a = +(this.visibleMinimum) != +(this.actualMinimumValue) || +(this.visibleMaximum) != +(this.actualMaximumValue);
            this.visibleMinimum = this.actualMinimumValue;
            this.visibleMaximum = this.actualMaximumValue;
            if (a && this.visibleRangeChanged != null) {
                this.visibleRangeChanged(this, EventArgs.empty);
            }
            return;
        }
        let b = new ScalerParams(1, this.bf.m4, this.bf.m3, this.b8);
        let c = truncate(Math.ceil(this.dw(this.bf.m3.left, b)));
        let d = truncate(Math.ceil(this.dw(this.bf.m3.right, b)));
        let e = c >= 0 && c < this.lg.count ? this.lg.item(c) : this.actualMinimumValue;
        let f = d >= 0 && d < this.lg.count ? this.lg.item(d) : this.actualMaximumValue;
        let g = +(this.visibleMinimum) != +e || +(this.visibleMaximum) != +f;
        this.visibleMinimum = e;
        this.visibleMaximum = f;
        if (g && this.visibleRangeChanged != null) {
            this.visibleRangeChanged(this, EventArgs.empty);
        }
    }
    get dateTimeMemberPath() {
        return this.lr;
    }
    set dateTimeMemberPath(a) {
        let b = this.dateTimeMemberPath;
        if (a != b) {
            this.lr = a;
            this.gu("DateTimeMemberPath", b, this.dateTimeMemberPath);
        }
    }
    get lg() {
        return this.lf;
    }
    set lg(a) {
        if (this.lf != a) {
            let b = this.lf;
            this.lf = a;
            this.gu("DateTimeColumn", b, this.lf);
        }
    }
    hc(a, b) {
        super.hc(a, b);
        this.l2();
    }
    getWindowRectForSelection(a, b) {
        if (this.lg == null) {
            return Rect.empty;
        }
        let c = a > b;
        let d = c ? b : a;
        let e = c ? a : b;
        let f = -1;
        while (++f < this.lg.count && this.lg.item(f) < d) {
            ;
        }
        let g = this.lg.count;
        while (--g >= 0 && this.lg.item(g) > e) {
            ;
        }
        let h = new ScalerParams(0, new Rect(0, 0, 0, 1, 1), new Rect(0, 0, 0, 1, 1), this.b8, this.bf.mu);
        let i = this.dt(f, h);
        let j = this.dt(g + 1, h);
        return new Rect(0, i, NaN, Math.max(0, j - i), NaN);
    }
    get le() {
        return this.ld;
    }
    l0(a, b) {
        this.g0(false);
    }
    gq(a) {
        super.gq(a);
        this.dateTimeMemberPath = a;
    }
    e1(a) {
        if (this.ar != null) {
            return this.ar(a);
        }
        if (!(typeCast(Date_$type, a) !== null)) {
            return super.e1(a);
        }
        let b = TimeXAxis.mg(a, this.lz, this.le, this.eo);
        return b == null ? super.e1(a) : b;
    }
    fe(a) {
        let b = 0;
        if (this.categoryMode == 0) {
            b = truncate(Math.round(a));
        }
        else {
            b = truncate(Math.floor(a));
        }
        if (b < 0 || b > this.js - 1) {
            return super.fe(a);
        }
        let c = this.lg.item(b);
        let d = this.e1(c);
        if (d == null) {
            return null;
        }
        return d.toString();
    }
    get lz() {
        return +(this.visibleMaximum) - +(this.visibleMinimum);
    }
    kr() {
        return this.lg;
    }
    hb(a, b) {
        super.hb(a, b);
        this.l2();
    }
    kx(a, b) {
        let c = this.categoryMode == 0 ? 0 : 0.5;
        return super.kx(a + c, b);
    }
    kw(a, b, c, d) {
        return b;
    }
    lx() {
        if (this.lg == null || this.lg.count < 2) {
            return (0);
        }
        return TimeXAxis.mo((a) => this.lg.item(a), this.lg.count);
    }
    itemLabelFormat() {
        return TimeXAxis.ml(this.ly);
    }
    get minimumValue() {
        return this.lk;
    }
    set minimumValue(a) {
        let b = this.minimumValue;
        if (+a != +b) {
            this.lk = a;
            this.go(this, "MinimumValue", b, this.minimumValue);
        }
    }
    get maximumValue() {
        return this.lj;
    }
    set maximumValue(a) {
        let b = this.maximumValue;
        if (+a != +b) {
            this.lj = a;
            this.go(this, "MaximumValue", b, this.maximumValue);
        }
    }
    cm() {
        if (+(this.minimumValue) == +(dateMaxValue()) || +(this.maximumValue) == +(dateMinValue())) {
            return super.cm();
        }
        if (this.cf && this.cn) {
            this.a9 = this.a8();
        }
        if (this.i8 == null || this.lg == null) {
            return false;
        }
        let a = this.k4;
        if (+(this.minimumValue) != +(dateMinValue())) {
            let b = -1;
            while (b++ <= this.lg.count) {
                if (this.lg.item(b) >= this.minimumValue) {
                    break;
                }
            }
            a = b;
        }
        let c = this.categoryMode == 0 ? this.i8.count - 1 : this.i8.count;
        if (+(this.maximumValue) != +(dateMaxValue())) {
            let d = this.i8.count;
            while (d-- > 0) {
                if (this.lg.item(d) <= this.maximumValue) {
                    break;
                }
            }
            c = d;
        }
        if (a != this.k4 || c != this.k3) {
            let e = new AxisRangeChangedEventArgs(0, this.k4, a, this.k3, c);
            this.k4 = a;
            this.k3 = c;
            this.gv(e);
            this.l1();
            this.j4();
            return true;
        }
        return false;
    }
    kd(a) {
        super.kd(a);
        this.cl(true);
    }
    get isVertical() {
        return this.ce;
    }
    j$c(a, b) {
        return this.dw.apply(this, arguments);
    }
    j$b(a, b) {
        return this.dt.apply(this, arguments);
    }
    j$d(a, b, c, d, e) {
        this.gg.apply(this, arguments);
    }
    j$e(a, b, c, d) {
        this.gh.apply(this, arguments);
    }
    j$f(a, b, c, d) {
        this.gi.apply(this, arguments);
    }
    get j$a() {
        return this.b8;
    }
}
OrdinalTimeXAxis.$t = markType(OrdinalTimeXAxis, 'OrdinalTimeXAxis', CategoryXAxis.$, [ITimeRangeSelectorAxis_$type]);
