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

export type ToggleTickBoxProps = Omit<React.HTMLProps<HTMLInputElement>, 'type' | 'style'> & {
  /**
   * 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,
  /**
   * Does the component have indeterminate check state?
   */
  indeterminate?: boolean,
}

/**
 * Component for performing an action upon activation (e.g. when clicked).
 *
 * This component functions as a regular button.
 */
export const ToggleTickBox = React.forwardRef<HTMLInputElement, ToggleTickBoxProps>(
  (
    {
      children,
      block = false,
      compact = false,
      subtext,
      indeterminate = false,
      className: _className,
      as: _as,
      ...etcProps
    }: ToggleTickBoxProps,
    ref,
  ) => {
    const styleProps = React.useMemo<CheckControlBase.CheckControlBaseArgs>(() => ({
      block,
      compact,
      appearance: CheckControlBase.CheckControlAppearance.TICK_BOX,
      type: 'checkbox',
    }), [block, compact]);
    const defaultRef = React.useRef<HTMLInputElement>(null);
    const theRef = (ref ?? defaultRef) as React.MutableRefObject<HTMLInputElement>;

    React.useEffect(() => {
      if (!(indeterminate && theRef.current)) {
        return;
      }
      theRef.current.indeterminate = indeterminate;
    }, [theRef, indeterminate]);

    return (
      <div
        className={CheckControlBase.ClickAreaWrapper(styleProps)}
      >
        <label
          className={CheckControlBase.ClickArea()}
        >
          <input
            {...etcProps}
            type="checkbox"
            ref={theRef}
            className={CheckControlBase.CheckStateContainer(styleProps)}
          />
          <span>
            <span />
            <span
              className={CheckControlBase.CheckIndicatorArea(styleProps)}
            >
              <span
                className={CheckControlBase.CheckIndicatorWrapper(styleProps)}
              >
                <svg
                  className={CheckControlBase.CheckIndicator(styleProps)}
                  viewBox="0 0 24 24"
                  role="presentation"
                >
                  <polyline
                    points="20 6 9 17 4 12"
                  />
                </svg>
                <svg
                  className={CheckControlBase.CheckIndicator(styleProps)}
                  viewBox="0 0 24 24"
                  role="presentation"
                >
                  <polyline
                    points="20 12 4 12"
                  />
                </svg>
              </span>
            </span>
            <span>
              {children}
              {
                subtext
                && (
                  <>
                    <br />
                    <span
                      className={CheckControlBase.Subtext()}
                      data-testid="subtext"
                    >
                      {subtext}
                    </span>
                  </>
                )
              }
            </span>
          </span>
        </label>
      </div>
    );
  },
);

ToggleTickBox.displayName = 'ActionButton';
