"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveGovernanceGitMetadata = resolveGovernanceGitMetadata;
const node_child_process_1 = require("node:child_process");
const node_util_1 = require("node:util");
const execFile = (0, node_util_1.promisify)(node_child_process_1.execFile);
async function runGitCommand(workspaceRoot, args) {
    try {
        const { stdout } = await execFile('git', args, { cwd: workspaceRoot });
        const value = stdout.trim();
        return value.length > 0 ? value : undefined;
    }
    catch {
        return undefined;
    }
}
function normalizeBranchName(branch) {
    if (!branch || branch === 'HEAD') {
        return undefined;
    }
    return branch;
}
function parseRemoteUrl(remoteUrl) {
    if (!remoteUrl) {
        return {};
    }
    const httpsMatch = remoteUrl.match(/^(https?):\/\/([^/]+)\/(.+)$/iu);
    if (httpsMatch) {
        const slug = `${httpsMatch[2]}/${httpsMatch[3].replace(/\.git$/iu, '')}`.replace(/\/+$/u, '');
        const repositoryName = slug.slice(slug.lastIndexOf('/') + 1).trim();
        return {
            repositorySlug: slug,
            repositoryName: repositoryName.length > 0 ? repositoryName : undefined,
            repositoryUrl: `${httpsMatch[1]}://${slug}`,
        };
    }
    const sshMatch = remoteUrl.match(/^(?:ssh:\/\/)?git@([^:/]+)[:/]([^#?]+)$/iu);
    if (sshMatch) {
        const slug = `${sshMatch[1]}/${sshMatch[2].replace(/\.git$/iu, '')}`.replace(/\/+$/u, '');
        const repositoryName = slug.slice(slug.lastIndexOf('/') + 1).trim();
        return {
            repositorySlug: slug,
            repositoryName: repositoryName.length > 0 ? repositoryName : undefined,
            repositoryUrl: `https://${slug}`,
        };
    }
    return {};
}
function parseDefaultBranchFromSymbolicRef(symbolicRef) {
    if (!symbolicRef) {
        return undefined;
    }
    const normalized = symbolicRef.trim();
    const prefix = 'refs/remotes/origin/';
    if (!normalized.startsWith(prefix)) {
        return undefined;
    }
    const branch = normalized.slice(prefix.length).trim();
    return branch.length > 0 ? branch : undefined;
}
async function resolveGovernanceGitMetadata(workspaceRoot) {
    const [remoteUrl, branch, commitSha, remoteHead] = await Promise.all([
        runGitCommand(workspaceRoot, ['remote', 'get-url', 'origin']),
        runGitCommand(workspaceRoot, ['rev-parse', '--abbrev-ref', 'HEAD']),
        runGitCommand(workspaceRoot, ['rev-parse', 'HEAD']),
        runGitCommand(workspaceRoot, ['symbolic-ref', 'refs/remotes/origin/HEAD']),
    ]);
    const parsedRemote = parseRemoteUrl(remoteUrl);
    return {
        ...parsedRemote,
        ...(normalizeBranchName(branch) ? { branch: normalizeBranchName(branch) } : {}),
        ...(commitSha ? { commitSha } : {}),
        ...(parseDefaultBranchFromSymbolicRef(remoteHead)
            ? { defaultBranch: parseDefaultBranchFromSymbolicRef(remoteHead) }
            : {}),
    };
}
//# sourceMappingURL=governanceGitMetadata.js.map