import { redirectToProxy as a } from "@scalar/helpers/url/redirect-to-proxy";
import { Type as n } from "@scalar/typebox";
import { coerceValue as p } from "@scalar/workspace-store/schemas/typebox-coerce";
const s = n.Object({
  /** URL of the OAuth 2.0 Authorization Endpoint */
  authorization_endpoint: n.Optional(n.String()),
  /** URL of the OAuth 2.0 Token Endpoint */
  token_endpoint: n.Optional(n.String()),
  /** List of OAuth 2.0 scope values that this server supports */
  scopes_supported: n.Optional(n.Array(n.String())),
  /** List of OAuth 2.0 grant type values that this server supports */
  grant_types_supported: n.Optional(n.Array(n.String())),
  /** Supported PKCE code challenge methods (RFC 7636) */
  code_challenge_methods_supported: n.Optional(n.Array(n.String()))
}), f = async (o, i) => {
  try {
    let e = o.trim();
    if (!e)
      return [new Error("URL cannot be empty"), null];
    e.endsWith("/") && (e = e.slice(0, -1)), e.includes("/.well-known/openid-configuration") || (e = `${e}/.well-known/openid-configuration`);
    const c = a(i, e), t = await fetch(c);
    if (!t.ok)
      return [
        new Error(`Failed to fetch OpenID Connect discovery document: ${t.status} ${t.statusText}`),
        null
      ];
    const r = p(s, await t.json());
    return !r.authorization_endpoint && !r.token_endpoint ? [new Error("Invalid OpenID Connect discovery document: missing required endpoints"), null] : [null, r];
  } catch (e) {
    return e instanceof Error ? [e, null] : [new Error("Failed to fetch OpenID Connect discovery document"), null];
  }
};
export {
  f as fetchOpenIDConnectDiscovery
};
