import { n as getPublicOrigin } from "../../../../../public-url-Danv47cS.mjs";
import { t as createOAuthStateStore } from "../../../../../oauth-state-store-YLFLcH5F.mjs";
import { createAuthorizationUrl } from "@emdash-cms/auth";

//#region src/astro/routes/api/auth/oauth/[provider].ts
const prerender = false;
const VALID_PROVIDERS = new Set(["github", "google"]);
function isValidProvider(provider) {
	return VALID_PROVIDERS.has(provider);
}
/** Safely extract a string value from an env-like record */
function envString(env, ...keys) {
	for (const key of keys) {
		const val = env[key];
		if (typeof val === "string" && val) return val;
	}
}
/**
* Get OAuth config from environment variables
*/
function getOAuthConfig(env) {
	const providers = {};
	const githubClientId = envString(env, "EMDASH_OAUTH_GITHUB_CLIENT_ID", "GITHUB_CLIENT_ID");
	const githubClientSecret = envString(env, "EMDASH_OAUTH_GITHUB_CLIENT_SECRET", "GITHUB_CLIENT_SECRET");
	if (githubClientId && githubClientSecret) providers.github = {
		clientId: githubClientId,
		clientSecret: githubClientSecret
	};
	const googleClientId = envString(env, "EMDASH_OAUTH_GOOGLE_CLIENT_ID", "GOOGLE_CLIENT_ID");
	const googleClientSecret = envString(env, "EMDASH_OAUTH_GOOGLE_CLIENT_SECRET", "GOOGLE_CLIENT_SECRET");
	if (googleClientId && googleClientSecret) providers.google = {
		clientId: googleClientId,
		clientSecret: googleClientSecret
	};
	return providers;
}
const GET = async ({ params, request, locals, redirect }) => {
	const { emdash } = locals;
	const provider = params.provider;
	const errorRedirectBase = (request.headers.get("referer") ?? "").includes("/setup") ? "/_emdash/admin/setup" : "/_emdash/admin/login";
	if (!provider || !isValidProvider(provider)) return redirect(`${errorRedirectBase}?error=invalid_provider&message=${encodeURIComponent("Invalid OAuth provider")}`);
	if (!emdash?.db) return redirect(`${errorRedirectBase}?error=server_error&message=${encodeURIComponent("Database not configured")}`);
	try {
		const url = new URL(request.url);
		const providers = getOAuthConfig(locals.runtime?.env ?? import.meta.env);
		if (!providers[provider]) return redirect(`${errorRedirectBase}?error=provider_not_configured&message=${encodeURIComponent(`OAuth provider ${provider} is not configured. Set either EMDASH_OAUTH_${provider.toUpperCase()}_CLIENT_ID and EMDASH_OAUTH_${provider.toUpperCase()}_CLIENT_SECRET, or ${provider.toUpperCase()}_CLIENT_ID and ${provider.toUpperCase()}_CLIENT_SECRET.`)}`);
		const { url: authUrl } = await createAuthorizationUrl({
			baseUrl: `${getPublicOrigin(url, emdash?.config)}/_emdash`,
			providers
		}, provider, createOAuthStateStore(emdash.db));
		return redirect(authUrl);
	} catch (error) {
		console.error("OAuth initiation error:", error);
		return redirect(`${errorRedirectBase}?error=oauth_error&message=${encodeURIComponent("Failed to start OAuth flow. Please try again.")}`);
	}
};

//#endregion
export { GET, prerender };
//# sourceMappingURL=_provider_.mjs.map