"use client";
import { useCallback, useState } from "react";
import { useStigmer } from "../hooks";
import { toError } from "../internal/toError";
/**
 * Behavior hook that creates a Stripe Checkout Session for credit
 * pack purchases.
 *
 * Wraps `billing.createCreditCheckoutSession` with loading and error
 * state management. On success, redirects the user to the Stripe-hosted
 * checkout page. Credits are provisioned asynchronously via webhook
 * after payment succeeds.
 *
 * @example
 * ```tsx
 * const { createSession, isSubmitting, error } = useCreateCheckoutSession();
 *
 * const handleBuy = () => {
 *   createSession({
 *     orgId,
 *     packId: "growth",
 *     successUrl: `${window.location.origin}/settings/billing?checkout=success`,
 *     cancelUrl: `${window.location.origin}/settings/billing`,
 *   });
 * };
 * ```
 */
export function useCreateCheckoutSession() {
    const stigmer = useStigmer();
    const [isSubmitting, setIsSubmitting] = useState(false);
    const [error, setError] = useState(null);
    const clearError = useCallback(() => setError(null), []);
    const createSession = useCallback(async (input) => {
        setIsSubmitting(true);
        setError(null);
        try {
            const response = await stigmer.billing.createCreditCheckoutSession({
                orgId: input.orgId,
                packId: input.packId,
                successUrl: input.successUrl,
                cancelUrl: input.cancelUrl,
            });
            if (response.checkoutUrl) {
                window.location.href = response.checkoutUrl;
            }
            return response;
        }
        catch (err) {
            setError(toError(err));
            throw err;
        }
        finally {
            setIsSubmitting(false);
        }
    }, [stigmer]);
    return { createSession, isSubmitting, error, clearError };
}
//# sourceMappingURL=useCreateCheckoutSession.js.map