import { IGenericSelect } from '../../../../generic/types/select';
import { ISlackSelectExternal } from '../../../types';
import { transformConfirm } from '../confirm';
import { convertSingleOption } from '../helpers/convert-options';
import { transformTextObject } from '../helpers/transform-text-object';

export const transformExternalSelect = ({ 
  type,
  placeholder,
  action_id,
  initial_option,
  min_query_length,
  confirm
  }: ISlackSelectExternal
): IGenericSelect => {
  const res: IGenericSelect = {
    type: 'select',
    props: {
      value: convertSingleOption(initial_option),
      placeholder: transformTextObject(placeholder).value,
      variableName: action_id,
      slack: {
        type
      }
    }
  };

  if (placeholder) {
    res.props.slack.placeholder_type = placeholder.type;
    res.props.slack.placeholder_emoji = placeholder.emoji;
    res.props.slack.placeholder_verbatim = placeholder.verbatim;
  }

  if (min_query_length) {
    res.props.slack.min_query_length = min_query_length;
  }

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

  return res;
};