"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Storage = void 0;
const path = require("path");
const fs = require("fs-extra");
const os = require("os");
const DATA_FOLDER = path.join(os.homedir(), '.wiz');
const DATA_FILE_PATH = path.join(DATA_FOLDER, 'config.json');
class Storage {
    static get() {
        return Storage.ensureFileExists()
            .then(() => getData());
    }
    static getSync() {
        Storage.ensureFileExistsSync();
        return fs.readJsonSync(DATA_FILE_PATH, { throws: false }) || {};
    }
    static set(newData) {
        return Storage.ensureFileExists()
            .then(() => getData())
            .then((oldData) => {
            const data = Object.assign(oldData, newData);
            return fs.outputJson(DATA_FILE_PATH, data);
        });
    }
    static clear() {
        return writeEmptyConfig();
    }
    static reset(fields) {
        return Storage.ensureFileExists()
            .then(() => fs.readJson(DATA_FILE_PATH))
            .then((config) => this.removeFields(config, fields))
            .then((config) => fs.outputJson(DATA_FILE_PATH, config));
    }
    static removeFields(obj, fields) {
        return Object.keys(obj)
            .reduce((acc, key) => {
            if (fields.indexOf(key) < 0) {
                acc[key] = obj[key];
            }
            return acc;
        }, {});
    }
    static ensureFileExists() {
        return fs.pathExists(DATA_FILE_PATH)
            .then((exists) => {
            if (!exists) {
                return writeEmptyConfig();
            }
        });
    }
    static ensureFileExistsSync() {
        const exists = fs.existsSync(DATA_FILE_PATH);
        if (!exists) {
            fs.outputJsonSync(DATA_FILE_PATH, {});
        }
    }
}
exports.Storage = Storage;
function writeEmptyConfig() {
    return fs.outputJson(DATA_FILE_PATH, {});
}
function getData() {
    return fs.readJson(DATA_FILE_PATH)
        .then((data) => {
        return data || {};
    })
        .catch(() => {
        return {};
    });
}
//# sourceMappingURL=index.js.map