/*
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, runOn, markType } from "igniteui-webcomponents-core";
import { ICellRenderingEngine_$type } from "./ICellRenderingEngine";
import { CurrentRendererSettings } from "./CurrentRendererSettings";
import { RenderingContext } from "igniteui-webcomponents-core";
import { Rect } from "igniteui-webcomponents-core";
import { CanvasViewRenderer } from "igniteui-webcomponents-core";
import { intDivide } from "igniteui-webcomponents-core";
import { stringFormat1 } from "igniteui-webcomponents-core";
/**
 * @hidden
 */
var DataGridCellLayoutPanel = /** @class */ /*@__PURE__*/ (function (_super) {
    tslib_1.__extends(DataGridCellLayoutPanel, _super);
    function DataGridCellLayoutPanel(a, b, c) {
        var _this = _super.call(this) || this;
        _this.e = null;
        _this.l = false;
        _this.r = null;
        _this._element = null;
        _this.g = null;
        _this.j = false;
        _this._rootElement = null;
        _this.n = false;
        _this.h = null;
        _this.m = false;
        _this._requestAnimationFrame = null;
        _this.v = 0;
        _this.p = -1;
        _this.o = -1;
        _this.k = false;
        _this.q = 0;
        _this.c = null;
        _this.a = null;
        _this.d = null;
        _this.b = null;
        _this.q = c;
        _this.r = a;
        _this.requestAnimationFrame = _this.r.getRequestAnimationFrame();
        _this.element = _this.r.createElement("div");
        _this.element.setRawStyleProperty("position", "relative");
        _this.element.setRawStyleProperty("box-sizing", "border-box");
        _this.refresh(_this.e, false);
        _this.g = b;
        _this.j = false;
        return _this;
    }
    Object.defineProperty(DataGridCellLayoutPanel.prototype, "element", {
        get: function () {
            return this._element;
        },
        set: function (a) {
            this._element = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(DataGridCellLayoutPanel.prototype, "s", {
        get: function () {
            return this.r;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(DataGridCellLayoutPanel.prototype, "f", {
        get: function () {
            return this.e;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(DataGridCellLayoutPanel.prototype, "rootElement", {
        get: function () {
            return this._rootElement;
        },
        set: function (a) {
            this._rootElement = a;
        },
        enumerable: true,
        configurable: true
    });
    DataGridCellLayoutPanel.prototype.refresh = function (a, b) {
        this.e = a;
        this.ar(b);
    };
    DataGridCellLayoutPanel.prototype.viewportChanged = function () {
        this.ar(true);
    };
    DataGridCellLayoutPanel.prototype.invalidateVisibility = function (a) {
        this.h = a;
        this.n = true;
    };
    Object.defineProperty(DataGridCellLayoutPanel.prototype, "initialRefreshFinished", {
        get: function () {
            return this.j;
        },
        enumerable: true,
        configurable: true
    });
    DataGridCellLayoutPanel.prototype.af = function (a, b, c, d) {
        this.ar(true);
    };
    DataGridCellLayoutPanel.prototype.aq = function (a) {
        var b = this.g.b(this.r, a);
        if (b.isCanvasBased) {
            var c = b.nativeCell.ac = false;
        }
        else {
            var d = b.nativeCell.z;
            d.setRawStyleProperty("top", "-1000px");
            d.setRawStyleProperty("left", "-1000px");
            if (a.k.d != 0) {
                d.setRawStyleProperty("clip", "auto");
            }
            this.ai(a, d);
        }
    };
    DataGridCellLayoutPanel.prototype.ar = function (a) {
        if (a) {
            this.l = true;
            this.y();
            return;
        }
        if (this.m) {
            return;
        }
        if (this.l) {
            return;
        }
        this.l = true;
        this.ab(runOn(this, this.y));
    };
    Object.defineProperty(DataGridCellLayoutPanel.prototype, "requestAnimationFrame", {
        get: function () {
            return this._requestAnimationFrame;
        },
        set: function (a) {
            this._requestAnimationFrame = a;
        },
        enumerable: true,
        configurable: true
    });
    DataGridCellLayoutPanel.prototype.ab = function (a) {
        this.requestAnimationFrame(a);
    };
    DataGridCellLayoutPanel.prototype.y = function () {
        if (!this.l) {
            return;
        }
        this.l = false;
        if (this.n) {
            this.n = false;
            this.h();
        }
        if (this.e == null) {
            return;
        }
        if (Rect.l_op_Equality(this.e.actualVisibleRegion, null) || this.e.actualVisibleRegion.isEmpty) {
            return;
        }
        this.j = true;
        if (this.p != this.e.absoluteWidth) {
            this.element.setRawStyleProperty("width", this.e.absoluteWidth + "px");
        }
        if (this.o != this.e.absoluteHeight) {
            this.element.setRawStyleProperty("height", this.e.absoluteHeight + "px");
        }
        this.p = this.e.absoluteWidth;
        this.o = this.e.absoluteHeight;
        var a = this.e.actualVisibleRegion.top;
        var b = this.e.actualVisibleRegion.left;
        var c = this.e.bu;
        var d = c.count;
        var e = this.e.bh;
        var f = this.e.bh.o;
        var g = f.count;
        var h = this.e.bh.p;
        for (var i = 0; i < g; i++) {
            if (h._inner[i]) {
                continue;
            }
            var j = e.item(f._inner[i]);
            for (var k = 0; k < j.count; k++) {
                var l = j._inner[k];
                this.aq(l);
            }
        }
        e.u();
        var m = false;
        var n = this.v;
        var o = true;
        var p = false;
        for (var q = 0; q < d; q++) {
            var r = c._inner[q].q;
            var s = c._inner[q].q.count;
            var t = c._inner[q].p;
            for (var u = 0; u < s; u++) {
                if (t._inner[u]) {
                    continue;
                }
                var v = r._inner[u];
                var w = this.g.b(this.r, v);
                if (!w.isCanvasBased) {
                    o = false;
                }
                else {
                    p = true;
                }
                if (v.a3) {
                    m = true;
                }
                if (v.t == 1 && v.gk != n) {
                    m = true;
                }
            }
        }
        if (p) {
            this.aa();
        }
        if (m) {
            if (this.v == 0x7FFFFFFFFFFFFFFF - 1) {
                this.v = 0;
            }
            else {
                this.v++;
            }
            n = this.v;
        }
        var x = 0;
        var y = new CurrentRendererSettings();
        var z = this.q;
        this.m = true;
        for (var aa = 0; aa < d; aa++) {
            var ab = c._inner[aa].q;
            var ac = c._inner[aa].q.count;
            var ad = c._inner[aa].p;
            for (var ae = 0; ae < ac; ae++) {
                if (ad._inner[ae]) {
                    continue;
                }
                var af = ab._inner[ae];
                var ag = this.g.b(this.r, af);
                if (ag.isCanvasBased) {
                    var ah = y.a;
                    this.av(y, aa, af, this.e, x, m, o);
                    if (y.a != ah && m) {
                        x++;
                    }
                }
                var ai = ag.isCanvasBased;
                var aj = null;
                if (!ai) {
                    aj = ag.nativeCell.z;
                    if (af.t == 0) {
                        this.element.append(aj);
                        this.ag(af, aj);
                    }
                }
                if (af.t == 1) {
                    if (ai) {
                        var ak = af.r.nativeCell;
                        ak.ac = true;
                    }
                }
                af.t = 2;
                var al = (af.f3 + intDivide(af.gg, 2));
                var am = (af.f5 + intDivide(af.e0, 2));
                var an = (al - b) / this.e.actualVisibleRegion.width;
                var ao = (am - a) / this.e.actualVisibleRegion.height;
                if (af.a5) {
                    if (!ai) {
                        aj.setRawPosition(af.cc, af.cd);
                    }
                }
                if (!ai) {
                    if (af.k.d != 0) {
                        if (this.e.bk) {
                            aj.setRawStyleProperty("clip", this.w(af.k.d, af.cc, af.cd));
                        }
                        else {
                            aj.setRawStyleProperty("clip", "auto");
                        }
                    }
                    else {
                        if (Rect.l_op_Inequality(af.kv, Rect.empty)) {
                            aj.setRawStyleProperty("clip", this.x(af.kv));
                        }
                        else {
                            aj.setRawStyleProperty("clip", "auto");
                        }
                    }
                }
                af.ji();
                if (ai) {
                    var ap = af.r.nativeCell;
                    ap.ch(z);
                }
                af.r.modelUpdated(af);
                if (ai && y.b) {
                    var aq = y.a;
                    aq.z();
                    if (af.k.d != 0 && this.e.bk) {
                        aq.ae(this.aw(af.k.d));
                    }
                    var ar = af.r.nativeCell;
                    ar.bz(aq, y.c, y.d, af.cc, af.cd, z);
                    aq.y();
                }
                af.jb();
                if (m) {
                    af.gk = n;
                    if (!ai) {
                        aj.setRawStyleProperty("z-index", x.toString());
                    }
                    x++;
                }
                this.ah(af, aj);
            }
        }
        this.m = false;
        if (y.a != null && this.q != 1) {
            y.a.y();
        }
        this.am();
    };
    DataGridCellLayoutPanel.prototype.am = function () {
    };
    DataGridCellLayoutPanel.prototype.aa = function () {
        if (!this.k) {
            this.k = true;
            var a = this.r.rootWrapper;
            var b = (window.navigator.msPointerEnabled && MSGesture !== undefined);
            var c = (window.PointerEvent || window.navigator.pointerEnabled);
            var d = (window.TouchEvent);
            if ((b || c)) {
                a.listen("pointerdown", runOn(this, this.an));
                a.listen("pointerup", runOn(this, this.ap));
                a.listen("pointermove", runOn(this, this.ao));
            }
            else {
                a.listen("mousedown", runOn(this, this.ac));
                a.listen("mouseup", runOn(this, this.ae));
                a.listen("mousemove", runOn(this, this.ad));
                a.listen("touchstart", runOn(this, this.as));
                a.listen("touchend", runOn(this, this.au));
                a.listen("touchmove", runOn(this, this.at));
            }
            a.listen("dblclick", runOn(this, this.z));
        }
    };
    DataGridCellLayoutPanel.prototype.ac = function (a) {
        this.al(a, false);
    };
    DataGridCellLayoutPanel.prototype.ae = function (a) {
        this.aj(a, false);
    };
    DataGridCellLayoutPanel.prototype.ad = function (a) {
        this.ak(a, false);
    };
    DataGridCellLayoutPanel.prototype.z = function (a) {
        if (this.e == null) {
            return;
        }
        var b = this.r.rootWrapper.getOffset();
        var c = this.e.e(a.pageX - b.left, a.pageY - b.top);
        if (c == null) {
            return;
        }
        if (c.r != null) {
            if (c.r.isCanvasBased && c.r.nativeCell != null) {
                c.r.nativeCell.onDoubleClick(a);
            }
        }
    };
    DataGridCellLayoutPanel.prototype.an = function (e_) {
        var a = (e_.originalEvent.pointerType);
        if (a == "touch") {
            this.as(e_);
        }
        else {
            this.ac(e_);
        }
    };
    DataGridCellLayoutPanel.prototype.ao = function (e_) {
        var a = (e_.originalEvent.pointerType);
        if (a == "touch") {
            this.at(e_);
        }
        else {
            this.ad(e_);
        }
    };
    DataGridCellLayoutPanel.prototype.ap = function (e_) {
        var a = (e_.originalEvent.pointerType);
        if (a == "touch") {
            this.au(e_);
        }
        else {
            this.ae(e_);
        }
    };
    DataGridCellLayoutPanel.prototype.as = function (a) {
        this.al(a, true);
    };
    DataGridCellLayoutPanel.prototype.al = function (a, b) {
        if (this.e == null) {
            return;
        }
        var c = this.r.rootWrapper.getOffset();
        var d = this.e.e(a.pageX - c.left, a.pageY - c.top);
        if (d == null) {
            return;
        }
        if (d.r != null) {
            if (d.r.isCanvasBased && d.r.nativeCell != null) {
                d.r.nativeCell.onContactStarted(a, b);
            }
        }
    };
    DataGridCellLayoutPanel.prototype.at = function (a) {
        this.ak(a, true);
    };
    DataGridCellLayoutPanel.prototype.ak = function (a, b) {
        if (this.e == null) {
            return;
        }
        var c = this.r.rootWrapper.getOffset();
        var d = this.e.e(a.pageX - c.left, a.pageY - c.top);
        if (d == null) {
            return;
        }
        if (d.r != null) {
            if (d.r.isCanvasBased && d.r.nativeCell != null) {
                d.r.nativeCell.onContactMoved(a, b);
            }
        }
    };
    DataGridCellLayoutPanel.prototype.au = function (a) {
        this.aj(a, true);
    };
    DataGridCellLayoutPanel.prototype.aj = function (a, b) {
        if (this.e == null) {
            return;
        }
        var c = this.r.rootWrapper.getOffset();
        var d = this.e.e(a.pageX - c.left, a.pageY - c.top);
        if (d == null) {
            return;
        }
        if (d.r != null) {
            if (d.r.isCanvasBased && d.r.nativeCell != null) {
                d.r.nativeCell.onContactCompleted(a, b);
            }
        }
    };
    DataGridCellLayoutPanel.prototype.av = function (a, b, c, d, e, f, g) {
        if (g) {
            b = 0;
        }
        if (this.c == null) {
            this.c = new Array(d.bu.count);
            this.a = new Array(d.bu.count);
            this.d = new Array(d.bu.count);
            this.b = new Array(d.bu.count);
            for (var h = 0; h < d.bu.count; h++) {
                this.c[h] = null;
                this.a[h] = null;
                this.d[h] = -1;
                this.b[h] = -1;
            }
        }
        if (this.c[b] == null) {
            var i = this.r.createElement("canvas");
            this.a[b] = i;
            i.setRawStyleProperty("position", "absolute");
            i.setRawStyleProperty("z-index", e.toString());
            this.element.append(i);
            var j_1 = this.r.get2DCanvasContext(i);
            var k = ((function () {
                var $ret = new CanvasViewRenderer();
                $ret.h$d = j_1;
                return $ret;
            })());
            this.c[b] = new RenderingContext(k, j_1);
        }
        if (a.a != this.c[b]) {
            a.a = this.c[b];
            a.b = true;
            a.c = d.actualVisibleRegion.left;
            a.d = d.actualVisibleRegion.top;
            var l = d.actualVisibleRegion.width * this.q;
            var m = d.actualVisibleRegion.height * this.q;
            if (l != this.d[b] || m != this.b[b]) {
                this.a[b].setRawSize(d.actualVisibleRegion.width, d.actualVisibleRegion.height);
                this.a[b].setAttribute("width", l + "px");
                this.a[b].setAttribute("height", m + "px");
                this.d[b] = l;
                this.b[b] = m;
            }
            if (f) {
                this.a[b].setRawStyleProperty("z-index", e.toString());
            }
            a.a.k(0, 0, d.actualVisibleRegion.width, d.actualVisibleRegion.height);
            this.a[b].setRawPosition(a.c, a.d);
        }
    };
    DataGridCellLayoutPanel.prototype.aw = function (a) {
        var b = this.e.b8;
        var c = this.e.b3 + this.e.b0;
        var d = this.e.cf;
        var e = this.e.by;
        if (a == 2) {
            c = Math.min(this.e.cj, this.e.b6) - this.e.cg - this.e.b1;
            d = this.e.cg;
        }
        return new Rect(0, c, b, d, e);
    };
    DataGridCellLayoutPanel.prototype.w = function (a, b, c) {
        if (a != 0) {
            var d = this.aw(a);
            var e = d.x - b;
            var f = d.y - c;
            var g = e + d.width;
            var h = f + d.height;
            return "rect(" + f + "px," + g + "px," + h + "px," + e + "px)";
        }
        return "auto";
    };
    DataGridCellLayoutPanel.prototype.x = function (a) {
        return stringFormat1("rect({0}px, {1}px, {2}px, {3}px)", a.top, a.right, a.bottom, a.left);
    };
    DataGridCellLayoutPanel.prototype.ag = function (a, b) {
    };
    DataGridCellLayoutPanel.prototype.ai = function (a, b) {
    };
    DataGridCellLayoutPanel.prototype.ah = function (a, b) {
    };
    DataGridCellLayoutPanel.$t = markType(DataGridCellLayoutPanel, 'DataGridCellLayoutPanel', Base.$, [ICellRenderingEngine_$type]);
    return DataGridCellLayoutPanel;
}(Base));
export { DataGridCellLayoutPanel };
//# sourceMappingURL=DataGridCellLayoutPanel.js.map
