"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveGitAuthor = resolveGitAuthor;
const catalog_1 = require("../i18n/catalog");
const native_git_cli_1 = require("./native-git-cli");
async function resolveGitAuthor(repositoryRoot, locale = catalog_1.DEFAULT_APP_LOCALE) {
    const alpacaAuthor = readAlpacaGitAuthorFromEnv(process.env);
    if (alpacaAuthor) {
        return alpacaAuthor;
    }
    const localAuthor = await readConfiguredGitAuthor(repositoryRoot);
    if (localAuthor) {
        return localAuthor;
    }
    const envAuthor = readStandardGitAuthorFromEnv(process.env);
    if (envAuthor) {
        return envAuthor;
    }
    const globalAuthor = await readGlobalGitAuthor(repositoryRoot);
    if (globalAuthor) {
        return globalAuthor;
    }
    throw new Error((0, catalog_1.translate)(locale, "git.error.authorNotConfigured"));
}
async function readConfiguredGitAuthor(repositoryRoot) {
    const [name, email] = await Promise.all([
        readGitConfigValue(repositoryRoot, "user.name"),
        readGitConfigValue(repositoryRoot, "user.email"),
    ]);
    return name && email ? { name, email } : null;
}
async function readGitConfigValue(repositoryRoot, key) {
    try {
        const value = await (0, native_git_cli_1.readNativeGit)(repositoryRoot, [
            "config",
            "--local",
            "--get",
            key,
        ]);
        return value.trim() || null;
    }
    catch (error) {
        if ((0, native_git_cli_1.isNativeGitExitCode)(error, 1)) {
            return null;
        }
        throw error;
    }
}
async function readGlobalGitAuthor(repositoryRoot) {
    const [name, email] = await Promise.all([
        readGlobalGitConfigValue(repositoryRoot, "user.name"),
        readGlobalGitConfigValue(repositoryRoot, "user.email"),
    ]);
    return name && email ? { name, email } : null;
}
async function readGlobalGitConfigValue(repositoryRoot, key) {
    try {
        const value = await (0, native_git_cli_1.readNativeGit)(repositoryRoot, [
            "config",
            "--global",
            "--get",
            key,
        ]);
        return value.trim() || null;
    }
    catch (error) {
        if ((0, native_git_cli_1.isNativeGitExitCode)(error, 1)) {
            return null;
        }
        throw error;
    }
}
function readAlpacaGitAuthorFromEnv(env) {
    const { ALPACA_GIT_USER_NAME, ALPACA_GIT_USER_EMAIL } = env;
    const name = ALPACA_GIT_USER_NAME?.trim();
    const email = ALPACA_GIT_USER_EMAIL?.trim();
    return name && email ? { name, email } : null;
}
function readStandardGitAuthorFromEnv(env) {
    const { GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, GIT_COMMITTER_NAME, GIT_COMMITTER_EMAIL, } = env;
    const authorName = GIT_AUTHOR_NAME?.trim();
    const authorEmail = GIT_AUTHOR_EMAIL?.trim();
    if (authorName && authorEmail) {
        return { name: authorName, email: authorEmail };
    }
    const name = GIT_COMMITTER_NAME?.trim();
    const email = GIT_COMMITTER_EMAIL?.trim();
    return name && email ? { name, email } : null;
}
