import { writeGoogleTokensJson } from "../google/token-file.js";
function getOnboardingServiceBase() {
    const serviceUrl = process.env.VISIONCLAW_ONBOARDING_SERVICE_URL ?? "https://onboarding.visionclaw.dev";
    return serviceUrl.replace(/\/+$/, "");
}
export async function applyServerGoogleOAuthTokensForSetup(setupCode) {
    if (!/^\d{6}$/.test(setupCode))
        return false;
    const url = `${getOnboardingServiceBase()}/api/provisioning/setup-code/${setupCode}/credentials`;
    const response = await fetch(url, { signal: AbortSignal.timeout(30_000) });
    if (response.status === 404)
        return false;
    if (!response.ok) {
        throw new Error(`Failed to fetch setup credentials: HTTP ${response.status}`);
    }
    const body = await response.json();
    const tokensJson = body.google?.oauthTokensJson?.trim();
    if (!tokensJson)
        return false;
    writeGoogleTokensJson(tokensJson);
    return true;
}
//# sourceMappingURL=setup-credentials.js.map