"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __values = (this && this.__values) || function(o) {
    var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
    if (m) return m.call(o);
    if (o && typeof o.length === "number") return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
    throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ResmonWidgetViewContribution = exports.ResmonWidget = exports.RESMON_WIDGET_ICON = exports.RESMON_WIDGET_ID = void 0;
var inversify_1 = require("inversify");
var browser_1 = require("@theia/core/lib/browser");
var widget_1 = require("@theia/core/lib/browser/widgets/widget");
var fxdk_data_service_1 = require("fxdk-services/lib/browser/fxdk-data-service");
var formatters_1 = require("./formatters");
exports.RESMON_WIDGET_ID = 'fxdk-resmon';
exports.RESMON_WIDGET_ICON = 'fa fa-dashboard';
var ResmonWidget = /** @class */ (function (_super) {
    __extends(ResmonWidget, _super);
    function ResmonWidget() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.resourceNodes = {};
        _this.initialized = false;
        return _this;
    }
    ResmonWidget.prototype.init = function () {
        var _this = this;
        this.id = exports.RESMON_WIDGET_ID;
        this.title.caption = 'Resource Monitor';
        this.title.label = 'Resource Monitor';
        this.title.iconClass = 'fa fa-dashboard';
        this.title.closable = true;
        this.addClass('fxdk-resmon-widget');
        this.scrollOptions = {};
        this.toDispose.pushAll([
            this.dataService.onClientResourcesData(function (data) { return _this.renderResourcesData(false, data); }),
            this.dataService.onServerResourcesData(function (data) { return _this.renderResourcesData(true, data); }),
        ]);
    };
    ResmonWidget.prototype.renderResourcesData = function (server, data) {
        var e_1, _a, e_2, _b;
        if (!this.initialized) {
            return;
        }
        var aliveResourceNames = Object.create(null);
        try {
            for (var data_1 = __values(data), data_1_1 = data_1.next(); !data_1_1.done; data_1_1 = data_1.next()) {
                var _c = __read(data_1_1.value, 5), resourceName = _c[0], cpu = _c[1], time = _c[2], memory = _c[3], streaming = _c[4];
                aliveResourceNames[resourceName] = true;
                var resourceNode = this.getOrCreateResourceNode(resourceName);
                if (server) {
                    resourceNode.serverCpu.innerHTML = formatters_1.formatMS(cpu) + '';
                    resourceNode.serverTime.innerHTML = formatters_1.formatTimePercentage(time) + '';
                    resourceNode.serverMemory.innerHTML = formatters_1.formatMemory('+', memory) + '';
                }
                else {
                    resourceNode.clientCpu.innerHTML = formatters_1.formatMS(cpu) + '';
                    resourceNode.clientTime.innerHTML = formatters_1.formatTimePercentage(time) + '';
                    resourceNode.clientMemory.innerHTML = formatters_1.formatMemory('+', memory) + '';
                    resourceNode.streaming.innerHTML = formatters_1.formatMemory('', streaming) + '';
                }
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (data_1_1 && !data_1_1.done && (_a = data_1.return)) _a.call(data_1);
            }
            finally { if (e_1) throw e_1.error; }
        }
        try {
            for (var _d = __values(Object.keys(this.resourceNodes)), _e = _d.next(); !_e.done; _e = _d.next()) {
                var resourceName = _e.value;
                if (!aliveResourceNames[resourceName]) {
                    this.deleteResourceNode(resourceName);
                }
            }
        }
        catch (e_2_1) { e_2 = { error: e_2_1 }; }
        finally {
            try {
                if (_e && !_e.done && (_b = _d.return)) _b.call(_d);
            }
            finally { if (e_2) throw e_2.error; }
        }
    };
    ResmonWidget.prototype.getOrCreateResourceNode = function (resourceName) {
        var _a;
        if (!this.resourceNodes[resourceName]) {
            var row = document.createElement('tr');
            var name_1 = document.createElement('td');
            name_1.classList.add('align-left');
            name_1.innerHTML = formatters_1.formatResourceName(resourceName) + '';
            var node = this.resourceNodes[resourceName] = {
                row: row,
                clientCpu: document.createElement('td'),
                serverCpu: document.createElement('td'),
                clientTime: document.createElement('td'),
                serverTime: document.createElement('td'),
                clientMemory: document.createElement('td'),
                serverMemory: document.createElement('td'),
                streaming: document.createElement('td'),
            };
            node.clientCpu.classList.add('align-right');
            node.serverCpu.classList.add('align-right');
            node.clientTime.classList.add('align-right');
            node.serverTime.classList.add('align-right');
            node.clientMemory.classList.add('align-right');
            node.serverMemory.classList.add('align-right');
            node.streaming.classList.add('align-right');
            row.append(name_1, node.clientCpu, node.serverCpu, node.clientTime, node.serverTime, node.clientMemory, node.serverMemory, node.streaming);
            (_a = this.tbodyNode) === null || _a === void 0 ? void 0 : _a.appendChild(row);
        }
        return this.resourceNodes[resourceName];
    };
    ResmonWidget.prototype.deleteResourceNode = function (resourceName) {
        var _a;
        var resourceNode = this.resourceNodes[resourceName];
        if (resourceNode) {
            (_a = this.tbodyNode) === null || _a === void 0 ? void 0 : _a.removeChild(resourceNode.row);
            delete this.resourceNodes[resourceName];
        }
    };
    ResmonWidget.prototype.onActivateRequest = function (msg) {
        _super.prototype.onActivateRequest.call(this, msg);
        this.node.focus();
    };
    ResmonWidget.prototype.onAfterShow = function (msg) {
        _super.prototype.onAfterShow.call(this, msg);
        this.update();
    };
    ResmonWidget.prototype.onAfterAttach = function (msg) {
        _super.prototype.onAfterAttach.call(this, msg);
        this.update();
    };
    ResmonWidget.prototype.onBeforeDetach = function (msg) {
        _super.prototype.onBeforeDetach.call(this, msg);
    };
    ResmonWidget.prototype.onResize = function (msg) {
        _super.prototype.onResize.call(this, msg);
        this.update();
        this.updateScroll();
    };
    ResmonWidget.prototype.onUpdateRequest = function (msg) {
        _super.prototype.onUpdateRequest.call(this, msg);
        if (!this.isVisible || !this.isAttached) {
            return;
        }
        if (!this.initialized) {
            this.initialized = true;
            this.node.innerHTML = "\n        <table cellpadding=0 cellspacing=0>\n          <thead>\n            <th class=\"align-left\">Resource</th>\n            <th class=\"align-right\">Client CPU</th>\n            <th class=\"align-right\">Server CPU</th>\n            <th class=\"align-right\">Client Time %</th>\n            <th class=\"align-right\">Server Time %</th>\n            <th class=\"align-right\">Client Memory</th>\n            <th class=\"align-right\">Server Memory</th>\n            <th class=\"align-right\">Streaming</th>\n          </thead>\n          <tbody>\n          </tbody>\n        </table>\n      ";
            this.tbodyNode = this.node.querySelector('tbody');
            this.renderResourcesData(false, this.dataService.getClientResourcesData());
            this.renderResourcesData(true, this.dataService.getServerResourcesData());
        }
        this.updateScroll();
    };
    ResmonWidget.prototype.updateScroll = function () {
        var _a;
        (_a = this.scrollBar) === null || _a === void 0 ? void 0 : _a.update();
    };
    __decorate([
        inversify_1.inject(fxdk_data_service_1.FxdkDataService),
        __metadata("design:type", fxdk_data_service_1.FxdkDataService)
    ], ResmonWidget.prototype, "dataService", void 0);
    __decorate([
        inversify_1.postConstruct(),
        __metadata("design:type", Function),
        __metadata("design:paramtypes", []),
        __metadata("design:returntype", void 0)
    ], ResmonWidget.prototype, "init", null);
    ResmonWidget = __decorate([
        inversify_1.injectable()
    ], ResmonWidget);
    return ResmonWidget;
}(widget_1.BaseWidget));
exports.ResmonWidget = ResmonWidget;
var ResmonWidgetViewContribution = /** @class */ (function (_super) {
    __extends(ResmonWidgetViewContribution, _super);
    function ResmonWidgetViewContribution() {
        return _super.call(this, {
            widgetId: exports.RESMON_WIDGET_ID,
            widgetName: 'Resource Monitor',
            toggleCommandId: ResmonWidgetViewContribution_1.TOGGLE_COMMAND_ID,
            defaultWidgetOptions: {
                area: 'bottom',
            },
        }) || this;
    }
    ResmonWidgetViewContribution_1 = ResmonWidgetViewContribution;
    var ResmonWidgetViewContribution_1;
    ResmonWidgetViewContribution.TOGGLE_COMMAND_ID = 'Z_fxdkResmon:toggle';
    ResmonWidgetViewContribution = ResmonWidgetViewContribution_1 = __decorate([
        inversify_1.injectable(),
        __metadata("design:paramtypes", [])
    ], ResmonWidgetViewContribution);
    return ResmonWidgetViewContribution;
}(browser_1.AbstractViewContribution));
exports.ResmonWidgetViewContribution = ResmonWidgetViewContribution;
//# sourceMappingURL=combined-resmon.js.map