//#region src/auth/oauth/minimax-cn.ts
const MINIMAX_CN_OAUTH_BASE_URL = "https://platform.minimax.chat";
const MINIMAX_CN_OAUTH_AUTHORIZE_URL = `${MINIMAX_CN_OAUTH_BASE_URL}/oauth/authorize`;
const MINIMAX_CN_OAUTH_TOKEN_URL = `${MINIMAX_CN_OAUTH_BASE_URL}/oauth/token`;
const MINIMAX_CN_OAUTH_CLIENT_ID = "your_client_id";
const minimaxCnOAuthProvider = {
	id: "minimax-cn",
	name: "MiniMax 国内版",
	usesCallbackServer: true,
	async login(callbacks) {
		const codeVerifier = generateCodeVerifier();
		const codeChallenge = await generateCodeChallenge(codeVerifier);
		const authUrl = new URL(MINIMAX_CN_OAUTH_AUTHORIZE_URL);
		authUrl.searchParams.set("client_id", MINIMAX_CN_OAUTH_CLIENT_ID);
		authUrl.searchParams.set("redirect_uri", "http://localhost:3000/callback");
		authUrl.searchParams.set("response_type", "code");
		authUrl.searchParams.set("scope", "api");
		authUrl.searchParams.set("code_challenge", codeChallenge);
		authUrl.searchParams.set("code_challenge_method", "S256");
		callbacks.onAuth({
			url: authUrl.toString(),
			instructions: "请在浏览器中授权应用，然后输入返回的授权码。"
		});
		const code = await callbacks.onPrompt({ message: "请输入授权码：" });
		const tokenResponse = await fetch(MINIMAX_CN_OAUTH_TOKEN_URL, {
			method: "POST",
			headers: { "Content-Type": "application/x-www-form-urlencoded" },
			body: new URLSearchParams({
				grant_type: "authorization_code",
				client_id: MINIMAX_CN_OAUTH_CLIENT_ID,
				code,
				redirect_uri: "http://localhost:3000/callback",
				code_verifier: codeVerifier
			})
		});
		if (!tokenResponse.ok) {
			const error = await tokenResponse.text();
			throw new Error(`Token exchange failed: ${error}`);
		}
		const tokenData = await tokenResponse.json();
		return {
			access: tokenData.access_token,
			refresh: tokenData.refresh_token,
			expires: Date.now() + tokenData.expires_in * 1e3
		};
	},
	async refreshToken(credentials) {
		if (!credentials.refresh) throw new Error("MiniMax CN OAuth refresh token missing. Re-authenticate.");
		const response = await fetch(MINIMAX_CN_OAUTH_TOKEN_URL, {
			method: "POST",
			headers: { "Content-Type": "application/x-www-form-urlencoded" },
			body: new URLSearchParams({
				grant_type: "refresh_token",
				client_id: MINIMAX_CN_OAUTH_CLIENT_ID,
				refresh_token: credentials.refresh
			})
		});
		if (!response.ok) {
			const text = await response.text();
			throw new Error(`MiniMax CN OAuth refresh failed: ${text}`);
		}
		const payload = await response.json();
		return {
			...credentials,
			access: payload.access_token,
			refresh: payload.refresh_token || credentials.refresh,
			expires: Date.now() + payload.expires_in * 1e3
		};
	},
	getApiKey(credentials) {
		return credentials.access;
	}
};
function generateCodeVerifier() {
	const array = new Uint8Array(32);
	crypto.getRandomValues(array);
	return base64UrlEncode(array);
}
async function generateCodeChallenge(verifier) {
	const data = new TextEncoder().encode(verifier);
	const digest = await crypto.subtle.digest("SHA-256", data);
	return base64UrlEncode(new Uint8Array(digest));
}
function base64UrlEncode(buffer) {
	let str = "";
	buffer.forEach((b) => str += String.fromCharCode(b));
	return btoa(str).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
}
//#endregion
export { minimaxCnOAuthProvider };

//# sourceMappingURL=minimax-cn.js.map