"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerDevelopmentDefaults = exports.getApplicationBaseUrl = exports.getOldEncryptionSecrets = exports.getSignaturePublicKey = exports.getEncryptionSecret = exports.getOldQuirrelToken = exports.getQuirrelToken = exports.getOldQuirrelBaseUrl = exports.getQuirrelBaseUrl = exports.prefixWithProtocol = exports.withoutLeadingSlash = exports.withoutTrailingSlash = void 0;
function isProduction() {
    if (process.env.VERCEL && process.env.CI) {
        return true;
    }
    return process.env.NODE_ENV === "production";
}
function resolveEnvReference(name) {
    const env = process.env[name];
    if (env === null || env === void 0 ? void 0 : env.startsWith("@")) {
        const referencedVar = process.env[env.slice(1)];
        return referencedVar;
    }
    return env;
}
function withoutTrailingSlash(url) {
    if (url.endsWith("/")) {
        return url.slice(0, url.length - 1);
    }
    return url;
}
exports.withoutTrailingSlash = withoutTrailingSlash;
function withoutLeadingSlash(url) {
    if (url.startsWith("/")) {
        return url.slice(1);
    }
    return url;
}
exports.withoutLeadingSlash = withoutLeadingSlash;
function prefixWithProtocol(string) {
    if (string.startsWith("http://") || string.startsWith("https://")) {
        return string;
    }
    if (isProduction()) {
        return "https://" + string;
    }
    else {
        return "http://" + string;
    }
}
exports.prefixWithProtocol = prefixWithProtocol;
function normalisedURL(string) {
    return prefixWithProtocol(withoutTrailingSlash(string));
}
function getQuirrelBaseUrl() {
    var _a;
    const fromEnvironment = (_a = process.env.QUIRREL_API_URL) !== null && _a !== void 0 ? _a : process.env.QUIRREL_URL;
    if (fromEnvironment) {
        return normalisedURL(fromEnvironment);
    }
    return isProduction() ? "https://api.quirrel.dev" : "http://localhost:9181";
}
exports.getQuirrelBaseUrl = getQuirrelBaseUrl;
function getOldQuirrelBaseUrl() {
    var _a;
    const fromEnvironment = (_a = process.env.QUIRREL_MIGRATE_OLD_API_URL) !== null && _a !== void 0 ? _a : process.env.QUIRREL_MIGRATE_OLD_URL;
    if (fromEnvironment) {
        return normalisedURL(fromEnvironment);
    }
    return undefined;
}
exports.getOldQuirrelBaseUrl = getOldQuirrelBaseUrl;
function getQuirrelToken() {
    return process.env.QUIRREL_TOKEN;
}
exports.getQuirrelToken = getQuirrelToken;
function getOldQuirrelToken() {
    return process.env.QUIRREL_MIGRATE_OLD_TOKEN;
}
exports.getOldQuirrelToken = getOldQuirrelToken;
function getEncryptionSecret() {
    return process.env.QUIRREL_ENCRYPTION_SECRET;
}
exports.getEncryptionSecret = getEncryptionSecret;
function getSignaturePublicKey() {
    return process.env.QUIRREL_SIGNATURE_PUBLIC_KEY;
}
exports.getSignaturePublicKey = getSignaturePublicKey;
function getOldEncryptionSecrets() {
    var _a;
    return JSON.parse((_a = process.env.QUIRREL_OLD_SECRETS) !== null && _a !== void 0 ? _a : "null");
}
exports.getOldEncryptionSecrets = getOldEncryptionSecrets;
let developmentApplicationPort;
function getNetlifyURL() {
    const siteId = process.env.SITE_ID;
    const netlifyDev = process.env.NETLIFY_DEV;
    const deployUrl = process.env.DEPLOY_URL;
    if (siteId) {
        return siteId + ".netlify.app";
    }
    else {
        if (netlifyDev) {
            return deployUrl;
        }
    }
}
function getVercelURL() {
    return process.env.VERCEL_URL;
}
function getApplicationBaseUrl(host = "localhost") {
    const baseUrl = resolveEnvReference("QUIRREL_BASE_URL") ||
        getNetlifyURL() ||
        getVercelURL() ||
        `http://${host}:${developmentApplicationPort}`;
    if (!baseUrl) {
        throw new Error("Please specify QUIRREL_BASE_URL.");
    }
    return normalisedURL(baseUrl);
}
exports.getApplicationBaseUrl = getApplicationBaseUrl;
function registerDevelopmentDefaults({ applicationPort, }) {
    if (isProduction()) {
        return;
    }
    if (developmentApplicationPort) {
        return;
    }
    developmentApplicationPort = applicationPort;
}
exports.registerDevelopmentDefaults = registerDevelopmentDefaults;
//# sourceMappingURL=config.js.map