"use client";
import { useCallback, useState } from "react";
import { useStigmer } from "../hooks";
import { toError } from "../internal/toError";
/**
 * Behavior hook for configuring auto-recharge settings.
 *
 * Wraps `billing.setAutoRechargeConfig` with loading and error state
 * management. Returns the updated `BillingAccount` on success so the
 * parent can refresh its cached data.
 *
 * @example
 * ```tsx
 * const { setConfig, isSubmitting, error } = useSetAutoRechargeConfig();
 *
 * await setConfig({
 *   orgId,
 *   enabled: true,
 *   thresholdMicros: BigInt(5_000_000),
 *   rechargeAmountMicros: BigInt(50_000_000),
 *   monthlyCapMicros: BigInt(200_000_000),
 * });
 * ```
 */
export function useSetAutoRechargeConfig() {
    const stigmer = useStigmer();
    const [isSubmitting, setIsSubmitting] = useState(false);
    const [error, setError] = useState(null);
    const clearError = useCallback(() => setError(null), []);
    const setConfig = useCallback(async (input) => {
        setIsSubmitting(true);
        setError(null);
        try {
            const account = await stigmer.billing.setAutoRechargeConfig({
                orgId: input.orgId,
                enabled: input.enabled,
                thresholdMicros: input.thresholdMicros,
                rechargeAmountMicros: input.rechargeAmountMicros,
                monthlyCapMicros: input.monthlyCapMicros,
            });
            return account;
        }
        catch (err) {
            setError(toError(err));
            throw err;
        }
        finally {
            setIsSubmitting(false);
        }
    }, [stigmer]);
    return { setConfig, isSubmitting, error, clearError };
}
//# sourceMappingURL=useSetAutoRechargeConfig.js.map