import { useEffect, useRef } from 'react';
import { useLocaleContext } from '@arcblock/ux/lib/Locale/context';
import Toast from '@arcblock/ux/lib/Toast';

interface StatusFeedbackProps {
  status: string;
  context: any;
  onReset: () => void;
}

export default function StatusFeedback({ status, context, onReset }: StatusFeedbackProps) {
  const { t } = useLocaleContext();
  const prevStatusRef = useRef(status);

  // Show toast for generic error states, then reset to idle
  // Special codes (CUSTOMER_LIMITED, PRICE_CHANGED, etc.) are handled by parent
  useEffect(() => {
    if (status === prevStatusRef.current) return;
    prevStatusRef.current = status;

    if (status === 'failed' && context?.type === 'error') {
      // CUSTOMER_LIMITED / STOP_ACCEPTING_ORDERS are handled by CheckoutDialogs
      if (context.code === 'CUSTOMER_LIMITED' || context.code === 'STOP_ACCEPTING_ORDERS') return;

      Toast.error(context.message || 'Payment failed');
      onReset();
    }

    // credit_insufficient is handled by CheckoutDialogs (ConfirmDialog), not Toast
  }, [status, context, t, onReset]);

  // Stripe dialog and DID Connect are handled by CheckoutDialogs and payment-panel respectively
  // No inline Alerts — matches V1 behavior (Toast for errors, dialogs for interaction)
  return null;
}
