import { IGenericButton } from '../../../generic/types';
import { ISlackButton } from '../../types';
import { transformConfirm } from './confirm';
import { transformTextObject } from './helpers/transform-text-object';

export const transformButton = ({
  text,
  action_id,
  url,
  value,
  style,
  confirm
}: ISlackButton): IGenericButton => {
  const res: IGenericButton = {
    type: 'button',
    props: {
      label: transformTextObject(text).value,
      slack: {
        action_id
      },
      web: {
        variableName: action_id
      }
    }
  };

  if (text.type) {
    res.props.slack.label_type = text.type;
    res.props.slack.label_emoji = text.emoji;
    res.props.slack.label_verbatim = text.verbatim;
  }

  const colorMap = {
    primary: 'green',
    danger: 'red'
  };

  if (style) res.props.color = colorMap[style] || 'default';
  if (url) res.props.slack.url = url;
  if (value) res.props.slack.value = value;
  if (confirm) res.props.slack.confirm = transformConfirm(confirm);
  
  res.props.web = {
    type: 'secondary',
    size: 'normal',
    variableName: action_id,
    ...res.props.web
  };
  return res;
};