/*
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 { Base, markType, PointUtil } from "igniteui-webcomponents-core";
import { INativeCell_$type } from "./INativeCell";
import { RenderingContext } from "igniteui-webcomponents-core";
import { Rectangle } from "igniteui-webcomponents-core";
import { Line } from "igniteui-webcomponents-core";
import { TextBlock } from "igniteui-webcomponents-core";
import { Color } from "igniteui-webcomponents-core";
import { FontInfo } from "igniteui-webcomponents-core";
import { Brush } from "igniteui-webcomponents-core";
import { CanvasViewRenderer } from "igniteui-webcomponents-core";
import { CanvasPlaceholderView } from "./CanvasPlaceholderView";
import { DeviceUtils } from "igniteui-webcomponents-core";
import { Random } from "igniteui-webcomponents-core";
import { FontUtil } from "igniteui-webcomponents-core";
import { truncate, isNaN_ } from "igniteui-webcomponents-core";
/**
 * @hidden
 */
export let CanvasGridCellBase = /*@__PURE__*/ (() => {
    class CanvasGridCellBase extends Base {
        constructor(a) {
            super();
            this.a = null;
            this.r = true;
            this.o = true;
            this.k = null;
            this.bk = null;
            this.bx = null;
            this.l = false;
            this.ao = 1;
            this.as = 5;
            this.au = 5;
            this.at = 5;
            this.ar = 5;
            this.cu = null;
            this.ag = 0;
            this.ai = 0;
            this.ah = 0;
            this.af = 0;
            this.c2 = new Rectangle();
            this.cy = new Line();
            this.c1 = new Line();
            this.cz = new Line();
            this.cx = new Line();
            this.c0 = new Line();
            this.ay = 0;
            this.a0 = 0;
            this.a1 = 0;
            this.a2 = 0;
            this.bm = null;
            this.j = null;
            this.ak = 0;
            this.aj = 0;
            this.am = 0;
            this.al = 0;
            this.t = true;
            this.az = NaN;
            this.ct = new TextBlock();
            this.cw = Color.u(255, 0, 0, 0);
            this.s = true;
            this.aq = 0;
            this.a3 = 0;
            this.cv = Color.u(255, 255, 255, 255);
            this.m = false;
            this.g = null;
            this.c = 0;
            this.e = 0;
            this.bw = 1;
            this.q = false;
            this.ax = 0;
            this.aw = 0;
            this.i = ((() => {
                let $ret = new FontInfo();
                $ret.n = "Verdana";
                $ret.d = 12;
                return $ret;
            })());
            this.p = true;
            this.bp = -1;
            this.bu = 0;
            this.bv = 0;
            this.n = false;
            this.bn = 0;
            this.b = 0;
            this.ap = NaN;
            this.h = null;
            this.br = -1;
            this.an = NaN;
            this.f = 1;
            this.av = 1;
            this.bk = a;
        }
        get y() {
            return this.o;
        }
        set y(a) {
            this.o = a;
        }
        ci(a) {
            this.k = a;
        }
        b3() {
            this.o = true;
            if (this.k != null) {
                this.k();
            }
        }
        get isCanvasBased() {
            return true;
        }
        get bl() {
            return this.bk;
        }
        ck(a) {
            this.bx = a;
            this.o = true;
            this.r = true;
        }
        get templateContext() {
            return this.j.g();
        }
        get u() {
            return this.l;
        }
        set u(a) {
            this.l = a;
        }
        get_aa() {
            return false;
        }
        get aa() {
            return this.get_aa();
        }
        ca(a, b, c, d, e) {
            this.o = true;
            this.cu = a;
            this.ag = b;
            this.ai = c;
            this.ah = d;
            this.af = e;
            this.r = true;
        }
        cb(a) {
            this.o = true;
            this.ao = a;
            this.ct._opacity = this.ao;
        }
        get ba() {
            return this.as;
        }
        get bc() {
            return this.au;
        }
        get bb() {
            return this.at;
        }
        get a9() {
            return this.ar;
        }
        get a5() {
            return this.ag;
        }
        get a7() {
            return this.ai;
        }
        get a6() {
            return this.ah;
        }
        get a4() {
            return this.af;
        }
        get be() {
            return this.ay;
        }
        get bg() {
            return this.a0;
        }
        get bh() {
            return this.a2;
        }
        get_ab() {
            return false;
        }
        get ab() {
            return this.get_ab();
        }
        bz(a, b, c, d, e, f) {
            let g = true;
            let h = true;
            let i = false;
            if (this.ab) {
                g = false;
                h = false;
                i = false;
            }
            this.b0(a, b, c, d, e, f, g, h, i);
        }
        get resized() {
            return this.t;
        }
        ensureCorrectSize() {
            this.t = this.v(this.av);
            return this.t;
        }
        b0(a, b, c, d, e, f, g, h, i) {
            if (!this.s) {
                return;
            }
            if (this.bw != 1) {
                a.ad(this.bw);
            }
            this.av = f;
            if (!i) {
                this.b1();
                this.ensureCorrectSize();
            }
            if (this.y || this.t || i) {
                this.y = false;
                let j = this.j;
                if (this.p || this.t || (this.i != null && this.i.g != this.bp)) {
                    this.p = false;
                    this.bp = this.i.g;
                    j.ac(this.i);
                }
                if (f != 1) {
                    this.j.z();
                    this.j.aa(f, f);
                }
                if (g) {
                    this.b4();
                }
                if (h) {
                    this.b5(this.j);
                }
                if (f != 1) {
                    this.j.y();
                }
            }
            if (!i) {
                let k = Math.round((d - b) * f);
                let l = Math.round((e - c) * f);
                if (this.ak == this.am && this.aj == this.al) {
                    a.m(this.bm.getNativeElement(), 1, k, l, this.ak, this.aj);
                }
                else {
                    a.n(this.bm.getNativeElement(), 1, 0, 0, this.ak, this.aj, k, l, this.ak, this.aj);
                }
            }
            if (this.bw != 1) {
                a.ad(1);
            }
        }
        b4() {
            if (!this.m) {
                this.j.k(0, 0, this.am, this.al);
            }
            this.c2.n = 0;
            this.c2.o = 0;
            this.c2.width = this.a3;
            this.c2.height = this.aq;
            this.j.v(this.c2);
            if (this.cu != null) {
                if (this.ag > 0) {
                    this.cy.am = 0;
                    this.cy.ak = this.ag / 2;
                    this.cy.an = this.aq;
                    this.cy.al = this.ag / 2;
                    this.cy._stroke = this.cu;
                    this.cy.ac = this.ag;
                    this.j.r(this.cy);
                }
                if (this.ah > 0) {
                    this.cz.am = 0;
                    this.cz.ak = Math.max(0, (this.a3 - (this.ah / 2)));
                    this.cz.an = this.aq;
                    this.cz.al = Math.max(0, (this.a3 - (this.ah / 2)));
                    this.cz._stroke = this.cu;
                    this.cz.ac = this.ah;
                    this.j.r(this.cz);
                }
                if (this.ai > 0) {
                    this.c1.am = this.ai / 2;
                    this.c1.ak = this.ag;
                    this.c1.an = this.ai / 2;
                    this.c1.al = Math.max(0, (this.a3 - (this.ah / 2)));
                    this.c1._stroke = this.cu;
                    this.c1.ac = this.ai;
                    this.j.r(this.c1);
                }
                if (this.af > 0) {
                    this.cx.am = Math.max(0, this.aq - (this.af / 2));
                    this.cx.ak = this.ag;
                    this.cx.an = Math.max(0, this.aq - (this.af / 2));
                    this.cx.al = Math.max(0, (this.a3 - (this.ah / 2)));
                    this.cx._stroke = this.cu;
                    this.cx.ac = this.af;
                    this.j.r(this.cx);
                }
            }
        }
        ch(a) {
            this.av = a;
        }
        renderStandardContent() {
            this.b0(this.j, 0, 0, 0, 0, this.av, false, true, true);
        }
        renderStandardBackground() {
            this.b0(this.j, 0, 0, 0, 0, this.av, true, false, true);
        }
        b5(a) {
            if (this.r) {
                this.cs();
                this.r = false;
            }
            if (this.ct.ak != null) {
                this.ct.n = this.ay;
                this.ct.o = this.a1;
                this.j.w(this.ct);
                if (this.q) {
                    this.c0.ac = 1;
                    this.c0.ak = this.ay;
                    this.c0.am = this.a2 - 2;
                    this.c0.al = this.ay + (this.a0 - this.ay);
                    this.c0.an = this.a2 - 2;
                    this.c0._stroke = this.ct.am;
                    this.j.r(this.c0);
                }
            }
        }
        b1() {
            if (this.bm == null) {
                this.bm = this.bk.createElement("canvas");
                let a = this.bk.get2DCanvasContext(this.bm);
                let b = ((() => {
                    let $ret = new CanvasViewRenderer();
                    $ret.h$d = a;
                    return $ret;
                })());
                this.j = new RenderingContext(b, a);
                this.r = true;
            }
        }
        v(a) {
            let b = Math.round(this.a3 * a);
            let c = Math.round(this.aq * a);
            this.ak = b;
            this.aj = c;
            let d = this.bd(b);
            let e = this.bd(c);
            if (d != this.am || e != this.al) {
                this.bm.setAttribute("width", d + "px");
                this.bm.setAttribute("height", e + "px");
                this.am = d;
                this.al = e;
                this.ak = b;
                this.aj = c;
                return true;
            }
            return false;
        }
        bd(a) {
            return a;
        }
        cg(a, b, c, d) {
            this.o = true;
            this.as = a;
            this.au = b;
            this.at = c;
            this.ar = d;
            this.r = true;
        }
        get bf() {
            return this.az;
        }
        set bf(a) {
            this.az = a;
        }
        cl(a) {
            this.o = true;
            this.cw = a;
            this.ct.am = ((() => {
                let $ret = new Brush();
                $ret.color = this.cw;
                return $ret;
            })());
        }
        get_z() {
            return false;
        }
        get z() {
            return this.get_z();
        }
        get ac() {
            return this.s;
        }
        set ac(a) {
            this.o = true;
            this.s = a;
        }
        get a8() {
            return this.aq;
        }
        get bj() {
            return this.a3;
        }
        cd(a) {
            this.o = true;
            this.aq = a;
            this.c2.height = this.aq;
        }
        cn(a) {
            this.o = true;
            this.a3 = a;
            this.c2.width = this.a3;
            this.r = true;
        }
        by() {
            if (!this.aa) {
                return;
            }
            this.l = true;
            this.g = new CanvasPlaceholderView();
            this.c = 0;
            this.e = 1;
            this.g.l = Color.u(120, 120, 120, 120);
            this.g.h = 0;
        }
        b9(a) {
            this.o = true;
            this.cv = a;
            this.m = this.cv.l == 255;
            this.c2._fill = ((() => {
                let $ret = new Brush();
                $ret.color = this.cv;
                return $ret;
            })());
        }
        cf(a) {
            this.o = true;
            this.bw = a;
        }
        cj(a) {
            this.q = a;
            this.o = true;
        }
        cr(a) {
            if (!this.aa) {
                return;
            }
            this.o = true;
            this.g.h = (a * 0.20000000298023224);
        }
        b2(a, b, c, d) {
            if (this.g != null) {
                let e = this.ax;
                let f = this.aw;
                if (this.e == 1) {
                    b = truncate((b + (d / 2) - (f / 2)));
                }
                else if (this.e == 2) {
                    b = truncate((b + (d) - (f)));
                }
                if (this.c == 1) {
                    a = truncate((a + (c / 2) - (e / 2)));
                }
                else if (this.c == 2) {
                    a = truncate((a + (c) - (e)));
                }
            }
        }
        cc(a) {
            this.i = a;
            this.p = true;
            this.o = true;
            this.r = true;
        }
        cq(a) {
            if (!this.aa) {
                return;
            }
            this.o = true;
            let b = DeviceUtils.f(12);
            if (a.af != null && !isNaN_(a.af.d)) {
                b = a.af.d;
            }
            if (this.aw != b) {
                this.aw = b;
            }
            let c = a.gg * 0.2;
            let d = a.e0 * 0.6;
            if (this.ax >= c && this.ax <= d) {
                return;
            }
            let e = (CanvasGridCellBase.bt.nextDouble() * (d - c) + c);
            if (this.ax != e) {
                this.ax = e;
            }
        }
        co(a, b) {
            if (!this.aa) {
                return;
            }
            this.o = true;
            this.c = a;
            this.e = b;
        }
        cp(a) {
            if (!this.aa) {
                return;
            }
            this.o = true;
        }
        b8(a) {
            this.a = a;
        }
        get d() {
            return this.b;
        }
        ce(a) {
            this.b = a;
            this.o = true;
            this.r = true;
        }
        cs() {
            if (this.bx == null) {
                return;
            }
            if (this.j == null) {
                return;
            }
            if (this.p || (this.i != null && this.i.g != this.bp)) {
                this.p = false;
                this.bp = this.i.g;
                this.j.ac(this.i);
            }
            let a;
            if (this.h != this.i || this.br != this.i.g) {
                this.h = this.i;
                this.br = this.i.g;
                this.ap = FontUtil.getCurrentFontHeight(this.bk, this.i);
            }
            a = this.ap;
            let b = 0;
            let c = 0;
            let d = this.a3;
            let e = this.aq;
            d -= (this.ag + this.as + this.ah + this.at);
            e -= (this.ai + this.au + this.af + this.ar);
            d = Math.max(d, 0);
            e = Math.max(e, 0);
            let f = d;
            if (!isNaN_(this.az) && this.az < d) {
                f = this.az;
            }
            let g = this.bi(this.ct, this.bx, f, this.j);
            switch (this.b) {
                case 0:
                    b = this.as + this.ag;
                    break;
                case 3:
                case 1:
                    b = this.as + this.ag + ((d / 2) - (g / 2));
                    break;
                case 2:
                    b = Math.max(0, (this.a3 - (this.at + this.ah))) - g;
                    break;
            }
            switch (this.f) {
                case 0:
                    c = this.au + this.ai;
                    break;
                case 3:
                case 1:
                    c = this.au + this.ai + ((e / 2) - (a / 2));
                    break;
                case 2:
                    c = Math.max(0, (this.aq - (this.ar + this.af))) - a;
                    break;
            }
            this.ay = b;
            this.a0 = this.ay + g;
            this.a1 = c;
            this.a2 = this.a1 + (a / 2);
            this.o = true;
        }
        bi(a, b, c, d) {
            let e = b;
            let f = e.length;
            let g = ("\u2026");
            if (isNaN_(this.an)) {
                this.an = d.f(g);
            }
            if (c <= this.an) {
                a.ak = g;
                return this.an;
            }
            let h = d.f(e);
            if (h <= c) {
                a.ak = b;
                return h;
            }
            let i = c / h;
            let j = truncate(Math.ceil(f * i));
            let k = e.substr(0, j) + g;
            let l = d.f(k);
            if (l > c) {
                f = j;
            }
            else {
                j = j + 1;
                k = e.substr(0, j) + g;
                l = d.f(k);
                if (l > c) {
                    f = j;
                }
            }
            while (h > c && f > 0) {
                f--;
                e = e.substr(0, f) + g;
                h = d.f(e);
            }
            a.ak = e;
            return h;
        }
        cm(a) {
            this.f = a;
            this.o = true;
            this.r = true;
        }
        ae(a) {
            return a.isClickActionDesired;
        }
        onContactStarted(a, b) {
            if (this.a != null && this.ae(this.a)) {
                this.bu = a.pageX;
                this.bv = a.pageY;
                this.a.mouseDownCell(this.bu, this.bv);
            }
        }
        onContactMoved(a, b) {
            if (this.a != null && this.ae(this.a)) {
                let c = a.pageX;
                let d = a.pageY;
                if (this.a.isMouseDown) {
                    if (b) {
                        if (PointUtil.equals(this.a.mousePosition, null) || Math.abs(c - this.a.mousePosition.x) > CanvasGridCellBase.bq || Math.abs(d - this.a.mousePosition.y) > CanvasGridCellBase.bq) {
                            this.a.mouseUpCell(c, d);
                        }
                        else {
                            a.stopPropagation();
                        }
                    }
                    else {
                        if (PointUtil.equals(this.a.mousePosition, null)) {
                            this.a.mouseUpCell(c, d);
                        }
                        else {
                            if (Math.abs(c - this.a.mousePosition.x) > CanvasGridCellBase.bo || Math.abs(d - this.a.mousePosition.y) > CanvasGridCellBase.bo) {
                                this.a.isControlPressed = a.ctrlKey;
                                this.a.dragStarted();
                            }
                        }
                    }
                }
            }
        }
        onContactCompleted(a, b) {
            if (this.a != null && this.ae(this.a)) {
                let c = a.pageX;
                let d = a.pageY;
                if (PointUtil.equals(this.a.mousePosition, null) || Math.abs(c - this.a.mousePosition.x) > CanvasGridCellBase.bq || Math.abs(d - this.a.mousePosition.y) > CanvasGridCellBase.bq || this.a.isDrag) {
                    this.a.mouseUpCell(c, d);
                }
                if (this.a.isCellDown()) {
                    this.a.isControlPressed = a.ctrlKey;
                    this.a.isShiftPressed = a.shiftKey;
                    switch (a.which) {
                        case 1:
                            this.a.clickCell(0);
                            break;
                        case 2:
                            this.a.clickCell(1);
                            break;
                        case 3:
                            this.a.clickCell(2);
                            break;
                        default:
                            this.a.clickCell(3);
                            break;
                    }
                    this.a.mouseUpCell(a.pageX, a.pageY);
                }
            }
        }
        onDoubleClick(a) {
            switch (a.which) {
                case 1:
                    this.a.doubleClicked(0);
                    break;
                case 2:
                    this.a.doubleClicked(1);
                    break;
                case 3:
                    this.a.doubleClicked(2);
                    break;
                default:
                    this.a.doubleClicked(3);
                    break;
            }
        }
    }
    CanvasGridCellBase.$t = /*@__PURE__*/ markType(CanvasGridCellBase, 'CanvasGridCellBase', Base.$, [INativeCell_$type]);
    CanvasGridCellBase.bt = /*@__PURE__*/ new Random(0);
    CanvasGridCellBase.bq = 10;
    CanvasGridCellBase.bo = 3;
    return CanvasGridCellBase;
})();
//# sourceMappingURL=CanvasGridCellBase.js.map
