"use client";
import { useEffect, useState } from "react";
import { useOrg } from "./OrgProvider";
const PROVISIONING_POLL_MS = 2_000;
const PROVISIONING_TIMEOUT_MS = 10_000;
// ---------------------------------------------------------------------------
// Hook
// ---------------------------------------------------------------------------
/**
 * Headless behavior hook that encapsulates the org-gate provisioning
 * state machine.
 *
 * The hook observes the organization context from {@link useOrg} and
 * drives the following lifecycle:
 *
 * 1. **`bypassed`** — `isBypassed` is true; the gate is inactive.
 * 2. **`loading`** — the initial org list fetch is in flight.
 * 3. **`provisioning`** — OIDC mode, zero orgs: the server is creating
 *    the personal org. The hook polls every 2 s and times out after 10 s.
 * 4. **`error`** — the org fetch failed; `message` carries the reason.
 * 5. **`no-orgs`** — no organizations exist (or provisioning timed out);
 *    the consumer should show an onboarding form.
 * 6. **`ready`** — at least one org exists; render the app.
 *
 * The consumer computes `isBypassed` and `isOidcMode` using
 * framework-specific APIs and passes them in, keeping this hook free of
 * routing or auth-framework dependencies (DD-004).
 *
 * @example
 * ```tsx
 * const { state, retry, refresh } = useOrgGate({ isBypassed, isOidcMode });
 *
 * switch (state.status) {
 *   case "bypassed":
 *   case "ready":
 *     return <>{children}</>;
 *   case "loading":
 *     return <Spinner />;
 *   case "provisioning":
 *     return <WelcomeScreen />;
 *   case "error":
 *     return <ErrorScreen message={state.message} onRetry={retry} />;
 *   case "no-orgs":
 *     return <OnboardingForm onCreated={(org) => refresh(org.slug)} />;
 * }
 * ```
 */
export function useOrgGate(options) {
    const { isBypassed, isOidcMode } = options;
    const { orgs, isLoading, error, retry, refresh } = useOrg();
    const [provisioningStarted, setProvisioningStarted] = useState(false);
    const [provisioningTimedOut, setProvisioningTimedOut] = useState(false);
    // React-sanctioned "adjust state during render" pattern: the guard on
    // `!provisioningStarted` prevents infinite re-render loops.
    if (!isBypassed &&
        !provisioningStarted &&
        !isLoading &&
        orgs.length === 0 &&
        !error &&
        isOidcMode) {
        setProvisioningStarted(true);
    }
    const isProvisioning = provisioningStarted && orgs.length === 0 && !provisioningTimedOut;
    // Poll for the personal org while provisioning is in progress.
    // Errors from refresh() are absorbed — transient failures (identity not
    // yet created) are expected during the provisioning window.
    useEffect(() => {
        if (!isProvisioning)
            return;
        const interval = setInterval(() => refresh(), PROVISIONING_POLL_MS);
        const timeout = setTimeout(() => setProvisioningTimedOut(true), PROVISIONING_TIMEOUT_MS);
        return () => {
            clearInterval(interval);
            clearTimeout(timeout);
        };
    }, [isProvisioning, refresh]);
    // Resolve state — order matters: bypass and provisioning take priority.
    let state;
    if (isBypassed) {
        state = { status: "bypassed" };
    }
    else if (isProvisioning) {
        state = { status: "provisioning" };
    }
    else if (isLoading) {
        state = { status: "loading" };
    }
    else if (error) {
        state = { status: "error", message: error };
    }
    else if (orgs.length === 0) {
        state = { status: "no-orgs" };
    }
    else {
        state = { status: "ready" };
    }
    return { state, retry, refresh };
}
//# sourceMappingURL=useOrgGate.js.map