import { useMutation } from '@tanstack/react-query';
import { bnplApi } from '../apis';
import type { IssueBnplTokenInput } from '../types';

/**
 * D4 final action — start the BNPL order and issue the provider token/redirect (contract `initiate`). The
 * caller owns the **per-attempt idempotency key** (a retried initiate reuses the same token; a new attempt
 * gets a new key), mirroring the f9 card initiate. On success the caller follows `redirectUrl` (the provider
 * handoff). A `409` here means "already paid / already in progress / window lapsed" — the wizard converges
 * (reads the order) instead of toasting; other domain 4xx render inline.
 */
export function useIssueBnplToken() {
  return useMutation({
    mutationFn: (input: IssueBnplTokenInput) => bnplApi.issueBnplToken(input),
  });
}
