/*
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.s3;
            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.sy = null;
            this.s2 = null;
            this.t5 = { $type: Point_$type, x: 0, y: 0 };
            this.s0 = null;
            this.sz = null;
            this.s0 = new RadialFrame(3);
            this.sy = new CategoryLineRasterizer();
        }
        bx() {
            return new AnchoredRadialSeriesView(this);
        }
        ku() {
            super.ku();
            this.rk = null;
            this.rr = null;
        }
        hw(a, b) {
            let c = this.jq(b);
            if (c == AnchoredRadialSeries.$$p[2]) {
                return this.hx(a, b, this.tj);
            }
            return super.hw(a, b);
        }
        ly(a) {
            super.ly(a);
            this.sx = a;
        }
        hj(a, b) {
            return this.hk(a, b, this.rk, null, this.s3);
        }
        hh(a, b) {
            return this.hi(a, b, this.rk, null, this.s3);
        }
        f3(a, b, c, d, e) {
            if (this.s3 == null) {
                return Number.POSITIVE_INFINITY;
            }
            return this.f4(a, b, this.rk, d, e, this.s3.count, null);
        }
        gj(a, b, c) {
            let d = this.getEffectiveViewport1(this.b0);
            let e = this.s8(this.rk, this.b0.bv, this.b0.bu, d);
            let f = new ScalerParams(0, this.b0.bv, this.b0.bu, this.rk.cg, d);
            f.c = this.cd.su;
            return this.gl(this.s3, a, this.rk, f, e, null, b, c);
        }
        pl(a, b, c) {
            if (this.rr == null || this.rk == null || this.b0 == null || this.b0.bv.isEmpty || this.b0.bu.isEmpty) {
                return { $type: Point_$type, x: NaN, y: NaN };
            }
            let d = this.getEffectiveViewport1(this.b0);
            let e = new ScalerParams(0, this.b0.bv, this.b0.bu, this.rr.cg, d);
            e.c = this.getEffectiveViewport1(this.b0);
            let f = this.gj(a, b, c);
            f = this.rr.ei(f, e);
            let g = this.rs.c(a);
            if (!b && this.rk != null) {
                let h = this.hj(a, c);
                let i = this.hh(a, c);
                let j = this.s8(this.rk, this.b0.bv, this.b0.bu, d);
                let k = new ScalerParams(0, this.b0.bv, this.b0.bu, this.rk.cg, d);
                k.c = this.cd.su;
                let l = this.f3(a, h, this.rk, k, j);
                let m = this.f3(a, i, this.rk, k, j);
                if (m <= l) {
                    g = this.rk.ei(i, k) + j;
                }
                else {
                    g = this.rk.ei(h, k) + j;
                }
            }
            let n = this.rs.d(g, f, this.b0.bv, this.b0.bu);
            let o = this.rs.e(g, f, this.b0.bv, this.b0.bu);
            return { $type: Point_$type, x: n, y: o };
        }
        get s3() {
            return this.s2;
        }
        set s3(a) {
            if (this.s2 != a) {
                let b = this.s2;
                this.s2 = a;
                this.raisePropertyChanged("ValueColumn", b, this.s2);
            }
        }
        getDataLegendSeriesContextAt(a, b) {
            let c = new DataLegendSeriesValueInfo();
            c.a = 0;
            c.l = this.gj({ $type: Point_$type, x: a, y: b }, false, false);
            c.p = this.tj != null ? this.tj : "Value";
            c.q = this.tf != null ? this.tf : "V:";
            c.r = this.th != null ? this.th : "";
            let d = new DataLegendSeriesContext();
            d.f = ((() => {
                let $ret = new List$1(DataLegendSeriesValueInfo.$, 0);
                $ret.add(c);
                return $ret;
            })());
            d.b = this.av();
            return d;
        }
        get t1() {
            return this.c(AnchoredRadialSeries.tq);
        }
        set t1(a) {
            this.h(AnchoredRadialSeries.tq, a);
        }
        ap(a) {
            if (a != null && a == this.rk && this.s3 != null && this.s3.count > 0) {
                return new AxisRange(0, this.s3.count - 1);
            }
            if (a != null && a == this.rr && this.s3 != null && this.s3.count > 0) {
                return new AxisRange(this.s3.minimum, this.s3.maximum);
            }
            return null;
        }
        l3(a, b, c, d) {
            super.l3(a, b, c, d);
            if (this.sx.cx.g$b(a, b, c, d)) {
                this.mh(false);
                this.lo();
            }
            let e = typeCast(NumericAxisBase.$, this.rr);
            switch (b) {
                case "FastItemsSource":
                    if (typeCast(IFastItemsSource_$type, c) !== null) {
                        c.deregisterColumn(this.s3);
                        this.s3 = null;
                    }
                    if (typeCast(IFastItemsSource_$type, d) !== null) {
                        this.s3 = this.ch(this.tj);
                        this.sx.cs.e(this.ft);
                    }
                    if (e != null && !e.c0()) {
                        this.sx.cs.e(this.ft);
                        this.mh(false);
                    }
                    break;
                case AnchoredRadialSeries.$$p[2]:
                    if (this.ck != null) {
                        this.ck.deregisterColumn(this.s3);
                        this.s3 = this.ch(this.tj);
                    }
                    break;
                case "ValueColumn":
                    if (e != null && !e.c0()) {
                        this.sx.cs.e(this.ft);
                        this.mh(false);
                    }
                    break;
                case "TrendLineBrush":
                    this.mv();
                    break;
                case "TrendLineType":
                    this.lo();
                    break;
            }
        }
        k0(a, b, c, d) {
            switch (a) {
                case 4:
                case 1:
                case 0:
                    this.sx.cs.e(this.ft);
                    break;
            }
            this.sx.cx.g$e(a, b, c, d);
            switch (a) {
                case 4:
                    if (this.rr != null && !this.rr.c0()) {
                        this.mh(true);
                    }
                    break;
                case 1:
                    if (this.rr != null && !this.rr.c0()) {
                        this.mh(true);
                    }
                    break;
                case 0:
                    if (this.rr != null && !this.rr.c0()) {
                        this.mh(true);
                    }
                    break;
                case 2:
                    if (this.tj != null && this.sx.cs.b > 0 && this.rr != null && !this.rr.c0()) {
                        this.mh(true);
                    }
                    break;
                case 3:
                    if (d == this.tj) {
                        if (this.rr != null && !this.rr.c0()) {
                            this.mh(true);
                        }
                    }
                    break;
            }
        }
        e9(a, b, c) {
            let d = super.e9(a, b, c);
            let e = c;
            if (this.ck == null || this.ck.count == 0 || this.rk == null || this.s3 == null || this.rk.le == 0 || this.rr == null || isInfinity(this.rr.lv) || isInfinity(this.rr.lu) || e.cs.b < 1) {
                d = false;
            }
            return d;
        }
        getOffsetValue() {
            return this.s8(this.rk, this.b0.bv, this.b0.bu, this.getEffectiveViewport1(this.b0));
        }
        getCategoryWidth() {
            return this.rk.getCategorySize(this.b0.bv, this.b0.bu, this.getEffectiveViewport1(this.b0));
        }
        get_s6() {
            return false;
        }
        get s6() {
            return this.get_s6();
        }
        get_c6() {
            return true;
        }
        get_ej() {
            return this.rr != null && this.rr.cg;
        }
        s8(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.se(), b, c, d);
                    break;
            }
            if (a.cg) {
                e = -e;
            }
            return e;
        }
        sm(a, b) {
            let c = b.bv;
            let d = b.bu;
            let e = this.getEffectiveViewport1(b);
            let f = this.rk;
            let g = this.rr;
            let h = g.lv;
            let i = g.lu;
            a.f.clear();
            a.m.clear();
            a.p.clear();
            let j = this.qe();
            let k = 0;
            let l = b;
            let m = new CollisionAvoider();
            let n = 0;
            let o = b.cs.d;
            n = this.s8(f, c, d, e);
            l.cx.i = this.rr.nm;
            l.cx.h = this.rr.nl;
            l.cx.j = (p, q) => this.s9(p, q, c, d);
            l.cx.k = (p, q) => this.ta(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.ft;
                $ret.f = d;
                $ret.g = c;
                return $ret;
            })());
            l.cx.l(a.p, this.s1, this.s3, this.tc, (r) => this.rk.getScaledAngle(r), (r) => this.rr.ns(r), q, p);
            let r = true;
            if (this.s7(b)) {
                o--;
            }
            for (let s = b.cs.c; s <= o; ++s) {
                let t = s;
                if (t * b.cs.b >= this.rk.le) {
                    t -= intDivide((this.rk.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.ns(u[1]);
                    if (b.cs.b > 1) {
                        if (u[2] < h || u[2] > i) {
                            continue;
                        }
                        u[2] = g.ns(u[2]);
                    }
                    else {
                        u[2] = u[1];
                    }
                    if ((isNaN_(u[1]) || isNaN_(u[2])) && r && this.s6 && this.s5()) {
                        o++;
                    }
                    else {
                        r = false;
                    }
                    a.f.add(u);
                    if (j) {
                        let v = Math.min(t * b.cs.b, this.ck.count - 1);
                        let w = this.rs.d(u[0], u[1], c, d);
                        let x = this.rs.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.ck.item(v);
                            ++k;
                        }
                    }
                }
                else {
                    if (r && this.s6 && this.s5()) {
                        o++;
                    }
                }
            }
            b.ct.count = k;
            return;
        }
        s9(a, b, c, d) {
            return this.rs.d(a, b, c, d);
        }
        ta(a, b, c, d) {
            return this.rs.e(a, b, c, d);
        }
        s7(a) {
            let b = a;
            return !this.s6 && b.cs.c == 0 && b.cs.d == this.rk.le;
        }
        sw(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;
        }
        tp(a, b, c, d, e) {
            if (a.count > 0 && d.count > 0) {
                if (!e || this.s4()) {
                    a.add(this.t5);
                    a.add(a._inner[0]);
                    d.add(this.t5);
                    d.add(d._inner[0]);
                }
            }
        }
        s5() {
            return !this.s4();
        }
        s4() {
            let a = this.b0.bv;
            return !a.containsPoint({ $type: Point_$type, x: 0.5, y: 0.5 });
        }
        kx(a, b) {
            super.kx(a, b);
            let c = b;
            if (a) {
                c.ct.clear();
            }
            c.cx.g$d();
        }
        sp(a, b) {
            let c = b.bv;
            let d = b.bu;
            let e = this.getEffectiveViewport1(b);
            let f = this.pq(b);
            let g = b;
            g.cx.g$g(a.p);
            CategoryMarkerManager.e(this, a.m, g.ct, this.qg);
            this.tm(b, a);
            g.cm();
            this.tl(d, c, e, f, g);
        }
        sv(a, b, c) {
            if (b < a || a < 0 || a > this.s3.count || b < 0 || b > this.s3.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.ck.item(g);
            }
            return d;
        }
        tn(a, b, c, d, e, f) {
            let g = this.bk != 1;
            this.ry.a1(this.ry.d, d.cm, a, b, c, d, e, f, true);
            this.ry.aa(this.fr);
            let h = this.ry.av;
            let i = null;
            let j = this.ck;
            let k = null;
            if (g && this.cd != null) {
                i = this.cd.a3.getHighlightingInfo(this, j, h.o, h.n, true);
                if (this.cd.a3.isCrossSeriesHighlight(this)) {
                    k = this.cd.a3.getHighlightingInfo(this, j, h.o, h.n, true);
                }
                h.b = i;
            }
            h.f = f;
            this.sn(h);
            this.ry.y(this, i, f, g, h.m, this.cd.a3.totalHighlightingProgress);
        }
        to(a, b, c, d, e, f) {
            let g = this.bk != 1;
            this.ry.a1(this.ry.b, d.cm, a, b, c, d, e, f, false);
            this.ry.ab(this.fr);
            let h = this.ry.aw;
            let i = null;
            let j = this.ck;
            if (g && this.cd != null) {
                i = this.cd.a3.getHighlightingInfo(this, j, h.o, h.n, false);
                h.b = i;
            }
            h.f = f;
            this.so(h);
            this.ry.z(this, i, f, g, b < 0);
        }
        tm(a, b) {
            a.b3 = true;
            this.ry.az(this, this.r9(), runOn(this, this.rh), this.sc(a), this.sd(a));
            let c = this.ry.d;
            let d = c != null;
            let e = this.rk;
            if (e == null) {
                return;
            }
            let f = this.getEffectiveViewport1(a);
            let g = new ScalerParams(0, a.bv, a.bu, e.cg, f);
            let h = this.ck == null ? 0 : this.ck.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.tn(j, r, h, e, g, a.q);
                }
                this.ry.ac(p, q);
            }
        }
        tl(a, b, c, d, e) {
            e.cy(a, b, c, d);
        }
        mv() {
            super.mv();
            if (this.index < 0) {
                return;
            }
            this.sx.cz();
        }
        hz(a, b) {
            if (a == this.sx.cx.g$j) {
                return null;
            }
            return super.hz(a, b);
        }
        en(a) {
            let b = this.b0.bv;
            b = b.copy();
            let c = this.b0.bu;
            let d = !b.isEmpty && !c.isEmpty && this.ck != null ? this.ck.indexOf(a) : -1;
            if (this.rk == null || this.s3 == null || this.rr == null) {
                return false;
            }
            if (d < 0 || d > this.s3.count - 1) {
                return false;
            }
            let e = this.rk.getScaledAngle(d);
            let f = this.rr.ns(this.s3.item(d));
            if (isNaN_(f)) {
                f = (this.rr.nl + this.rr.nm) / 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.b8 != null) {
                this.b8.au(this.cd, b, true);
            }
            return d >= 0;
        }
        mj(a, b) {
            super.mj(a, b);
            if (!this.ez) {
                this.b0.a2(b);
                return;
            }
            this.b0.a2(b);
            let c = typeCast(AnchoredRadialSeriesView.$, this.bz);
            c.cs.e(this.ft);
            if (this.cx(this.bz)) {
                return;
            }
            if (!this.et) {
                this.s0 = new RadialFrame(3);
                this.sm(this.s0, c);
            }
            this.et = false;
            this.sp(this.s0, c);
            this.ez = false;
        }
        mg(a, b, c, d, e) {
            super.mg(a, b, c, d, e);
            let f = this.a6.a$h.item(d);
            let g = f;
            g.cs.e(this.ft);
            f.a1(c);
            if (this.cx(g)) {
                return;
            }
            if (this.sz == null) {
                this.sz = new RadialFrame(3);
            }
            this.sz.u();
            this.sm(this.sz, g);
            this.sp(this.sz, g);
        }
        k7(a) {
            super.k7(a);
            this.a8.exportTrendlineData(this, a, this.sx.cx.g$j);
        }
    }
    AnchoredRadialSeries.$t = markType(AnchoredRadialSeries, 'AnchoredRadialSeries', RadialBase.$);
    AnchoredRadialSeries.tq = 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:tf:ty', [2, null], 'ValueMemberAsLegendUnit:th:tz', [2, null], 'ValueMemberPath:tj:t0', [2, null], 'TrendLineType:s1:tw', [TrendLineType_$type, enumGetBox(TrendLineType_$type, 0)], 'TrendLineThickness:tb:tv', [1, 1.5], 'TrendLinePeriod:tc:tu', [1, 7], 'TrendLineZIndex:td:tx', [1, 1], 'TrendLineBrush:t2:tr', [Brush.$, null], 'TrendLineDashArray:t3:ts', [DoubleCollection.$], 'TrendLineDashCap:t4:tt', [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.t1 = null;
            if (this.cw.t2 != null) {
                this.cw.t1 = this.cw.t2;
            }
            else {
                this.cw.t1 = this.cw.oy;
            }
        }
        a5(a, b) {
            if (a.d) {
                if (!b) {
                }
                if (this.cw.s1 != 0 && !b) {
                    let c = this.cx.g$j;
                    c.ac = this.cw.tb;
                    c._stroke = this.cw.t1;
                    c.ai = this.cw.t3;
                    c.ad = this.cw.t4;
                    a.u(c);
                }
            }
            super.a5(a, b);
        }
        getDefaultTooltipTemplate() {
            let a = "<div class='ui-chart-default-tooltip-content'>";
            if (this.cw.rk.fz != null) {
                a += "<span>${item." + this.cw.rk.fz + "}</span><br/>";
            }
            a += "<span";
            let b = this.e.a6.a$r(this.e);
            if (!stringIsNullOrEmpty(b)) {
                a += " style='color:" + b + "'";
            }
            a += ">" + this.cw.title + ": </span><span class='ui-priority-primary'>" + "${item." + this.cw.tj + "}</span></div>";
            return a;
        }
    }
    AnchoredRadialSeriesView.$t = markType(AnchoredRadialSeriesView, 'AnchoredRadialSeriesView', RadialBaseView.$);
    return AnchoredRadialSeriesView;
})();
