import { generatePKCE } from "./pkce.js";
//#region src/auth/oauth/anthropic.ts
/**
* Anthropic OAuth flow (Claude Pro/Max)
*/
const decode = (s) => atob(s);
const CLIENT_ID = decode("OWQxYzI1MGEtZTYxYi00NGQ5LTg4ZWQtNTk0NGQxOTYyZjVl");
const AUTHORIZE_URL = "https://claude.ai/oauth/authorize";
const TOKEN_URL = "https://console.anthropic.com/v1/oauth/token";
const REDIRECT_URI = "https://console.anthropic.com/oauth/code/callback";
const SCOPES = "org:create_api_key user:profile user:inference";
async function loginAnthropic(onAuthUrl, onPromptCode) {
	const { verifier, challenge } = await generatePKCE();
	onAuthUrl(`${AUTHORIZE_URL}?${new URLSearchParams({
		code: "true",
		client_id: CLIENT_ID,
		response_type: "code",
		redirect_uri: REDIRECT_URI,
		scope: SCOPES,
		code_challenge: challenge,
		code_challenge_method: "S256",
		state: verifier
	}).toString()}`);
	const splits = (await onPromptCode()).split("#");
	const code = splits[0];
	const state = splits[1];
	const tokenResponse = await fetch(TOKEN_URL, {
		method: "POST",
		headers: { "Content-Type": "application/json" },
		body: JSON.stringify({
			grant_type: "authorization_code",
			client_id: CLIENT_ID,
			code,
			state,
			redirect_uri: REDIRECT_URI,
			code_verifier: verifier
		})
	});
	if (!tokenResponse.ok) {
		const error = await tokenResponse.text();
		throw new Error(`Token exchange failed: ${error}`);
	}
	const tokenData = await tokenResponse.json();
	const expiresAt = Date.now() + tokenData.expires_in * 1e3 - 300 * 1e3;
	return {
		refresh: tokenData.refresh_token,
		access: tokenData.access_token,
		expires: expiresAt
	};
}
async function refreshAnthropicToken(refreshToken) {
	const response = await fetch(TOKEN_URL, {
		method: "POST",
		headers: { "Content-Type": "application/json" },
		body: JSON.stringify({
			grant_type: "refresh_token",
			client_id: CLIENT_ID,
			refresh_token: refreshToken
		})
	});
	if (!response.ok) {
		const error = await response.text();
		throw new Error(`Anthropic token refresh failed: ${error}`);
	}
	const data = await response.json();
	return {
		refresh: data.refresh_token,
		access: data.access_token,
		expires: Date.now() + data.expires_in * 1e3 - 300 * 1e3
	};
}
const anthropicOAuthProvider = {
	id: "anthropic",
	name: "Anthropic (Claude Pro/Max)",
	async login(callbacks) {
		return loginAnthropic((url) => callbacks.onAuth({ url }), () => callbacks.onPrompt({ message: "Paste the authorization code:" }));
	},
	async refreshToken(credentials) {
		return refreshAnthropicToken(credentials.refresh);
	},
	getApiKey(credentials) {
		return credentials.access;
	}
};
//#endregion
export { anthropicOAuthProvider, loginAnthropic, refreshAnthropicToken };

//# sourceMappingURL=anthropic.js.map