import { IGenericRadio, IGenericRadioValue } from '../../../generic/types';
import { ISlackRadioButton } from '../../types';
import { convertOptions } from './helpers/convert-options';
import { transformConfirm } from './confirm';

export const transformRadioButtons = ({ 
  action_id,
  options,
  initial_option,
  confirm
}: ISlackRadioButton): IGenericRadio => {
  const res: IGenericRadio = {
    type: 'radio',
    props: {
      variableName: action_id,
      options: convertOptions(options),
      slack: {}
    }
  };

  if (initial_option) {
    const value: IGenericRadioValue = {};
    options?.forEach(op => {
      value[op.value] = initial_option.value === op.value;
    });

    res.props.value = value;

    // const selected = convertOptions([initial_option]);
    // if (selected?.length) {
    //   res.props.value = selected;
    // }
  }

  if (confirm) {
    res.props.slack.confirm = transformConfirm(confirm);
  }

  res.props.web = {
    vertical: true,
    ...res.props.web
  };

  return res;
};