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

/**
 * Available ActionButton type values.
 */
export enum ActionButtonType {
  SUBMIT = 'submit',
  RESET = 'reset',
  BUTTON = 'button',
}

/**
 * Props for the component.
 */
export type ActionButtonProps = Omit<React.HTMLProps<HTMLButtonElement>, 'size' | 'type' | 'style'> & {
  /**
   * Size of the component.
   */
  size?: ButtonBase.ButtonSize,
  /**
   * Variant of the component.
   */
  variant?: ButtonBase.ButtonVariant,
  /**
   * Should the component display a border?
   */
  border?: boolean,
  /**
   * Should the component occupy the whole width of its parent?
   */
  block?: boolean,
  /**
   * Type of the component.
   */
  type?: ActionButtonType,
  /**
   * Does the component need to conserve space?
   */
  compact?: boolean,
  /**
   * Complementary content of the component.
   */
  subtext?: React.ReactNode,
  /**
   * Short complementary content displayed at the edge of the component.
   */
  badge?: React.ReactNode,
  /**
   * Is this component part of a menu?
   */
  menuItem?: boolean,
}

/**
 * Component for performing an action upon activation (e.g. when clicked).
 *
 * This component functions as a regular button.
 */
export const ActionButton = React.forwardRef<HTMLButtonElement, ActionButtonProps>(
  (
    {
      size = ButtonBase.ButtonSize.MEDIUM,
      variant = ButtonBase.ButtonVariant.OUTLINE,
      border = false,
      children,
      type = ActionButtonType.BUTTON,
      block = false,
      disabled = false,
      compact = false,
      subtext,
      badge,
      menuItem = false,
      className: _className,
      as: _as,
      ...etcProps
    }: ActionButtonProps,
    ref,
  ) => {
    const styleProps = React.useMemo<ButtonBase.ButtonBaseArgs>(() => ({
      size,
      block,
      variant,
      border,
      compact,
      menuItem,
      disabled,
    }), [size, block, variant, border, compact, menuItem, disabled]);

    return (
      <button
        {...etcProps}
        disabled={disabled}
        className={ButtonBase.Button(styleProps)}
        ref={ref}
        type={type}
      >
        <span
          className={ButtonBase.Border(styleProps)}
        />
        <span
          className={ButtonBase.Label(styleProps)}
        >
          <span
            className={ButtonBase.MainText()}
            data-testid="children"
          >
            <span
              className={ButtonBase.OverflowText()}
            >
              {children}
            </span>
          </span>
          {
            subtext
            && (
              <>
                {' '}
                <span
                  className={ButtonBase.Subtext()}
                  data-testid="subtext"
                >
                  <span
                    className={ButtonBase.OverflowText()}
                  >
                    {subtext}
                  </span>
                </span>
              </>
            )
          }
        </span>
        {
          badge
          && (
            <>
              {' '}
              <span
                className={ButtonBase.BadgeContainer(styleProps)}
                data-testid="badge"
              >
                {badge}
              </span>
            </>
          )
        }
        {
          menuItem
          && (
            <>
              {' '}
              <span
                className={ButtonBase.IndicatorWrapper(styleProps)}
                data-testid="menuItemIndicator"
              >
                <svg
                  className={ButtonBase.Indicator()}
                  viewBox="0 0 24 24"
                  role="presentation"
                >
                  <polyline points="9 18 15 12 9 6"/>
                </svg>
              </span>
            </>
          )
        }
      </button>
    );
  },
);

ActionButton.displayName = 'ActionButton';
