"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getConfig = getConfig;
exports.setStoragePath = setStoragePath;
exports.configDir = configDir;
exports.getState = getState;
exports.setState = setState;
const tslib_1 = require("tslib");
const path_1 = tslib_1.__importDefault(require("path"));
const os_1 = tslib_1.__importDefault(require("os"));
const fs_1 = tslib_1.__importDefault(require("fs"));
const coc_nvim_1 = require("coc.nvim");
const fs_2 = require("./fs");
const state = {};
function getConfig() {
    return coc_nvim_1.workspace.getConfiguration().get("go");
}
function setStoragePath(dir) {
    state.storagePath = dir;
}
async function configDir(...names) {
    const storage = state.storagePath || path_1.default.join(os_1.default.homedir(), ".config", "coc", "go");
    const dir = path_1.default.join(storage, ...names);
    return new Promise((resolve) => {
        (0, fs_2.createDir)(dir);
        resolve(dir);
    });
}
async function stateFile() {
    return path_1.default.join(await configDir(), 'state.json');
}
async function getStateData() {
    try {
        const f = await stateFile();
        const d = JSON.parse(await fs_1.default.promises.readFile(f, 'utf-8'));
        return d || {};
    }
    catch (err) { /* mute */ }
    return {};
}
async function setStateData(data) {
    try {
        await fs_1.default.promises.writeFile(await stateFile(), JSON.stringify(data, null, '  '));
    }
    catch (err) { /* mute */ }
}
async function getState(key) {
    const d = await getStateData();
    return d[key] || '';
}
async function setState(key, value) {
    const d = await getStateData();
    d[key] = value;
    await setStateData(d);
}
//# sourceMappingURL=config.js.map