/*
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 { AnnotationLayer } from "./AnnotationLayer";
import { Series } from "./Series";
import { DependencyProperty } from "igniteui-react-core/ES2015/DependencyProperty";
import { CategoryItemHighlightType_$type } from "./CategoryItemHighlightType";
import { DataTemplate } from "igniteui-react-core/ES2015/DataTemplate";
import { CategoryItemHighlightLayerFrame } from "./CategoryItemHighlightLayerFrame";
import { Type, Base, enumGetBox, fromEnum, typeCast, Point_$type, markType, runOn } from "igniteui-react-core/ES2015/type";
import { Dictionary$2 } from "igniteui-react-core/ES2015/Dictionary$2";
import { List$1 } from "igniteui-react-core/ES2015/List$1";
import { IHasCategoryModePreference_$type } from "./IHasCategoryModePreference";
import { Rect } from "igniteui-react-core/ES2015/Rect";
import { ItemHighlightFrameRect } from "./ItemHighlightFrameRect";
import { MarkerSeries } from "./MarkerSeries";
import { DataContext } from "igniteui-react-core/ES2015/DataContext";
import { Pool$1 } from "igniteui-react-core/ES2015/Pool$1";
import { ContentControl } from "igniteui-react-core/ES2015/ContentControl";
import { Rectangle } from "igniteui-react-core/ES2015/Rectangle";
import { PropertyMetadata } from "igniteui-react-core/ES2015/PropertyMetadata";
import { isNaN_, isInfinity, truncate } from "igniteui-react-core/ES2015/number";
import { AnnotationLayerView } from "./AnnotationLayerView";
import { Marker } from "./Marker";
import { BrushUtil } from "igniteui-react-core/ES2015/BrushUtil";
import { Color } from "igniteui-react-core/ES2015/Color";
import { DataTemplatePassInfo } from "igniteui-react-core/ES2015/DataTemplatePassInfo";
import { DataTemplateRenderInfo } from "igniteui-react-core/ES2015/DataTemplateRenderInfo";
import { DataTemplateMeasureInfo } from "igniteui-react-core/ES2015/DataTemplateMeasureInfo";
export class CategoryItemHighlightLayer extends AnnotationLayer {
    constructor() {
        super();
        this.mv = false;
        this.mu = true;
        this.my = 10;
        let a = new CategoryItemHighlightLayerFrame();
        let b = new CategoryItemHighlightLayerFrame();
        let c = new CategoryItemHighlightLayerFrame();
        this.lq = a;
        this.lp = b;
        this.lr = c;
        this.ab = CategoryItemHighlightLayer.$;
    }
    bf() {
        return new CategoryItemHighlightLayerView(this);
    }
    ii(a) {
        super.ii(a);
        this.mq = a;
    }
    l7(a, b) {
        if (this.mt == null || this.mt == a) {
            if (!this.aq.c) {
                this.i1(b);
            }
        }
    }
    lu(a) {
        if (!super.lu(a)) {
            return false;
        }
        let b = this.l2(a);
        if (!b.a) {
            return false;
        }
        return true;
    }
    l9(a, b) {
        super.l9(a, b);
        let c = a;
        let d = this.mx;
        c.g.clear();
        if (isNaN_(this.mn.x) && isNaN_(this.mn.y)) {
            return;
        }
        if (this.lu(this.mt)) {
            this.m8(this.mt, c, d);
        }
        else {
            for (let e of fromEnum(this.bs.series)) {
                if (this.lu(e)) {
                    this.m8(e, c, d);
                }
            }
        }
    }
    io(a, b, c, d) {
        super.io(a, b, c, d);
        switch (b) {
            case CategoryItemHighlightLayer.$$p[4]:
                this.i1(true);
                break;
            case CategoryItemHighlightLayer.$$p[5]:
                this.mv = d;
                this.i1(true);
                break;
            case CategoryItemHighlightLayer.$$p[1]:
                this.i1(true);
                break;
            case CategoryItemHighlightLayer.$$p[0]:
                this.my = this.mz;
                this.i1(true);
                break;
            case CategoryItemHighlightLayer.$$p[3]:
                this.mu = d;
                this.i1(true);
                break;
        }
    }
    m8(a, b, c) {
        if (this.bi == null || this.bi.bq.isEmpty || this.bi.bp.isEmpty) {
            return;
        }
        if (!a.ck && !a.cq) {
            return;
        }
        let d = a;
        if (d.categoryAxis == null) {
            return;
        }
        let e = this.mn.x;
        e = this.bi.bp.left + this.bi.bp.width * (e - this.bi.bq.left) / this.bi.bq.width;
        let f = this.mn.y;
        f = this.bi.bp.top + this.bi.bp.height * (f - this.bi.bq.top) / this.bi.bq.height;
        let g = this.mw;
        let h = true;
        let i = typeCast(IHasCategoryModePreference_$type, a);
        let j = 0;
        if (i != null) {
            j = i.preferredCategoryMode(d.categoryAxis);
            if (j == 2) {
                h = false;
            }
        }
        let k = true;
        if (!h || this.mr == 2) {
            k = false;
        }
        if (this.mr == 1) {
            k = true;
        }
        let l = this.l2(a);
        if (!l.a) {
            return;
        }
        if (k) {
            let m = a.k9(l.b, this.mx, g);
            if (j != 0 && !d.categoryAxis.cc) {
                let n = m.x;
                if (a.cq) {
                    n -= a.getOffsetValue();
                    n += a.getCategoryWidth() * 0.5;
                    m = { $type: Point_$type, x: n, y: m.y };
                }
                else if (a.ck) {
                    n -= a.getOffsetValue();
                    n += a.getCategoryWidth() * 0.5;
                    m = { $type: Point_$type, x: n, y: m.y };
                }
            }
            if (!isNaN_(m.x) && !isNaN_(m.y)) {
                m = this.lc(m);
            }
            this.m6(m, b, a);
        }
        else {
            let o = a.k9(l.b, this.mx, g);
            let p;
            let q = this.getEffectiveViewport1(this.bi);
            if (a.isVertical) {
                let r = o.y;
                let s = d.categoryAxis;
                let t = this.my;
                if (j != 0) {
                    t = s.jj(this.bi.bq, this.bi.bp, q);
                }
                if (isNaN_(t) || isInfinity(t)) {
                    return;
                }
                p = new Rect(0, this.lm.left, r - t / 2, this.lm.width, t);
            }
            else {
                let u = o.x;
                let v = d.categoryAxis;
                let w = this.my;
                if (j != 0) {
                    w = v.jj(this.bi.bq, this.bi.bp, q);
                }
                if (isNaN_(w) || isInfinity(w)) {
                    return;
                }
                p = new Rect(0, u - w / 2, this.lm.top, w, this.lm.height);
            }
            p.intersect(this.bi.bp);
            this.m7(p, b, a);
        }
    }
    m6(a, b, c) {
        let d = new ItemHighlightFrameRect();
        d.b = a.x;
        d.e = a.y;
        d.i = this.ml();
        d.j = this.mm();
        if (d.i == null) {
            if (c.hasMarkers) {
                d.i = this.mq.cd(c.ku());
            }
            else {
                d.i = this.mq.cd(c.kr);
            }
        }
        if (d.j == null) {
            if (c.hasMarkers) {
                d.j = this.mq.cd(c.kv());
            }
            else {
                d.j = this.mq.cd(c.ks);
            }
        }
        d.d = this.es;
        d.f = Base.getHashCodeStatic(c);
        d.g = this.ms(c, a);
        let e = (typeCast(MarkerSeries.$, c) !== null) ? c.l6 : null;
        if (e == null || e == MarkerSeries.l9) {
            let f = MarkerSeries.lq(c, 2);
            switch (f) {
                case 3:
                    e = this.bs.ki;
                    break;
                case 4:
                    e = this.bs.kr;
                    break;
                case 5:
                    e = this.bs.ko;
                    break;
                case 6:
                    e = this.bs.kp;
                    break;
                case 7:
                    e = this.bs.kj;
                    break;
                case 8:
                    e = this.bs.km;
                    break;
                case 9:
                    e = this.bs.kk;
                    break;
                case 10:
                    e = this.bs.kq;
                    break;
                case 11:
                    e = this.bs.kn;
                    break;
                case 12:
                    e = this.bs.kl;
                    break;
            }
        }
        d.h = this.nb != null ? this.nb : e;
        if (!isNaN_(d.b) && !isNaN_(d.e)) {
            b.g.addItem(d.f, d);
        }
    }
    ms(a, b) {
        let c = a.fj(b);
        let d = a.d6(b);
        if (this.mx && d != Math.floor(d)) {
            c = null;
        }
        return ((() => {
            let $ret = new DataContext();
            $ret.series = a;
            $ret.item = c;
            return $ret;
        })());
    }
    m7(a, b, c) {
        let d = new ItemHighlightFrameRect();
        d.b = a.left;
        d.e = a.top;
        d.c = a.right;
        d.a = a.bottom;
        d.i = this.ml();
        d.j = this.mm();
        if (d.i == null) {
            if (c != null) {
                d.i = this.mq.ce(c.kr);
            }
        }
        if (d.j == null) {
            if (c != null) {
                d.j = this.mq.ce(c.ks);
            }
        }
        d.d = this.es;
        d.f = Base.getHashCodeStatic(c);
        if (!a.isEmpty) {
            b.g.addItem(d.f, d);
        }
    }
    mc(a, b) {
        super.mc(a, b);
        let c = this.mq.b1;
        let d = this.mq.b0;
        let e = 0;
        let f = 0;
        let g = a;
        for (let h of fromEnum(g.g.values)) {
            if (h.h != null) {
                let i = d.item(f);
                this.m9(h, i);
                f++;
            }
            else {
                let j = c.item(e);
                this.na(h, j);
                e++;
            }
        }
        c.count = e;
        d.count = f;
    }
    m9(a, b) {
        if (isNaN_(a.b) || isNaN_(a.e) || isNaN_(a.d) || a.g == null) {
            b._visibility = 1;
            return;
        }
        let c = { $type: Point_$type, x: a.b, y: a.e };
        let d = a.g;
        d.itemBrush = a.i;
        let e = d.series.k9(c, this.ca() || this.mv, this.mu);
        if (isNaN_(e.x) || isNaN_(e.y)) {
            b._visibility = 1;
            return;
        }
        this.mq.b7(b, e.x, e.y);
        b.ah = a.h;
        b.content = a.g;
    }
    na(a, b) {
        if (isNaN_(a.b) || isNaN_(a.e) || isNaN_(a.a) || isNaN_(a.c) || isNaN_(a.d)) {
            b._visibility = 1;
            return;
        }
        this.mq.b8(b, a.b, a.e, a.c - a.b, a.a - a.e);
        b._fill = a.i;
        b._stroke = a.j;
        b.ac = a.d;
        if (this.kz != null) {
            b.ai = this.kz;
        }
        else {
            b.ai = null;
        }
        b.ad = this.k0;
    }
    c5() {
        return true;
    }
}
CategoryItemHighlightLayer.$t = markType(CategoryItemHighlightLayer, 'CategoryItemHighlightLayer', AnnotationLayer.$);
Type.dep(DependencyProperty, PropertyMetadata, CategoryItemHighlightLayer, 'raisePropertyChanged', ['BandHighlightWidth:mz:nc', [1, 10], 'HighlightType:mr:nd', [CategoryItemHighlightType_$type, enumGetBox(CategoryItemHighlightType_$type, 0)], 'MarkerTemplate:nb:ne', [DataTemplate.$, null], 'SkipUnknownValues:mw:nf', [0, true], 'TargetSeries:mt:ng', [Series.$, null], 'UseInterpolation:mx:nh', [0, false]]);
export class CategoryItemHighlightLayerView extends AnnotationLayerView {
    constructor(a) {
        super(a);
        this.bz = null;
        this.b1 = null;
        this.b0 = null;
        this.b2 = null;
        this.b3 = null;
        this.bz = a;
        this.b2 = new List$1(ContentControl.$, 0);
        this.b3 = new List$1(Rectangle.$, 0);
    }
    ap() {
        super.ap();
        this.b1 = ((() => {
            let $ret = new Pool$1(Rectangle.$);
            $ret.create = runOn(this, this.cf);
            $ret.activate = runOn(this, this.b9);
            $ret.disactivate = runOn(this, this.cb);
            $ret.destroy = runOn(this, this.ca);
            return $ret;
        })());
        this.b0 = ((() => {
            let $ret = new Pool$1(ContentControl.$);
            $ret.create = runOn(this, this.cc);
            $ret.activate = runOn(this, this.b4);
            $ret.disactivate = runOn(this, this.b6);
            $ret.destroy = runOn(this, this.b5);
            return $ret;
        })());
    }
    cf() {
        let a = new Rectangle();
        a.ab = false;
        this.b3.add(a);
        return a;
    }
    b9(a) {
        a._visibility = 0;
    }
    cb(a) {
        a._visibility = 1;
    }
    ca(a) {
        this.b3.remove(a);
    }
    cc() {
        let a = new Marker();
        this.b2.add(a);
        return a;
    }
    b4(a) {
        a._visibility = 0;
    }
    b6(a) {
        a._visibility = 1;
    }
    b5(a) {
        this.b2.remove(a);
    }
    cd(a) {
        if (a == null) {
            return a;
        }
        return BrushUtil.h(a, 0.3);
    }
    ce(a) {
        if (a != null) {
            let b = BrushUtil.h(a, 0.3);
            if (b.isGradient) {
                let c = b;
                if (c.gradientStops != null) {
                    for (let d = 0; d < c.gradientStops.length; d++) {
                        let e = c.gradientStops[d];
                        e.color = Color.u(truncate(Math.round((e.color.l) * 0.3)), e.color.o, e.color.n, e.color.m);
                        e.color = e.color;
                    }
                }
            }
            else {
                b.color = Color.u(truncate(Math.round((b.color.l & 255) * 0.3)), b.color.o, b.color.n, b.color.m);
                b.color = b.color;
            }
            return b;
        }
        else {
            return null;
        }
    }
    b8(a, b, c, d, e) {
        a._visibility = 0;
        a.n = b;
        a.o = c;
        a.width = d;
        a.height = e;
    }
    b7(a, b, c) {
        a.n = b;
        a.o = c;
    }
    be(a, b) {
        super.be(a, b);
        let c = a;
        let d = c.content;
        if (d != null) {
            if (d.itemBrush != null) {
                d.actualItemBrush = d.itemBrush;
            }
            let e = d.series;
            if (e.hasMarkers) {
                d.outline = e.kv();
            }
            d.thickness = 0.5;
        }
    }
    a3(a, b) {
        super.a3(a, b);
        if (a.d && !b) {
            for (let c = 0; c < this.b3.count; c++) {
                let d = this.b3._inner[c];
                if (d._visibility == 0) {
                    a.v(d);
                }
            }
            if (this.b2.count > 0) {
                let e = new DataTemplatePassInfo();
                e.isHitTestRender = b;
                e.context = a.g();
                e.viewportTop = this.bp.top;
                e.viewportLeft = this.bp.left;
                e.viewportWidth = this.bp.width;
                e.viewportHeight = this.bp.height;
                e.passID = "ItemMarkers";
                let f = new DataTemplateRenderInfo();
                f.isHitTestRender = b;
                f.passInfo = e;
                let g = new DataTemplateMeasureInfo();
                g.passInfo = e;
                let h = false;
                let i = a.g();
                g.context = i;
                f.context = i;
                g.renderContext = a;
                f.renderContext = a;
                let j = 0;
                let k = 0;
                let l = new Dictionary$2(DataTemplate.$, DataTemplate.$, 0);
                for (let m = 0; m < this.b2.count; m++) {
                    let n = this.b2._inner[m];
                    let o = n.ah;
                    if (!l.containsKey(o)) {
                        l.addItem(o, o);
                        if (o != null && o.passStarting != null) {
                            o.passStarting(e);
                        }
                    }
                    if (n._visibility == 1) {
                        continue;
                    }
                    this.bd(n, m, b);
                    if (!h) {
                        g.data = n.content;
                        g.width = n.width;
                        g.height = n.height;
                        if (o.measure != null) {
                            g.data = n.content;
                            o.measure(g);
                            h = g.isConstant;
                            if (h) {
                                j = g.width;
                                k = g.height;
                            }
                        }
                        f.availableWidth = g.width;
                        f.availableHeight = g.height;
                    }
                    else {
                        f.availableWidth = j;
                        f.availableHeight = k;
                    }
                    if (!isNaN_(n.width) && !isInfinity(n.width)) {
                        f.availableWidth = n.width;
                    }
                    if (!isNaN_(n.height) && !isInfinity(n.height)) {
                        f.availableHeight = n.height;
                    }
                    a.p(f, n);
                    n.m = f.availableWidth;
                    n.l = f.availableHeight;
                }
                for (let p of fromEnum(l.keys)) {
                    if (p != null && p.passCompleted != null) {
                        p.passCompleted(e);
                    }
                }
            }
        }
    }
    ai(a) {
        super.ai(a);
        for (let b = 0; b < this.b3.count; b++) {
            this.e.a2.exportRectangleData(a, this.b3._inner[b], "catItemRect", ["Main", "CategoryItem"]);
        }
        for (let c = 0; c < this.b2.count; c++) {
            this.e.a2.exportMarkerVisualDataFromMarker(this.e, a, this.b2._inner[c]);
        }
    }
}
CategoryItemHighlightLayerView.$t = markType(CategoryItemHighlightLayerView, 'CategoryItemHighlightLayerView', AnnotationLayerView.$);
/*<BeginStatics Name="Infragistics.Controls.Charts.CategoryItemHighlightLayer" />*/
/*<EndStatics Name="Infragistics.Controls.Charts.CategoryItemHighlightLayer" />*/
