import {
  getFirstPaymentSearchParam,
  parsePaymentReturnSearchParams,
  type PaymentSearchParams,
} from "@/lib/payment/payment_search_params";
import {
  normalizeTipGiftParam,
  TIP_GIFT_CATEGORY_PARAM,
  TIP_GIFT_PLAN_ID_PARAM,
} from "@/lib/tip/tip_gift";
import { TipScreen } from "@/app/tip/tip-screen";

export default async function CharacterTipPage({
  searchParams,
}: {
  searchParams: Promise<PaymentSearchParams>;
}) {
  const query = await searchParams;
  const paymentReturn = parsePaymentReturnSearchParams(query);
  const initialCategory = normalizeTipGiftParam(
    getFirstPaymentSearchParam(query[TIP_GIFT_CATEGORY_PARAM]),
  );
  const initialPlanId = normalizeTipGiftParam(
    getFirstPaymentSearchParam(query[TIP_GIFT_PLAN_ID_PARAM]),
  );

  return (
    <TipScreen
      initialCategory={initialCategory}
      initialPlanId={initialPlanId}
      shouldResumePendingOrder={paymentReturn.shouldResumePendingOrder}
      initialPayChannel={paymentReturn.initialPayChannel}
    />
  );
}
