/*
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, delegateCombine, fromEnum, markType, String_$type } from "igniteui-angular-core";
import { Rect } from "igniteui-angular-core";
import { Dictionary$2 } from "igniteui-angular-core";
import { ITileZoomTile_$type } from "./ITileZoomTile";
import { List$1 } from "igniteui-angular-core";
import { TileZoomTileInfo } from "./TileZoomTileInfo";
import { TileZoomInfo } from "./TileZoomInfo";
import { truncate, logBase, isInfinity, isNaN_ } from "igniteui-angular-core";
import { LinkedList$1 } from "igniteui-angular-core";
/**
 * @hidden
 */
export let TileZoomManager = /*@__PURE__*/ (() => {
    class TileZoomManager extends Base {
        constructor(a, b, c) {
            super();
            this.e = null;
            this.av = null;
            this.aw = null;
            this.au = null;
            this.i = false;
            this.v = 0;
            this.k = null;
            this.l = null;
            this.n = null;
            this.q = -1;
            this.ao = null;
            this.p = 1;
            this.r = 1;
            this.y = 0;
            this.o = null;
            this.m = null;
            this.d = null;
            this.f = false;
            this.h = false;
            this.e = a;
            let d = this.e;
            d.actualWindowRectChanged = delegateCombine(d.actualWindowRectChanged, runOn(this, this.am));
            let e = this.e;
            e.gridAreaRectChanged = delegateCombine(e.gridAreaRectChanged, runOn(this, this.an));
            this.d = new TileZoomTileCache(c, this);
            this.o = new List$1(TileZoomTileInfo.$, 0);
            this.m = new List$1(TileZoomTile.$, 0);
            this.k = new Dictionary$2(TileZoomTile.$, ITileZoomTile_$type, 0);
            this.l = new Dictionary$2(TileZoomTile.$, ITileZoomTile_$type, 0);
            this.n = new List$1(ITileZoomTile_$type, 0);
            this.h = b;
            this.ai();
        }
        ai() {
            this.av = this.e.x4;
            this.ao = this.ap(this.av);
            let a = this.s();
            this.ac();
            this.ab();
            this.q = a;
            this.v = this.t(a);
            this.au = new Rect(0, 0, 0, a, a);
        }
        ap(a) {
            return new Rect(0, a.left, a.top, a.width * this.p, a.height * this.r);
        }
        t(a) {
            let b = a / 512;
            let c = logBase(b, 2);
            return c;
        }
        s() {
            let a = Math.max(this.ao.width, this.ao.height);
            let b = 1;
            let c = 512;
            while (c < a) {
                b++;
                c = 512 * b;
            }
            return c;
        }
        an(a, b) {
            this.ai();
            this.aj();
        }
        am(a, b) {
            this.aj();
        }
        aj() {
            this.f = true;
            this.aw = this.e.xm;
            if (this.i) {
                if (this.j(this.aw)) {
                    this.ai();
                }
                this.af();
                this.ak();
                this.ag();
            }
        }
        j(a) {
            let b = 1;
            let c = 1;
            if (this.g(a)) {
                if (a.width > a.height) {
                    let d = 1;
                    if (a.height != 0) {
                        d = a.width / a.height;
                    }
                    let e = 0;
                    let f = 1;
                    while (f < d) {
                        e++;
                        f = truncate(Math.pow(3, e));
                    }
                    f = truncate(Math.pow(3, e - 1));
                    c = f;
                    if (c == 0 || isInfinity(c) || isNaN_(c)) {
                        c = 1;
                    }
                }
                else {
                    let g = 1;
                    if (a.width != 0) {
                        g = a.height / a.width;
                    }
                    let h = 0;
                    let i = 1;
                    while (i < g) {
                        h++;
                        i = truncate(Math.pow(3, h));
                    }
                    i = truncate(Math.pow(3, h - 1));
                    b = i;
                    if (isInfinity(b) || isNaN_(b)) {
                        b = 1;
                    }
                }
            }
            else {
                b = 1;
                c = 1;
            }
            let j = c != this.r || b != this.p;
            this.p = b;
            this.r = c;
            return j;
        }
        g(a) {
            return (a.width / a.height) > 3 || (a.height / a.width) > 3;
        }
        ag() {
            let a = this.a();
            for (let b = 0; b < this.m.count; b++) {
                this.aa(a, this.m._inner[b]);
            }
            let c = new Dictionary$2(Base.$, ITileZoomTile_$type, 0);
            for (let d of fromEnum(this.l.keys)) {
                c.addItem(this.l.item(d).content, this.l.item(d));
            }
            this.e.dp.b3(c, this.n, 512);
        }
        aa(a, b) {
            let c = this.as(b, a);
            let d = c.left * a.b;
            let e = c.top * a.a;
            let f = c.right * a.b;
            let g = c.bottom * a.a;
            let h = d - a.f.left;
            let i = e - a.f.top;
            let j = f - d;
            let k = g - e;
            b.xPosition = h / this.p;
            b.yPosition = i / this.r;
            b.width = j / this.p;
            b.height = k / this.r;
        }
        ak() {
            let a = this.a();
            this.k.clear();
            for (let b = 0; b < this.m.count; b++) {
                let c = this.m._inner[b];
                this.k.addItem(c, c);
            }
            this.m.clear();
            this.l.clear();
            this.n.clear();
            for (let d = 0; d < this.o.count; d++) {
                let e = this.o._inner[d];
                let f = this.d.b(e);
                if (f == null) {
                    f = this.b(e, a);
                    this.d.f(f);
                }
                if (this.k.containsKey(f)) {
                    this.k.removeItem(f);
                    this.l.addItem(f, f);
                }
                else {
                    this.z(f);
                    this.n.add(f);
                }
                this.m.add(f);
            }
            for (let g of fromEnum(this.k.keys)) {
                this.ad(g);
            }
        }
        ad(a) {
        }
        z(a) {
        }
        b(a, b) {
            let c = this.c(a);
            let d = this.as(c, b);
            let e = this.ar();
            let f = 1 / e.width;
            let g = e.height;
            if (e.width > e.height) {
                f = e.width;
                g = 1 / e.height;
            }
            let h = (d.left - e.left) * f;
            let i = (d.top - e.top) * g;
            let j = (d.right - e.left) * f;
            let k = (d.bottom - e.top) * g;
            d = new Rect(0, h, i, j - h, k - i);
            let l = this.e.dp.bh(d, c, truncate(512));
            c.content = l;
            return c;
        }
        ar() {
            let a = this.e.x4;
            let b = this.e.xq;
            if (b.isEmpty) {
                return new Rect(0, 0, 0, 1, 1);
            }
            let c = (b.left - a.left) / a.width;
            let d = (b.top - a.top) / a.height;
            let e = (b.right - a.left) / a.width;
            let f = (b.bottom - a.top) / a.height;
            return new Rect(0, c, d, e - c, f - d);
        }
        as(a, b) {
            let c = a.info.xPosition * 512;
            let d = a.info.yPosition * 512;
            let e = c + 512;
            let f = d + 512;
            let g = c / b.d;
            let h = d / b.c;
            let i = 512 / b.d;
            let j = 512 / b.c;
            return new Rect(0, g, h, i, j);
        }
        c(a) {
            let b = 0;
            b = this.y;
            this.y++;
            let c = new TileZoomTile();
            c.info = a;
            c.iD = b;
            return c;
        }
        a() {
            let a = this.aq(this.aw);
            let b = this.x(a);
            let c = Math.pow(2, b) * 512;
            let d = c;
            let e = (this.ao.width / this.au.width) * c;
            let f = (this.ao.height / this.au.height) * d;
            let g = (this.ao.width / a.width);
            let h = (this.ao.height / a.height);
            let i = a.left * g;
            let j = a.top * h;
            let k = a.right * g;
            let l = a.bottom * h;
            let m = new TileZoomInfo();
            m.f = new Rect(0, i, j, k - i, l - j);
            let n = this.at(this.aw, m.f, b, g, h, e, f);
            let o = n.left * e;
            let p = n.top * f;
            let q = n.right * e;
            let r = n.bottom * f;
            m.g = new Rect(0, o, p, q - o, r - p);
            m.e = b;
            m.d = e;
            m.c = f;
            m.b = g;
            m.a = h;
            return m;
        }
        aq(a) {
            return a;
        }
        at(a, b, c, d, e, f, g) {
            let h = (a.left + a.right) / 2;
            let i = (a.top + a.bottom) / 2;
            let j = a.width;
            let k = a.height;
            j = j * d / f;
            k = k * e / g;
            let l = j / 2;
            let m = k / 2;
            return new Rect(0, h - l, i - m, j, k);
        }
        af() {
            let a = this.a();
            let b = 512 * a.b / a.d;
            let c = 512 * a.a / a.c;
            let d = truncate(Math.floor(a.f.left / b));
            let e = truncate(Math.floor(a.f.top / c));
            let f = truncate(Math.floor(a.f.right / b));
            let g = truncate(Math.floor(a.f.bottom / c));
            this.o.clear();
            for (let h = e; h <= g; h++) {
                for (let i = d; i <= f; i++) {
                    this.o.add(((() => {
                        let $ret = new TileZoomTileInfo();
                        $ret.xPosition = i;
                        $ret.yPosition = h;
                        $ret.zoomLevel = a.e;
                        return $ret;
                    })()));
                }
            }
        }
        x(a) {
            let b = 1 / a.width;
            let c = 1 / a.height;
            let d;
            let e;
            if (this.h) {
                d = Math.floor(logBase(b, 2));
                e = Math.floor(logBase(c, 2));
            }
            else {
                d = Math.ceil(logBase(b, 2));
                e = Math.ceil(logBase(c, 2));
            }
            let f = Math.min(d, e) + (this.v);
            return truncate(f);
        }
        ab() {
            if (!this.f) {
                return;
            }
            this.f = false;
            this.e.dp.b0();
            this.d.g();
        }
        al() {
            this.i = true;
        }
        ah() {
            this.i = false;
            this.e.dp.cm();
        }
        ae(a) {
            this.e.dp.b1(a);
        }
        ac() {
            this.m.clear();
            this.o.clear();
            this.k.clear();
        }
    }
    TileZoomManager.$t = markType(TileZoomManager, 'TileZoomManager');
    return TileZoomManager;
})();
/**
 * @hidden
 */
export let TileZoomTileCache = /*@__PURE__*/ (() => {
    class TileZoomTileCache extends Base {
        constructor(a, b) {
            super();
            this.c = new Dictionary$2(String_$type, TileZoomTile.$, 0);
            this.d = new LinkedList$1(TileZoomTile.$);
            this.e = 0;
            this.a = null;
            this.e = a;
            this.a = b;
        }
        g() {
            this.c.clear();
            this.d = new LinkedList$1(TileZoomTile.$);
        }
        i(a) {
            this.d.h(a.b);
            this.d.f(a);
            a.b = this.d.d;
        }
        f(a) {
            this.d.f(a);
            this.c.addItem(a.info.toString(), a);
            a.a = this;
            a.b = this.d.d;
            while (this.c.count > this.e) {
                this.h(this.d.c.c);
            }
        }
        h(a) {
            this.d.h(a.b);
            this.c.removeItem(a.info.toString());
            this.a.ae(a);
        }
        b(a) {
            if (this.c.containsKey(a.toString())) {
                let b = this.c.item(a.toString());
                b.touch();
                return b;
            }
            return null;
        }
    }
    TileZoomTileCache.$t = markType(TileZoomTileCache, 'TileZoomTileCache');
    return TileZoomTileCache;
})();
/**
 * @hidden
 */
export let TileZoomTile = /*@__PURE__*/ (() => {
    class TileZoomTile extends Base {
        constructor() {
            super(...arguments);
            this._info = null;
            this.a = null;
            this.b = null;
            this._content = null;
            this._xPosition = 0;
            this._width = 0;
            this._yPosition = 0;
            this._height = 0;
            this._iD = 0;
        }
        get info() {
            return this._info;
        }
        set info(a) {
            this._info = a;
        }
        touch() {
            if (this.a == null) {
                return;
            }
            this.a.i(this);
        }
        get content() {
            return this._content;
        }
        set content(a) {
            this._content = a;
        }
        get xPosition() {
            return this._xPosition;
        }
        set xPosition(a) {
            this._xPosition = a;
        }
        get width() {
            return this._width;
        }
        set width(a) {
            this._width = a;
        }
        get yPosition() {
            return this._yPosition;
        }
        set yPosition(a) {
            this._yPosition = a;
        }
        get height() {
            return this._height;
        }
        set height(a) {
            this._height = a;
        }
        get iD() {
            return this._iD;
        }
        set iD(a) {
            this._iD = a;
        }
    }
    TileZoomTile.$t = markType(TileZoomTile, 'TileZoomTile', Base.$, [ITileZoomTile_$type]);
    return TileZoomTile;
})();
