const API_KEY_HEADER = 'x-agentuity-auth-api-key';
const AUTHORIZATION_HEADER = 'Authorization';
const ORG_HEADER = 'x-agentuity-orgid';
const DEFAULT_ORG_ID_ENV_VARS = ['AGENTUITY_ORGID', 'AGENTUITY_CLOUD_ORG_ID'];
function normalizeApiKey(apiKey) {
    const trimmed = apiKey?.trim();
    return trimmed ? trimmed : null;
}
function normalizeOrgId(orgId) {
    const trimmed = orgId?.trim();
    return trimmed ? trimmed : null;
}
export function resolveCoderOrgId(orgId) {
    const explicitOrgId = normalizeOrgId(orgId);
    if (explicitOrgId) {
        return explicitOrgId;
    }
    for (const envName of DEFAULT_ORG_ID_ENV_VARS) {
        const envOrgId = normalizeOrgId(process.env[envName]);
        if (envOrgId) {
            return envOrgId;
        }
    }
    return null;
}
export function isHubApiKey(apiKey) {
    const token = normalizeApiKey(apiKey);
    return token?.startsWith('agc_') ?? false;
}
export function applyCoderAuthHeaders(headers, apiKey, orgId) {
    const token = normalizeApiKey(apiKey);
    const normalizedOrgId = resolveCoderOrgId(orgId);
    const nextHeaders = { ...headers };
    if (normalizedOrgId) {
        nextHeaders[ORG_HEADER] = normalizedOrgId;
    }
    if (!token)
        return nextHeaders;
    if (isHubApiKey(token)) {
        nextHeaders[API_KEY_HEADER] = token;
        return nextHeaders;
    }
    nextHeaders[AUTHORIZATION_HEADER] = `Bearer ${token}`;
    return nextHeaders;
}
export function getCoderAuthCurlArgs(apiKey, orgId) {
    const token = normalizeApiKey(apiKey);
    const args = [];
    const normalizedOrgId = resolveCoderOrgId(orgId);
    if (normalizedOrgId) {
        args.push('-H', `${ORG_HEADER}: ${normalizedOrgId}`);
    }
    if (!token)
        return args;
    if (isHubApiKey(token)) {
        args.push('-H', `${API_KEY_HEADER}: ${token}`);
        return args;
    }
    args.push('-H', `${AUTHORIZATION_HEADER}: Bearer ${token}`);
    return args;
}
//# sourceMappingURL=auth.js.map