"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveTrimmedEnvValue = resolveTrimmedEnvValue;
exports.resolveEnvCredentialPair = resolveEnvCredentialPair;
exports.resolveTokenCredentialFromSources = resolveTokenCredentialFromSources;
exports.resolveUsernamePasswordCredentialFromSources = resolveUsernamePasswordCredentialFromSources;
const command_1 = require("../../core/command");
const git_command_env_1 = require("../../core/git-command-env");
function resolveTrimmedEnvValue(env, envNames) {
    for (const envName of envNames) {
        const value = env[envName]?.trim();
        if (value) {
            return value;
        }
    }
    return null;
}
function resolveEnvCredentialPair(env, envPairs) {
    for (const pair of envPairs) {
        const username = env[pair.username]?.trim();
        const password = env[pair.password]?.trim();
        if (username && password) {
            return { username, password };
        }
    }
    return null;
}
async function resolveTokenCredentialFromSources(options) {
    const token = resolveTrimmedEnvValue(options.env, options.envNames);
    if (token) {
        return token;
    }
    const commandExistsFn = options.commandExists ?? command_1.commandExists;
    const runCommandFn = options.runCommand ?? command_1.runCommand;
    if (!(await commandExistsFn(options.command.command))) {
        return null;
    }
    try {
        const commandOptions = {
            command: options.command.command,
            args: options.command.args,
            allowNonZeroExit: true,
        };
        if (options.command.env !== undefined) {
            commandOptions.env = options.command.env;
        }
        if (options.command.timeoutMs !== undefined) {
            commandOptions.timeoutMs = options.command.timeoutMs;
        }
        const result = await runCommandFn(commandOptions);
        const trimmed = result.stdout.trim();
        return trimmed.length > 0 ? trimmed : null;
    }
    catch {
        return null;
    }
}
async function resolveUsernamePasswordCredentialFromSources(options) {
    const envCredential = resolveEnvCredentialPair(options.env, options.envPairs);
    if (envCredential) {
        return envCredential;
    }
    if (!options.gitCredential) {
        return null;
    }
    const commandExistsFn = options.commandExists ?? command_1.commandExists;
    const runCommandFn = options.runCommand ?? command_1.runCommand;
    if (!(await commandExistsFn("git"))) {
        return null;
    }
    const input = [
        `protocol=${options.gitCredential.protocol}`,
        `host=${options.gitCredential.host}`,
        "",
        "",
    ].join("\n");
    try {
        const commandOptions = {
            command: "git",
            args: ["credential", "fill"],
            input,
            allowNonZeroExit: true,
            env: buildGitCredentialFillEnv(options.env),
        };
        if (options.gitCredential.timeoutMs !== undefined) {
            commandOptions.timeoutMs = options.gitCredential.timeoutMs;
        }
        else {
            commandOptions.timeoutMs = 15_000;
        }
        const result = await runCommandFn(commandOptions);
        return parseGitCredentialFillResult(result.stdout);
    }
    catch {
        return null;
    }
}
function buildGitCredentialFillEnv(env) {
    const commandEnv = (0, git_command_env_1.buildGitCommandEnv)(env);
    commandEnv["GIT_TERMINAL_PROMPT"] = "0";
    return commandEnv;
}
function parseGitCredentialFillResult(stdout) {
    let username = null;
    let password = null;
    for (const line of stdout.split(/\r?\n/)) {
        if (line.startsWith("username=")) {
            username = line.slice("username=".length);
        }
        if (line.startsWith("password=")) {
            password = line.slice("password=".length);
        }
    }
    if (!username || !password) {
        return null;
    }
    return { username, password };
}
