"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 (b.hasOwnProperty(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.");
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenRecentSketch = void 0;
var inversify_1 = require("inversify");
var workspace_protocol_1 = require("@theia/workspace/lib/common/workspace-protocol");
var disposable_1 = require("@theia/core/lib/common/disposable");
var contribution_1 = require("./contribution");
var arduino_menus_1 = require("../menu/arduino-menus");
var main_menu_manager_1 = require("../../common/main-menu-manager");
var open_sketch_1 = require("./open-sketch");
var notification_center_1 = require("../notification-center");
var OpenRecentSketch = /** @class */ (function (_super) {
    __extends(OpenRecentSketch, _super);
    function OpenRecentSketch() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.toDisposeBeforeRegister = new Map();
        return _this;
    }
    OpenRecentSketch.prototype.onStart = function () {
        var _this = this;
        var refreshMenu = function (sketches) {
            _this.register(sketches);
            _this.mainMenuManager.update();
        };
        this.notificationCenter.onRecentSketchesChanged(function (_a) {
            var sketches = _a.sketches;
            return refreshMenu(sketches);
        });
        this.sketchService.recentlyOpenedSketches().then(refreshMenu);
    };
    OpenRecentSketch.prototype.registerMenus = function (registry) {
        registry.registerSubmenu(arduino_menus_1.ArduinoMenus.FILE__OPEN_RECENT_SUBMENU, 'Open Recent', { order: '2' });
    };
    OpenRecentSketch.prototype.register = function (sketches) {
        var e_1, _a;
        var _this = this;
        var order = 0;
        var _loop_1 = function (sketch) {
            var uri = sketch.uri;
            var toDispose = this_1.toDisposeBeforeRegister.get(uri);
            if (toDispose) {
                toDispose.dispose();
            }
            var command = { id: "arduino-open-recent--" + uri };
            var handler = { execute: function () { return _this.commandRegistry.executeCommand(open_sketch_1.OpenSketch.Commands.OPEN_SKETCH.id, sketch); } };
            this_1.commandRegistry.registerCommand(command, handler);
            this_1.menuRegistry.registerMenuAction(arduino_menus_1.ArduinoMenus.FILE__OPEN_RECENT_SUBMENU, { commandId: command.id, label: sketch.name, order: String(order) });
            this_1.toDisposeBeforeRegister.set(sketch.uri, new disposable_1.DisposableCollection(disposable_1.Disposable.create(function () { return _this.commandRegistry.unregisterCommand(command); }), disposable_1.Disposable.create(function () { return _this.menuRegistry.unregisterMenuAction(command); })));
        };
        var this_1 = this;
        try {
            for (var sketches_1 = __values(sketches), sketches_1_1 = sketches_1.next(); !sketches_1_1.done; sketches_1_1 = sketches_1.next()) {
                var sketch = sketches_1_1.value;
                _loop_1(sketch);
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (sketches_1_1 && !sketches_1_1.done && (_a = sketches_1.return)) _a.call(sketches_1);
            }
            finally { if (e_1) throw e_1.error; }
        }
    };
    __decorate([
        inversify_1.inject(contribution_1.CommandRegistry),
        __metadata("design:type", contribution_1.CommandRegistry)
    ], OpenRecentSketch.prototype, "commandRegistry", void 0);
    __decorate([
        inversify_1.inject(contribution_1.MenuModelRegistry),
        __metadata("design:type", contribution_1.MenuModelRegistry)
    ], OpenRecentSketch.prototype, "menuRegistry", void 0);
    __decorate([
        inversify_1.inject(main_menu_manager_1.MainMenuManager),
        __metadata("design:type", Object)
    ], OpenRecentSketch.prototype, "mainMenuManager", void 0);
    __decorate([
        inversify_1.inject(workspace_protocol_1.WorkspaceServer),
        __metadata("design:type", Object)
    ], OpenRecentSketch.prototype, "workspaceServer", void 0);
    __decorate([
        inversify_1.inject(notification_center_1.NotificationCenter),
        __metadata("design:type", notification_center_1.NotificationCenter)
    ], OpenRecentSketch.prototype, "notificationCenter", void 0);
    OpenRecentSketch = __decorate([
        inversify_1.injectable()
    ], OpenRecentSketch);
    return OpenRecentSketch;
}(contribution_1.SketchContribution));
exports.OpenRecentSketch = OpenRecentSketch;
//# sourceMappingURL=open-recent-sketch.js.map