"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var Direct_1 = require("./Direct");
var State_1 = require("../State");
var Loop = /** @class */ (function (_super) {
    tslib_1.__extends(Loop, _super);
    function Loop() {
        var _this = _super.call(this) || this;
        _this.logDrawTimes = false;
        _this.maxFPS = 24;
        _this.lastRender = 0;
        _this.enabled = false;
        _this.redraw = _this.redraw.bind(_this);
        _this.framedRedraw = _this.framedRedraw.bind(_this);
        return _this;
    }
    Loop.prototype.requestRedraw = function (force) {
        if (force === true) {
            this.lastRender = Date.now();
            this.redraw();
        }
    };
    /** enable render loop */
    Loop.prototype.enable = function () {
        if (this.enabled === true) {
            return;
        }
        this.enabled = true;
        this.framedRedraw();
    };
    /** disable render loop. if requestRedraw(true) is called, the redraw will be done anyway */
    Loop.prototype.disable = function () {
        this.enabled = false;
        if (this.currentFrame !== undefined) {
            window.cancelAnimationFrame(this.currentFrame);
            this.currentFrame = undefined;
        }
    };
    Loop.prototype.framedRedraw = function () {
        var tTimestamp = Date.now();
        var tDiff = tTimestamp - this.lastRender;
        var tInterval = 1000 / this.maxFPS;
        if (tDiff > tInterval) {
            if (this.logDrawTimes === true) {
                console.log("Redrawn in " + tDiff + "ms");
            }
            this.redraw();
            this.lastRender = tTimestamp - (tDiff % tInterval);
        }
        this.currentFrame = window.requestAnimationFrame(this.framedRedraw);
    };
    Loop.prototype.redraw = function () {
        State_1.State.API["RenderInternal"]();
    };
    return Loop;
}(Direct_1.Direct));
exports.Loop = Loop;
