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

expect.extend(matchers);

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

  it('renders a combobox', () => {
    render(
      <ComboBox />,
    );
    const combobox = screen.getByRole('combobox');
    expect(combobox).toBeInTheDocument();
    expect(combobox).toHaveProperty('type', 'text');
  });

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

  it('renders a label', () => {
    render(
      <ComboBox
        label="foo"
      />,
    );
    const combobox = screen.getByLabelText('foo');
    expect(combobox).toBeInTheDocument();
    const label = screen.getByTestId('label');
    expect(label).toHaveTextContent('foo');
  });

  it('renders a hidden label', () => {
    render(
      <ComboBox
        label="foo"
        hiddenLabel
      />,
    );
    const combobox = screen.getByLabelText('foo');
    expect(combobox).toBeInTheDocument();
    const label = screen.queryByTestId('label');
    expect(label).toBeInTheDocument();
    expect(label).toHaveClass('sr-only');
  });

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

  it('renders an indicator', () => {
    render(
      <ComboBox
        indicator={
          <div />
        }
      />,
    );
    const indicator = screen.getByTestId('indicator');
    expect(indicator).toBeInTheDocument();
  });

  describe.each`
    size        | inputClassName | hintClassName | indicatorClassName
    ${'small'}  | ${'h-10'}      | ${'pr-10'}    | ${'w-10'}
    ${'medium'} | ${'h-12'}      | ${'pr-12'}    | ${'w-12'}
    ${'large'}  | ${'h-16'}      | ${'pr-16'}    | ${'w-16'}
  `('on $size size', ({
    size,
    inputClassName,
    hintClassName,
    indicatorClassName,
  }: {
    size: TextControl.Size,
    inputClassName: string,
    hintClassName: string,
    indicatorClassName: string,
  }) => {
    it('renders input styles', () => {
      render(
        <ComboBox
          size={size}
        />,
      );

      const input = screen.getByTestId('input');
      expect(input).toHaveClass(inputClassName);
    });

    it('renders label styles with indicator', () => {
      render(
        <ComboBox
          size={size}
          label="foo"
          indicator={<div />}
        />,
      );
      const label = screen.getByTestId('label');
      expect(label).toHaveClass(hintClassName);
    });

    it('renders hint styles when indicator is present', () => {
      render(
        <ComboBox
          size={size}
          hint="hint"
          indicator={<div />}
        />,
      );

      const hint = screen.getByTestId('hint');
      expect(hint).toHaveClass(hintClassName);
    });

    it('renders indicator styles', () => {
      render(
        <ComboBox
          size={size}
          indicator={
            <div />
          }
        />,
      );

      const indicator = screen.getByTestId('indicator');
      expect(indicator).toHaveClass(indicatorClassName);
    });
  });

  it('renders a block combobox', () => {
    render(
      <ComboBox
        block
      />,
    );

    const base = screen.getByTestId('base');
    expect(base).toHaveClass('block');
  });

  it.each(TextControl.AVAILABLE_INPUT_TYPES)('renders a combobox with type %s', (inputType) => {
    render(
      <ComboBox
        type={inputType}
      />,
    );
    const combobox = screen.getByTestId('input');
    expect(combobox).toHaveProperty('type', inputType);
  });

  it('falls back to text input mode when it clashes with the input type', () => {
    render(
      <ComboBox
        type="text"
        inputMode="search"
      />,
    );
    const combobox = screen.getByTestId('input');
    expect(combobox).toHaveProperty('inputMode', 'text');
  });

  describe.each`
    variant        | inputClassName   | hintClassName
    ${'default'}   | ${'pl-4'}        | ${'bottom-0 pl-4 pb-1'}
    ${'alternate'} | ${'pl-1.5 pt-4'} | ${'top-0.5'}
  `('on $variant style', ({
    variant,
    inputClassName,
    hintClassName,
  }: {
    variant: TextControl.Variant,
    inputClassName: string,
    hintClassName: string,
  }) => {
    it('renders input styles', () => {
      render(
        <ComboBox
          variant={variant}
        />,
      );

      const input = screen.getByTestId('input');
      expect(input).toHaveClass(inputClassName);
    });

    it('renders hint styles', () => {
      render(
        <ComboBox
          variant={variant}
          hint="hint"
        />,
      );

      const hint = screen.getByTestId('hint');
      expect(hint).toHaveClass(hintClassName);
    });
  });

  it('handles change events', async () => {
    const onChange = vi.fn().mockImplementationOnce(
      (e: React.ChangeEvent<ComboBoxDerivedElement>) => {
        e.preventDefault();
      },
    );
    render(
      <ComboBox
        onChange={onChange}
      />,
    );
    const combobox: HTMLInputElement = screen.getByRole('combobox');
    await userEvent.type(combobox, 'foobar');
    expect(onChange).toBeCalled();
  });

  it('handles input events', async () => {
    const onInput = vi.fn().mockImplementationOnce(
      (e: React.SyntheticEvent<ComboBoxDerivedElement>) => {
        e.preventDefault();
      },
    );
    render(
      <ComboBox
        onInput={onInput}
      />,
    );
    const combobox: HTMLInputElement = screen.getByTestId('input');
    await userEvent.type(combobox, 'foobar');
    expect(onInput).toBeCalled();
  });
});
