import { BookingAdjustmentApplyType } from '@/manager/modules/booking/booking-types';
import { RadioButton } from '@/manager/shared/components/RadioButton';
import { SingleDatePicker } from '@/manager/shared/components/SingleDatePicker';
import { getFieldSetHost } from '@/manager/shared/utils/locator-utils';
import { BaseComponent } from '@/shared/base/BaseComponent';

export class BookingAdjustmentApplyChangesRadioGroup extends BaseComponent {
  readonly main = getFieldSetHost(this.host, 'Apply changes');
  readonly immediatelyRadioButton = new RadioButton(
    this.main.getByTestId('booking-adjust-apply-changes-radio-group-item-immediately')
  );
  readonly endOfCurrentPeriodRadioButton = new RadioButton(
    this.main.getByTestId('booking-adjust-apply-changes-radio-group-item-end_current_period')
  );
  readonly customDateRadioButton = new RadioButton(
    this.main.getByTestId('booking-adjust-apply-changes-radio-group-item-custom')
  );
  readonly customDatePicker = new SingleDatePicker(this.main.getByTestId('date-picker'));

  async fill(applyType: BookingAdjustmentApplyType, applyDate?: Date): Promise<void> {
    await this.check(applyType);

    if (applyType === 'custom' && applyDate) {
      await this.customDatePicker.fill(applyDate);
    }
  }

  private check(applyType: BookingAdjustmentApplyType): Promise<void> {
    switch (applyType) {
      case 'immediately':
        return this.immediatelyRadioButton.check();
      case 'end_current_period':
        return this.endOfCurrentPeriodRadioButton.check();
      case 'custom':
        return this.customDateRadioButton.check();
    }
  }
}
