"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) {
    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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const events_1 = __importDefault(require("events"));
const jsonfile_1 = __importDefault(require("jsonfile"));
const lodash_1 = require("lodash");
const path_1 = __importDefault(require("path"));
const typedi_1 = require("typedi");
const appInfo_1 = require("./appInfo");
const defaultConfigure = {
    gitlabToken: '',
    proxyPort: 8001,
    requestTimeoutTime: 30000,
};
/**
 * 代理运转需要的规则数据
 * 代理端口、超时时间、gitlab token、工程路径、是否启用转发规则
 * Created by tsxuehu on 8/3/17.
 */
let ConfigureService = class ConfigureService extends events_1.default {
    constructor(appInfoService) {
        super();
        const proxyDataDir = appInfoService.getProxyDataDir();
        this.configureFile = path_1.default.join(proxyDataDir, 'configure.json');
        this.configure = lodash_1.assign({}, defaultConfigure, jsonfile_1.default.readFileSync(this.configureFile));
    }
    // 获取配置
    getConfigure() {
        return this.configure;
    }
    // 设置配置，保存到文件
    setConfigure(userId, configure) {
        return __awaiter(this, void 0, void 0, function* () {
            this.configure = configure;
            jsonfile_1.default.writeFileSync(this.configureFile, this.configure, {
                encoding: 'utf-8',
            });
            // 发送通知
            this.emit('data-change', userId, this.configure);
        });
    }
    // 获取代理端口
    getProxyPort() {
        return this.configure.proxyPort;
    }
};
ConfigureService = __decorate([
    typedi_1.Service(),
    __metadata("design:paramtypes", [appInfo_1.AppInfoService])
], ConfigureService);
exports.ConfigureService = ConfigureService;
//# sourceMappingURL=configure.js.map