---
const opensInNewTab = (href: string) => /^https?:\/\//.test(href);

interface Props {
  href?: string;
  variant?: "primary" | "secondary" | "pill" | "text";
  compact?: boolean;
  full?: boolean;
  type?: "button" | "submit" | "reset";
  class?: string;
}

const {
  href,
  variant = "primary",
  compact = false,
  full = false,
  type = "button",
  class: className,
} = Astro.props;

const classes = [
  "button",
  variant !== "primary" && `button--${variant}`,
  compact && "button--compact",
  full && "button--full",
  className,
];
const external = href ? opensInNewTab(href) : false;
---

{href ? (
  <a
    href={href}
    class:list={classes}
    {...(external ? { target: "_blank", rel: "noopener noreferrer" } : {})}
  >
    <slot />
  </a>
) : (
  <button type={type} class:list={classes}>
    <slot />
  </button>
)}
