/*
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 { ChartSelection } from "./ChartSelection";
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 { BarTrendLineManager } from "./BarTrendLineManager";
import { PropertyMetadata } from "igniteui-angular-core";
import { truncate, isNaN_, isInfinity } from "igniteui-angular-core";
import { ISupportsMarkers_$type } from "./ISupportsMarkers";
import { Color } from "igniteui-angular-core";
/**
 * @hidden
 */
export let BarSeries = /*@__PURE__*/ (() => {
    class BarSeries extends VerticalAnchoredCategorySeries {
        constructor() {
            super();
            this.ab = BarSeries.$;
            this.y9 = new BarFramePreparer(1, this, this.ac0, this, this, this.ac0.c9);
        }
        bk() {
            return new BarSeries();
        }
        get_ez() {
            return true;
        }
        jn() {
            if (!isNaN_(this.jm)) {
                return super.jn();
            }
            return Defaults.h;
        }
        get_fm() {
            return true;
        }
        ct() {
            return new BarSeriesView(this);
        }
        ze() {
            return 1;
        }
        cz() {
            if (this.c0 == 0) {
                return 2;
            }
            return super.cz();
        }
        q7(a) {
            super.q7(a);
            this.ac0 = a;
        }
        get_ef() {
            return true;
        }
        wx(a) {
            if (this.xAxis == null || this.yAxis == null || this.y4.h.count == 0) {
                return Rect.empty;
            }
            let b = this.cw.ca;
            let c = this.cw.b9;
            let d = this.getEffectiveViewport1(this.cw);
            let e = new ScalerParams(0, b, c, this.xAxis.df, d);
            let f = this.wh(a);
            let g = this.ke(a, true);
            let h = this.kc(a, true);
            let i = truncate(Math.floor(g / this.z5(this.cw)));
            let j = truncate(Math.floor(h / this.z5(this.cw)));
            let k = i - this.z6(this.cw);
            let l = j - this.z6(this.cw);
            if (k < 0) {
                k = 0;
            }
            if (k > this.y4.h.count - 1) {
                k = this.y4.h.count - 1;
            }
            if (l < 0) {
                l = 0;
            }
            if (l > this.y4.h.count - 1) {
                l = this.y4.h.count - 1;
            }
            let m = this.y4.h._inner[k];
            let n = this.y4.h._inner[l];
            let o = Math.abs(f.y - m[0]);
            let p = Math.abs(f.y - n[0]);
            let q = this.abl(this.zd);
            let r = this.yAxis.ou(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);
            }
        }
        gx(a, b) {
            if (this.g0(a, b)) {
                return true;
            }
            if (this.xr(a, b)) {
                return true;
            }
            return false;
        }
        ak(a, b) {
            let c = new List$1(ChartSelection.$, 0);
            this.qa(a, c, b);
            return c.toArray();
        }
        qa(a, b, c) {
            this.aap(a, b, c, this.y3, 1);
        }
        za(a) {
            let b = typeCast(CategorySeriesView.$, a);
            if (b != null && b.t) {
                if (b.s) {
                    return new BarFramePreparer(1, this, typeCast(ISupportsMarkers_$type, b), b, this, b.c9);
                }
                else {
                    return new BarFramePreparer(1, this, typeCast(ISupportsMarkers_$type, b), this.dc.dp.o, this, b.c9);
                }
            }
            else {
                return this.y9;
            }
        }
        ac() {
            super.ac();
        }
        preferredCategoryMode(a) {
            return 2;
        }
        pb(a, b) {
            super.pb(a, b);
            let c = typeCast(BarSeriesView.$, b);
            if (a && c != null && c.du != null) {
                c.du.count = 0;
            }
        }
        z8() {
            if (this.bo != null) {
                let a = this.bo;
                return a.z8();
            }
            let b = 0;
            for (let c of fromEnum(this.dc.series)) {
                if (c == this) {
                    return b;
                }
                let d = typeCast(IBarSeries_$type, c);
                if (d != null && d.yAxis == this.yAxis && d.getPreferredCategoryMode() == 2) {
                    b++;
                }
            }
            return -1;
        }
        abl(a) {
            let b = 0;
            let c = a.ca;
            let d = a.b9;
            let e = this.getEffectiveViewport1(a);
            if (!c.isEmpty && !d.isEmpty && this.xAxis != null) {
                let f = new ScalerParams(0, c, d, this.xAxis.df, e);
                b = this.xAxis.ft(this.xAxis.p5, f);
            }
            return b;
        }
        ay(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;
        }
        ge(a) {
            let b = this.cw.ca;
            b = b.copy();
            let c = this.cw.b9;
            let d = new Rect(0, 0, 0, 1, 1);
            let e = !b.isEmpty && !c.isEmpty && this.dp != null ? this.dp.indexOf(a) : -1;
            let f = this.wv(this.cw);
            let g = new ScalerParams(0, d, d, this.xAxis.df, f);
            let h = new ScalerParams(0, d, d, this.yAxis.df, f);
            let i = this.yAxis != null ? this.yAxis.ft(e, h) : NaN;
            let j = this.yAxis != null ? this.y9.r(this.yAxis, d, d, f) : 0;
            i += j;
            let k = this.xAxis != null && this.valueColumn != null && e < this.valueColumn.count ? this.xAxis.ft(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.c4 != null) {
                this.c4.au(this.dc, b, true);
            }
            return e >= 0;
        }
        iy() {
            return this.yAxis.ou(this.cw.ca, this.cw.b9, this.getEffectiveViewport1(this.cw));
        }
        aax(a, b) {
            super.aax(a, b);
            let c = a.h;
            if (!b.r()) {
                return;
            }
            let d = b.ca;
            let e = b.b9;
            let f = this.getEffectiveViewport1(b);
            let g = new ScalerParams(0, d, e, this.xAxis.df, f);
            let h = this.yAxis;
            let i = new ScalerParams(0, d, e, this.yAxis.df, f);
            let j = this.xAxis;
            let k = j.ft(j.p5, g);
            let l = this.yAxis.ou(d, e, f);
            let m = typeCast(BarSeriesView.$, b);
            if (isNaN_(l) || isInfinity(l) || isNaN_(k)) {
                m.du.count = 0;
                return;
            }
            this.zc.a2(this, this.zw(), runOn(this, this.yw), this.z5(b), this.z6(b));
            this.zc.r = this.ac1;
            this.zc.s = this.ac2;
            this.zc.k = this.ac1;
            this.zc.l = this.ac2;
            let n = false;
            let o = this.zc.b;
            if (o != null) {
                n = true;
            }
            let p = this.xAxis.dm;
            let q = p ? this.xAxis : null;
            let r = this.valueColumn.count;
            let s = this.z5(b);
            let t = 0;
            for (let u = 0; u < c.count; ++u) {
                let v = c._inner[u][0] - 0.5 * l;
                let w = c._inner[u][1];
                let x = k;
                x = Math.max(x, -100);
                w = Math.min(w, e.right + 100);
                let y = Math.abs(w - x);
                if (isInfinity(y) || isNaN_(y)) {
                    continue;
                }
                let z = m.du.item(t);
                t++;
                z.height = l;
                z.width = y;
                let aa = this.zc.aw(r, u)[0];
                if (n) {
                    this.aat(c, u, r, h, i, b.t, aa);
                }
                if (z.dataContext != null) {
                    let ab = z.dataContext;
                    if (s == 1) {
                        if (aa >= 0 && aa < this.dp.count) {
                            if (q != null && q.i$b != null && q.i$b.count > aa) {
                                aa = q.i$b._inner[aa];
                            }
                            ab.item = this.dp.item(aa);
                        }
                    }
                    else {
                        ab.item = null;
                    }
                }
                this.zc.ad(z, false, false, false, false);
                z.ap = this.zc.k;
                z.aq = this.zc.l;
                m.dz(z, Math.min(w, x), v);
            }
            m.du.count = t;
            b.dg(a);
        }
        ph(a, b, c, d) {
            switch (a) {
                case 4:
                case 1:
                case 0:
                    this.aa7.c9.g(this.ie);
                    break;
            }
            if (this.aa7.dl) {
                this.aa7.dk.g$i(a, b, c, d);
            }
            if (this.aa7.aa.count > 0) {
                for (let e of fromEnum(this.aa7.aa)) {
                    e.g$i(a, b, c, d);
                }
            }
            switch (a) {
                case 4:
                    if (this.xAxis != null && !this.xAxis.d2()) {
                        this.ry(true);
                    }
                    break;
                case 1:
                    if (this.xAxis != null && !this.xAxis.d2()) {
                        this.ry(true);
                    }
                    break;
                case 0:
                    if (this.xAxis != null && !this.xAxis.d2()) {
                        this.ry(true);
                    }
                    break;
                case 2:
                    if (this.abz != null && this.aa7.c9.d > 0) {
                        this.ry(true);
                    }
                    break;
                case 3:
                    if (d == this.abz) {
                        if (this.xAxis != null && !this.xAxis.d2()) {
                            this.ry(true);
                        }
                    }
                    break;
            }
        }
        rg(a, b, c, d) {
            super.rg(a, b, c, d);
            switch (b) {
                case VerticalAnchoredCategorySeries.$$p[0]:
                    if (c != null) {
                        c.cq(this);
                    }
                    if (d != null) {
                        d.du(this);
                    }
                    this.aa7.c9.g(this.ie);
                    if (this.xAxis != null && this.xAxis.d2()) {
                        this.ry(false);
                    }
                    break;
                case VerticalAnchoredCategorySeries.$$p[1]:
                    if (c != null) {
                        c.cq(this);
                    }
                    if (d != null) {
                        d.du(this);
                    }
                    if (this.aa7.dl) {
                        this.aa7.dk = CategoryTrendLineManagerFactory.a(this.aa7.dk, this.yAxis, this.s3, this, this.ds);
                    }
                    this.aa7.c9.g(this.ie);
                    this.ry(false);
                    break;
                case "FastItemsSource":
                    if (this.xAxis != null && !this.xAxis.d2()) {
                        this.aa7.c9.g(this.ie);
                        this.ry(this.gh && c != null);
                    }
                    break;
                case "ValueColumn":
                    if (this.xAxis != null && !this.xAxis.d2()) {
                        this.aa7.c9.g(this.ie);
                        this.ry(this.gh && this.d9);
                    }
                    break;
                case "SeriesViewer":
                    if (c != null && d == null) {
                        this.pi(this.xAxis);
                        this.pi(this.yAxis);
                    }
                    if (c == null && d != null) {
                        this.ro(this.xAxis);
                        this.ro(this.yAxis);
                    }
                    this.aa7.c9.g(this.ie);
                    this.ry(false);
                    break;
            }
        }
        getPreferredCategoryMode() {
            return this.preferredCategoryMode(this.yAxis);
        }
        bd() {
            return new BarTrendLineManager();
        }
        get useHighMarkerFidelity() {
            return this.z0;
        }
    }
    BarSeries.$t = markType(BarSeries, 'BarSeries', VerticalAnchoredCategorySeries.$, [IBarSeries_$type]);
    BarSeries.$$p = markDep(DependencyProperty, PropertyMetadata, BarSeries, 'raisePropertyChanged', ['RadiusX:ac1:ac5', [1, 0], 'RadiusY:ac2:ac6', [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.ca;
            let c = this.c.b9;
            let d = this.c.db.dp;
            if (b.isEmpty || c.isEmpty || this.i.dt.yAxis == null || d == null || d.count == 0) {
                this.d = 0;
                return;
            }
            let e = typeCast(BarSeries.$, this.c.db);
            let f = e.wt(this.c);
            let g = e.getEffectiveViewport1(this.c);
            let h = new ScalerParams(0, b, c, e.yAxis.df, g);
            let i = Math.floor(e.yAxis.fx(f.top, h, 0));
            let j = Math.ceil(e.yAxis.fx(f.bottom, h, 0));
            if (!e.yAxis.df) {
                j = Math.ceil(e.yAxis.fx(f.top, h, 0));
                i = Math.floor(e.yAxis.fx(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.dt.valueColumn.count;
            this.h = this.i.dt.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.dt = null;
            this.du = null;
            this.dv = null;
            this.d0 = new Rectangle();
            this.dt = a;
            this.du = ((() => {
                let $ret = new Pool$1(Rectangle.$);
                $ret.create = runOn(this, this.d1);
                $ret.activate = runOn(this, this.dw);
                $ret.disactivate = runOn(this, this.dy);
                $ret.destroy = runOn(this, this.dx);
                return $ret;
            })());
            this.dk = new BarTrendLineManager();
        }
        a4() {
            super.a4();
            this.dv = new List$1(Rectangle.$, 0);
            if (!this.t) {
                this.dt.xk = Defaults.c;
                this.dt.jt = Defaults.i;
            }
        }
        d1() {
            let a = new Rectangle();
            this.dv.add(a);
            a._visibility = 1;
            return a;
        }
        dw(a) {
            a._visibility = 0;
        }
        dy(a) {
            a._visibility = 1;
        }
        dx(a) {
            this.dv.remove(a);
        }
        dz(a, b, c) {
            if (!this.isDirty) {
                this.a2();
            }
            a.o = c;
            a.n = b;
        }
        da() {
            return new BarBucketCalculator(this);
        }
        bt(a, b) {
            super.bt(a, b);
            let c = a;
            let d = this.b4(b);
            c._fill = d;
            c._stroke = d;
            c.ad = this.f.ig;
        }
        ak(a) {
            return this.dv._inner[a];
        }
        aj(a) {
            let b = this.dv._inner[a];
            this.d0.n = b.n;
            this.d0.o = b.o;
            this.d0.width = b.width;
            this.d0.height = b.height;
            let c = this.b4(a);
            this.d0._fill = c;
            this.d0._stroke = c;
            this.d0.ad = this.f.ig + 3;
            return this.d0;
        }
        bj(a, b) {
            super.bj(a, b);
            if (a.d) {
                for (let c = 0; c < this.dv.count; c++) {
                    let d = this.ai(c, b);
                    this.br(d, c, b);
                    a.w(d);
                }
            }
        }
        ax(a) {
            super.ax(a);
            let b = 0;
            let c = new List$1(Rectangle.$, 0);
            for (let d of fromEnum(this.du.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.f.bc.exportRectangleData(a, e, "column" + b, ["Main", "Fill"]);
            }
            b++;
        }
        an() {
            let a = new Color();
            a.colorString = "rgba(95,95,95,0.5)";
            this.f.wb = a;
            this.f.jq = 5;
            this.f.jr = 5;
            this.f.js = -5;
        }
    }
    BarSeriesView.$t = markType(BarSeriesView, 'BarSeriesView', AnchoredCategorySeriesView.$);
    return BarSeriesView;
})();
