import { IGenericOptions, IGenericRadio } from "../../../generic/types";
import { IRWCAnswerComponent } from "../../types";
import { removeBackticks } from "../../utils";

export const transformRadioGroup = (msg: IRWCAnswerComponent): IGenericRadio => {
  const options: Array<IGenericOptions> = msg.vBind?.options.map(({ label, value } )=> {
    return {
      label,
      value,
      name: value,
      key: value
    };
  });

  
  const result: IGenericRadio = {
    type: 'radio',
    props: {
      value: {},
      options
    }
  };
  
  if (msg.vBind.defaultValue) {
    options.forEach(op => {
      result.props.value[op.name] = removeBackticks(msg.vBind.defaultValue) === removeBackticks(op.label);
    });
  }

  return result;
};
