"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 __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.ClientResmonViewContribution = exports.ClientResmon = exports.CLIENT_RESMON_WIDGET_ICON = exports.CLIENT_RESMON_WIDGET_ID = void 0;
var browser_1 = require("@theia/core/lib/browser");
var fxdk_data_service_1 = require("fxdk-services/lib/browser/fxdk-data-service");
var inversify_1 = require("inversify");
var color_1 = require("../utils/color");
var base_resmon_1 = require("./base-resmon");
exports.CLIENT_RESMON_WIDGET_ID = 'fxdk-client-resmon';
exports.CLIENT_RESMON_WIDGET_ICON = 'fa fa-dashboard';
function f(n) {
    return n.toFixed(2);
}
function formatMemory(mem) {
    if (typeof mem === 'number') {
        if (mem <= 0) {
            return '?';
        }
        switch (true) {
            case mem >= (Math.pow(1024, 3)): {
                return f(mem / (Math.pow(1024, 3))) + ' GiB';
            }
            case mem >= (Math.pow(1024, 2)): {
                return f(mem / (Math.pow(1024, 2))) + ' MiB';
            }
            case mem >= 1024: {
                return f(mem / 1024) + ' KiB';
            }
            default: {
                return f(mem) + ' B';
            }
        }
    }
    return mem;
}
var ClientResmon = /** @class */ (function (_super) {
    __extends(ClientResmon, _super);
    function ClientResmon() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    ClientResmon.prototype.setup = function () {
        var _this = this;
        this.id = exports.CLIENT_RESMON_WIDGET_ID;
        this.title.caption = 'Client Resource Monitor';
        this.title.label = 'Client Resource Monitor';
        this.title.iconClass = 'fa fa-dashboard';
        this.title.closable = true;
        this.rowDefs = [
            {
                title: 'Resource',
                align: 'left',
                format: function (cell) {
                    if (typeof cell === 'string') {
                        var _a = __read(color_1.rgbForKey(cell), 3), r = _a[0], g = _a[1], b = _a[2];
                        return "<span class=resource-name style=\"background:rgb(" + r + ", " + g + ", " + b + ")\">" + cell + "</span>";
                    }
                    return cell;
                },
            },
            {
                title: 'CPU ms',
                align: 'right',
                format: function (cell) {
                    if (typeof cell === 'number') {
                        if (cell < 0) {
                            return '-';
                        }
                        return cell.toFixed(2) + ' ms';
                    }
                    return cell;
                },
            },
            {
                title: 'Time %',
                align: 'right',
                format: function (cell) {
                    if (typeof cell === 'number') {
                        if (cell < 0) {
                            return '-';
                        }
                        var intensityColor = "rgba(244, 5, 82, " + cell + ")";
                        var color = cell > .3
                            ? '#fff'
                            : 'inherit';
                        return "<span style=\"padding: 0 2px;background:" + intensityColor + "\"><b style=\"mix-blend-mode: difference;\">" + (cell * 100).toFixed(2) + "%</b></span>";
                    }
                    return cell;
                },
            },
            {
                title: 'Memory',
                align: 'right',
                format: formatMemory,
            },
            {
                title: 'Streaming',
                align: 'right',
                format: formatMemory,
            },
        ];
        this.toDispose.push(this.dataService.onClientResourcesData(function (data) { return _this.renderRows(data); }));
    };
    ClientResmon.prototype.initializeResmon = function () {
        this.renderRows(this.dataService.getClientResourcesData());
    };
    __decorate([
        inversify_1.inject(fxdk_data_service_1.FxdkDataService),
        __metadata("design:type", fxdk_data_service_1.FxdkDataService)
    ], ClientResmon.prototype, "dataService", void 0);
    ClientResmon = __decorate([
        inversify_1.injectable()
    ], ClientResmon);
    return ClientResmon;
}(base_resmon_1.BaseResmon));
exports.ClientResmon = ClientResmon;
var ClientResmonViewContribution = /** @class */ (function (_super) {
    __extends(ClientResmonViewContribution, _super);
    function ClientResmonViewContribution() {
        return _super.call(this, {
            widgetId: exports.CLIENT_RESMON_WIDGET_ID,
            widgetName: 'Client Resource Monitor',
            toggleCommandId: ClientResmonViewContribution_1.TOGGLE_COMMAND_ID,
            defaultWidgetOptions: {
                area: 'bottom',
            }
        }) || this;
    }
    ClientResmonViewContribution_1 = ClientResmonViewContribution;
    var ClientResmonViewContribution_1;
    ClientResmonViewContribution.TOGGLE_COMMAND_ID = 'Z_fxdkClientResmon:toggle';
    ClientResmonViewContribution = ClientResmonViewContribution_1 = __decorate([
        inversify_1.injectable(),
        __metadata("design:paramtypes", [])
    ], ClientResmonViewContribution);
    return ClientResmonViewContribution;
}(browser_1.AbstractViewContribution));
exports.ClientResmonViewContribution = ClientResmonViewContribution;
//# sourceMappingURL=client-resmon.js.map