import * as React from 'react';
import {
  cleanup,
  render,
  screen,
} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { Button } from '@tesseract-design/web-base';
import {
  vi,
  expect,
  describe,
  it,
  afterEach,
} from 'vitest';
import matchers from '@testing-library/jest-dom/matchers';
import {
  ToggleButton,
  ToggleButtonDerivedElement,
} from '.';

expect.extend(matchers);

describe('ToggleButton', () => {
  afterEach(() => {
    cleanup();
  });

  it('renders a checkbox', () => {
    render(
      <ToggleButton />,
    );
    const checkbox = screen.getByRole('checkbox');
    expect(checkbox).toBeInTheDocument();
  });

  it('renders a subtext', () => {
    render(
      <ToggleButton
        subtext="subtext"
      />,
    );
    const subtext = screen.getByTestId('subtext');
    expect(subtext).toBeInTheDocument();
  });

  it('renders a badge', () => {
    render(
      <ToggleButton
        badge="badge"
      />,
    );
    const badge = screen.getByTestId('badge');
    expect(badge).toBeInTheDocument();
  });

  describe('on indeterminate', () => {
    it('renders an indeterminate checkbox', () => {
      render(
        <ToggleButton
          indeterminate
        />,
      );
      const checkbox = screen.getByRole('checkbox');
      expect(checkbox).toHaveProperty('indeterminate', true);
    });

    it('acknowledges passed ref object', () => {
      const ref = React.createRef<ToggleButtonDerivedElement>();
      render(
        <ToggleButton
          indeterminate
          ref={ref}
        />,
      );
      expect(ref.current).toHaveProperty('indeterminate', true);
    });

    it('acknowledges passed legacy ref', () => {
      let refElement = null as null | ToggleButtonDerivedElement;
      const ref = (element: ToggleButtonDerivedElement) => {
        refElement = element;
      };

      render(
        <ToggleButton
          indeterminate
          ref={ref}
        />,
      );

      const checkbox = screen.getByRole('checkbox');
      expect(checkbox).toHaveProperty('indeterminate', true);
      expect(refElement).toBe(checkbox);
    });
  });

  it('handles click events', async () => {
    const onClick = vi.fn().mockImplementationOnce(
      (e: React.MouseEvent<ToggleButtonDerivedElement>) => {
        e.preventDefault();
      },
    );
    render(
      <ToggleButton
        onClick={onClick}
      />,
    );
    const checkbox = screen.getByRole('checkbox');
    await userEvent.click(checkbox);
    expect(onClick).toBeCalled();
  });

  it('renders a compact button', () => {
    render(
      <ToggleButton
        compact
      />,
    );

    const button = screen.getByTestId('button');
    expect(button).toHaveClass('pl-2 gap-2 pr-2');
  });

  describe.each`
    size        | className
    ${'small'}  | ${'h-10'}  
    ${'medium'} | ${'h-12'}  
    ${'large'}  | ${'h-16'}  
  `('on $size size', ({
    size,
    className,
  }: { size: Button.Size, className: string }) => {
    it('renders button styles', () => {
      render(
        <ToggleButton
          size={size}
        />,
      );

      const button = screen.getByTestId('button');
      expect(button).toHaveClass(className);
    });

    it('renders badge styles', () => {
      render(
        <ToggleButton
          size={size}
          badge="badge"
        />,
      );

      const badge = screen.getByTestId('badge');
      expect(badge).toBeInTheDocument();
    });
  });

  it.each`
    variant      | className
    ${'bare'}    | ${'bg-negative'}
    ${'outline'} | ${'border-2'}
    ${'filled'}  | ${'bg-primary'}
  `('renders a button with $variant variant', ({
    variant,
    className,
  }: { variant: Button.Variant, className: string }) => {
    render(
      <ToggleButton
        variant={variant}
      />,
    );

    const button = screen.getByTestId('button');
    expect(button).toHaveClass(className);
  });

  it('renders a block button', () => {
    render(
      <ToggleButton
        block
      />,
    );

    const button = screen.getByTestId('button');
    expect(button).toHaveClass('w-full flex');
  });

  it('renders children', () => {
    render(
      <ToggleButton>
        Foo
      </ToggleButton>,
    );

    const children = screen.getByTestId('children');
    expect(children).toHaveTextContent('Foo');
  });

  it('renders a disabled button', () => {
    render(
      <ToggleButton
        disabled
      />,
    );
    const checkbox = screen.getByRole('checkbox');
    expect(checkbox).toBeDisabled();
  });

  it('handles change events', async () => {
    const onChange = vi.fn().mockImplementationOnce(
      (e: React.ChangeEvent<ToggleButtonDerivedElement>) => {
        e.preventDefault();
      },
    );
    render(
      <ToggleButton
        onChange={onChange}
      />,
    );
    const checkbox = screen.getByRole('checkbox');
    await userEvent.click(checkbox);
    expect(onChange).toBeCalled();
  });
});
