import * as React from 'react';
import clsx from 'clsx';
import * as ButtonBase from '@tesseract-design/web-base-button';

export type ActionButtonDerivedElement = HTMLButtonElement;

export interface ActionButtonProps extends Omit<React.HTMLProps<ActionButtonDerivedElement>, 'type' | 'size'> {
  type?: ButtonBase.ButtonType;
  variant: ButtonBase.ButtonVariant;
  block?: boolean;
  subtext?: React.ReactNode;
  badge?: React.ReactNode;
  menuItem?: boolean;
  size?: ButtonBase.ButtonSize;
  compact?: boolean;
}

export const ActionButton = React.forwardRef<ActionButtonDerivedElement, ActionButtonProps>(({
  type = 'button' as const,
  variant,
  subtext,
  badge,
  menuItem = false,
  children,
  size = 'medium' as const,
  compact = false,
  className,
  block = false,
  ...etcProps
}, forwardedRef) => (
  <button
    {...etcProps}
    type={type}
    ref={forwardedRef}
    className={clsx(
      'items-center justify-center rounded overflow-hidden ring-secondary/50 leading-none gap-4 select-none',
      'focus:outline-0 focus:ring-4',
      'active:ring-tertiary/50',
      'disabled:opacity-50 disabled:cursor-not-allowed',
      {
        'flex w-full': block,
        'inline-flex max-w-full align-middle': !block,
      },
      {
        'pl-2': compact,
        'pl-4': !compact,
        'pr-4': !(compact || menuItem),
        'pr-2': compact || menuItem,
      },
      {
        'border-2 border-primary disabled:border-primary focus:border-secondary active:border-tertiary' : variant !== 'bare',
        'bg-negative text-primary disabled:text-primary focus:text-secondary active:text-tertiary': variant !== 'filled',
        'bg-primary text-negative disabled:bg-primary focus:bg-secondary active:bg-tertiary': variant === 'filled',
      },
      {
        'h-10': size === 'small',
        'h-12': size === 'medium',
        'h-16': size === 'large',
      },
      className,
    )}
  >
    <span
      className={clsx(
        'flex-auto min-w-0',
        {
          'text-left': compact || menuItem,
          'text-center': !(compact || menuItem),
        },
      )}
    >
      <span
        className="block uppercase font-bold h-[1.1em] w-full whitespace-nowrap overflow-hidden text-ellipsis font-semi-expanded"
        data-testid="children"
      >
        {children}
      </span>
      {subtext && (
        <>
          <span className="sr-only">
            {' - '}
          </span>
          <span
            className="block h-[1.3em] w-full whitespace-nowrap overflow-hidden text-ellipsis font-semi-expanded font-bold text-xs"
            data-testid="subtext"
          >
            {subtext}
          </span>
        </>
      )}
    </span>
    {badge && (
      <>
        <span className="sr-only">
          {' - '}
        </span>
        <span
          data-testid="badge"
        >
          {badge}
        </span>
      </>
    )}
    {menuItem && (
      <span
        data-testid="menuItemIndicator"
      >
        <svg
          className="w-6 h-6 fill-none stroke-current stroke-2 linejoin-round linecap-round"
          viewBox="0 0 24 24"
          role="presentation"
        >
          <polyline points="9 18 15 12 9 6"/>
        </svg>
      </span>
    )}
  </button>
));

ActionButton.displayName = 'ActionButton';
