/*
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, delegateCombine, fromEnum, markType, String_$type } from "igniteui-react-core/ES5/type";
import { Rect } from "igniteui-react-core/ES5/Rect";
import { Dictionary$2 } from "igniteui-react-core/ES5/Dictionary$2";
import { ITileZoomTile_$type } from "./ITileZoomTile";
import { List$1 } from "igniteui-react-core/ES5/List$1";
import { TileZoomTileInfo } from "./TileZoomTileInfo";
import { TileZoomInfo } from "./TileZoomInfo";
import { truncate, logBase, isInfinity, isNaN_ } from "igniteui-react-core/ES5/number";
import { LinkedList$1 } from "igniteui-react-core/ES5/LinkedList$1";
/**
 * @hidden
 */
var TileZoomManager = /** @class */ (function (_super) {
    tslib_1.__extends(TileZoomManager, _super);
    function TileZoomManager(a, b, c) {
        var _this = _super.call(this) || this;
        _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;
        var d = _this.e;
        d.actualWindowRectChanged = delegateCombine(d.actualWindowRectChanged, runOn(_this, _this.am));
        var 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();
        return _this;
    }
    TileZoomManager.prototype.ai = function () {
        this.av = this.e.m3;
        this.ao = this.ap(this.av);
        var a = this.s();
        this.ac();
        this.ab();
        this.q = a;
        this.v = this.t(a);
        this.au = new Rect(0, 0, 0, a, a);
    };
    TileZoomManager.prototype.ap = function (a) {
        return new Rect(0, a.left, a.top, a.width * this.p, a.height * this.r);
    };
    TileZoomManager.prototype.t = function (a) {
        var b = a / 512;
        var c = logBase(b, 2);
        return c;
    };
    TileZoomManager.prototype.s = function () {
        var a = Math.max(this.ao.width, this.ao.height);
        var b = 1;
        var c = 512;
        while (c < a) {
            b++;
            c = 512 * b;
        }
        return c;
    };
    TileZoomManager.prototype.an = function (a, b) {
        this.ai();
        this.aj();
    };
    TileZoomManager.prototype.am = function (a, b) {
        this.aj();
    };
    TileZoomManager.prototype.aj = function () {
        this.f = true;
        this.aw = this.e.mq;
        if (this.i) {
            if (this.j(this.aw)) {
                this.ai();
            }
            this.af();
            this.ak();
            this.ag();
        }
    };
    TileZoomManager.prototype.j = function (a) {
        var b = 1;
        var c = 1;
        if (this.g(a)) {
            if (a.width > a.height) {
                var d = 1;
                if (a.height != 0) {
                    d = a.width / a.height;
                }
                var e = 0;
                var 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 {
                var g = 1;
                if (a.width != 0) {
                    g = a.height / a.width;
                }
                var h = 0;
                var 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;
        }
        var j = c != this.r || b != this.p;
        this.p = b;
        this.r = c;
        return j;
    };
    TileZoomManager.prototype.g = function (a) {
        return (a.width / a.height) > 3 || (a.height / a.width) > 3;
    };
    TileZoomManager.prototype.ag = function () {
        var e_1, _a;
        var a = this.a();
        for (var b = 0; b < this.m.count; b++) {
            this.aa(a, this.m._inner[b]);
        }
        var c = new Dictionary$2(Base.$, ITileZoomTile_$type, 0);
        try {
            for (var _b = tslib_1.__values(fromEnum(this.l.keys)), _c = _b.next(); !_c.done; _c = _b.next()) {
                var d = _c.value;
                c.addItem(this.l.item(d).content, this.l.item(d));
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
            }
            finally { if (e_1) throw e_1.error; }
        }
        this.e.bn.b0(c, this.n, 512);
    };
    TileZoomManager.prototype.aa = function (a, b) {
        var c = this.as(b, a);
        var d = c.left * a.b;
        var e = c.top * a.a;
        var f = c.right * a.b;
        var g = c.bottom * a.a;
        var h = d - a.f.left;
        var i = e - a.f.top;
        var j = f - d;
        var k = g - e;
        b.xPosition = h / this.p;
        b.yPosition = i / this.r;
        b.width = j / this.p;
        b.height = k / this.r;
    };
    TileZoomManager.prototype.ak = function () {
        var e_2, _a;
        var a = this.a();
        this.k.clear();
        for (var b = 0; b < this.m.count; b++) {
            var c = this.m._inner[b];
            this.k.addItem(c, c);
        }
        this.m.clear();
        this.l.clear();
        this.n.clear();
        for (var d = 0; d < this.o.count; d++) {
            var e = this.o._inner[d];
            var 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);
        }
        try {
            for (var _b = tslib_1.__values(fromEnum(this.k.keys)), _c = _b.next(); !_c.done; _c = _b.next()) {
                var g = _c.value;
                this.ad(g);
            }
        }
        catch (e_2_1) { e_2 = { error: e_2_1 }; }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
            }
            finally { if (e_2) throw e_2.error; }
        }
    };
    TileZoomManager.prototype.ad = function (a) {
    };
    TileZoomManager.prototype.z = function (a) {
    };
    TileZoomManager.prototype.b = function (a, b) {
        var c = this.c(a);
        var d = this.as(c, b);
        var e = this.ar();
        var f = 1 / e.width;
        var g = e.height;
        if (e.width > e.height) {
            f = e.width;
            g = 1 / e.height;
        }
        var h = (d.left - e.left) * f;
        var i = (d.top - e.top) * g;
        var j = (d.right - e.left) * f;
        var k = (d.bottom - e.top) * g;
        d = new Rect(0, h, i, j - h, k - i);
        var l = this.e.bn.bf(d, c, truncate(512));
        c.content = l;
        return c;
    };
    TileZoomManager.prototype.ar = function () {
        var a = this.e.m3;
        var b = this.e.mu;
        if (b.isEmpty) {
            return new Rect(0, 0, 0, 1, 1);
        }
        var c = (b.left - a.left) / a.width;
        var d = (b.top - a.top) / a.height;
        var e = (b.right - a.left) / a.width;
        var f = (b.bottom - a.top) / a.height;
        return new Rect(0, c, d, e - c, f - d);
    };
    TileZoomManager.prototype.as = function (a, b) {
        var c = a.info.xPosition * 512;
        var d = a.info.yPosition * 512;
        var e = c + 512;
        var f = d + 512;
        var g = c / b.d;
        var h = d / b.c;
        var i = 512 / b.d;
        var j = 512 / b.c;
        return new Rect(0, g, h, i, j);
    };
    TileZoomManager.prototype.c = function (a) {
        var b = 0;
        b = this.y;
        this.y++;
        var c = new TileZoomTile();
        c.info = a;
        c.iD = b;
        return c;
    };
    TileZoomManager.prototype.a = function () {
        var a = this.aq(this.aw);
        var b = this.x(a);
        var c = Math.pow(2, b) * 512;
        var d = c;
        var e = (this.ao.width / this.au.width) * c;
        var f = (this.ao.height / this.au.height) * d;
        var g = (this.ao.width / a.width);
        var h = (this.ao.height / a.height);
        var i = a.left * g;
        var j = a.top * h;
        var k = a.right * g;
        var l = a.bottom * h;
        var m = new TileZoomInfo();
        m.f = new Rect(0, i, j, k - i, l - j);
        var n = this.at(this.aw, m.f, b, g, h, e, f);
        var o = n.left * e;
        var p = n.top * f;
        var q = n.right * e;
        var 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;
    };
    TileZoomManager.prototype.aq = function (a) {
        return a;
    };
    TileZoomManager.prototype.at = function (a, b, c, d, e, f, g) {
        var h = (a.left + a.right) / 2;
        var i = (a.top + a.bottom) / 2;
        var j = a.width;
        var k = a.height;
        j = j * d / f;
        k = k * e / g;
        var l = j / 2;
        var m = k / 2;
        return new Rect(0, h - l, i - m, j, k);
    };
    TileZoomManager.prototype.af = function () {
        var a = this.a();
        var b = 512 * a.b / a.d;
        var c = 512 * a.a / a.c;
        var d = truncate(Math.floor(a.f.left / b));
        var e = truncate(Math.floor(a.f.top / c));
        var f = truncate(Math.floor(a.f.right / b));
        var g = truncate(Math.floor(a.f.bottom / c));
        this.o.clear();
        var _loop_1 = function (h) {
            var _loop_2 = function (i) {
                this_1.o.add(((function () {
                    var $ret = new TileZoomTileInfo();
                    $ret.xPosition = i;
                    $ret.yPosition = h;
                    $ret.zoomLevel = a.e;
                    return $ret;
                })()));
            };
            for (var i = d; i <= f; i++) {
                _loop_2(i);
            }
        };
        var this_1 = this;
        for (var h = e; h <= g; h++) {
            _loop_1(h);
        }
    };
    TileZoomManager.prototype.x = function (a) {
        var b = 1 / a.width;
        var c = 1 / a.height;
        var d;
        var 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));
        }
        var f = Math.min(d, e) + (this.v);
        return truncate(f);
    };
    TileZoomManager.prototype.ab = function () {
        if (!this.f) {
            return;
        }
        this.f = false;
        this.e.bn.bx();
        this.d.g();
    };
    TileZoomManager.prototype.al = function () {
        this.i = true;
    };
    TileZoomManager.prototype.ah = function () {
        this.i = false;
        this.e.bn.cf();
    };
    TileZoomManager.prototype.ae = function (a) {
        this.e.bn.by(a);
    };
    TileZoomManager.prototype.ac = function () {
        this.m.clear();
        this.o.clear();
        this.k.clear();
    };
    TileZoomManager.$t = markType(TileZoomManager, 'TileZoomManager');
    return TileZoomManager;
}(Base));
export { TileZoomManager };
/**
 * @hidden
 */
var TileZoomTileCache = /** @class */ (function (_super) {
    tslib_1.__extends(TileZoomTileCache, _super);
    function TileZoomTileCache(a, b) {
        var _this = _super.call(this) || this;
        _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;
        return _this;
    }
    TileZoomTileCache.prototype.g = function () {
        this.c.clear();
        this.d = new LinkedList$1(TileZoomTile.$);
    };
    TileZoomTileCache.prototype.i = function (a) {
        this.d.h(a.b);
        this.d.f(a);
        a.b = this.d.d;
    };
    TileZoomTileCache.prototype.f = function (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);
        }
    };
    TileZoomTileCache.prototype.h = function (a) {
        this.d.h(a.b);
        this.c.removeItem(a.info.toString());
        this.a.ae(a);
    };
    TileZoomTileCache.prototype.b = function (a) {
        if (this.c.containsKey(a.toString())) {
            var b = this.c.item(a.toString());
            b.touch();
            return b;
        }
        return null;
    };
    TileZoomTileCache.$t = markType(TileZoomTileCache, 'TileZoomTileCache');
    return TileZoomTileCache;
}(Base));
export { TileZoomTileCache };
/**
 * @hidden
 */
var TileZoomTile = /** @class */ (function (_super) {
    tslib_1.__extends(TileZoomTile, _super);
    function TileZoomTile() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _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;
        return _this;
    }
    Object.defineProperty(TileZoomTile.prototype, "info", {
        get: function () {
            return this._info;
        },
        set: function (a) {
            this._info = a;
        },
        enumerable: true,
        configurable: true
    });
    TileZoomTile.prototype.touch = function () {
        if (this.a == null) {
            return;
        }
        this.a.i(this);
    };
    Object.defineProperty(TileZoomTile.prototype, "content", {
        get: function () {
            return this._content;
        },
        set: function (a) {
            this._content = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(TileZoomTile.prototype, "xPosition", {
        get: function () {
            return this._xPosition;
        },
        set: function (a) {
            this._xPosition = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(TileZoomTile.prototype, "width", {
        get: function () {
            return this._width;
        },
        set: function (a) {
            this._width = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(TileZoomTile.prototype, "yPosition", {
        get: function () {
            return this._yPosition;
        },
        set: function (a) {
            this._yPosition = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(TileZoomTile.prototype, "height", {
        get: function () {
            return this._height;
        },
        set: function (a) {
            this._height = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(TileZoomTile.prototype, "iD", {
        get: function () {
            return this._iD;
        },
        set: function (a) {
            this._iD = a;
        },
        enumerable: true,
        configurable: true
    });
    TileZoomTile.$t = markType(TileZoomTile, 'TileZoomTile', Base.$, [ITileZoomTile_$type]);
    return TileZoomTile;
}(Base));
export { TileZoomTile };
