import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { RadioButtonGroup } from './models';
import { RadioButton } from '../radio-button/models';

@Component({
  selector: 'lib-radio-button-group',
  templateUrl: './radio-button-group.component.html',
  styleUrls: ['./radio-button-group.component.scss'],
})
export class RadioButtonGroupComponent implements OnInit {
  @Input() radioButtonGroup: RadioButtonGroup;
  @Output() clickRadio: EventEmitter<RadioButtonGroup> = new EventEmitter();
  constructor() {}

  ngOnInit() {}

  radioButtonClicked(radioButton: RadioButton) {
    if (this.radioButtonGroup) {
      const updatedRadioButtonGroup = <RadioButtonGroup>{
        radioButtons: this.radioButtonGroup.radioButtons.map((t) => {
          if (t.id === radioButton.id) {
            return radioButton;
          } else {
            return { ...t, selected: false };
          }
        }),
      };
      this.clickRadio.emit(updatedRadioButtonGroup);
    }
  }
}
