import CreditCard from "@/components/ui/PaymentMethodsButtons/CreditCard";
import PaymentButtons from "@/components/ui/PaymentMethodsButtons/PaymentButtons";

export enum EPaymentMethod {
  CREDIT_CARD = "card",
  PAYMENT_BUTTONS = "paymentButtons",
}

export interface IPaymentMethod {
  id: EPaymentMethod;
  component: JSX.Element;
}

export const checkExpressCheckoutPaymentFormAvailable = (
  availablePaymentMethods: undefined | unknown
) => {
  if (!availablePaymentMethods) return false;
  let result = false;
  for (const key in availablePaymentMethods) {
    if (availablePaymentMethods[key as keyof unknown]) {
      result = true;
      break;
    }
  }
  return result;
};

export function paymentMethods(
  availableMethods: {
    applePay: boolean;
    googlePay: boolean;
    amazon: boolean;
    link: boolean;
  }
): IPaymentMethod[] {
  let methods = [
    {
      id: EPaymentMethod.PAYMENT_BUTTONS,
      component: <PaymentButtons availableMethods={availableMethods} />,
    },
    {
      id: EPaymentMethod.CREDIT_CARD,
      component: <CreditCard />,
    },
  ];

  if (
    !availableMethods ||
    !checkExpressCheckoutPaymentFormAvailable(
      availableMethods as unknown
    )
  ) {
    methods = methods.filter(
      (method) => method.id !== EPaymentMethod.PAYMENT_BUTTONS
    );
  }
  return methods;
}
