import * as React from 'react';
import {
  render,
  screen
} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import * as TextControlBase from '@tesseract-design/web-base-textcontrol';
import { vi } from 'vitest';
import {
  EmailAddressInput,
} from '.';

vi.mock('@tesseract-design/web-base-textcontrol');

describe('EmailAddressInput', () => {
  it('renders a textbox', () => {
    render(
      <EmailAddressInput />
    );
    const textbox = screen.getByRole('textbox');
    expect(textbox).toBeInTheDocument();
    expect(textbox).toHaveProperty('type', 'email');
  });

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

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

  it('renders a hidden label', () => {
    render(
      <EmailAddressInput
        label="foo"
        hiddenLabel
      />
    );
    const textbox = screen.getByLabelText('foo');
    expect(textbox).toBeInTheDocument();
    const label = screen.queryByTestId('label');
    expect(label).toBeNull();
  });

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

  it('renders an indicator', () => {
    render(
      <EmailAddressInput
        indicator={
          <div data-testid="indicator" />
        }
      />
    );
    const indicator = screen.getByTestId('indicator');
    expect(indicator).toBeInTheDocument();
  });

  describe.each(Object.values(TextControlBase.TextControlSize))('on %s size', (size) => {
    it('renders input styles', () => {
      render(
        <EmailAddressInput
          size={size}
        />
      );

      expect(TextControlBase.Input).toBeCalledWith(expect.objectContaining({
        size,
      }));
    });

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

      expect(TextControlBase.HintWrapper).toBeCalledWith(expect.objectContaining({
        size,
      }));
    });

    it('renders indicator styles', () => {
      render(
        <EmailAddressInput
          size={size}
          indicator={
            <div data-testid="indicator" />
          }
        />
      );

      expect(TextControlBase.IndicatorWrapper).toBeCalledWith(expect.objectContaining({
        size,
      }));
    });
  });

  it('renders a block textbox', () => {
    render(
      <EmailAddressInput
        block
      />
    );

    expect(TextControlBase.Root).toBeCalledWith(expect.objectContaining({
      block: true,
    }));
  });

  describe.each(Object.values(TextControlBase.TextControlStyle))('on %s style', (style) => {
    it('renders input styles', () => {
      render(
        <EmailAddressInput
          style={style}
        />
      );

      expect(TextControlBase.Input).toBeCalledWith(expect.objectContaining({
        style,
      }));
    });

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

      expect(TextControlBase.HintWrapper).toBeCalledWith(expect.objectContaining({
        style,
      }));
    });

    it('renders indicator styles', () => {
      render(
        <EmailAddressInput
          style={style}
          indicator={
            <div
              data-testid="indicator"
            />
          }
        />
      );

      expect(TextControlBase.IndicatorWrapper).toBeCalledWith(expect.objectContaining({
        style,
      }));
    });
  });

  it('handles change events', () => {
    const onChange = vi.fn().mockImplementationOnce((e) => { e.preventDefault() });
    render(
      <EmailAddressInput
        onChange={onChange}
      />
    );
    const textbox: HTMLInputElement = screen.getByRole('textbox');
    userEvent.type(textbox, 'foobar');
    expect(onChange).toBeCalled();
  });

  it('handles input events', () => {
    const onInput = vi.fn().mockImplementationOnce((e) => { e.preventDefault() });
    render(
      <EmailAddressInput
        onInput={onInput}
      />
    );
    const textbox: HTMLInputElement = screen.getByTestId('input');
    userEvent.type(textbox, 'foobar');
    expect(onInput).toBeCalled();
  });
});
