import { expect } from '@/core/fixtures';
import { AbstractFormInputWidget } from './AbstractFormInputWidget';
import { widgetStep } from '@/core/utils';

export class TextInput extends AbstractFormInputWidget<string | number> {
  type = 'formTextInput';

  protected getInputLocator() {
    return this.l.locator('input');
  }

  @widgetStep
  async change(val: string | number = '') {
    await this.getInputLocator().fill(val.toString());
  }
  @widgetStep
  async clear() {
    await this.getInputLocator().fill('');
  }

  @widgetStep
  async hasValue(val: string | number, exact = true) {
    await expect(this.getInputLocator()).toHaveValue(
      (exact
        ? `${val}`
        : new RegExp(
            val.toString().replace(/[.*+?^${}()|[\]\\]/g, '\\$&'),
            'g'
          )) || ''
    );
  }
  @widgetStep
  async hasNotValue(val?: string | number) {
    await expect(this.getInputLocator()).not.toHaveValue(val?.toString() || '');
  }
  @widgetStep
  async isEmpty() {
    await expect(this.getInputLocator()).toHaveValue('');
  }
}

export class NumberInput extends TextInput {
  type = 'formNumberInput';
}

export class TextareaInput extends TextInput {
  type = 'formTextAreaInput';

  getInputLocator() {
    return this.l.locator('textarea');
  }
}
