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

export const transformButton = ({ props }: IGenericButton): ISlackButton => {
  
  const res: ISlackButton = {
    type: 'button',
    text: transformTextObject({
      value: props.label,
      type: 'plain',
      slack: {
        type: props.slack?.label_type,
        emoji: props.slack?.label_emoji,
        verbatim: props.slack?.label_verbatim
      }
    }),
    action_id: props.slack?.action_id || props.variableName || props.web?.variableName
  };

  const url = props.slack?.url;
  const value = props.slack?.value;
  const style = props.color;
  const confirm = props.slack?.confirm;
  const buttonType = props.web?.buttonType;

  if (buttonType) {
    res.action_id = `${buttonType}::${res.action_id}`;
  }

  if (url) res.url = url;
  if (value) res.value = value;
  if (style && style !== 'default') {
    let color = undefined;
    if (style === 'green') {
      color = 'primary';
    } else if (style === 'red') {
      color = 'danger';
    }

    res.style = color;
  }

  if (confirm) res.confirm = transformConfirm(confirm);

  return res;
};