"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isEmptyString = exports.getSQLFriendlyEventName = exports.getHumanizedEventName = exports.asciiWords = exports.capitalize = exports.lowerize = exports.createDir = exports.getTestSpecFolder = exports.getSchemaFolder = exports.logVerbose = exports.toggleVerbosity = exports.logDetail = exports.logInfo = exports.logFatal = exports.logUnknownError = exports.logError = void 0;
const emoji = require("node-emoji");
const chalk = require("chalk");
const findUp = require("find-up");
const fs = require("fs");
const path = require("path");
const constants_1 = require("./constants");
const log = console.log;
const logException = console.error;
const logError = (s) => {
    log(emoji.emojify(chalk.bold.red(s)));
};
exports.logError = logError;
const logUnknownError = (s, e) => {
    if (s != null) {
        (0, exports.logError)(s);
    }
    (0, exports.logInfo)(`We are sorry that you ran into this error.
Please file a bug here: ${constants_1.FILE_A_BUG}
We hang out on this discord channel: ${constants_1.DISCORD_LINK}.
`);
    if (e.stack != null) {
        (0, exports.logVerbose)(`Stack trace: ${e.stack}`);
    }
};
exports.logUnknownError = logUnknownError;
const logFatal = (s) => {
    logException(s);
    (0, exports.logInfo)(`For more details: ${constants_1.SYFT_DOCUMENTATION}.
We hang out on this discord channel: ${constants_1.DISCORD_LINK}.`);
};
exports.logFatal = logFatal;
const logInfo = (s) => {
    log(emoji.emojify(chalk.bold(s)));
};
exports.logInfo = logInfo;
const logDetail = (s) => {
    log(emoji.emojify(chalk.dim(s)));
};
exports.logDetail = logDetail;
let VERBOSE_ENABLED = false;
const toggleVerbosity = (val) => {
    VERBOSE_ENABLED = val;
};
exports.toggleVerbosity = toggleVerbosity;
const logVerbose = (s) => {
    if (VERBOSE_ENABLED) {
        (0, exports.logDetail)(s);
    }
};
exports.logVerbose = logVerbose;
const SCHEMA_FOLDER = 'syft';
function getSchemaFolder() {
    let projectRootFolder = '.';
    const projectFilePath = findUp.sync(['package.json']);
    if (projectFilePath !== undefined && projectFilePath !== null) {
        projectRootFolder = path.relative(path.dirname(projectFilePath), projectRootFolder);
    }
    return path.join(projectRootFolder, SCHEMA_FOLDER);
}
exports.getSchemaFolder = getSchemaFolder;
function getTestSpecFolder(schemaFolder) {
    const dir = schemaFolder ?? getSchemaFolder();
    return path.join(dir, 'specs');
}
exports.getTestSpecFolder = getTestSpecFolder;
function createDir(dir) {
    if (!fs.existsSync(dir)) {
        fs.mkdirSync(dir, { recursive: true });
    }
}
exports.createDir = createDir;
function lowerize(s) {
    return s[0].toLocaleLowerCase() + s.slice(1);
}
exports.lowerize = lowerize;
function capitalize(s) {
    return s[0].toLocaleUpperCase() + s.slice(1);
}
exports.capitalize = capitalize;
const reAsciiWord = /[^\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f]+/g;
function asciiWords(val) {
    return val.replace(/['\u2019]/g, '').match(reAsciiWord) ?? [];
}
exports.asciiWords = asciiWords;
function getHumanizedEventName(name) {
    return asciiWords(name).map(capitalize).join(' ');
}
exports.getHumanizedEventName = getHumanizedEventName;
function getSQLFriendlyEventName(name) {
    return lowerize(name)
        .replace(/([A-Z])/g, '_$1')
        .toLocaleLowerCase();
}
exports.getSQLFriendlyEventName = getSQLFriendlyEventName;
function isEmptyString(value) {
    return value == null || value.trim().length === 0;
}
exports.isEmptyString = isEmptyString;
