"use strict";
class UserSettingsServiceBase {
    constructor(userSettingsFilePath, $fs) {
        this.$fs = $fs;
        this.userSettingsFilePath = null;
        this.userSettingsData = null;
        this.userSettingsFilePath = userSettingsFilePath;
    }
    getSettingValue(settingName) {
        return (() => {
            this.loadUserSettingsFile().wait();
            return this.userSettingsData ? this.userSettingsData[settingName] : null;
        }).future()();
    }
    saveSetting(key, value) {
        let settingObject = {};
        settingObject[key] = value;
        return this.saveSettings(settingObject);
    }
    removeSetting(key) {
        return (() => {
            this.loadUserSettingsFile().wait();
            delete this.userSettingsData[key];
            this.saveSettings().wait();
        }).future()();
    }
    saveSettings(data) {
        return (() => {
            this.loadUserSettingsFile().wait();
            this.userSettingsData = this.userSettingsData || {};
            _(data)
                .keys()
                .each(propertyName => {
                this.userSettingsData[propertyName] = data[propertyName];
            });
            this.$fs.writeJson(this.userSettingsFilePath, this.userSettingsData).wait();
        }).future()();
    }
    loadUserSettingsFile() {
        return (() => {
            if (!this.userSettingsData) {
                if (!this.$fs.exists(this.userSettingsFilePath).wait()) {
                    this.$fs.writeFile(this.userSettingsFilePath, null).wait();
                }
                this.userSettingsData = this.$fs.readJson(this.userSettingsFilePath).wait();
            }
        }).future()();
    }
}
exports.UserSettingsServiceBase = UserSettingsServiceBase;
