import { blueberryTwilightPalette } from "@mui/x-charts/colorPalettes";

export interface HumanReadablePaymentMethod {
  name: string;
  color: string;
}

/**
 * Function to get the payment method name and color based on the method string.
 * @param method The payment method string to match.
 * @param theme Optional MUI theme object for accessing palette colors.
 * @returns An object containing the payment method name and color.
 */

export function getHumanReadablePaymentMethod(
  method: string,
  {
    theme,
    disambiguateCards,
  }: {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    theme?: any;
    disambiguateCards?: boolean;
  } = {
    disambiguateCards: true,
  },
): HumanReadablePaymentMethod {
  const colorMode = theme?.palette?.mode ?? "light";

  const paymentMethods = [
    {
      match: /googlepay|google pay|gogglepay|google/i,
      name: "Google Pay",
      color:
        theme?.palette?.paymentProvider?.googlepay?.main ??
        blueberryTwilightPalette(colorMode)[4] ??
        "#34A853",
    },
    {
      match: /applepay|apple pay|applepay|apple/i,
      name: "Apple Pay",
      color:
        theme?.palette?.paymentProvider?.applepay?.main ??
        blueberryTwilightPalette(colorMode)[5] ??
        "#A2AAAD",
    },
    {
      match: /paypal/i,
      name: "PayPal",
      color:
        theme?.palette?.paymentProvider?.paypal?.main ??
        blueberryTwilightPalette(colorMode)[3] ??
        "#00457C",
    },
    {
      match: /swish/i,
      name: "Swish",
      color:
        theme?.palette?.paymentProvider?.swish?.main ??
        blueberryTwilightPalette(colorMode)[6] ??
        "#FF6D00",
    },
    {
      match: /klarna/i,
      name: "Klarna",
      color:
        theme?.palette?.paymentProvider?.klarna?.main ??
        blueberryTwilightPalette(colorMode)[7] ??
        "#F8A000",
    },
    ...(disambiguateCards
      ? [
          {
            match: /mastercard|mc/i,
            name: "Mastercard",
            color:
              theme?.palette?.paymentProvider?.mc?.main ??
              blueberryTwilightPalette(colorMode)[0] ??
              "#FF5F00",
          },
          {
            match: /visa/i,
            name: "Visa",
            color:
              theme?.palette?.paymentProvider?.visa?.main ??
              blueberryTwilightPalette(colorMode)[1] ??
              "#1A1F71",
          },
          {
            match: /american express|amex/i,
            name: "American Express",
            color:
              theme?.palette?.paymentProvider?.amex?.main ??
              blueberryTwilightPalette(colorMode)[2] ??
              "#016FD0",
          },
        ]
      : [
          {
            match: /mastercard|mc|visa|american express|amex/i,
            name: "Card",
            color:
              theme?.palette?.paymentProvider?.card?.main ??
              blueberryTwilightPalette(colorMode)[0] ??
              "#FF5F00",
          },
        ]),
  ];

  for (const pm of paymentMethods) {
    if (pm.match.test(method)) {
      return { name: pm.name, color: pm.color };
    }
  }

  // Default if no match found
  return {
    name: method,
    color: blueberryTwilightPalette(colorMode)[8] ?? "grey",
  };
}
