import { IGenericInput } from '../../../generic/types';
import { ISlackInput, ISlackInputText } from '../../types';
import { getElementWithSectionWrapper } from '../helpers/section-wrapper';
import { transformTextObject } from './text';

export const transformPlainTextInput = ({
  props
}: IGenericInput): ISlackInput => {
  const element: ISlackInputText = {
    type: 'plain_text_input',
    action_id: props.slack?.action_id || props.variableName || props.web?.variableName
  };

  if (props.placeholder) {
    element.placeholder = transformTextObject({
      value: props.placeholder,
      slack: {
        type: props.slack?.placeholder_type,
        emoji: props.slack?.placeholder_emoji,
        verbatim: props.slack?.placeholder_verbatim
      }
    });
  }

  if (props.value) {
    element.initial_value = props.value;
  }

  const multiline = props?.slack?.multiline || props.multiline;
  if (multiline !== undefined && multiline !== null) {
    element.multiline = multiline;
  }

  if (props.maxLength) {
    element.max_length = props.maxLength;
  }

  if (props.minLength) {
    element.min_length = props.minLength;
  }

  if (props?.slack?.dispatch_action_config) {
    element.dispatch_action_config = props.slack.dispatch_action_config;
  }

  const label = transformTextObject({
    value: props.label || ' ',
    type: 'plain'
  });

  const interactive = props.web?.interactive;

  return getElementWithSectionWrapper(element, label, interactive);
};
