import { type ReactElement } from "react";
import * as React from "react";
import { cva, type VariantProps } from "class-variance-authority";
import { Toggle as ToggleComponent } from "@base-ui/react/toggle";
import { cn } from "@/lib/utils";

/**
 * Toggle — WealthX DS overrides (shadcn base)
 *
 * Changes from shadcn default:
 *   - `rounded-md` removed — sharp corners per WealthX DS
 *   - `shadow-xs` removed from outline variant — flat panels
 *   - Pressed/on state uses `bg-primary/10` + `inset-ring-primary` + `text-foreground` via Figma tokens
 *     (inset-ring instead of border-primary so indicator is independent of border-width,
 *      works correctly inside ToggleGroup where border-l is collapsed for connected items)
 */
const toggleVariants = cva(
  "inline-flex items-center justify-center gap-2 whitespace-nowrap transition-[color,box-shadow] outline-none focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:pointer-events-none disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-destructive/20 data-pressed:bg-primary/10 data-pressed:inset-ring data-pressed:inset-ring-primary data-pressed:text-foreground data-pressed:hover:bg-primary/10 data-pressed:hover:text-foreground dark:aria-invalid:ring-destructive/40 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
  {
    variants: {
      variant: {
        default: "bg-transparent hover:bg-muted hover:text-muted-foreground",
        outline:
          "border border-input bg-transparent hover:bg-accent hover:text-accent-foreground",
      },
      size: {
        default: "h-9 min-w-9 px-2 text-label-large",
        sm: "h-8 min-w-8 px-1.5 text-label-medium",
        lg: "h-10 min-w-10 px-2.5 text-h5",
      },
    },
    defaultVariants: {
      variant: "default",
      size: "default",
    },
  },
);

export type ToggleProps = React.ComponentProps<typeof ToggleComponent> &
  VariantProps<typeof toggleVariants>;

function Toggle({
  className,
  variant,
  size,
  ...props
}: ToggleProps): ReactElement {
  return (
    <ToggleComponent
      className={cn(toggleVariants({ variant, size, className }))}
      data-slot="toggle"
      {...props}
    />
  );
}

export { Toggle, toggleVariants };
