"use client";
import { useCallback, useState } from "react";
/**
 * Behavior hook that fetches an OIDC Discovery document from the
 * browser.
 *
 * Used by the "Custom OIDC" path in the identity provider creation
 * wizard. The hook exposes an imperative `discover()` function
 * (rather than an effect-based pattern) because discovery is
 * triggered by user action ("Continue"), not by render.
 *
 * CORS may block the fetch for some providers. When this happens
 * the hook returns `null` from `discover()` and populates `error`
 * so the wizard can fall back to manual configuration.
 */
export function useOidcDiscovery() {
    const [isDiscovering, setIsDiscovering] = useState(false);
    const [error, setError] = useState(null);
    const discover = useCallback(async (issuerUrl) => {
        setIsDiscovering(true);
        setError(null);
        try {
            const result = await fetchOidcDiscovery(issuerUrl);
            return result;
        }
        catch (err) {
            const wrapped = err instanceof Error ? err : new Error(String(err));
            setError(wrapped);
            return null;
        }
        finally {
            setIsDiscovering(false);
        }
    }, []);
    return { discover, isDiscovering, error };
}
// ---------------------------------------------------------------------------
// Internal fetch
// ---------------------------------------------------------------------------
async function fetchOidcDiscovery(issuerUrl) {
    const normalized = issuerUrl.replace(/\/+$/, "");
    const url = `${normalized}/.well-known/openid-configuration`;
    let response;
    try {
        response = await fetch(url, { mode: "cors" });
    }
    catch {
        throw new Error("Could not reach the issuer's discovery endpoint. " +
            "The provider may not allow browser requests (CORS). " +
            "You can enter the configuration manually instead.");
    }
    if (!response.ok) {
        throw new Error(`Discovery endpoint returned HTTP ${response.status}. ` +
            "Verify the issuer URL is correct.");
    }
    let data;
    try {
        data = (await response.json());
    }
    catch {
        throw new Error("Discovery endpoint returned invalid JSON. " +
            "Verify the issuer URL points to a valid OIDC provider.");
    }
    if (typeof data.issuer !== "string" || typeof data.jwks_uri !== "string") {
        throw new Error("Discovery document is missing required fields (issuer, jwks_uri). " +
            "This may not be a compliant OpenID Connect provider.");
    }
    return {
        issuer: data.issuer,
        jwksUri: data.jwks_uri,
        userinfoEndpoint: typeof data.userinfo_endpoint === "string"
            ? data.userinfo_endpoint
            : undefined,
    };
}
//# sourceMappingURL=useOidcDiscovery.js.map