/* eslint-disable @next/next/no-img-element */
/* Requires React and TypeScript. Include nicky-crypto-button.css and assets/n-mark.png. */
import "./nicky-crypto-button.css";

type CryptoPaymentButtonProps = {
  checkoutUrl?: string;
  variant?: "dark" | "lime" | "light";
  height?: 44 | 48 | 56;
  width?: "content" | "container";
  action?: "pay" | "donate" | "send" | "buy" | "tip" | "crypto";
  locale?: "en" | "pt-br" | "el" | "es" | "fr" | "ru" | "vi" | "ne";
};

const markSizes = {
  "44": 20,
  "48": 20,
  "56": 24
};
const labels = {
  "en": "Pay with Crypto",
  "pt-br": "Pagar com Cripto",
  "el": "Πληρωμή με crypto",
  "es": "Pagar con Cripto",
  "fr": "Payer en crypto",
  "ru": "Оплатить криптовалютой",
  "vi": "Thanh toán bằng tiền điện tử",
  "ne": "क्रिप्टोसँग भुक्तानी गर्नुहोस्"
};
const actionLabels = {
  "en": {
    "pay": "Pay with Nicky",
    "donate": "Donate with Nicky",
    "send": "Send with Nicky",
    "buy": "Buy with Nicky",
    "tip": "Tip with Nicky"
  },
  "pt-br": {
    "pay": "Pagar com Nicky",
    "donate": "Doar com Nicky",
    "send": "Enviar com Nicky",
    "buy": "Comprar com Nicky",
    "tip": "Dar gorjeta com Nicky"
  },
  "el": {
    "pay": "Πληρώστε με Nicky",
    "donate": "Κάντε δωρεά με Nicky",
    "send": "Στείλτε με Nicky",
    "buy": "Αγοράστε με Nicky",
    "tip": "Αφήστε φιλοδώρημα με Nicky"
  },
  "es": {
    "pay": "Pagar con Nicky",
    "donate": "Donar con Nicky",
    "send": "Enviar con Nicky",
    "buy": "Comprar con Nicky",
    "tip": "Dejar propina con Nicky"
  },
  "fr": {
    "pay": "Payer avec Nicky",
    "donate": "Faire un don avec Nicky",
    "send": "Envoyer avec Nicky",
    "buy": "Acheter avec Nicky",
    "tip": "Laisser un pourboire avec Nicky"
  },
  "ru": {
    "pay": "Оплатить через Nicky",
    "donate": "Пожертвовать через Nicky",
    "send": "Отправить через Nicky",
    "buy": "Купить через Nicky",
    "tip": "Оставить чаевые через Nicky"
  },
  "vi": {
    "pay": "Thanh toán bằng Nicky",
    "donate": "Quyên góp qua Nicky",
    "send": "Gửi bằng Nicky",
    "buy": "Mua bằng Nicky",
    "tip": "Để lại tiền boa qua Nicky"
  },
  "ne": {
    "pay": "Nicky मार्फत भुक्तानी गर्नुहोस्",
    "donate": "Nicky मार्फत दान गर्नुहोस्",
    "send": "Nicky मार्फत पठाउनुहोस्",
    "buy": "Nicky मार्फत किन्नुहोस्",
    "tip": "Nicky मार्फत टिप दिनुहोस्"
  }
};

function getCheckoutHref(checkoutUrl: string) {
  try {
    const url = new URL(checkoutUrl.trim());
    const credentialKeys = /^(access_token|refresh_token|token|jwt|api_key|apikey|client_secret|secret|password|authorization|auth|signature|sig|private_key|code|session|session_id)$/i;
    const signupPath = /(^|[\/_-])(signup|sign-up|register)([\/_-]|$)/i;
    const hasQueryCredential = [...url.searchParams.keys()].some((key) => credentialKeys.test(key));
    if (url.protocol !== "https:" || !url.hostname || url.username || url.password) return;
    if (hasQueryCredential || signupPath.test(url.pathname)) return;
    return url.href;
  } catch {
    // Keep the button disabled until a valid HTTPS checkout URL is supplied.
  }
}

function NickyMark({ size }: { size: number }) {
  return <img className="nicky-crypto-button__mark" src="/assets/n-mark.png" width={size} height={size} alt="" />;
}

export function CryptoPaymentButton({
  checkoutUrl = "https://pay.nicky.me/payment-order/NICKY",
  variant = "dark",
  height = 48,
  width = "content",
  locale = "en",
  action = "pay",
}: CryptoPaymentButtonProps) {
  const checkoutHref = getCheckoutHref(checkoutUrl);
  const label = action === "crypto"
    ? labels[locale] ?? labels.en
    : actionLabels[locale][action] ?? actionLabels[locale].pay;
  const markSize = markSizes[height];
  const mark = <NickyMark size={markSize} />;
  const accessibleLabel = label + ", Nicky";

  const className = [
    "nicky-crypto-button",
    "nicky-crypto-button--" + variant,
    "nicky-crypto-button--" + height,
    "nicky-crypto-button--" + width,
  ].filter(Boolean).join(" ");
  const direction = "ltr";
  const content = (
    <>
      {mark}
      <span className="nicky-crypto-button__label">{label}</span>
    </>
  );

  if (!checkoutHref) {
    return (
      <button
        className={className}
        type="button"
        disabled
        lang={locale} dir={direction}
        aria-label={accessibleLabel + ". Provide a valid HTTPS checkout URL."}
      >
        {content}
      </button>
    );
  }

  return (
    <a className={className} href={checkoutHref} lang={locale} dir={direction} aria-label={accessibleLabel}>
      {content}
    </a>
  );
}
