/*
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, 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
 */
export let DataGridCellLayoutPanel = /*@__PURE__*/ (() => {
    class DataGridCellLayoutPanel extends Base {
        constructor(a, b, c) {
            super();
            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;
        }
        get element() {
            return this._element;
        }
        set element(a) {
            this._element = a;
        }
        get s() {
            return this.r;
        }
        get f() {
            return this.e;
        }
        get rootElement() {
            return this._rootElement;
        }
        set rootElement(a) {
            this._rootElement = a;
        }
        refresh(a, b) {
            this.e = a;
            this.ar(b);
        }
        viewportChanged() {
            this.ar(true);
        }
        invalidateVisibility(a) {
            this.h = a;
            this.n = true;
        }
        get initialRefreshFinished() {
            return this.j;
        }
        af(a, b, c, d) {
            this.ar(true);
        }
        aq(a) {
            let b = this.g.b(this.r, a);
            if (b.isCanvasBased) {
                let c = b.nativeCell.ac = false;
            }
            else {
                let 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);
            }
        }
        ar(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));
        }
        get requestAnimationFrame() {
            return this._requestAnimationFrame;
        }
        set requestAnimationFrame(a) {
            this._requestAnimationFrame = a;
        }
        ab(a) {
            this.requestAnimationFrame(a);
        }
        y() {
            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;
            let a = this.e.actualVisibleRegion.top;
            let b = this.e.actualVisibleRegion.left;
            let c = this.e.bu;
            let d = c.count;
            let e = this.e.bh;
            let f = this.e.bh.o;
            let g = f.count;
            let h = this.e.bh.p;
            for (let i = 0; i < g; i++) {
                if (h._inner[i]) {
                    continue;
                }
                let j = e.item(f._inner[i]);
                for (let k = 0; k < j.count; k++) {
                    let l = j._inner[k];
                    this.aq(l);
                }
            }
            e.u();
            let m = false;
            let n = this.v;
            let o = true;
            let p = false;
            for (let q = 0; q < d; q++) {
                let r = c._inner[q].q;
                let s = c._inner[q].q.count;
                let t = c._inner[q].p;
                for (let u = 0; u < s; u++) {
                    if (t._inner[u]) {
                        continue;
                    }
                    let v = r._inner[u];
                    let 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;
            }
            let x = 0;
            let y = new CurrentRendererSettings();
            let z = this.q;
            this.m = true;
            for (let aa = 0; aa < d; aa++) {
                let ab = c._inner[aa].q;
                let ac = c._inner[aa].q.count;
                let ad = c._inner[aa].p;
                for (let ae = 0; ae < ac; ae++) {
                    if (ad._inner[ae]) {
                        continue;
                    }
                    let af = ab._inner[ae];
                    let ag = this.g.b(this.r, af);
                    if (ag.isCanvasBased) {
                        let ah = y.a;
                        this.av(y, aa, af, this.e, x, m, o);
                        if (y.a != ah && m) {
                            x++;
                        }
                    }
                    let ai = ag.isCanvasBased;
                    let 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) {
                            let ak = af.r.nativeCell;
                            ak.ac = true;
                        }
                    }
                    af.t = 2;
                    let al = (af.f3 + intDivide(af.gg, 2));
                    let am = (af.f5 + intDivide(af.e0, 2));
                    let an = (al - b) / this.e.actualVisibleRegion.width;
                    let 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) {
                        let ap = af.r.nativeCell;
                        ap.ch(z);
                    }
                    af.r.modelUpdated(af);
                    if (ai && y.b) {
                        let aq = y.a;
                        aq.z();
                        if (af.k.d != 0 && this.e.bk) {
                            aq.ae(this.aw(af.k.d));
                        }
                        let 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();
        }
        am() {
        }
        aa() {
            if (!this.k) {
                this.k = true;
                let a = this.r.rootWrapper;
                let b = (window.navigator.msPointerEnabled && MSGesture !== undefined);
                let c = (window.PointerEvent || window.navigator.pointerEnabled);
                let 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));
            }
        }
        ac(a) {
            this.al(a, false);
        }
        ae(a) {
            this.aj(a, false);
        }
        ad(a) {
            this.ak(a, false);
        }
        z(a) {
            if (this.e == null) {
                return;
            }
            let b = this.r.rootWrapper.getOffset();
            let 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);
                }
            }
        }
        an(e_) {
            let a = (e_.originalEvent.pointerType);
            if (a == "touch") {
                this.as(e_);
            }
            else {
                this.ac(e_);
            }
        }
        ao(e_) {
            let a = (e_.originalEvent.pointerType);
            if (a == "touch") {
                this.at(e_);
            }
            else {
                this.ad(e_);
            }
        }
        ap(e_) {
            let a = (e_.originalEvent.pointerType);
            if (a == "touch") {
                this.au(e_);
            }
            else {
                this.ae(e_);
            }
        }
        as(a) {
            this.al(a, true);
        }
        al(a, b) {
            if (this.e == null) {
                return;
            }
            let c = this.r.rootWrapper.getOffset();
            let 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);
                }
            }
        }
        at(a) {
            this.ak(a, true);
        }
        ak(a, b) {
            if (this.e == null) {
                return;
            }
            let c = this.r.rootWrapper.getOffset();
            let 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);
                }
            }
        }
        au(a) {
            this.aj(a, true);
        }
        aj(a, b) {
            if (this.e == null) {
                return;
            }
            let c = this.r.rootWrapper.getOffset();
            let 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);
                }
            }
        }
        av(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 (let 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) {
                let i = this.r.createElement("canvas");
                this.a[b] = i;
                i.setRawStyleProperty("position", "absolute");
                i.setRawStyleProperty("z-index", e.toString());
                this.element.append(i);
                let j = this.r.get2DCanvasContext(i);
                let k = ((() => {
                    let $ret = new CanvasViewRenderer();
                    $ret.h$d = j;
                    return $ret;
                })());
                this.c[b] = new RenderingContext(k, j);
            }
            if (a.a != this.c[b]) {
                a.a = this.c[b];
                a.b = true;
                a.c = d.actualVisibleRegion.left;
                a.d = d.actualVisibleRegion.top;
                let l = d.actualVisibleRegion.width * this.q;
                let 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);
            }
        }
        aw(a) {
            let b = this.e.b8;
            let c = this.e.b3 + this.e.b0;
            let d = this.e.cf;
            let 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);
        }
        w(a, b, c) {
            if (a != 0) {
                let d = this.aw(a);
                let e = d.x - b;
                let f = d.y - c;
                let g = e + d.width;
                let h = f + d.height;
                return "rect(" + f + "px," + g + "px," + h + "px," + e + "px)";
            }
            return "auto";
        }
        x(a) {
            return stringFormat1("rect({0}px, {1}px, {2}px, {3}px)", a.top, a.right, a.bottom, a.left);
        }
        ag(a, b) {
        }
        ai(a, b) {
        }
        ah(a, b) {
        }
    }
    DataGridCellLayoutPanel.$t = /*@__PURE__*/ markType(DataGridCellLayoutPanel, 'DataGridCellLayoutPanel', Base.$, [ICellRenderingEngine_$type]);
    return DataGridCellLayoutPanel;
})();
//# sourceMappingURL=DataGridCellLayoutPanel.js.map
