import * as client from "openid-client";

//#region src/ssoHelpers.ts
async function createOIDCProvider(options) {
	try {
		const issuerUrl = new URL(`https://${options.domain}`);
		const metadata = (await client.discovery(issuerUrl, client.randomPKCECodeVerifier())).serverMetadata();
		if (!metadata.issuer || !metadata.authorization_endpoint || !metadata.token_endpoint || !metadata.userinfo_endpoint || !metadata.jwks_uri) console.log("Some OIDC configuration endpoints are missing, which might cause issues with SSO");
		const oidcConfig = {
			clientId: options.clientId,
			clientSecret: options.clientSecret,
			discoveryEndpoint: `https://${options.domain}/.well-known/openid-configuration`,
			authorizationEndpoint: metadata.authorization_endpoint,
			tokenEndpoint: metadata.token_endpoint,
			userinfoEndpoint: metadata.userinfo_endpoint,
			jwksEndpoint: metadata.jwks_uri,
			scopes: options.scopes || [
				"openid",
				"email",
				"profile"
			],
			pkce: options.pkce !== false,
			mapping: {
				id: "sub",
				email: "email",
				emailVerified: "email_verified",
				name: "name",
				image: "picture"
			}
		};
		return {
			providerId: options.providerId,
			issuer: metadata.issuer,
			domain: options.domain,
			organizationId: options.organizationId,
			oidcConfig
		};
	} catch (error) {
		console.error(`Error discovering OIDC configuration for ${options.domain}:`, error);
		return null;
	}
}
async function createAuth0Provider(options) {
	return await createOIDCProvider({
		...options,
		providerId: "auth0"
	});
}

//#endregion
export { createAuth0Provider, createOIDCProvider };