/*
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 { VerticalAnchoredCategorySeries } from "./VerticalAnchoredCategorySeries";
import { IBarSeries_$type } from "./IBarSeries";
import { DependencyProperty } from "igniteui-angular-core";
import { Defaults } from "./Defaults";
import { Rect } from "igniteui-angular-core";
import { typeCast, fromEnum, runOn, markType, markDep } from "igniteui-angular-core";
import { ScalerParams } from "./ScalerParams";
import { List$1 } from "igniteui-angular-core";
import { CategorySeriesView } from "./CategorySeriesView";
import { BarFramePreparer } from "./BarFramePreparer";
import { Pool$1 } from "igniteui-angular-core";
import { Rectangle } from "igniteui-angular-core";
import { AxisRange } from "./AxisRange";
import { CategoryBucketCalculator } from "./CategoryBucketCalculator";
import { AnchoredCategorySeriesView } from "./AnchoredCategorySeriesView";
import { CategoryTrendLineManagerFactory } from "./CategoryTrendLineManagerFactory";
import { PropertyMetadata } from "igniteui-angular-core";
import { truncate, isNaN_, isInfinity } from "igniteui-angular-core";
import { ISupportsMarkers_$type } from "./ISupportsMarkers";
import { BarTrendLineManager } from "./BarTrendLineManager";
import { Color } from "igniteui-angular-core";
/**
 * @hidden
 */
export let BarSeries = /*@__PURE__*/ (() => {
    class BarSeries extends VerticalAnchoredCategorySeries {
        constructor() {
            super();
            this.ab = BarSeries.$;
            this.rt = new BarFramePreparer(1, this, this.uy, this, this, this.uy.cr);
        }
        get_df() {
            return true;
        }
        gu() {
            if (!isNaN_(this.gt)) {
                return super.gu();
            }
            return Defaults.h;
        }
        get_d2() {
            return true;
        }
        bx() {
            return new BarSeriesView(this);
        }
        ry() {
            return 1;
        }
        b3() {
            if (this.b4 == 0) {
                return 2;
            }
            return super.b3();
        }
        ly(a) {
            super.ly(a);
            this.uy = a;
        }
        get_c2() {
            return true;
        }
        pu(a) {
            if (this.xAxis == null || this.yAxis == null || this.ro.f.count == 0) {
                return Rect.empty;
            }
            let b = this.b0.bv;
            let c = this.b0.bu;
            let d = this.getEffectiveViewport1(this.b0);
            let e = new ScalerParams(0, b, c, this.xAxis.cg, d);
            let f = this.pg(a);
            let g = this.hj(a, true);
            let h = this.hh(a, true);
            let i = truncate(Math.floor(g / this.so(this.b0)));
            let j = truncate(Math.floor(h / this.so(this.b0)));
            let k = i - this.sp(this.b0);
            let l = j - this.sp(this.b0);
            if (k < 0) {
                k = 0;
            }
            if (k > this.ro.f.count - 1) {
                k = this.ro.f.count - 1;
            }
            if (l < 0) {
                l = 0;
            }
            if (l > this.ro.f.count - 1) {
                l = this.ro.f.count - 1;
            }
            let m = this.ro.f._inner[k];
            let n = this.ro.f._inner[l];
            let o = Math.abs(f.y - m[0]);
            let p = Math.abs(f.y - n[0]);
            let q = this.tp(this.rx);
            let r = this.yAxis.k3(b, c, d);
            if (o < p) {
                let s = m[0] - 0.5 * r;
                let t = m[1];
                let u = q;
                return new Rect(0, Math.min(u, t), s, Math.max(u, t) - Math.min(u, t), r);
            }
            else {
                let v = n[0] - 0.5 * r;
                let w = n[1];
                let x = q;
                return new Rect(0, Math.min(x, w), v, Math.max(x, w) - Math.min(x, w), r);
            }
        }
        ev(a, b) {
            if (this.ey(a, b)) {
                return true;
            }
            if (this.qf(a, b)) {
                return true;
            }
            return false;
        }
        ru(a) {
            let b = typeCast(CategorySeriesView.$, a);
            if (b != null && b.q) {
                if (b.p) {
                    return new BarFramePreparer(1, this, typeCast(ISupportsMarkers_$type, b), b, this, b.cr);
                }
                else {
                    return new BarFramePreparer(1, this, typeCast(ISupportsMarkers_$type, b), this.cd.cq.o, this, b.cr);
                }
            }
            else {
                return this.rt;
            }
        }
        ac() {
            super.ac();
        }
        preferredCategoryMode(a) {
            return 2;
        }
        kx(a, b) {
            super.kx(a, b);
            let c = typeCast(BarSeriesView.$, b);
            if (a && c != null && c.db != null) {
                c.db.count = 0;
            }
        }
        sr() {
            let a = 0;
            for (let b of fromEnum(this.cd.series)) {
                if (b == this) {
                    return a;
                }
                let c = typeCast(IBarSeries_$type, b);
                if (c != null && c.yAxis == this.yAxis && c.getPreferredCategoryMode() == 2) {
                    a++;
                }
            }
            return -1;
        }
        tp(a) {
            let b = 0;
            let c = a.bv;
            let d = a.bu;
            let e = this.getEffectiveViewport1(a);
            if (!c.isEmpty && !d.isEmpty && this.xAxis != null) {
                let f = new ScalerParams(0, c, d, this.xAxis.cg, e);
                b = this.xAxis.ei(this.xAxis.l5, f);
            }
            return b;
        }
        ap(a) {
            if (this.valueColumn == null || this.valueColumn.count == 0) {
                return null;
            }
            if (a == this.yAxis) {
                return new AxisRange(0, this.valueColumn.count - 1);
            }
            if (a == this.xAxis) {
                return new AxisRange(this.valueColumn.minimum, this.valueColumn.maximum);
            }
            return null;
        }
        en(a) {
            let b = this.b0.bv;
            b = b.copy();
            let c = this.b0.bu;
            let d = new Rect(0, 0, 0, 1, 1);
            let e = !b.isEmpty && !c.isEmpty && this.ck != null ? this.ck.indexOf(a) : -1;
            let f = this.ps(this.b0);
            let g = new ScalerParams(0, d, d, this.xAxis.cg, f);
            let h = new ScalerParams(0, d, d, this.yAxis.cg, f);
            let i = this.yAxis != null ? this.yAxis.ei(e, h) : NaN;
            let j = this.yAxis != null ? this.rt.r(this.yAxis, d, d, f) : 0;
            i += j;
            let k = this.xAxis != null && this.valueColumn != null && e < this.valueColumn.count ? this.xAxis.ei(this.valueColumn.item(e), g) : NaN;
            if (!isNaN_(k)) {
                if (k < b.left + 0.1 * b.width) {
                    k = k + 0.4 * b.width;
                    b.x = k - 0.5 * b.width;
                }
                if (k > b.right - 0.1 * b.width) {
                    k = k - 0.4 * b.width;
                    b.x = k - 0.5 * b.width;
                }
            }
            if (!isNaN_(i)) {
                if (i < b.top + 0.1 * b.height) {
                    i = i + 0.4 * b.height;
                    b.y = i - 0.5 * b.height;
                }
                if (i > b.bottom - 0.1 * b.height) {
                    i = i - 0.4 * b.height;
                    b.y = i - 0.5 * b.height;
                }
            }
            if (this.b8 != null) {
                this.b8.au(this.cd, b, true);
            }
            return e >= 0;
        }
        ga() {
            return this.yAxis.k3(this.b0.bv, this.b0.bu, this.getEffectiveViewport1(this.b0));
        }
        s6(a, b) {
            super.s6(a, b);
            let c = a.f;
            if (!b.o()) {
                return;
            }
            let d = b.bv;
            let e = b.bu;
            let f = this.getEffectiveViewport1(b);
            let g = new ScalerParams(0, d, e, this.xAxis.cg, f);
            let h = this.yAxis;
            let i = new ScalerParams(0, d, e, this.yAxis.cg, f);
            let j = this.xAxis;
            let k = j.ei(j.l5, g);
            let l = this.yAxis.k3(d, e, f);
            let m = typeCast(BarSeriesView.$, b);
            if (isNaN_(l) || isInfinity(l) || isNaN_(k)) {
                m.db.count = 0;
                return;
            }
            this.rw.a0(this, this.sg(), runOn(this, this.rh), this.so(b), this.sp(b));
            this.rw.r = this.uz;
            this.rw.s = this.u0;
            this.rw.k = this.uz;
            this.rw.l = this.u0;
            let n = false;
            let o = this.rw.b;
            if (o != null) {
                n = true;
            }
            let p = this.xAxis.cm;
            let q = this.valueColumn.count;
            for (let r = 0; r < c.count; ++r) {
                let s = c._inner[r][0] - 0.5 * l;
                let t = c._inner[r][1];
                let u = k;
                u = Math.max(u, -100);
                t = Math.min(t, e.right + 100);
                let v = m.db.item(r);
                v.height = l;
                v.width = Math.abs(t - u);
                if (n) {
                    this.s2(c, r, q, h, i, b.q);
                }
                this.rw.ad(v, false, false, false, false);
                v.am = this.rw.k;
                v.an = this.rw.l;
                m.dg(v, Math.min(t, u), s);
            }
            m.db.count = c.count;
            b.cy(a);
        }
        k0(a, b, c, d) {
            switch (a) {
                case 4:
                case 1:
                case 0:
                    this.tg.cr.g(this.ft);
                    break;
            }
            if (this.tg.c3) {
                this.tg.c2.g$e(a, b, c, d);
            }
            switch (a) {
                case 4:
                    if (this.xAxis != null && !this.xAxis.c0()) {
                        this.mh(true);
                    }
                    break;
                case 1:
                    if (this.xAxis != null && !this.xAxis.c0()) {
                        this.mh(true);
                    }
                    break;
                case 0:
                    if (this.xAxis != null && !this.xAxis.c0()) {
                        this.mh(true);
                    }
                    break;
                case 2:
                    if (this.t0 != null && this.tg.cr.d > 0) {
                        this.mh(true);
                    }
                    break;
                case 3:
                    if (d == this.t0) {
                        if (this.xAxis != null && !this.xAxis.c0()) {
                            this.mh(true);
                        }
                    }
                    break;
            }
        }
        l3(a, b, c, d) {
            super.l3(a, b, c, d);
            switch (b) {
                case VerticalAnchoredCategorySeries.$$p[0]:
                    if (c != null) {
                        c.bz(this);
                    }
                    if (d != null) {
                        d.cs(this);
                    }
                    this.tg.cr.g(this.ft);
                    if (this.xAxis != null && this.xAxis.c0()) {
                        this.mh(false);
                    }
                    break;
                case VerticalAnchoredCategorySeries.$$p[1]:
                    if (c != null) {
                        c.bz(this);
                    }
                    if (d != null) {
                        d.cs(this);
                    }
                    if (this.tg.c3) {
                        this.tg.c2 = CategoryTrendLineManagerFactory.a(this.tg.c2, this.yAxis, this.m9, this, this.cn);
                    }
                    this.tg.cr.g(this.ft);
                    this.mh(false);
                    break;
                case "FastItemsSource":
                    if (this.xAxis != null && !this.xAxis.c0()) {
                        this.tg.cr.g(this.ft);
                        this.mh(false);
                    }
                    break;
                case "ValueColumn":
                    if (this.xAxis != null && !this.xAxis.c0()) {
                        this.tg.cr.g(this.ft);
                        this.mh(false);
                    }
                    break;
                case "SeriesViewer":
                    if (c != null && d == null) {
                        this.k1(this.xAxis);
                        this.k1(this.yAxis);
                    }
                    if (c == null && d != null) {
                        this.l8(this.xAxis);
                        this.l8(this.yAxis);
                    }
                    this.tg.cr.g(this.ft);
                    this.mh(false);
                    break;
            }
        }
        getPreferredCategoryMode() {
            return this.preferredCategoryMode(this.yAxis);
        }
        get useHighMarkerFidelity() {
            return this.sj;
        }
    }
    BarSeries.$t = markType(BarSeries, 'BarSeries', VerticalAnchoredCategorySeries.$, [IBarSeries_$type]);
    BarSeries.$$p = markDep(DependencyProperty, PropertyMetadata, BarSeries, 'raisePropertyChanged', ['RadiusX:uz:u3', [1, 0], 'RadiusY:u0:u4', [1, 0]]);
    return BarSeries;
})();
/**
 * @hidden
 */
export let BarBucketCalculator = /*@__PURE__*/ (() => {
    class BarBucketCalculator extends CategoryBucketCalculator {
        constructor(a) {
            super(a);
            this.i = null;
            this.h = null;
            this.j = 0;
            this.i = a;
        }
        g(a) {
            let b = this.c.bv;
            let c = this.c.bu;
            let d = this.c.ct.ck;
            if (b.isEmpty || c.isEmpty || this.i.da.yAxis == null || d == null || d.count == 0) {
                this.d = 0;
                return;
            }
            let e = typeCast(BarSeries.$, this.c.ct);
            let f = e.pq(this.c);
            let g = e.getEffectiveViewport1(this.c);
            let h = new ScalerParams(0, b, c, e.yAxis.cg, g);
            let i = Math.floor(e.yAxis.em(f.top, h, 0));
            let j = Math.ceil(e.yAxis.em(f.bottom, h, 0));
            if (!e.yAxis.cg) {
                j = Math.ceil(e.yAxis.em(f.top, h, 0));
                i = Math.floor(e.yAxis.em(f.bottom, h, 0));
            }
            let k = Math.floor((j - i + 1) * a / f.height);
            this.d = truncate(Math.max(1, k));
            this.e = truncate(Math.max(0, Math.floor(i / this.d) - 1));
            this.f = truncate(Math.ceil(j / this.d));
        }
        getBucketWithoutUnknowns(a) {
            let b = this.h;
            let c = this.j;
            let d = a * this.d;
            let e = d + this.d - 1;
            let f = c - 1;
            d = d < f ? d : f;
            e = e < f ? e : f;
            let g = 1.7976931348623157E+308;
            let h = -1.7976931348623157E+308;
            let i = true;
            for (let j = d; j <= e; ++j) {
                let k = b[j];
                g = g < k ? g : k;
                h = h > k ? h : k;
                Math.max(h, k);
                i = false;
            }
            if (!i) {
                return [0.5 * (d + e), g, h];
            }
            return [0.5 * (d + e), NaN, NaN];
        }
        getBucket(a) {
            let b = this.h;
            let c = this.j;
            let d = a * this.d;
            let e = d + this.d - 1;
            let f = c - 1;
            d = d < f ? d : f;
            e = e < f ? e : f;
            let g = NaN;
            let h = NaN;
            for (let i = d; i <= e; ++i) {
                let j = b[i];
                if (!isNaN_(g)) {
                    if (!isNaN_(j)) {
                        g = Math.min(g, j);
                        h = Math.max(h, j);
                    }
                }
                else {
                    g = j;
                    h = j;
                }
            }
            if (!isNaN_(g)) {
                return [0.5 * (d + e), g, h];
            }
            return [0.5 * (d + e), NaN, NaN];
        }
        cacheValues() {
            this.j = this.i.da.valueColumn.count;
            this.h = this.i.da.valueColumn.asArray();
        }
        unCacheValues() {
            this.h = null;
        }
    }
    BarBucketCalculator.$t = markType(BarBucketCalculator, 'BarBucketCalculator', CategoryBucketCalculator.$);
    return BarBucketCalculator;
})();
/**
 * @hidden
 */
export let BarSeriesView = /*@__PURE__*/ (() => {
    class BarSeriesView extends AnchoredCategorySeriesView {
        constructor(a) {
            super(a);
            this.da = null;
            this.db = null;
            this.dc = null;
            this.dh = new Rectangle();
            this.da = a;
            this.db = ((() => {
                let $ret = new Pool$1(Rectangle.$);
                $ret.create = runOn(this, this.di);
                $ret.activate = runOn(this, this.dd);
                $ret.disactivate = runOn(this, this.df);
                $ret.destroy = runOn(this, this.de);
                return $ret;
            })());
            this.c2 = new BarTrendLineManager();
        }
        as() {
            super.as();
            this.dc = new List$1(Rectangle.$, 0);
            if (!this.q) {
                this.da.qc = Defaults.c;
                this.da.gz = Defaults.i;
            }
        }
        di() {
            let a = new Rectangle();
            this.dc.add(a);
            a._visibility = 1;
            return a;
        }
        dd(a) {
            a._visibility = 0;
        }
        df(a) {
            a._visibility = 1;
        }
        de(a) {
            this.dc.remove(a);
        }
        dg(a, b, c) {
            if (!this.isDirty) {
                this.aq();
            }
            a.o = c;
            a.n = b;
        }
        cs() {
            return new BarBucketCalculator(this);
        }
        bf(a, b) {
            super.bf(a, b);
            let c = a;
            let d = this.bp(b);
            c._fill = d;
            c._stroke = d;
            c.ac = this.e.fu;
        }
        ac(a) {
            return this.dc._inner[a];
        }
        ab(a) {
            let b = this.dc._inner[a];
            this.dh.n = b.n;
            this.dh.o = b.o;
            this.dh.width = b.width;
            this.dh.height = b.height;
            let c = this.bp(a);
            this.dh._fill = c;
            this.dh._stroke = c;
            this.dh.ac = this.e.fu + 3;
            return this.dh;
        }
        a6(a, b) {
            super.a6(a, b);
            if (a.d) {
                for (let c = 0; c < this.dc.count; c++) {
                    let d = this.aa(c, b);
                    this.bd(d, c, b);
                    a.v(d);
                }
            }
        }
        al(a) {
            super.al(a);
            let b = 0;
            let c = new List$1(Rectangle.$, 0);
            for (let d of fromEnum(this.db.a)) {
                c.add(d);
            }
            c.aa((e, f) => {
                if (e.o > f.o) {
                    return -1;
                }
                else if (e.o < f.o) {
                    return 1;
                }
                else {
                    return 0;
                }
            });
            for (let e of fromEnum(c)) {
                this.e.a8.exportRectangleData(a, e, "column" + b, ["Main", "Fill"]);
            }
            b++;
        }
        af() {
            let a = new Color();
            a.colorString = "rgba(95,95,95,0.5)";
            this.e.pa = a;
            this.e.gw = 5;
            this.e.gx = 5;
            this.e.gy = -5;
        }
    }
    BarSeriesView.$t = markType(BarSeriesView, 'BarSeriesView', AnchoredCategorySeriesView.$);
    return BarSeriesView;
})();
