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

export type RadioButtonProps = Omit<React.HTMLProps<HTMLInputElement>, '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,
  /**
   * 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,
}

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

    return (
      <div
        className={CheckControlBase.ClickAreaWrapper(styleProps)}
      >
        <label
          className={CheckControlBase.ClickArea()}
        >
          <input
            {...etcProps}
            disabled={disabled}
            type="radio"
            ref={ref}
            className={CheckControlBase.CheckStateContainer(styleProps)}
          />
          <span
            className={ButtonBase.Button(styleProps)}
          >
            <span
              className={ButtonBase.Border(styleProps)}
            />
            <span
              className={CheckControlBase.CheckIndicatorArea(styleProps)}
            >
              <span
                className={CheckControlBase.CheckIndicatorWrapper(styleProps)}
              />
            </span>
            <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>
                </>
              )
            }
          </span>
        </label>
      </div>
    );
  },
);

RadioButton.displayName = 'ActionButton';
