"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.assertRemoteUrlDoesNotContainCredentials = assertRemoteUrlDoesNotContainCredentials;
exports.redactRemoteUrlCredentials = redactRemoteUrlCredentials;
function assertRemoteUrlDoesNotContainCredentials(remoteUrl) {
    if (!remoteUrlContainsCredentials(remoteUrl)) {
        return;
    }
    throw new Error(`Git remote URL must not include credentials: ${redactRemoteUrlCredentials(remoteUrl)}. Configure Git credentials through AlpacaLoop environment variables or Git credentials instead.`);
}
function redactRemoteUrlCredentials(remoteUrl) {
    const parsed = parseRemoteUrl(remoteUrl);
    if (!parsed || (!parsed.username && !parsed.password)) {
        return remoteUrl;
    }
    return `${parsed.protocol}//<redacted>@${parsed.host}${parsed.pathname}${parsed.search}${parsed.hash}`;
}
function remoteUrlContainsCredentials(remoteUrl) {
    const parsed = parseRemoteUrl(remoteUrl);
    if (!parsed) {
        return false;
    }
    if (parsed.password) {
        return true;
    }
    return (parsed.username.length > 0 &&
        (parsed.protocol === "http:" || parsed.protocol === "https:"));
}
function parseRemoteUrl(remoteUrl) {
    try {
        return new URL(remoteUrl);
    }
    catch {
        return null;
    }
}
