"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkAuthFile = exports.deleteAuthFile = exports.getAuthFile = exports.writeAuthFile = exports.initAuthFiles = exports.grabAuthDirs = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const grabAuthDirs = () => {
    const DSQL_AUTH_DIR = process.env.DSQL_AUTH_DIR;
    const ROOT_DIR = (DSQL_AUTH_DIR === null || DSQL_AUTH_DIR === void 0 ? void 0 : DSQL_AUTH_DIR.match(/./))
        ? DSQL_AUTH_DIR
        : path_1.default.resolve(process.cwd(), "./.tmp");
    const AUTH_DIR = path_1.default.join(ROOT_DIR, "logins");
    return { root: ROOT_DIR, auth: AUTH_DIR };
};
exports.grabAuthDirs = grabAuthDirs;
const initAuthFiles = () => {
    try {
        const authDirs = (0, exports.grabAuthDirs)();
        if (!fs_1.default.existsSync(authDirs.root))
            fs_1.default.mkdirSync(authDirs.root, { recursive: true });
        if (!fs_1.default.existsSync(authDirs.auth))
            fs_1.default.mkdirSync(authDirs.auth, { recursive: true });
        return true;
    }
    catch (error) {
        console.log(`Error initializing Auth Files: ${error.message}`);
        return false;
    }
};
exports.initAuthFiles = initAuthFiles;
/**
 * # Write Auth Files
 */
const writeAuthFile = (name, data) => {
    (0, exports.initAuthFiles)();
    try {
        fs_1.default.writeFileSync(path_1.default.join((0, exports.grabAuthDirs)().auth, name), data);
        return true;
    }
    catch ( /** @type {any} */error) {
        console.log(`Error writing Auth File: ${error.message}`);
        return false;
    }
};
exports.writeAuthFile = writeAuthFile;
/**
 * # Get Auth Files
 */
const getAuthFile = (name) => {
    try {
        const authFilePath = path_1.default.join((0, exports.grabAuthDirs)().auth, name);
        return fs_1.default.readFileSync(authFilePath, "utf-8");
    }
    catch ( /** @type {any} */error) {
        console.log(`Error getting Auth File: ${error.message}`);
        return null;
    }
};
exports.getAuthFile = getAuthFile;
/**
 * # Delete Auth Files
 * @param {string} name
 */
const deleteAuthFile = (name) => {
    try {
        return fs_1.default.rmSync(path_1.default.join((0, exports.grabAuthDirs)().auth, name));
    }
    catch ( /** @type {any} */error) {
        console.log(`Error deleting Auth File: ${error.message}`);
        return null;
    }
};
exports.deleteAuthFile = deleteAuthFile;
/**
 * # Delete Auth Files
 * @param {string} name
 */
const checkAuthFile = (name) => {
    try {
        return fs_1.default.existsSync(path_1.default.join((0, exports.grabAuthDirs)().auth, name));
        return true;
    }
    catch ( /** @type {any} */error) {
        console.log(`Error checking Auth File: ${error.message}`);
        return false;
    }
};
exports.checkAuthFile = checkAuthFile;