/*
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 { TimeAxisBase } from "./TimeAxisBase";
import { TimeAxisDisplayType_$type } from "./TimeAxisDisplayType";
import { DependencyProperty } from "igniteui-react-core/ES2015/DependencyProperty";
import { Type, Base, enumGetBox, Number_$type, typeGetValue, typeCast, markType } from "igniteui-react-core/ES2015/type";
import { Rect } from "igniteui-react-core/ES2015/Rect";
import { ScalerParams } from "./ScalerParams";
import { NumericYAxis } from "./NumericYAxis";
import { LinearNumericSnapper } from "./LinearNumericSnapper";
import { LabelPosition } from "./LabelPosition";
import { PropertyMetadata } from "igniteui-react-core/ES2015/PropertyMetadata";
import { truncate, intDivide, isNaN_, isInfinity } from "igniteui-react-core/ES2015/number";
import { dateMinValue, dateFromTicks } from "igniteui-react-core/ES2015/date";
import { timeSpanFromMilliseconds } from "igniteui-react-core/ES2015/timespan";
import { CategoryAxisBaseView } from "./CategoryAxisBaseView";
/**
 * @hidden
 */
export class CategoryDateTimeXAxis extends TimeAxisBase {
    constructor() {
        super();
        this.l2 = 0;
        this.l3 = 0;
        this.ab = CategoryDateTimeXAxis.$;
        this.actualMinimumValue = (new Date());
        this.actualMaximumValue = (new Date());
    }
    get_b4() {
        return this.lx == 0;
    }
    a1() {
        return new CategoryDateTimeXAxisView(this);
    }
    gm(a) {
        super.gm(a);
        this.lw = a;
    }
    get l4() {
        return this.l2;
    }
    set l4(a) {
        if (this.l4 != a) {
            let b = this.l2;
            this.l2 = a;
            this.gu("ActualInterval", b, this.l4);
        }
    }
    d4() {
        return this.l4;
    }
    get l8() {
        return typeGetValue(this.c(CategoryDateTimeXAxis.mj));
    }
    set l8(a) {
        this.h(CategoryDateTimeXAxis.mj, a);
    }
    get l5() {
        return this.l3;
    }
    set l5(a) {
        if (this.l5 != a) {
            let b = this.l3;
            this.l3 = a;
            this.gu("ActualMinorInterval", b, this.l5);
        }
    }
    cq(a, b, c) {
        let d = super.cq(a, b, c);
        if (!d) {
            return false;
        }
        return +(this.actualMinimumValue) != +(this.actualMaximumValue);
    }
    dg() {
        return this.l4;
    }
    g1(a) {
        super.g1(a);
        let b = this.bf != null ? this.bf.mq : Rect.empty;
        let c = !b.isEmpty ? this.iu : Rect.empty;
        let d = this.ip();
        let e = this.io();
        let f = new ScalerParams(0, b, c, this.b8, d);
        if (this.kq == null && !this.kw() && !this.kx()) {
            return;
        }
        if (!b.isEmpty && !c.isEmpty && (this.lx != 1 || this.kq != null)) {
            let g = c.bottom;
            let h = g - c.top;
            if (this.ag != null) {
                let i = typeCast(NumericYAxis.$, this.ag);
                if (i != null) {
                    let j = this.g4(g, h, b, c, d, c.top, c.bottom);
                    g = j.p0;
                    h = j.p1;
                }
            }
            if (isNaN_(g)) {
                g = 0;
            }
            this.gk(this.a2.ax(), g, c, this.a2.e(), true);
            this.at.a8 = h;
            let k = this.a2.a0();
            let l = this.a2.f();
            let m = this.a2.ay();
            let n = this.a2.az();
            let o = this.a2.g();
            if (this.lx == 1) {
                let p = this.i$e(b, c, d, e);
                let q = this.i$g(b, c, d, e);
                if (p < 0 || q < 0) {
                    return;
                }
                let r = NaN;
                for (let s = p; s <= q; s++) {
                    let t = this.kz == null ? s : this.kz._inner[s];
                    let u = this.dt(this.kq.item(t).getTime(), f);
                    if (u == r) {
                        continue;
                    }
                    r = u;
                    if (this.categoryMode == 2) {
                        u += this.b8 ? -this.getGroupCenter(s, b, c, d) : this.getGroupCenter(s, b, c, d);
                    }
                    if (u < e.left || u > e.right) {
                        continue;
                    }
                    this.g9(m, u, c, l, false);
                    this.jf.add(u);
                    if (this.i8 != null && s < this.i8.count) {
                        let v = this.i8.item(t);
                        let w = super.e1(v);
                        if (!isNaN_(u) && !isInfinity(u) && w != null) {
                            if ((typeof w === 'string') && Base.equalsStatic(w, "")) {
                            }
                            else {
                                this.cs.add1(w);
                                this.ct.add(new LabelPosition(u));
                            }
                        }
                    }
                }
            }
            else {
                let x = this.dw(e.left, f);
                let y = this.dw(e.right, f);
                let z = Math.min(x, y);
                let aa = Math.max(x, y);
                let ab = new LinearNumericSnapper(0, z, aa, e.width, this.d6());
                let ac = this.ku ? this.l1() : ab.g;
                this.mf(truncate(ac));
                let ad = truncate(Math.floor((z - this.actualMinimumValue.getTime()) / ac));
                let ae = truncate(Math.ceil((aa - this.actualMinimumValue.getTime()) / ac));
                let af = 0;
                if (this.categoryMode == 2) {
                    af = this.getGroupCenter(0, b, c, d);
                    af = this.b8 ? -af : af;
                }
                let ag = truncate(Math.ceil(e.right));
                let ah = truncate(Math.floor(e.left));
                let ai = this.dt(this.actualMinimumValue.getTime() + ad * ac, f) + af;
                for (let aj = ad; aj <= ae; aj++) {
                    let ak = this.dt(this.actualMinimumValue.getTime() + (aj + 1) * ac, f) + af;
                    if (!isNaN_(ai) && !isInfinity(ai)) {
                        let al = truncate(Math.round(ai));
                        if (al <= ag) {
                            if (aj % 2 == 0) {
                                this.ha(k, ai, ak, c);
                            }
                            this.g9(m, ai, c, l, false);
                            this.jf.add(ai);
                            if (this.ci) {
                                let am = this.l6();
                                if (am > 0 && am < ac) {
                                    if (ac / am < 20) {
                                        let an = ai;
                                        let ao = Math.abs(this.dt(this.actualMinimumValue.getTime() + am, f) - this.dt(this.actualMinimumValue.getTime(), f));
                                        while (an + ao < ak) {
                                            if (Math.abs(an + ao - ak) < 0.0001) {
                                                break;
                                            }
                                            an += ao;
                                            if (an > 0) {
                                                this.g9(n, an, c, o, false);
                                            }
                                        }
                                        this.mg(am);
                                    }
                                }
                                else {
                                    for (let ap = 1; ap < ab.i; ++ap) {
                                        let aq = this.dt(this.actualMinimumValue.getTime() + aj * ac + (ap * ac) / ab.i, f) + af;
                                        this.g9(n, aq, c, o, false);
                                    }
                                    if (ab.i > 0) {
                                        this.mg(truncate((ac / ab.i)));
                                    }
                                }
                            }
                        }
                        if (al >= ah && al <= ag) {
                            let ar = this.actualMinimumValue.getTime() + aj * ac;
                            let ticks_ = truncate(Math.floor(ar));
                            let as = (new Date(ticks_));
                            let at = this.e1(as);
                            if (at != null) {
                                this.cs.add1(at);
                                this.ct.add(new LabelPosition(ai));
                            }
                        }
                    }
                    ai = ak;
                }
            }
            if ((this.aw == null || this.aw.visibility == 0) && this.ag != null) {
                if (this.aw != null && (this.aw.location == 4 || this.aw.location == 5)) {
                    this.bf.ht();
                }
            }
            this.at.a1 = this.cs;
            this.at.a2 = this.ct;
            this.kf();
        }
    }
    l1() {
        return this.l4;
    }
    l6() {
        return this.l8;
    }
    mf(a) {
        this.l4 = a;
    }
    mg(a) {
        this.l5 = a;
    }
    lz() {
        return this.l7 != 0;
    }
    ly() {
        return this.l4 == 0;
    }
    l0(a) {
        return this.kq == null ? dateMinValue() : this.kq.item(a);
    }
    me(a, b) {
        if (a > b || this.i8 == null || this.i8.count == 0) {
            return;
        }
        let c = (a.getTime() - this.actualMinimumValue.getTime()) / (this.actualMaximumValue.getTime() - this.actualMinimumValue.getTime());
        let d = (b.getTime() - this.actualMinimumValue.getTime()) / (this.actualMaximumValue.getTime() - this.actualMinimumValue.getTime());
        this.bf.m4 = new Rect(0, c, this.bf.m4.y, d - c, this.bf.m4.height);
    }
    go(a, b, c, d) {
        super.go(a, b, c, d);
        switch (b) {
            case CategoryDateTimeXAxis.$$p[0]:
                this.cf = true;
                this.at.ak = this.lx == 1;
                this.g0(false);
                break;
            case CategoryDateTimeXAxis.$$p[1]:
                this.cf = true;
                this.g0(false);
                break;
        }
    }
    lm() {
        super.lm();
        if (this.lz()) {
            this.l4 = this.l7;
            let a = this.bf != null ? this.bf.mq : Rect.empty;
            let b = !a.isEmpty ? this.iu : Rect.empty;
            let c = Math.abs(this.actualMaximumValue.getTime() - this.actualMinimumValue.getTime());
            this.ku = this.ly() || (this.lx == 1) || (1 * c / this.l1() > (b.width / a.width)) ? false : true;
        }
        else {
            this.ku = false;
        }
    }
    lk(a, b) {
        let c = b.getTime() - a.getTime();
        let timeOffset_ = truncate(Math.round(intDivide(c, this.jr) / 2));
        return timeOffset_ == 0 ? timeSpanFromMilliseconds(1) : timeSpanFromMilliseconds(timeOffset_);
    }
    get_b3() {
        return 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;
    }
    get i$a() {
        return this.b5;
    }
}
CategoryDateTimeXAxis.$t = markType(CategoryDateTimeXAxis, 'CategoryDateTimeXAxis', TimeAxisBase.$);
CategoryDateTimeXAxis.mj = DependencyProperty.i("MinorInterval", Number_$type, CategoryDateTimeXAxis.$, new PropertyMetadata(2, 0, (a, b) => {
    (typeCast(CategoryDateTimeXAxis.$, a)).gu("MinorInterval", b.oldValue, b.newValue);
    (typeCast(CategoryDateTimeXAxis.$, a)).g0(false);
}));
Type.dep(DependencyProperty, PropertyMetadata, CategoryDateTimeXAxis, 'gu', ['DisplayType:lx:mh', [TimeAxisDisplayType_$type, enumGetBox(TimeAxisDisplayType_$type, 0)], 'Interval:l7:mi', [1, 0]]);
/**
 * @hidden
 */
export class CategoryDateTimeXAxisView extends CategoryAxisBaseView {
    constructor(a) {
        super(a);
        this.bf = null;
        this.bf = a;
    }
    n(a) {
        let b = this.m(dateFromTicks(truncate(a)));
        return b;
    }
}
CategoryDateTimeXAxisView.$t = markType(CategoryDateTimeXAxisView, 'CategoryDateTimeXAxisView', CategoryAxisBaseView.$);
