"use strict";
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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MonitorModel = void 0;
var inversify_1 = require("inversify");
var event_1 = require("@theia/core/lib/common/event");
var monitor_service_1 = require("../../common/protocol/monitor-service");
var browser_1 = require("@theia/core/lib/browser");
var boards_service_provider_1 = require("../boards/boards-service-provider");
var MonitorModel = /** @class */ (function () {
    function MonitorModel() {
        this._autoscroll = true;
        this._timestamp = false;
        this._baudRate = monitor_service_1.MonitorConfig.BaudRate.DEFAULT;
        this._lineEnding = MonitorModel_1.EOL.DEFAULT;
        this.onChangeEmitter = new event_1.Emitter();
    }
    MonitorModel_1 = MonitorModel;
    MonitorModel.prototype.onStart = function () {
        var _this = this;
        this.localStorageService.getData(MonitorModel_1.STORAGE_ID).then(function (state) {
            if (state) {
                _this.restoreState(state);
            }
        });
    };
    Object.defineProperty(MonitorModel.prototype, "onChange", {
        get: function () {
            return this.onChangeEmitter.event;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MonitorModel.prototype, "autoscroll", {
        get: function () {
            return this._autoscroll;
        },
        enumerable: false,
        configurable: true
    });
    MonitorModel.prototype.toggleAutoscroll = function () {
        var _this = this;
        this._autoscroll = !this._autoscroll;
        this.storeState();
        this.storeState().then(function () { return _this.onChangeEmitter.fire({ property: 'autoscroll', value: _this._autoscroll }); });
    };
    Object.defineProperty(MonitorModel.prototype, "timestamp", {
        get: function () {
            return this._timestamp;
        },
        enumerable: false,
        configurable: true
    });
    MonitorModel.prototype.toggleTimestamp = function () {
        var _this = this;
        this._timestamp = !this._timestamp;
        this.storeState().then(function () { return _this.onChangeEmitter.fire({ property: 'timestamp', value: _this._timestamp }); });
    };
    Object.defineProperty(MonitorModel.prototype, "baudRate", {
        get: function () {
            return this._baudRate;
        },
        set: function (baudRate) {
            var _this = this;
            this._baudRate = baudRate;
            this.storeState().then(function () { return _this.onChangeEmitter.fire({ property: 'baudRate', value: _this._baudRate }); });
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MonitorModel.prototype, "lineEnding", {
        get: function () {
            return this._lineEnding;
        },
        set: function (lineEnding) {
            var _this = this;
            this._lineEnding = lineEnding;
            this.storeState().then(function () { return _this.onChangeEmitter.fire({ property: 'lineEnding', value: _this._lineEnding }); });
        },
        enumerable: false,
        configurable: true
    });
    MonitorModel.prototype.restoreState = function (state) {
        this._autoscroll = state.autoscroll;
        this._timestamp = state.timestamp;
        this._baudRate = state.baudRate;
        this._lineEnding = state.lineEnding;
    };
    MonitorModel.prototype.storeState = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                return [2 /*return*/, this.localStorageService.setData(MonitorModel_1.STORAGE_ID, {
                        autoscroll: this._autoscroll,
                        timestamp: this._timestamp,
                        baudRate: this._baudRate,
                        lineEnding: this._lineEnding
                    })];
            });
        });
    };
    var MonitorModel_1;
    MonitorModel.STORAGE_ID = 'arduino-monitor-model';
    __decorate([
        inversify_1.inject(browser_1.LocalStorageService),
        __metadata("design:type", browser_1.LocalStorageService)
    ], MonitorModel.prototype, "localStorageService", void 0);
    __decorate([
        inversify_1.inject(boards_service_provider_1.BoardsServiceProvider),
        __metadata("design:type", boards_service_provider_1.BoardsServiceProvider)
    ], MonitorModel.prototype, "boardsServiceClient", void 0);
    MonitorModel = MonitorModel_1 = __decorate([
        inversify_1.injectable(),
        __metadata("design:paramtypes", [])
    ], MonitorModel);
    return MonitorModel;
}());
exports.MonitorModel = MonitorModel;
(function (MonitorModel) {
    var EOL;
    (function (EOL) {
        EOL.DEFAULT = '\n';
    })(EOL = MonitorModel.EOL || (MonitorModel.EOL = {}));
})(MonitorModel = exports.MonitorModel || (exports.MonitorModel = {}));
exports.MonitorModel = MonitorModel;
//# sourceMappingURL=monitor-model.js.map