/*
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 { RadialBucketCalculator } from "./RadialBucketCalculator";
import { markType, Point_$type, enumGetBox, typeCast, runOn, markDep } from "igniteui-angular-core";
import { isNaN_, intDivide, isInfinity } from "igniteui-angular-core";
import { RadialBase } from "./RadialBase";
import { CategoryLineRasterizer } from "./CategoryLineRasterizer";
import { DependencyProperty } from "igniteui-angular-core";
import { TrendLineType_$type } from "igniteui-angular-core";
import { Brush } from "igniteui-angular-core";
import { PenLineCap_$type } from "igniteui-angular-core";
import { DoubleCollection } from "igniteui-angular-core";
import { RadialFrame } from "./RadialFrame";
import { ScalerParams } from "./ScalerParams";
import { Rect } from "igniteui-angular-core";
import { DataLegendSeriesValueInfo } from "igniteui-angular-core";
import { DataLegendSeriesContext } from "igniteui-angular-core";
import { List$1 } from "igniteui-angular-core";
import { AxisRange } from "./AxisRange";
import { NumericAxisBase } from "./NumericAxisBase";
import { IFastItemsSource_$type } from "igniteui-angular-core";
import { RadialBaseView } from "./RadialBaseView";
import { CollisionAvoider } from "./CollisionAvoider";
import { Clipper } from "igniteui-angular-core";
import { TrendResolutionParams } from "./TrendResolutionParams";
import { RadialTrendLineManager } from "./RadialTrendLineManager";
import { CategoryMarkerManager } from "./CategoryMarkerManager";
import { PropertyMetadata } from "igniteui-angular-core";
import { stringIsNullOrEmpty } from "igniteui-angular-core";
/**
 * @hidden
 */
export let AnchoredRadialBucketCalculator = /*@__PURE__*/ (() => {
    class AnchoredRadialBucketCalculator extends RadialBucketCalculator {
        constructor(a) {
            super(a);
            this.f = null;
            this.f = a;
        }
        getBucket(a) {
            let b = this.f.cw.tc;
            let c = Math.min(a * this.b, b.count - 1);
            let d = Math.min(c + this.b - 1, b.count - 1);
            let e = NaN;
            let f = NaN;
            for (let g = c; g <= d; ++g) {
                let h = b.item(g);
                if (!isNaN_(e)) {
                    if (!isNaN_(h)) {
                        e = Math.min(e, h);
                        f = Math.max(f, h);
                    }
                }
                else {
                    e = h;
                    f = h;
                }
            }
            if (!isNaN_(e)) {
                return [0.5 * (c + d), e, f];
            }
            return [0.5 * (c + d), NaN, NaN];
        }
    }
    AnchoredRadialBucketCalculator.$t = markType(AnchoredRadialBucketCalculator, 'AnchoredRadialBucketCalculator', RadialBucketCalculator.$);
    return AnchoredRadialBucketCalculator;
})();
/**
 * @hidden
 */
export let AnchoredRadialSeries = /*@__PURE__*/ (() => {
    class AnchoredRadialSeries extends RadialBase {
        constructor() {
            super();
            this.s7 = null;
            this.tb = null;
            this.ue = { $type: Point_$type, x: 0, y: 0 };
            this.s9 = null;
            this.s8 = null;
            this.s9 = new RadialFrame(3);
            this.s7 = new CategoryLineRasterizer();
        }
        by() {
            return new AnchoredRadialSeriesView(this);
        }
        kz() {
            super.kz();
            this.rt = null;
            this.r0 = null;
        }
        h1(a, b) {
            let c = this.jv(b);
            if (c == AnchoredRadialSeries.$$p[2]) {
                return this.h2(a, b, this.ts);
            }
            return super.h1(a, b);
        }
        l4(a) {
            super.l4(a);
            this.s6 = a;
        }
        ho(a, b) {
            return this.hp(a, b, this.rt, null, this.tc);
        }
        hm(a, b) {
            return this.hn(a, b, this.rt, null, this.tc);
        }
        f7(a, b, c, d, e) {
            if (this.tc == null) {
                return Number.POSITIVE_INFINITY;
            }
            return this.f8(a, b, this.rt, d, e, this.tc.count, null);
        }
        gn(a, b, c) {
            let d = this.getEffectiveViewport1(this.b1);
            let e = this.th(this.rt, this.b1.bv, this.b1.bu, d);
            let f = new ScalerParams(0, this.b1.bv, this.b1.bu, this.rt.cg, d);
            f.c = this.ce.tp;
            return this.gp(this.tc, a, this.rt, f, e, null, b, c);
        }
        ps(a, b, c) {
            if (this.r0 == null || this.rt == null || this.b1 == null || this.b1.bv.isEmpty || this.b1.bu.isEmpty) {
                return { $type: Point_$type, x: NaN, y: NaN };
            }
            let d = this.getEffectiveViewport1(this.b1);
            let e = new ScalerParams(0, this.b1.bv, this.b1.bu, this.r0.cg, d);
            e.c = this.getEffectiveViewport1(this.b1);
            let f = this.gn(a, b, c);
            f = this.r0.ei(f, e);
            let g = this.r1.c(a);
            if (!b && this.rt != null) {
                let h = this.ho(a, c);
                let i = this.hm(a, c);
                let j = this.th(this.rt, this.b1.bv, this.b1.bu, d);
                let k = new ScalerParams(0, this.b1.bv, this.b1.bu, this.rt.cg, d);
                k.c = this.ce.tp;
                let l = this.f7(a, h, this.rt, k, j);
                let m = this.f7(a, i, this.rt, k, j);
                if (m <= l) {
                    g = this.rt.ei(i, k) + j;
                }
                else {
                    g = this.rt.ei(h, k) + j;
                }
            }
            let n = this.r1.d(g, f, this.b1.bv, this.b1.bu);
            let o = this.r1.e(g, f, this.b1.bv, this.b1.bu);
            return { $type: Point_$type, x: n, y: o };
        }
        get tc() {
            return this.tb;
        }
        set tc(a) {
            if (this.tb != a) {
                let b = this.tb;
                this.tb = a;
                this.raisePropertyChanged("ValueColumn", b, this.tb);
            }
        }
        getDataLegendSeriesContextAt(a, b) {
            let c = new DataLegendSeriesValueInfo();
            c.a = 0;
            c.l = this.gn({ $type: Point_$type, x: a, y: b }, false, false);
            c.p = "Value";
            c.q = this.ts != null ? this.ts : "Value";
            c.r = this.to != null ? this.to : "V:";
            c.s = this.tq != null ? this.tq : "";
            let d = new DataLegendSeriesContext();
            d.f = ((() => {
                let $ret = new List$1(DataLegendSeriesValueInfo.$, 0);
                $ret.add(c);
                return $ret;
            })());
            d.b = this.aw();
            return d;
        }
        get ua() {
            return this.c(AnchoredRadialSeries.tz);
        }
        set ua(a) {
            this.h(AnchoredRadialSeries.tz, a);
        }
        aq(a) {
            if (a != null && a == this.rt && this.tc != null && this.tc.count > 0) {
                return new AxisRange(0, this.tc.count - 1);
            }
            if (a != null && a == this.r0 && this.tc != null && this.tc.count > 0) {
                return new AxisRange(this.tc.minimum, this.tc.maximum);
            }
            return null;
        }
        l9(a, b, c, d) {
            super.l9(a, b, c, d);
            if (this.s6.cx.g$b(a, b, c, d)) {
                this.mn(false);
                this.lu();
            }
            let e = typeCast(NumericAxisBase.$, this.r0);
            switch (b) {
                case "FastItemsSource":
                    if (typeCast(IFastItemsSource_$type, c) !== null) {
                        c.deregisterColumn(this.tc);
                        this.tc = null;
                    }
                    if (typeCast(IFastItemsSource_$type, d) !== null) {
                        this.tc = this.ci(this.ts);
                        this.s6.cs.e(this.fx);
                    }
                    if (e != null && !e.c0()) {
                        this.s6.cs.e(this.fx);
                        this.mn(false);
                    }
                    break;
                case AnchoredRadialSeries.$$p[2]:
                    if (this.cl != null) {
                        this.cl.deregisterColumn(this.tc);
                        this.tc = this.ci(this.ts);
                    }
                    break;
                case "ValueColumn":
                    if (e != null && !e.c0()) {
                        this.s6.cs.e(this.fx);
                        this.mn(false);
                    }
                    break;
                case "TrendLineBrush":
                    this.m1();
                    break;
                case "TrendLineType":
                    this.lu();
                    break;
            }
        }
        k5(a, b, c, d) {
            switch (a) {
                case 4:
                case 1:
                case 0:
                    this.s6.cs.e(this.fx);
                    break;
            }
            this.s6.cx.g$e(a, b, c, d);
            switch (a) {
                case 4:
                    if (this.r0 != null && !this.r0.c0()) {
                        this.mn(true);
                    }
                    break;
                case 1:
                    if (this.r0 != null && !this.r0.c0()) {
                        this.mn(true);
                    }
                    break;
                case 0:
                    if (this.r0 != null && !this.r0.c0()) {
                        this.mn(true);
                    }
                    break;
                case 2:
                    if (this.ts != null && this.s6.cs.b > 0 && this.r0 != null && !this.r0.c0()) {
                        this.mn(true);
                    }
                    break;
                case 3:
                    if (d == this.ts) {
                        if (this.r0 != null && !this.r0.c0()) {
                            this.mn(true);
                        }
                    }
                    break;
            }
        }
        fd(a, b, c) {
            let d = super.fd(a, b, c);
            let e = c;
            if (this.cl == null || this.cl.count == 0 || this.rt == null || this.tc == null || this.rt.le == 0 || this.r0 == null || isInfinity(this.r0.lw) || isInfinity(this.r0.lv) || e.cs.b < 1) {
                d = false;
            }
            return d;
        }
        getOffsetValue() {
            return this.th(this.rt, this.b1.bv, this.b1.bu, this.getEffectiveViewport1(this.b1));
        }
        getCategoryWidth() {
            return this.rt.getCategorySize(this.b1.bv, this.b1.bu, this.getEffectiveViewport1(this.b1));
        }
        get_tf() {
            return false;
        }
        get tf() {
            return this.get_tf();
        }
        get_c7() {
            return true;
        }
        get_em() {
            return this.r0 != null && this.r0.cg;
        }
        th(a, b, c, d) {
            let e = 0;
            let f = this.preferredCategoryMode(a);
            if (f == 0 && a.categoryMode != 0) {
                f = 1;
            }
            switch (f) {
                case 0:
                    e = 0;
                    break;
                case 1:
                    e = 0.5 * a.getCategorySize(b, c, d);
                    break;
                case 2:
                    e = a.getGroupCenter(this.sn(), b, c, d);
                    break;
            }
            if (a.cg) {
                e = -e;
            }
            return e;
        }
        sv(a, b) {
            let c = b.bv;
            let d = b.bu;
            let e = this.getEffectiveViewport1(b);
            let f = this.rt;
            let g = this.r0;
            let h = g.lw;
            let i = g.lv;
            a.f.clear();
            a.m.clear();
            a.p.clear();
            let j = this.qn();
            let k = 0;
            let l = b;
            let m = new CollisionAvoider();
            let n = 0;
            let o = b.cs.d;
            n = this.th(f, c, d, e);
            l.cx.i = this.r0.nn;
            l.cx.h = this.r0.nm;
            l.cx.j = (p, q) => this.ti(p, q, c, d);
            l.cx.k = (p, q) => this.tj(p, q, c, d);
            let p = ((() => {
                let $ret = new Clipper(0, d, false);
                $ret.i = a.p;
                return $ret;
            })());
            let q = ((() => {
                let $ret = new TrendResolutionParams();
                $ret.c = b.cs.b;
                $ret.d = b.cs.c;
                $ret.e = o;
                $ret.a = n;
                $ret.b = this.fx;
                $ret.f = d;
                $ret.g = c;
                return $ret;
            })());
            l.cx.l(a.p, this.ta, this.tc, this.tl, (r) => this.rt.getScaledAngle(r), (r) => this.r0.nt(r), q, p);
            let r = true;
            if (this.tg(b)) {
                o--;
            }
            for (let s = b.cs.c; s <= o; ++s) {
                let t = s;
                if (t * b.cs.b >= this.rt.le) {
                    t -= intDivide((this.rt.le), b.cs.b);
                }
                let u = b.cs.getBucket(t);
                if (!isNaN_(u[0])) {
                    u[0] = f.getScaledAngle(u[0]) + n;
                    if (u[1] < h || u[1] > i) {
                        continue;
                    }
                    u[1] = g.nt(u[1]);
                    if (b.cs.b > 1) {
                        if (u[2] < h || u[2] > i) {
                            continue;
                        }
                        u[2] = g.nt(u[2]);
                    }
                    else {
                        u[2] = u[1];
                    }
                    if ((isNaN_(u[1]) || isNaN_(u[2])) && r && this.tf && this.te()) {
                        o++;
                    }
                    else {
                        r = false;
                    }
                    a.f.add(u);
                    if (j) {
                        let v = Math.min(t * b.cs.b, this.cl.count - 1);
                        let w = this.r1.d(u[0], u[1], c, d);
                        let x = this.r1.e(u[0], u[1], c, d);
                        let y = new Rect(0, w - 5, x - 5, 11, 11);
                        if (!isNaN_(w) && !isNaN_(x) && !isInfinity(w) && !isInfinity(x) && m.tryAdd(y)) {
                            a.m.add({ $type: Point_$type, x: w, y: x });
                            let z = b.ct.item(k);
                            z.al = t;
                            z.ak = v;
                            z.content.item = this.cl.item(v);
                            ++k;
                        }
                    }
                }
                else {
                    if (r && this.tf && this.te()) {
                        o++;
                    }
                }
            }
            b.ct.count = k;
            return;
        }
        ti(a, b, c, d) {
            return this.r1.d(a, b, c, d);
        }
        tj(a, b, c, d) {
            return this.r1.e(a, b, c, d);
        }
        tg(a) {
            let b = a;
            return !this.tf && b.cs.c == 0 && b.cs.d == this.rt.le;
        }
        s5(a, b, c) {
            let d = null;
            let e = c.bv;
            let f = c.bu;
            if (b > -1 && !e.isEmpty && !f.isEmpty) {
                let g = a._inner[0][0] < f.left - 2000 ? f.left - 10 : NaN;
                let h = f.bottom + 10;
                let i = a._inner[b][0] > f.right + 2000 ? f.right + 10 : NaN;
                let j = f.top - 10;
                d = new Clipper(1, g, h, i, j, false);
            }
            return d;
        }
        ty(a, b, c, d, e) {
            if (a.count > 0 && d.count > 0) {
                if (!e || this.td()) {
                    a.add(this.ue);
                    a.add(a._inner[0]);
                    d.add(this.ue);
                    d.add(d._inner[0]);
                }
            }
        }
        te() {
            return !this.td();
        }
        td() {
            let a = this.b1.bv;
            return !a.containsPoint({ $type: Point_$type, x: 0.5, y: 0.5 });
        }
        k2(a, b) {
            super.k2(a, b);
            let c = b;
            if (a) {
                c.ct.clear();
            }
            c.cx.g$d();
        }
        sy(a, b) {
            let c = b.bv;
            let d = b.bu;
            let e = this.getEffectiveViewport1(b);
            let f = this.pz(b);
            let g = b;
            g.cx.g$g(a.p);
            CategoryMarkerManager.e(this, a.m, g.ct, this.qp);
            this.tv(b, a);
            g.cm();
            this.tu(d, c, e, f, g);
        }
        s4(a, b, c) {
            if (b < a || a < 0 || a > this.tc.count || b < 0 || b > this.tc.count || c == null) {
                return null;
            }
            let d = new Array((b - a) + 1);
            let e = c.cm;
            let f = null;
            if (e) {
                f = c.i$b;
            }
            for (let g = a; g <= b; g++) {
                let h = g;
                if (e) {
                    h = f._inner[h];
                }
                d[g - a] = this.cl.item(g);
            }
            return d;
        }
        tw(a, b, c, d, e, f) {
            let g = this.bl != 1;
            this.r7.a3(this.r7.d, d.cm, a, b, c, d, e, f, true);
            this.r7.aa(this.fv);
            let h = this.r7.ax;
            let i = null;
            let j = this.cl;
            let k = null;
            if (g && this.ce != null) {
                i = this.ce.a7.getHighlightingInfo(this, j, h.o, h.n, true);
                if (this.ce.a7.isCrossSeriesHighlight(this)) {
                    k = this.ce.a7.getHighlightingInfo(this, j, h.o, h.n, true);
                }
                h.b = i;
            }
            h.f = f;
            this.sw(h);
            this.r7.y(this, i, f, g, h.m, this.ce.a7.totalHighlightingProgress);
        }
        tx(a, b, c, d, e, f) {
            let g = this.bl != 1;
            this.r7.a3(this.r7.b, d.cm, a, b, c, d, e, f, false);
            this.r7.ab(this.fv);
            let h = this.r7.ay;
            let i = null;
            let j = this.cl;
            if (g && this.ce != null) {
                i = this.ce.a7.getHighlightingInfo(this, j, h.o, h.n, false);
                h.b = i;
            }
            h.f = f;
            this.sx(h);
            this.r7.z(this, i, f, g, b < 0);
        }
        tv(a, b) {
            a.b3 = true;
            this.r7.a1(this, this.si(), runOn(this, this.rq), this.sl(a), this.sm(a));
            let c = this.r7.d;
            let d = c != null;
            let e = this.rt;
            if (e == null) {
                return;
            }
            let f = this.getEffectiveViewport1(a);
            let g = new ScalerParams(0, a.bv, a.bu, e.cg, f);
            let h = this.cl == null ? 0 : this.cl.count;
            let i = a;
            let j = b.f;
            let k = i.cs.c;
            let l = i.cs.d;
            let m = i.cs.b;
            let n = k * m;
            for (let o = 0; o < a.ct.count; o++) {
                let p = a.ct.item(o);
                let q = p.content;
                if (d) {
                    let r = p.al;
                    this.tw(j, r, h, e, g, a.q);
                }
                this.r7.ac(p, q);
            }
        }
        tu(a, b, c, d, e) {
            e.cy(a, b, c, d);
        }
        m1() {
            super.m1();
            if (this.index < 0) {
                return;
            }
            this.s6.cz();
        }
        h4(a, b) {
            if (a == this.s6.cx.g$j) {
                return null;
            }
            return super.h4(a, b);
        }
        eq(a) {
            let b = this.b1.bv;
            b = b.copy();
            let c = this.b1.bu;
            let d = !b.isEmpty && !c.isEmpty && this.cl != null ? this.cl.indexOf(a) : -1;
            if (this.rt == null || this.tc == null || this.r0 == null) {
                return false;
            }
            if (d < 0 || d > this.tc.count - 1) {
                return false;
            }
            let e = this.rt.getScaledAngle(d);
            let f = this.r0.nt(this.tc.item(d));
            if (isNaN_(f)) {
                f = (this.r0.nm + this.r0.nn) / 2;
            }
            let g = 0.5 + (Math.cos(e) * f);
            let h = 0.5 + (Math.sin(e) * f);
            if (!isNaN_(g)) {
                if (g < b.left + 0.1 * b.width) {
                    g = g + 0.4 * b.width;
                    b.x = g - 0.5 * b.width;
                }
                if (g > b.right - 0.1 * b.width) {
                    g = g - 0.4 * b.width;
                    b.x = g - 0.5 * b.width;
                }
            }
            if (!isNaN_(h)) {
                if (h < b.top + 0.1 * b.height) {
                    h = h + 0.4 * b.height;
                    b.y = h - 0.5 * b.height;
                }
                if (h > b.bottom - 0.1 * b.height) {
                    h = h - 0.4 * b.height;
                    b.y = h - 0.5 * b.height;
                }
            }
            if (this.b9 != null) {
                this.b9.au(this.ce, b, true);
            }
            return d >= 0;
        }
        mp(a, b) {
            super.mp(a, b);
            if (!this.e3) {
                this.b1.a2(b);
                return;
            }
            this.b1.a2(b);
            let c = typeCast(AnchoredRadialSeriesView.$, this.b0);
            c.cs.e(this.fx);
            if (this.cy(this.b0)) {
                return;
            }
            if (!this.ex) {
                this.s9 = new RadialFrame(3);
                this.sv(this.s9, c);
            }
            this.ex = false;
            this.sy(this.s9, c);
            this.e3 = false;
        }
        mm(a, b, c, d, e) {
            super.mm(a, b, c, d, e);
            let f = this.a7.a$h.item(d);
            let g = f;
            g.cs.e(this.fx);
            f.a1(c);
            if (this.cy(g)) {
                return;
            }
            if (this.s8 == null) {
                this.s8 = new RadialFrame(3);
            }
            this.s8.u();
            this.sv(this.s8, g);
            this.sy(this.s8, g);
        }
        lc(a) {
            super.lc(a);
            this.a9.exportTrendlineData(this, a, this.s6.cx.g$j);
        }
    }
    AnchoredRadialSeries.$t = markType(AnchoredRadialSeries, 'AnchoredRadialSeries', RadialBase.$);
    AnchoredRadialSeries.tz = DependencyProperty.i("ActualTrendLineBrush", Brush.$, AnchoredRadialSeries.$, new PropertyMetadata(2, null, (a, b) => a.raisePropertyChanged("ActualTrendLineBrush", b.oldValue, b.newValue)));
    AnchoredRadialSeries.$$p = markDep(DependencyProperty, PropertyMetadata, AnchoredRadialSeries, 'raisePropertyChanged', ['ValueMemberAsLegendLabel:to:t7', [2, null], 'ValueMemberAsLegendUnit:tq:t8', [2, null], 'ValueMemberPath:ts:t9', [2, null], 'TrendLineType:ta:t5', [TrendLineType_$type, enumGetBox(TrendLineType_$type, 0)], 'TrendLineThickness:tk:t4', [1, 1.5], 'TrendLinePeriod:tl:t3', [1, 7], 'TrendLineZIndex:tm:t6', [1, 1], 'TrendLineBrush:ub:t0', [Brush.$, null], 'TrendLineDashArray:uc:t1', [DoubleCollection.$], 'TrendLineDashCap:ud:t2', [PenLineCap_$type, enumGetBox(PenLineCap_$type, 0)]]);
    return AnchoredRadialSeries;
})();
/**
 * @hidden
 */
export let AnchoredRadialSeriesView = /*@__PURE__*/ (() => {
    class AnchoredRadialSeriesView extends RadialBaseView {
        constructor(a) {
            super(a);
            this.cw = null;
            this.cx = null;
            this.cs = new AnchoredRadialBucketCalculator(this);
            this.cw = a;
            this.cx = new RadialTrendLineManager();
        }
        cy(a, b, c, d) {
        }
        cz() {
            this.cw.ua = null;
            if (this.cw.ub != null) {
                this.cw.ua = this.cw.ub;
            }
            else {
                this.cw.ua = this.cw.o5;
            }
        }
        a5(a, b) {
            if (a.d) {
                if (!b) {
                }
                if (this.cw.ta != 0 && !b) {
                    let c = this.cx.g$j;
                    c.ad = this.cw.tk;
                    c._stroke = this.cw.ua;
                    c.ai = this.cw.uc;
                    c.aj = this.cw.ud;
                    a.v(c);
                }
            }
            super.a5(a, b);
        }
        getDefaultTooltipTemplate() {
            let a = "<div class='ui-chart-default-tooltip-content'>";
            if (this.cw.rt.fz != null) {
                a += "<span>${item." + this.cw.rt.fz + "}</span><br/>";
            }
            a += "<span";
            let b = this.e.a7.a$r(this.e);
            if (!stringIsNullOrEmpty(b)) {
                a += " style='color:" + b + "'";
            }
            a += ">" + this.cw.title + ": </span><span class='ui-priority-primary'>" + "${item." + this.cw.ts + "}</span></div>";
            return a;
        }
    }
    AnchoredRadialSeriesView.$t = markType(AnchoredRadialSeriesView, 'AnchoredRadialSeriesView', RadialBaseView.$);
    return AnchoredRadialSeriesView;
})();
