"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path = __importStar(require("path"));
const fs_1 = __importDefault(require("fs"));
const fsPromises = fs_1.default.promises;
class Config {
    constructor(configFolder, profile) {
        this.configFileDir = path.resolve(configFolder, profile);
        this.configFilePath = path.resolve(this.configFileDir, "config.json");
    }
    // Getters + Setters
    async getApiKey() {
        if (process.env.TRELLO_API_KEY) {
            return process.env.TRELLO_API_KEY;
        }
        try {
            return await this.getConfigKey("apiKey");
        }
        catch (e) {
            const url = "https://trello.com/app-key";
            throw {
                message: `[apiKey] not found in ${this.configFilePath}. Get one at ${url}`,
                code: "ERR_NO_APP_KEY",
                data: {
                    url,
                },
            };
        }
    }
    setApiKey(apiKey) {
        return this.setConfigKey("apiKey", apiKey);
    }
    async getToken() {
        if (process.env.TRELLO_TOKEN) {
            return process.env.TRELLO_TOKEN;
        }
        try {
            return await this.getConfigKey("token");
        }
        catch (e) {
            const url = "https://trello.com/1/connect?key=" +
                (await this.getApiKey()) +
                "&name=trello-cli&response_type=token&scope=account,read,write&expiration=never";
            throw {
                message: `The [token] field is missing from ${this.configFilePath}`,
                code: "ERR_NO_TOKEN",
                data: {
                    url,
                },
            };
        }
    }
    setToken(token) {
        return this.setConfigKey("token", token);
    }
    // Config File
    configDirExists() {
        return fs_1.default.existsSync(this.configFileDir);
    }
    configFileExists() {
        return fs_1.default.existsSync(this.configFilePath);
    }
    // Helpers
    async setConfigKey(key, value) {
        if (!this.configDirExists()) {
            fs_1.default.mkdirSync(this.configFileDir, {
                recursive: true,
                mode: "0700",
            });
        }
        let current;
        if (this.configFileExists()) {
            current = JSON.parse((await fsPromises.readFile(this.configFilePath)).toString());
        }
        else {
            current = {};
        }
        current[key] = value;
        return fsPromises.writeFile(this.configFilePath, JSON.stringify(current));
    }
    async getConfigKey(key) {
        if (!this.configDirExists() || !this.configFileExists()) {
            throw {
                message: `No file found at ${this.configFilePath}`,
                code: "ERR_NO_CONFIG",
            };
        }
        const data = JSON.parse((await fsPromises.readFile(this.configFilePath)).toString());
        if (data[key] === undefined) {
            throw {
                message: `The [${key}] field is missing from ${this.configFilePath}`,
                code: `ERR_MISSING_CONFIG`,
            };
        }
        return data[key];
    }
}
exports.default = Config;
//# sourceMappingURL=index.js.map