"use strict";
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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const path = require("path");
class UserSettingsServiceBase {
    constructor(userSettingsFilePath, $fs) {
        this.$fs = $fs;
        this.userSettingsFilePath = null;
        this.userSettingsData = null;
        this.userSettingsFilePath = userSettingsFilePath;
    }
    getSettingValue(settingName) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.loadUserSettingsFile();
            return this.userSettingsData ? this.userSettingsData[settingName] : null;
        });
    }
    saveSetting(key, value) {
        return __awaiter(this, void 0, void 0, function* () {
            const settingObject = {};
            settingObject[key] = value;
            return this.saveSettings(settingObject);
        });
    }
    removeSetting(key) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.loadUserSettingsFile();
            delete this.userSettingsData[key];
            yield this.saveSettings();
        });
    }
    saveSettings(data) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.loadUserSettingsFile();
            this.userSettingsData = this.userSettingsData || {};
            _(data)
                .keys()
                .each(propertyName => {
                this.userSettingsData[propertyName] = data[propertyName];
            });
            this.$fs.writeJson(this.userSettingsFilePath, this.userSettingsData);
        });
    }
    loadUserSettingsFile() {
        return __awaiter(this, void 0, void 0, function* () {
            if (!this.userSettingsData) {
                if (!this.$fs.exists(this.userSettingsFilePath)) {
                    const unexistingDirs = this.getUnexistingDirectories(this.userSettingsFilePath);
                    this.$fs.writeFile(this.userSettingsFilePath, null);
                    if (process.env.SUDO_USER) {
                        for (const dir of unexistingDirs) {
                            yield this.$fs.setCurrentUserAsOwner(dir, process.env.SUDO_USER);
                        }
                    }
                }
                this.userSettingsData = this.$fs.readJson(this.userSettingsFilePath);
            }
        });
    }
    getUnexistingDirectories(filePath) {
        const unexistingDirs = [];
        let currentDir = path.join(filePath, "..");
        while (true) {
            if (this.$fs.exists(currentDir)) {
                break;
            }
            unexistingDirs.push(currentDir);
            currentDir = path.join(currentDir, "..");
        }
        return unexistingDirs;
    }
}
exports.UserSettingsServiceBase = UserSettingsServiceBase;
