/*
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 * as tslib_1 from "tslib";
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
 */
var CanvasGridCellBase = /** @class */ /*@__PURE__*/ (function (_super) {
    tslib_1.__extends(CanvasGridCellBase, _super);
    function CanvasGridCellBase(a) {
        var _this = _super.call(this) || this;
        _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 = ((function () {
            var $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;
        return _this;
    }
    Object.defineProperty(CanvasGridCellBase.prototype, "y", {
        get: function () {
            return this.o;
        },
        set: function (a) {
            this.o = a;
        },
        enumerable: true,
        configurable: true
    });
    CanvasGridCellBase.prototype.ci = function (a) {
        this.k = a;
    };
    CanvasGridCellBase.prototype.b3 = function () {
        this.o = true;
        if (this.k != null) {
            this.k();
        }
    };
    Object.defineProperty(CanvasGridCellBase.prototype, "isCanvasBased", {
        get: function () {
            return true;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(CanvasGridCellBase.prototype, "bl", {
        get: function () {
            return this.bk;
        },
        enumerable: true,
        configurable: true
    });
    CanvasGridCellBase.prototype.ck = function (a) {
        this.bx = a;
        this.o = true;
        this.r = true;
    };
    Object.defineProperty(CanvasGridCellBase.prototype, "templateContext", {
        get: function () {
            return this.j.g();
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(CanvasGridCellBase.prototype, "u", {
        get: function () {
            return this.l;
        },
        set: function (a) {
            this.l = a;
        },
        enumerable: true,
        configurable: true
    });
    CanvasGridCellBase.prototype.get_aa = function () {
        return false;
    };
    Object.defineProperty(CanvasGridCellBase.prototype, "aa", {
        get: function () {
            return this.get_aa();
        },
        enumerable: true,
        configurable: true
    });
    CanvasGridCellBase.prototype.ca = function (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;
    };
    CanvasGridCellBase.prototype.cb = function (a) {
        this.o = true;
        this.ao = a;
        this.ct._opacity = this.ao;
    };
    Object.defineProperty(CanvasGridCellBase.prototype, "ba", {
        get: function () {
            return this.as;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(CanvasGridCellBase.prototype, "bc", {
        get: function () {
            return this.au;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(CanvasGridCellBase.prototype, "bb", {
        get: function () {
            return this.at;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(CanvasGridCellBase.prototype, "a9", {
        get: function () {
            return this.ar;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(CanvasGridCellBase.prototype, "a5", {
        get: function () {
            return this.ag;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(CanvasGridCellBase.prototype, "a7", {
        get: function () {
            return this.ai;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(CanvasGridCellBase.prototype, "a6", {
        get: function () {
            return this.ah;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(CanvasGridCellBase.prototype, "a4", {
        get: function () {
            return this.af;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(CanvasGridCellBase.prototype, "be", {
        get: function () {
            return this.ay;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(CanvasGridCellBase.prototype, "bg", {
        get: function () {
            return this.a0;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(CanvasGridCellBase.prototype, "bh", {
        get: function () {
            return this.a2;
        },
        enumerable: true,
        configurable: true
    });
    CanvasGridCellBase.prototype.get_ab = function () {
        return false;
    };
    Object.defineProperty(CanvasGridCellBase.prototype, "ab", {
        get: function () {
            return this.get_ab();
        },
        enumerable: true,
        configurable: true
    });
    CanvasGridCellBase.prototype.bz = function (a, b, c, d, e, f) {
        var g = true;
        var h = true;
        var i = false;
        if (this.ab) {
            g = false;
            h = false;
            i = false;
        }
        this.b0(a, b, c, d, e, f, g, h, i);
    };
    Object.defineProperty(CanvasGridCellBase.prototype, "resized", {
        get: function () {
            return this.t;
        },
        enumerable: true,
        configurable: true
    });
    CanvasGridCellBase.prototype.ensureCorrectSize = function () {
        this.t = this.v(this.av);
        return this.t;
    };
    CanvasGridCellBase.prototype.b0 = function (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;
            var 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) {
            var k = Math.round((d - b) * f);
            var 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);
        }
    };
    CanvasGridCellBase.prototype.b4 = function () {
        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);
            }
        }
    };
    CanvasGridCellBase.prototype.ch = function (a) {
        this.av = a;
    };
    CanvasGridCellBase.prototype.renderStandardContent = function () {
        this.b0(this.j, 0, 0, 0, 0, this.av, false, true, true);
    };
    CanvasGridCellBase.prototype.renderStandardBackground = function () {
        this.b0(this.j, 0, 0, 0, 0, this.av, true, false, true);
    };
    CanvasGridCellBase.prototype.b5 = function (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);
            }
        }
    };
    CanvasGridCellBase.prototype.b1 = function () {
        if (this.bm == null) {
            this.bm = this.bk.createElement("canvas");
            var a_1 = this.bk.get2DCanvasContext(this.bm);
            var b = ((function () {
                var $ret = new CanvasViewRenderer();
                $ret.h$d = a_1;
                return $ret;
            })());
            this.j = new RenderingContext(b, a_1);
            this.r = true;
        }
    };
    CanvasGridCellBase.prototype.v = function (a) {
        var b = Math.round(this.a3 * a);
        var c = Math.round(this.aq * a);
        this.ak = b;
        this.aj = c;
        var d = this.bd(b);
        var 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;
    };
    CanvasGridCellBase.prototype.bd = function (a) {
        return a;
    };
    CanvasGridCellBase.prototype.cg = function (a, b, c, d) {
        this.o = true;
        this.as = a;
        this.au = b;
        this.at = c;
        this.ar = d;
        this.r = true;
    };
    Object.defineProperty(CanvasGridCellBase.prototype, "bf", {
        get: function () {
            return this.az;
        },
        set: function (a) {
            this.az = a;
        },
        enumerable: true,
        configurable: true
    });
    CanvasGridCellBase.prototype.cl = function (a) {
        var _this = this;
        this.o = true;
        this.cw = a;
        this.ct.am = ((function () {
            var $ret = new Brush();
            $ret.color = _this.cw;
            return $ret;
        })());
    };
    CanvasGridCellBase.prototype.get_z = function () {
        return false;
    };
    Object.defineProperty(CanvasGridCellBase.prototype, "z", {
        get: function () {
            return this.get_z();
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(CanvasGridCellBase.prototype, "ac", {
        get: function () {
            return this.s;
        },
        set: function (a) {
            this.o = true;
            this.s = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(CanvasGridCellBase.prototype, "a8", {
        get: function () {
            return this.aq;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(CanvasGridCellBase.prototype, "bj", {
        get: function () {
            return this.a3;
        },
        enumerable: true,
        configurable: true
    });
    CanvasGridCellBase.prototype.cd = function (a) {
        this.o = true;
        this.aq = a;
        this.c2.height = this.aq;
    };
    CanvasGridCellBase.prototype.cn = function (a) {
        this.o = true;
        this.a3 = a;
        this.c2.width = this.a3;
        this.r = true;
    };
    CanvasGridCellBase.prototype.by = function () {
        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;
    };
    CanvasGridCellBase.prototype.b9 = function (a) {
        var _this = this;
        this.o = true;
        this.cv = a;
        this.m = this.cv.l == 255;
        this.c2._fill = ((function () {
            var $ret = new Brush();
            $ret.color = _this.cv;
            return $ret;
        })());
    };
    CanvasGridCellBase.prototype.cf = function (a) {
        this.o = true;
        this.bw = a;
    };
    CanvasGridCellBase.prototype.cj = function (a) {
        this.q = a;
        this.o = true;
    };
    CanvasGridCellBase.prototype.cr = function (a) {
        if (!this.aa) {
            return;
        }
        this.o = true;
        this.g.h = (a * 0.20000000298023224);
    };
    CanvasGridCellBase.prototype.b2 = function (a, b, c, d) {
        if (this.g != null) {
            var e = this.ax;
            var 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)));
            }
        }
    };
    CanvasGridCellBase.prototype.cc = function (a) {
        this.i = a;
        this.p = true;
        this.o = true;
        this.r = true;
    };
    CanvasGridCellBase.prototype.cq = function (a) {
        if (!this.aa) {
            return;
        }
        this.o = true;
        var b = DeviceUtils.f(12);
        if (a.af != null && !isNaN_(a.af.d)) {
            b = a.af.d;
        }
        if (this.aw != b) {
            this.aw = b;
        }
        var c = a.gg * 0.2;
        var d = a.e0 * 0.6;
        if (this.ax >= c && this.ax <= d) {
            return;
        }
        var e = (CanvasGridCellBase.bt.nextDouble() * (d - c) + c);
        if (this.ax != e) {
            this.ax = e;
        }
    };
    CanvasGridCellBase.prototype.co = function (a, b) {
        if (!this.aa) {
            return;
        }
        this.o = true;
        this.c = a;
        this.e = b;
    };
    CanvasGridCellBase.prototype.cp = function (a) {
        if (!this.aa) {
            return;
        }
        this.o = true;
    };
    CanvasGridCellBase.prototype.b8 = function (a) {
        this.a = a;
    };
    Object.defineProperty(CanvasGridCellBase.prototype, "d", {
        get: function () {
            return this.b;
        },
        enumerable: true,
        configurable: true
    });
    CanvasGridCellBase.prototype.ce = function (a) {
        this.b = a;
        this.o = true;
        this.r = true;
    };
    CanvasGridCellBase.prototype.cs = function () {
        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);
        }
        var 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;
        var b = 0;
        var c = 0;
        var d = this.a3;
        var 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);
        var f = d;
        if (!isNaN_(this.az) && this.az < d) {
            f = this.az;
        }
        var 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;
    };
    CanvasGridCellBase.prototype.bi = function (a, b, c, d) {
        var e = b;
        var f = e.length;
        var g = ("\u2026");
        if (isNaN_(this.an)) {
            this.an = d.f(g);
        }
        if (c <= this.an) {
            a.ak = g;
            return this.an;
        }
        var h = d.f(e);
        if (h <= c) {
            a.ak = b;
            return h;
        }
        var i = c / h;
        var j = truncate(Math.ceil(f * i));
        var k = e.substr(0, j) + g;
        var 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;
    };
    CanvasGridCellBase.prototype.cm = function (a) {
        this.f = a;
        this.o = true;
        this.r = true;
    };
    CanvasGridCellBase.prototype.ae = function (a) {
        return a.isClickActionDesired;
    };
    CanvasGridCellBase.prototype.onContactStarted = function (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);
        }
    };
    CanvasGridCellBase.prototype.onContactMoved = function (a, b) {
        if (this.a != null && this.ae(this.a)) {
            var c = a.pageX;
            var 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();
                        }
                    }
                }
            }
        }
    };
    CanvasGridCellBase.prototype.onContactCompleted = function (a, b) {
        if (this.a != null && this.ae(this.a)) {
            var c = a.pageX;
            var 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);
            }
        }
    };
    CanvasGridCellBase.prototype.onDoubleClick = function (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 = markType(CanvasGridCellBase, 'CanvasGridCellBase', Base.$, [INativeCell_$type]);
    CanvasGridCellBase.bt = new Random(0);
    CanvasGridCellBase.bq = 10;
    CanvasGridCellBase.bo = 3;
    return CanvasGridCellBase;
}(Base));
export { CanvasGridCellBase };
//# sourceMappingURL=CanvasGridCellBase.js.map
