"use client";
import { useCallback, useState } from "react";
import { useStigmer } from "../hooks";
import { toError } from "../internal/toError";
/**
 * Behavior hook that wraps `apiKey.create()` with loading and error
 * state.
 *
 * Creates an API key resource. The caller provides an
 * {@link ApiKeyInput} with `name`, `org`, and optionally `expiresAt`
 * or `neverExpires`.
 *
 * **Critical**: The returned {@link ApiKey} is the *only* time the
 * raw key value is available — it is returned in `spec.keyHash` of
 * the create response. After creation the server stores only the
 * hash; subsequent queries return an empty `keyHash` and only the
 * `fingerprint` (last 6 characters) for display.
 *
 * Callers must capture the raw key from the resolved promise and
 * present it to the user immediately (e.g. via
 * {@link ApiKeyCreatedAlert}).
 *
 * @example
 * ```tsx
 * const { create, isCreating, error } = useCreateApiKey();
 *
 * const apiKey = await create({
 *   name: "ci-deploy-key",
 *   org: "acme",
 *   neverExpires: true,
 * });
 * // apiKey.spec?.keyHash contains the raw key — show it once
 * ```
 */
export function useCreateApiKey() {
    const stigmer = useStigmer();
    const [isCreating, setIsCreating] = useState(false);
    const [error, setError] = useState(null);
    const clearError = useCallback(() => setError(null), []);
    const create = useCallback(async (input) => {
        setIsCreating(true);
        setError(null);
        try {
            return await stigmer.apiKey.create(input);
        }
        catch (err) {
            setError(toError(err));
            throw err;
        }
        finally {
            setIsCreating(false);
        }
    }, [stigmer]);
    return { create, isCreating, error, clearError };
}
//# sourceMappingURL=useCreateApiKey.js.map