import { IGenericWrapper } from '../../generic/types/wrapper';
import { ISNowEvent, IMessageEvent, IInputText } from '../types/index';

import { getMethods } from './methods';

export function transformServiceNowToGeneric(data: ISNowEvent): IGenericWrapper;
export function transformServiceNowToGeneric(data: IMessageEvent): IGenericWrapper;
export function transformServiceNowToGeneric(data): IGenericWrapper {
  const messages = data.messages || [];
  const props: any = {};

  if (data.result) {
    messages.push(data);
  } else {
    props.rwc = { mergeTextSections: false };
  }

  const children = messages.reduce((acc, { result }) => {
    if (typeof result === 'string') {
      const msg = {
        uiType: 'InputText',
        label: result
      } as IInputText;
      return acc.concat(getMethods(msg.uiType)(msg));
    }
  
    return acc.concat(getMethods(result.uiType)(result));
  }, []);

  return {
    type: 'wrapper',
    props,
    children
  };
}
