import React from "react";

enum BtnSize {
  SMALL = "small",
  MEDIUM = "medium",
  XS = "xs",
  LARGE = "large",
  MODAL = "modal",
  XSS = "xss",
}

export interface ButtonProps
  extends React.ButtonHTMLAttributes<HTMLButtonElement> {
  children: React.ReactNode;
  className?: string;
  variant?:
    | "primary"
    | "secondary"
    | "danger"
    | "custom"
    | "selectable"
    | "secondarys"
    | "orange"
    | "blue";
  size?: "small" | "medium" | "xs" | "large" | "modal" | "xss";
  isSelected?: boolean;
  /**
   * Shows a spinner inside the button and blocks further clicks. Use this for
   * work the button itself started — a page-covering overlay is the wrong
   * feedback for a form submit.
   */
  loading?: boolean;
}

/**
 * The button's visual classes, without the button.
 *
 * Exported so a *link* can wear the same styling — see `ButtonLink`. Four
 * screens rendered `<Link><Button>Label</Button></Link>`, which puts a
 * `<button>` inside an `<a>`: invalid content, two Tab stops for one
 * destination, and the inner one announced as a button that does nothing of
 * its own. They were written that way because the styling lived only inside
 * the component; now it does not.
 */
export function buttonClasses({
  variant = "primary",
  size = BtnSize.SMALL,
  isSelected = false,
  className,
}: {
  variant?: NonNullable<ButtonProps["variant"]>;
  size?: NonNullable<ButtonProps["size"]>;
  isSelected?: boolean;
  className?: string;
}): string {
  /*
   * Disabled reads as unavailable: half opacity and a not-allowed cursor. A
   * button showing its own `loading` spinner is disabled too, but stays more
   * legible (`aria-busy`). `opacity-100` used to be unconditional here, which
   * is why every disabled `Button` looked exactly like an enabled one.
   */
  const baseClassNames =
    "single-line-button whitespace-nowrap shrink-0 transition focus:outline-none text-center flex justify-center items-center gap-2 opacity-100 focus:ring-2 font-bold text-base disabled:cursor-not-allowed disabled:opacity-50 aria-busy:cursor-wait aria-busy:opacity-75";

  const variantClassNames = {
    primary: "bg-primary-5 text-white",
    secondary: "bg-[#F4F4F7] text-primary-5",
    secondarys: "bg-primary-5 text-primary-400 text-white",
    danger: "bg-red-500 focus:bg-red-400 text-white",
    selectable: `p-[8.5px] border border-secondary-20 text-sm font-normal rounded-[24px] bg-secondary-10 text-seconday-250 ${
      isSelected ? "text-white" : ""
    }`,
    custom: "",
    orange: "bg-primary-801 text-white",
    blue: "bg-primary-802 text-white",
  };

  const sizeClassNames = {
    [BtnSize.SMALL]: "rounded-[10px]",
    [BtnSize.MEDIUM]:
      "w-[160px] 2xl:w-[190px] rounded-[30px] 2xl:text-sm lg:text-[14px] laptop:w-[180px] lg:w-[150px] laptop:rounded-[30px] xss:rounded-[30px]",
    [BtnSize.XS]: "w-[122px] rounded-[30px]",
    [BtnSize.XSS]:
      "w-[122px] rounded-[30px] xss:rounded-[30px]",
    [BtnSize.LARGE]: "w-[100%] rounded-[15px] text-sm",
    [BtnSize.MODAL]: "w-[415.45px] rounded-[15px] text-sm",
  };

  return [
    baseClassNames,
    size ? sizeClassNames[size] : "",
    variantClassNames[variant],
    className,
  ]
    .filter(Boolean)
    .join(" ");
}

const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
  (
    {
      children,
      className,
      variant = "primary",
      size = BtnSize.SMALL,
      isSelected = false,
      loading = false,
      disabled,
      ...props
    },
    ref
  ) => {
    const buttonClassNames = buttonClasses({
      variant,
      size,
      isSelected,
      className,
    });

    return (
      <button
        className={buttonClassNames}
        ref={ref}
        {...props}
        disabled={disabled || loading}
        aria-busy={loading || undefined}
      >
        {loading && (
          <span
            aria-hidden="true"
            // `border-current` so the spinner takes the button's text colour on
            // every variant, dark-on-light and light-on-dark alike.
            className="inline-block shrink-0 w-4 h-4 border-2 border-current border-t-transparent rounded-full animate-spin"
          />
        )}
        {children}
      </button>
    );
  }
);

Button.displayName = "Button";

export { Button };
export default Button;
