"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 __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
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 };
    }
};
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;
};
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.AdditionalUrlsDialog = exports.SettingsDialog = exports.SettingsDialogProps = exports.SettingsWidget = exports.SettingsComponent = exports.SettingsService = exports.Settings = void 0;
var React = require("react");
var inversify_1 = require("inversify");
var widgets_1 = require("@phosphor/widgets");
var react_tabs_1 = require("react-tabs");
require("react-tabs/style/react-tabs.css");
var react_disable_1 = require("react-disable");
var uri_1 = require("@theia/core/lib/common/uri");
var event_1 = require("@theia/core/lib/common/event");
var promise_util_1 = require("@theia/core/lib/common/promise-util");
var objects_1 = require("@theia/core/lib/common/objects");
var file_service_1 = require("@theia/filesystem/lib/browser/file-service");
var theming_1 = require("@theia/core/lib/browser/theming");
var window_service_1 = require("@theia/core/lib/browser/window/window-service");
var file_dialog_service_1 = require("@theia/filesystem/lib/browser/file-dialog/file-dialog-service");
var disposable_1 = require("@theia/core/lib/common/disposable");
var frontend_application_state_1 = require("@theia/core/lib/browser/frontend-application-state");
var browser_1 = require("@theia/core/lib/browser");
var protocol_1 = require("../common/protocol");
var Settings;
(function (Settings) {
    function belongsToCli(key) {
        return key === 'sketchbookPath' || key === 'additionalUrls';
    }
    Settings.belongsToCli = belongsToCli;
})(Settings = exports.Settings || (exports.Settings = {}));
var SettingsService = /** @class */ (function () {
    function SettingsService() {
        this.onDidChangeEmitter = new event_1.Emitter();
        this.onDidChange = this.onDidChangeEmitter.event;
        this.ready = new promise_util_1.Deferred();
    }
    SettingsService.prototype.init = function () {
        return __awaiter(this, void 0, void 0, function () {
            var settings;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.appStateService.reachedState('ready')];
                    case 1:
                        _a.sent(); // Hack for https://github.com/eclipse-theia/theia/issues/8993
                        return [4 /*yield*/, this.loadSettings()];
                    case 2:
                        settings = _a.sent();
                        this._settings = objects_1.deepClone(settings);
                        this.ready.resolve();
                        return [2 /*return*/];
                }
            });
        });
    };
    SettingsService.prototype.loadSettings = function () {
        return __awaiter(this, void 0, void 0, function () {
            var _a, editorFontSize, themeId, autoSave, autoScaleInterface, interfaceScale, 
            // checkForUpdates,
            verboseOnCompile, verboseOnUpload, verifyAfterUpload, enableLsLogs, cliConfig, additionalUrls, sketchDirUri, network, sketchbookPath;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0: return [4 /*yield*/, this.preferenceService.ready];
                    case 1:
                        _b.sent();
                        return [4 /*yield*/, Promise.all([
                                this.preferenceService.get('editor.fontSize', 12),
                                this.preferenceService.get('workbench.colorTheme', 'arduino-theme'),
                                this.preferenceService.get('editor.autoSave', 'on'),
                                this.preferenceService.get('arduino.window.autoScale', true),
                                this.preferenceService.get('arduino.window.zoomLevel', 0),
                                // this.preferenceService.get<string>('arduino.ide.autoUpdate', true),
                                this.preferenceService.get('arduino.compile.verbose', true),
                                this.preferenceService.get('arduino.upload.verbose', true),
                                this.preferenceService.get('arduino.upload.verify', true),
                                this.preferenceService.get('arduino.language.log', true),
                                this.configService.getConfiguration()
                            ])];
                    case 2:
                        _a = __read.apply(void 0, [_b.sent(), 10]), editorFontSize = _a[0], themeId = _a[1], autoSave = _a[2], autoScaleInterface = _a[3], interfaceScale = _a[4], verboseOnCompile = _a[5], verboseOnUpload = _a[6], verifyAfterUpload = _a[7], enableLsLogs = _a[8], cliConfig = _a[9];
                        additionalUrls = cliConfig.additionalUrls, sketchDirUri = cliConfig.sketchDirUri, network = cliConfig.network;
                        return [4 /*yield*/, this.fileService.fsPath(new uri_1.default(sketchDirUri))];
                    case 3:
                        sketchbookPath = _b.sent();
                        return [2 /*return*/, {
                                editorFontSize: editorFontSize,
                                themeId: themeId,
                                autoSave: autoSave,
                                autoScaleInterface: autoScaleInterface,
                                interfaceScale: interfaceScale,
                                // checkForUpdates,
                                verboseOnCompile: verboseOnCompile,
                                verboseOnUpload: verboseOnUpload,
                                verifyAfterUpload: verifyAfterUpload,
                                enableLsLogs: enableLsLogs,
                                additionalUrls: additionalUrls,
                                sketchbookPath: sketchbookPath,
                                network: network
                            }];
                }
            });
        });
    };
    SettingsService.prototype.settings = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.ready.promise];
                    case 1:
                        _a.sent();
                        return [2 /*return*/, this._settings];
                }
            });
        });
    };
    SettingsService.prototype.update = function (settings, fireDidChange) {
        if (fireDidChange === void 0) { fireDidChange = false; }
        return __awaiter(this, void 0, void 0, function () {
            var _a, _b, key;
            var e_1, _c;
            return __generator(this, function (_d) {
                switch (_d.label) {
                    case 0: return [4 /*yield*/, this.ready.promise];
                    case 1:
                        _d.sent();
                        try {
                            for (_a = __values(Object.keys(settings)), _b = _a.next(); !_b.done; _b = _a.next()) {
                                key = _b.value;
                                this._settings[key] = settings[key];
                            }
                        }
                        catch (e_1_1) { e_1 = { error: e_1_1 }; }
                        finally {
                            try {
                                if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
                            }
                            finally { if (e_1) throw e_1.error; }
                        }
                        if (fireDidChange) {
                            this.onDidChangeEmitter.fire(this._settings);
                        }
                        return [2 /*return*/];
                }
            });
        });
    };
    SettingsService.prototype.reset = function () {
        return __awaiter(this, void 0, void 0, function () {
            var settings;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.loadSettings()];
                    case 1:
                        settings = _a.sent();
                        return [2 /*return*/, this.update(settings, true)];
                }
            });
        });
    };
    SettingsService.prototype.validate = function (settings) {
        if (settings === void 0) { settings = this.settings(); }
        return __awaiter(this, void 0, void 0, function () {
            var _a, sketchbookPath, editorFontSize, themeId_1, sketchbookDir, err_1;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        _b.trys.push([0, 4, , 5]);
                        return [4 /*yield*/, settings];
                    case 1:
                        _a = _b.sent(), sketchbookPath = _a.sketchbookPath, editorFontSize = _a.editorFontSize, themeId_1 = _a.themeId;
                        return [4 /*yield*/, this.fileSystemExt.getUri(sketchbookPath)];
                    case 2:
                        sketchbookDir = _b.sent();
                        return [4 /*yield*/, this.fileService.exists(new uri_1.default(sketchbookDir))];
                    case 3:
                        if (!(_b.sent())) {
                            return [2 /*return*/, "Invalid sketchbook location: " + sketchbookPath];
                        }
                        if (editorFontSize <= 0) {
                            return [2 /*return*/, "Invalid editor font size. It must be a positive integer."];
                        }
                        if (!theming_1.ThemeService.get().getThemes().find(function (_a) {
                            var id = _a.id;
                            return id === themeId_1;
                        })) {
                            return [2 /*return*/, "Invalid theme."];
                        }
                        return [2 /*return*/, true];
                    case 4:
                        err_1 = _b.sent();
                        if (err_1 instanceof Error) {
                            return [2 /*return*/, err_1.message];
                        }
                        return [2 /*return*/, String(err_1)];
                    case 5: return [2 /*return*/];
                }
            });
        });
    };
    SettingsService.prototype.save = function () {
        return __awaiter(this, void 0, void 0, function () {
            var _a, editorFontSize, themeId, autoSave, autoScaleInterface, interfaceScale, 
            // checkForUpdates,
            verboseOnCompile, verboseOnUpload, verifyAfterUpload, enableLsLogs, sketchbookPath, additionalUrls, network, _b, config, sketchDirUri;
            return __generator(this, function (_c) {
                switch (_c.label) {
                    case 0: return [4 /*yield*/, this.ready.promise];
                    case 1:
                        _c.sent();
                        _a = this._settings, editorFontSize = _a.editorFontSize, themeId = _a.themeId, autoSave = _a.autoSave, autoScaleInterface = _a.autoScaleInterface, interfaceScale = _a.interfaceScale, verboseOnCompile = _a.verboseOnCompile, verboseOnUpload = _a.verboseOnUpload, verifyAfterUpload = _a.verifyAfterUpload, enableLsLogs = _a.enableLsLogs, sketchbookPath = _a.sketchbookPath, additionalUrls = _a.additionalUrls, network = _a.network;
                        return [4 /*yield*/, Promise.all([
                                this.configService.getConfiguration(),
                                this.fileSystemExt.getUri(sketchbookPath)
                            ])];
                    case 2:
                        _b = __read.apply(void 0, [_c.sent(), 2]), config = _b[0], sketchDirUri = _b[1];
                        config.additionalUrls = additionalUrls;
                        config.sketchDirUri = sketchDirUri;
                        config.network = network;
                        return [4 /*yield*/, Promise.all([
                                this.preferenceService.set('editor.fontSize', editorFontSize, browser_1.PreferenceScope.User),
                                this.preferenceService.set('workbench.colorTheme', themeId, browser_1.PreferenceScope.User),
                                this.preferenceService.set('editor.autoSave', autoSave, browser_1.PreferenceScope.User),
                                this.preferenceService.set('arduino.window.autoScale', autoScaleInterface, browser_1.PreferenceScope.User),
                                this.preferenceService.set('arduino.window.zoomLevel', interfaceScale, browser_1.PreferenceScope.User),
                                // this.preferenceService.set('arduino.ide.autoUpdate', checkForUpdates, PreferenceScope.User),
                                this.preferenceService.set('arduino.compile.verbose', verboseOnCompile, browser_1.PreferenceScope.User),
                                this.preferenceService.set('arduino.upload.verbose', verboseOnUpload, browser_1.PreferenceScope.User),
                                this.preferenceService.set('arduino.upload.verify', verifyAfterUpload, browser_1.PreferenceScope.User),
                                this.preferenceService.set('arduino.language.log', enableLsLogs, browser_1.PreferenceScope.User),
                                this.configService.setConfiguration(config)
                            ])];
                    case 3:
                        _c.sent();
                        this.onDidChangeEmitter.fire(this._settings);
                        return [2 /*return*/, true];
                }
            });
        });
    };
    __decorate([
        inversify_1.inject(file_service_1.FileService),
        __metadata("design:type", file_service_1.FileService)
    ], SettingsService.prototype, "fileService", void 0);
    __decorate([
        inversify_1.inject(protocol_1.FileSystemExt),
        __metadata("design:type", Object)
    ], SettingsService.prototype, "fileSystemExt", void 0);
    __decorate([
        inversify_1.inject(protocol_1.ConfigService),
        __metadata("design:type", Object)
    ], SettingsService.prototype, "configService", void 0);
    __decorate([
        inversify_1.inject(browser_1.PreferenceService),
        __metadata("design:type", Object)
    ], SettingsService.prototype, "preferenceService", void 0);
    __decorate([
        inversify_1.inject(frontend_application_state_1.FrontendApplicationStateService),
        __metadata("design:type", frontend_application_state_1.FrontendApplicationStateService)
    ], SettingsService.prototype, "appStateService", void 0);
    __decorate([
        inversify_1.postConstruct(),
        __metadata("design:type", Function),
        __metadata("design:paramtypes", []),
        __metadata("design:returntype", Promise)
    ], SettingsService.prototype, "init", null);
    SettingsService = __decorate([
        inversify_1.injectable()
    ], SettingsService);
    return SettingsService;
}());
exports.SettingsService = SettingsService;
var SettingsComponent = /** @class */ (function (_super) {
    __extends(SettingsComponent, _super);
    function SettingsComponent(props) {
        var _this = _super.call(this, props) || this;
        _this.toDispose = new disposable_1.DisposableCollection();
        _this.noopKeyDown = function (event) {
            if (_this.isControlKey(event)) {
                return;
            }
            event.nativeEvent.preventDefault();
            event.nativeEvent.returnValue = false;
        };
        _this.numbersOnlyKeyDown = function (event) {
            if (_this.isControlKey(event)) {
                return;
            }
            var key = Number(event.key);
            if (isNaN(key) || event.key === null || event.key === ' ') {
                event.nativeEvent.preventDefault();
                event.nativeEvent.returnValue = false;
                return;
            }
        };
        _this.browseSketchbookDidClick = function () { return __awaiter(_this, void 0, void 0, function () {
            var uri, sketchbookPath;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.props.fileDialogService.showOpenDialog({
                            title: 'Select new sketchbook location',
                            openLabel: 'Chose',
                            canSelectFiles: false,
                            canSelectMany: false,
                            canSelectFolders: true
                        })];
                    case 1:
                        uri = _a.sent();
                        if (!uri) return [3 /*break*/, 3];
                        return [4 /*yield*/, this.props.fileService.fsPath(uri)];
                    case 2:
                        sketchbookPath = _a.sent();
                        this.setState({ sketchbookPath: sketchbookPath });
                        _a.label = 3;
                    case 3: return [2 /*return*/];
                }
            });
        }); };
        _this.editAdditionalUrlDidClick = function () { return __awaiter(_this, void 0, void 0, function () {
            var additionalUrls;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, new AdditionalUrlsDialog(this.state.additionalUrls, this.props.windowService).open()];
                    case 1:
                        additionalUrls = _a.sent();
                        if (additionalUrls) {
                            this.setState({ additionalUrls: additionalUrls });
                        }
                        return [2 /*return*/];
                }
            });
        }); };
        _this.editorFontSizeDidChange = function (event) {
            var value = event.target.value;
            if (value) {
                _this.setState({ editorFontSize: parseInt(value, 10) });
            }
        };
        _this.additionalUrlsDidChange = function (event) {
            _this.setState({ additionalUrls: event.target.value.split(',').map(function (url) { return url.trim(); }) });
        };
        _this.autoScaleInterfaceDidChange = function (event) {
            _this.setState({ autoScaleInterface: event.target.checked });
        };
        _this.enableLsLogsDidChange = function (event) {
            _this.setState({ enableLsLogs: event.target.checked });
        };
        _this.interfaceScaleDidChange = function (event) {
            var value = event.target.value;
            var percentage = parseInt(value, 10);
            if (isNaN(percentage)) {
                return;
            }
            var interfaceScale = (percentage - 100) / 20;
            if (!isNaN(interfaceScale)) {
                _this.setState({ interfaceScale: interfaceScale });
            }
        };
        _this.verifyAfterUploadDidChange = function (event) {
            _this.setState({ verifyAfterUpload: event.target.checked });
        };
        _this.checkForUpdatesDidChange = function (event) {
            _this.setState({ checkForUpdates: event.target.checked });
        };
        _this.autoSaveDidChange = function (event) {
            _this.setState({ autoSave: event.target.checked ? 'on' : 'off' });
        };
        _this.themeDidChange = function (event) {
            var selectedIndex = event.target.options.selectedIndex;
            var theme = theming_1.ThemeService.get().getThemes()[selectedIndex];
            if (theme) {
                _this.setState({ themeId: theme.id });
            }
        };
        _this.verboseOnCompileDidChange = function (event) {
            _this.setState({ verboseOnCompile: event.target.checked });
        };
        _this.verboseOnUploadDidChange = function (event) {
            _this.setState({ verboseOnUpload: event.target.checked });
        };
        _this.sketchpathDidChange = function (event) {
            var sketchbookPath = event.target.value;
            if (sketchbookPath) {
                _this.setState({ sketchbookPath: sketchbookPath });
            }
        };
        _this.noProxyDidChange = function (event) {
            if (event.target.checked) {
                _this.setState({ network: 'none' });
            }
            else {
                _this.setState({ network: protocol_1.Network.Default() });
            }
        };
        _this.manualProxyDidChange = function (event) {
            if (event.target.checked) {
                _this.setState({ network: protocol_1.Network.Default() });
            }
            else {
                _this.setState({ network: 'none' });
            }
        };
        _this.httpProtocolDidChange = function (event) {
            if (_this.state.network !== 'none') {
                var network = _this.cloneProxySettings;
                network.protocol = event.target.checked ? 'http' : 'socks';
                _this.setState({ network: network });
            }
        };
        _this.socksProtocolDidChange = function (event) {
            if (_this.state.network !== 'none') {
                var network = _this.cloneProxySettings;
                network.protocol = event.target.checked ? 'socks' : 'http';
                _this.setState({ network: network });
            }
        };
        _this.hostnameDidChange = function (event) {
            if (_this.state.network !== 'none') {
                var network = _this.cloneProxySettings;
                network.hostname = event.target.value;
                _this.setState({ network: network });
            }
        };
        _this.portDidChange = function (event) {
            if (_this.state.network !== 'none') {
                var network = _this.cloneProxySettings;
                network.port = event.target.value;
                _this.setState({ network: network });
            }
        };
        _this.usernameDidChange = function (event) {
            if (_this.state.network !== 'none') {
                var network = _this.cloneProxySettings;
                network.username = event.target.value;
                _this.setState({ network: network });
            }
        };
        _this.passwordDidChange = function (event) {
            if (_this.state.network !== 'none') {
                var network = _this.cloneProxySettings;
                network.password = event.target.value;
                _this.setState({ network: network });
            }
        };
        return _this;
    }
    SettingsComponent.prototype.componentDidUpdate = function (_, prevState) {
        if (this.state && prevState && JSON.stringify(this.state) !== JSON.stringify(prevState)) {
            this.props.settingsService.update(this.state, true);
        }
    };
    SettingsComponent.prototype.componentDidMount = function () {
        var _this = this;
        this.props.settingsService.settings().then(function (settings) { return _this.setState(settings); });
        this.toDispose.push(this.props.settingsService.onDidChange(function (settings) { return _this.setState(settings); }));
    };
    SettingsComponent.prototype.componentWillUnmount = function () {
        this.toDispose.dispose();
    };
    SettingsComponent.prototype.render = function () {
        if (!this.state) {
            return React.createElement("div", null);
        }
        return React.createElement(react_tabs_1.Tabs, null,
            React.createElement(react_tabs_1.TabList, null,
                React.createElement(react_tabs_1.Tab, null, "Settings"),
                React.createElement(react_tabs_1.Tab, null, "Network")),
            React.createElement(react_tabs_1.TabPanel, null, this.renderSettings()),
            React.createElement(react_tabs_1.TabPanel, null, this.renderNetwork()));
    };
    SettingsComponent.prototype.renderSettings = function () {
        return React.createElement("div", { className: 'content noselect' },
            "Sketchbook location:",
            React.createElement("div", { className: 'flex-line' },
                React.createElement("input", { className: 'theia-input stretch', type: 'text', value: this.state.sketchbookPath, onChange: this.sketchpathDidChange }),
                React.createElement("button", { className: 'theia-button shrink', onClick: this.browseSketchbookDidClick }, "Browse")),
            React.createElement("div", { className: 'flex-line' },
                React.createElement("div", { className: 'column' },
                    React.createElement("div", { className: 'flex-line' }, "Editor font size:"),
                    React.createElement("div", { className: 'flex-line' }, "Interface scale:"),
                    React.createElement("div", { className: 'flex-line' }, "Theme:"),
                    React.createElement("div", { className: 'flex-line' }, "Show verbose output during:")),
                React.createElement("div", { className: 'column' },
                    React.createElement("div", { className: 'flex-line' },
                        React.createElement("input", { className: 'theia-input small', type: 'number', step: 1, pattern: '[0-9]+', onKeyDown: this.numbersOnlyKeyDown, value: this.state.editorFontSize, onChange: this.editorFontSizeDidChange })),
                    React.createElement("div", { className: 'flex-line' },
                        React.createElement("label", { className: 'flex-line' },
                            React.createElement("input", { type: 'checkbox', checked: this.state.autoScaleInterface, onChange: this.autoScaleInterfaceDidChange }),
                            "Automatic"),
                        React.createElement("input", { className: 'theia-input small with-margin', type: 'number', step: 20, pattern: '[0-9]+', onKeyDown: this.noopKeyDown, value: 100 + this.state.interfaceScale * 20, onChange: this.interfaceScaleDidChange }),
                        "%"),
                    React.createElement("div", { className: 'flex-line' },
                        React.createElement("select", { className: 'theia-select', value: theming_1.ThemeService.get().getCurrentTheme().label, onChange: this.themeDidChange }, theming_1.ThemeService.get().getThemes().map(function (_a) {
                            var id = _a.id, label = _a.label;
                            return React.createElement("option", { key: id, value: label }, label);
                        }))),
                    React.createElement("div", { className: 'flex-line' },
                        React.createElement("label", { className: 'flex-line' },
                            React.createElement("input", { type: 'checkbox', checked: this.state.verboseOnCompile, onChange: this.verboseOnCompileDidChange }),
                            "compile"),
                        React.createElement("label", { className: 'flex-line' },
                            React.createElement("input", { type: 'checkbox', checked: this.state.verboseOnUpload, onChange: this.verboseOnUploadDidChange }),
                            "upload")))),
            React.createElement("label", { className: 'flex-line' },
                React.createElement("input", { type: 'checkbox', checked: this.state.verifyAfterUpload, onChange: this.verifyAfterUploadDidChange }),
                "Verify code after upload"),
            React.createElement("label", { className: 'flex-line' },
                React.createElement("input", { type: 'checkbox', checked: this.state.checkForUpdates, onChange: this.checkForUpdatesDidChange, disabled: true }),
                "Check for updates on startup"),
            React.createElement("label", { className: 'flex-line' },
                React.createElement("input", { type: 'checkbox', checked: this.state.autoSave === 'on', onChange: this.autoSaveDidChange }),
                "Auto save"),
            React.createElement("label", { className: 'flex-line' },
                React.createElement("input", { type: 'checkbox', checked: this.state.enableLsLogs, onChange: this.enableLsLogsDidChange }),
                "Enable language server logging"),
            React.createElement("div", { className: 'flex-line' },
                "Additional boards manager URLs:",
                React.createElement("input", { className: 'theia-input stretch with-margin', type: 'text', value: this.state.additionalUrls.join(','), onChange: this.additionalUrlsDidChange }),
                React.createElement("i", { className: 'fa fa-window-restore theia-button shrink', onClick: this.editAdditionalUrlDidClick })));
    };
    SettingsComponent.prototype.renderNetwork = function () {
        return React.createElement("div", { className: 'content noselect' },
            React.createElement("form", null,
                React.createElement("label", { className: 'flex-line' },
                    React.createElement("input", { type: 'radio', checked: this.state.network === 'none', onChange: this.noProxyDidChange }),
                    "No proxy"),
                React.createElement("label", { className: 'flex-line' },
                    React.createElement("input", { type: 'radio', checked: this.state.network !== 'none', onChange: this.manualProxyDidChange }),
                    "Manual proxy configuration")),
            this.renderProxySettings());
    };
    SettingsComponent.prototype.renderProxySettings = function () {
        var disabled = this.state.network === 'none';
        return React.createElement(react_disable_1.Disable, { disabled: disabled },
            React.createElement("div", { className: 'proxy-settings', "aria-disabled": disabled },
                React.createElement("form", { className: 'flex-line' },
                    React.createElement("input", { type: 'radio', checked: this.state.network === 'none' ? true : this.state.network.protocol === 'http', onChange: this.httpProtocolDidChange }),
                    "HTTP",
                    React.createElement("label", { className: 'flex-line' },
                        React.createElement("input", { type: 'radio', checked: this.state.network === 'none' ? false : this.state.network.protocol !== 'http', onChange: this.socksProtocolDidChange }),
                        "SOCKS")),
                React.createElement("div", { className: 'flex-line proxy-settings' },
                    React.createElement("div", { className: 'column' },
                        React.createElement("div", { className: 'flex-line' }, "Host name:"),
                        React.createElement("div", { className: 'flex-line' }, "Port number:"),
                        React.createElement("div", { className: 'flex-line' }, "Username:"),
                        React.createElement("div", { className: 'flex-line' }, "Password:")),
                    React.createElement("div", { className: 'column stretch' },
                        React.createElement("div", { className: 'flex-line' },
                            React.createElement("input", { className: 'theia-input stretch with-margin', type: 'text', value: this.state.network === 'none' ? '' : this.state.network.hostname, onChange: this.hostnameDidChange })),
                        React.createElement("div", { className: 'flex-line' },
                            React.createElement("input", { className: 'theia-input small with-margin', type: 'number', pattern: '[0-9]', value: this.state.network === 'none' ? '' : this.state.network.port, onKeyDown: this.numbersOnlyKeyDown, onChange: this.portDidChange })),
                        React.createElement("div", { className: 'flex-line' },
                            React.createElement("input", { className: 'theia-input stretch with-margin', type: 'text', value: this.state.network === 'none' ? '' : this.state.network.username, onChange: this.usernameDidChange })),
                        React.createElement("div", { className: 'flex-line' },
                            React.createElement("input", { className: 'theia-input stretch with-margin', type: 'password', value: this.state.network === 'none' ? '' : this.state.network.password, onChange: this.passwordDidChange }))))));
    };
    SettingsComponent.prototype.isControlKey = function (event) {
        return !!event.key && ['tab', 'delete', 'backspace', 'arrowleft', 'arrowright'].some(function (key) { return event.key.toLocaleLowerCase() === key; });
    };
    Object.defineProperty(SettingsComponent.prototype, "cloneProxySettings", {
        get: function () {
            var network = this.state.network;
            if (network === 'none') {
                throw new Error('Must be called when proxy is enabled.');
            }
            var copyNetwork = objects_1.deepClone(network);
            return copyNetwork;
        },
        enumerable: false,
        configurable: true
    });
    return SettingsComponent;
}(React.Component));
exports.SettingsComponent = SettingsComponent;
var SettingsWidget = /** @class */ (function (_super) {
    __extends(SettingsWidget, _super);
    function SettingsWidget() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    SettingsWidget.prototype.render = function () {
        return React.createElement(SettingsComponent, { settingsService: this.settingsService, fileService: this.fileService, fileDialogService: this.fileDialogService, windowService: this.windowService });
    };
    __decorate([
        inversify_1.inject(SettingsService),
        __metadata("design:type", SettingsService)
    ], SettingsWidget.prototype, "settingsService", void 0);
    __decorate([
        inversify_1.inject(file_service_1.FileService),
        __metadata("design:type", file_service_1.FileService)
    ], SettingsWidget.prototype, "fileService", void 0);
    __decorate([
        inversify_1.inject(file_dialog_service_1.FileDialogService),
        __metadata("design:type", Object)
    ], SettingsWidget.prototype, "fileDialogService", void 0);
    __decorate([
        inversify_1.inject(window_service_1.WindowService),
        __metadata("design:type", Object)
    ], SettingsWidget.prototype, "windowService", void 0);
    SettingsWidget = __decorate([
        inversify_1.injectable()
    ], SettingsWidget);
    return SettingsWidget;
}(browser_1.ReactWidget));
exports.SettingsWidget = SettingsWidget;
var SettingsDialogProps = /** @class */ (function (_super) {
    __extends(SettingsDialogProps, _super);
    function SettingsDialogProps() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    SettingsDialogProps = __decorate([
        inversify_1.injectable()
    ], SettingsDialogProps);
    return SettingsDialogProps;
}(browser_1.DialogProps));
exports.SettingsDialogProps = SettingsDialogProps;
var SettingsDialog = /** @class */ (function (_super) {
    __extends(SettingsDialog, _super);
    function SettingsDialog(props) {
        var _this = _super.call(this, props) || this;
        _this.props = props;
        _this.contentNode.classList.add('arduino-settings-dialog');
        _this.appendCloseButton('CANCEL');
        _this.appendAcceptButton('OK');
        return _this;
    }
    SettingsDialog.prototype.init = function () {
        this.toDispose.push(this.settingsService.onDidChange(this.validate.bind(this)));
    };
    SettingsDialog.prototype.isValid = function (settings) {
        return __awaiter(this, void 0, void 0, function () {
            var result;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.settingsService.validate(settings)];
                    case 1:
                        result = _a.sent();
                        if (typeof result === 'string') {
                            return [2 /*return*/, result];
                        }
                        return [2 /*return*/, ''];
                }
            });
        });
    };
    Object.defineProperty(SettingsDialog.prototype, "value", {
        get: function () {
            return this.settingsService.settings();
        },
        enumerable: false,
        configurable: true
    });
    SettingsDialog.prototype.onAfterAttach = function (msg) {
        var _this = this;
        if (this.widget.isAttached) {
            widgets_1.Widget.detach(this.widget);
        }
        widgets_1.Widget.attach(this.widget, this.contentNode);
        this.toDisposeOnDetach.push(this.settingsService.onDidChange(function () { return _this.update(); }));
        _super.prototype.onAfterAttach.call(this, msg);
        this.update();
    };
    SettingsDialog.prototype.onUpdateRequest = function (msg) {
        _super.prototype.onUpdateRequest.call(this, msg);
        this.widget.update();
    };
    SettingsDialog.prototype.onActivateRequest = function (msg) {
        _super.prototype.onActivateRequest.call(this, msg);
        this.widget.activate();
    };
    __decorate([
        inversify_1.inject(SettingsService),
        __metadata("design:type", SettingsService)
    ], SettingsDialog.prototype, "settingsService", void 0);
    __decorate([
        inversify_1.inject(SettingsWidget),
        __metadata("design:type", SettingsWidget)
    ], SettingsDialog.prototype, "widget", void 0);
    __decorate([
        inversify_1.postConstruct(),
        __metadata("design:type", Function),
        __metadata("design:paramtypes", []),
        __metadata("design:returntype", void 0)
    ], SettingsDialog.prototype, "init", null);
    SettingsDialog = __decorate([
        inversify_1.injectable(),
        __param(0, inversify_1.inject(SettingsDialogProps)),
        __metadata("design:paramtypes", [SettingsDialogProps])
    ], SettingsDialog);
    return SettingsDialog;
}(browser_1.AbstractDialog));
exports.SettingsDialog = SettingsDialog;
var AdditionalUrlsDialog = /** @class */ (function (_super) {
    __extends(AdditionalUrlsDialog, _super);
    function AdditionalUrlsDialog(urls, windowService) {
        var _this = _super.call(this, { title: 'Additional Boards Manager URLs' }) || this;
        _this.contentNode.classList.add('additional-urls-dialog');
        var description = document.createElement('div');
        description.textContent = 'Enter additional URLs, one for each row';
        description.style.marginBottom = '5px';
        _this.contentNode.appendChild(description);
        _this.textArea = document.createElement('textarea');
        _this.textArea.className = 'theia-input';
        _this.textArea.setAttribute('style', 'flex: 0;');
        _this.textArea.value = urls.filter(function (url) { return url.trim(); }).filter(function (url) { return !!url; }).join('\n');
        _this.textArea.wrap = 'soft';
        _this.textArea.cols = 90;
        _this.textArea.rows = 5;
        _this.contentNode.appendChild(_this.textArea);
        var anchor = document.createElement('div');
        anchor.classList.add('link');
        anchor.textContent = 'Click for a list of unofficial board support URLs';
        anchor.style.marginTop = '5px';
        anchor.style.cursor = 'pointer';
        _this.addEventListener(anchor, 'click', function () { return windowService.openNewWindow('https://github.com/arduino/Arduino/wiki/Unofficial-list-of-3rd-party-boards-support-urls', { external: true }); });
        _this.contentNode.appendChild(anchor);
        _this.appendAcceptButton('OK');
        _this.appendCloseButton('Cancel');
        return _this;
    }
    Object.defineProperty(AdditionalUrlsDialog.prototype, "value", {
        get: function () {
            return this.textArea.value.split('\n').map(function (url) { return url.trim(); });
        },
        enumerable: false,
        configurable: true
    });
    AdditionalUrlsDialog.prototype.onAfterAttach = function (message) {
        _super.prototype.onAfterAttach.call(this, message);
        this.addUpdateListener(this.textArea, 'input');
    };
    AdditionalUrlsDialog.prototype.onActivateRequest = function (message) {
        _super.prototype.onActivateRequest.call(this, message);
        this.textArea.focus();
    };
    AdditionalUrlsDialog.prototype.handleEnter = function (event) {
        if (event.target instanceof HTMLInputElement) {
            return _super.prototype.handleEnter.call(this, event);
        }
        return false;
    };
    return AdditionalUrlsDialog;
}(browser_1.AbstractDialog));
exports.AdditionalUrlsDialog = AdditionalUrlsDialog;
//# sourceMappingURL=settings.js.map