"use client";

import { useRouter } from "next/navigation";
import { useEffect, type ReactNode } from "react";
import { isCreditFundingPlayer } from "@/lib/player-funding-mode";
import type { WalletBalanceData } from "@/types/api/wallet-balance";

type WalletTransferCreditGuardProps = {
  balance: WalletBalanceData | null | undefined;
  loading: boolean;
  children: ReactNode;
};

/** 信用盘玩家不可主站划转：加载完成后重定向回钱包页 */
export function WalletTransferCreditGuard({
  balance,
  loading,
  children,
}: WalletTransferCreditGuardProps) {
  const router = useRouter();

  useEffect(() => {
    if (loading || !balance) {
      return;
    }
    if (isCreditFundingPlayer(balance)) {
      router.replace("/wallet");
    }
  }, [balance, loading, router]);

  if (loading || !balance) {
    return null;
  }

  if (isCreditFundingPlayer(balance)) {
    return null;
  }

  return <>{children}</>;
}
