import consola from 'consola';
import { config as configDotenv } from 'dotenv';
import { CountryCode } from 'libphonenumber-js';
import ValidatorJS from 'validator';

configDotenv();
export const config = {
    internal_port: parseInt(process.env.APP_PORT) || 4010,
    development: process.env.NODE_ENV === "production",
    testing: process.env.NODE_ENV === "test",
    jwt_secret: process.env.JWT_SECRET || "secretjwtsecret",
    station_token_secret: process.env.STATION_TOKEN_SECRET || "",
    nats_url: process.env.NATS_URL || "nats://localhost:4222",
    nats_prewarm: process.env.NATS_PREWARM === "true",
    phone_validation_countrycode: getPhoneCodeLocale(),
    postalcode_validation_countrycode: getPostalCodeLocale(),
    version: process.env.VERSION || require('../package.json').version,
    seedTestData: getDataSeeding(),
    app_url: process.env.APP_URL || "http://localhost:8080",
    privacy_url: process.env.PRIVACY_URL || "/privacy",
    imprint_url: process.env.IMPRINT_URL || "/imprint",
    mailer_url: process.env.MAILER_URL || "",
    mailer_key: process.env.MAILER_KEY || ""
}
let errors = 0
if (typeof config.internal_port !== "number") {
    consola.error("Error: APP_PORT is not a number")
    errors++
}
if (typeof config.development !== "boolean") {
    consola.error("Error: NODE_ENV is not a boolean")
    errors++
}
if (config.mailer_url == "" || config.mailer_key == "") {
    consola.error("Error: invalid mailer config")
    errors++;
}
if (config.station_token_secret.length < 32) {
    consola.error("Error: STATION_TOKEN_SECRET must be set and at least 32 characters long")
    errors++;
}
function getPhoneCodeLocale(): CountryCode {
    return (process.env.PHONE_COUNTRYCODE as CountryCode);
}
function getPostalCodeLocale(): any {
    try {
        const stringArray: String[] = ValidatorJS.isPostalCodeLocales;
        let index = stringArray.indexOf(process.env.POSTALCODE_COUNTRYCODE);
        return ValidatorJS.isPostalCodeLocales[index];
    } catch (error) {
        return null;
    }
}
function getDataSeeding(): Boolean {
    try {
        return JSON.parse(process.env.SEED_TEST_DATA);
    } catch (error) {
        return false;
    }
}
export let e = errors