import { BookingCancelData, BookingCancelOption } from '@/manager/modules/booking/booking-types';
import { RadioButton } from '@/manager/shared/components/RadioButton';
import { SingleDatePicker } from '@/manager/shared/components/SingleDatePicker';
import { getInputHostByLabel, getRadioGroupHost } from '@/manager/shared/utils/locator-utils';
import { BaseComponent } from '@/shared/base/BaseComponent';

export class BookingCancelRadioGroup extends BaseComponent {
  readonly main = getRadioGroupHost(this.host);
  readonly immediatelyRadioButton = new RadioButton(this.main.getByTestId('booking-cancel-radio-group-item-immediate'));
  readonly endOfCurrentPeriodRadioButton = new RadioButton(
    this.main.getByTestId('booking-cancel-radio-group-item-end-of-period')
  );
  readonly customDateRadioButton = new RadioButton(this.main.getByTestId('booking-cancel-radio-group-item-custom'));
  readonly customDatePicker = new SingleDatePicker(getInputHostByLabel(this.main.locator('.content'), 'Date *'));

  async fill(data: BookingCancelData): Promise<void> {
    await this.check(data.option);

    if (data.option === 'custom_date' && data.date) {
      await this.customDatePicker.fill(data.date);
    }
  }

  private check(option: BookingCancelOption): Promise<void> {
    switch (option) {
      case 'immediately':
        return this.immediatelyRadioButton.check();
      case 'end_of_current_period':
        return this.endOfCurrentPeriodRadioButton.check();
      case 'custom_date':
        return this.customDateRadioButton.check();
    }
  }
}
