import {
  IGenericSection, 
  IGenericText, 
  IGenericButton, 
  IGenericSelect, 
  IGenericOptions, 
  IGenericFile
} from '../../../generic/types';
import { IChatMessage, IRichMessage, IFileTransfer, IRichMessageItem } from '../../types';
import { getMethods } from '../methods';

export const transformText = (text: string): IGenericText => {
  const result: IGenericText = {
    type: 'text',
    props: {
      value: text
    }
  };

  return result;
};

const convertMenuOptions = (items: Array<IRichMessageItem>): Array<IGenericOptions> => {
  return items.map((item: IRichMessageItem) => {
    return {
      key: item.text,
      value: item.text,
      label: item.text
    };
  });
};

export const transformMenu = (items: Array<IRichMessageItem>): IGenericSelect => {
  const result: IGenericSelect = {
    type: 'select',
    props: {
      options: convertMenuOptions(items)
    }
  };

  return result;
};

const transformSingleButton = (item: IRichMessageItem): IGenericButton => {
  const result: IGenericButton = {
    type: 'button',
    props: {
      label: item.text,
      value: item.text
    }
  };

  return result;
};

export const transformButtons = (items: Array<IRichMessageItem>): Array<IGenericButton> => {
  return items.map(transformSingleButton);
};

export const transformChatMessage = ({ text }: IChatMessage): IGenericSection => {
  const result: IGenericSection = {
    type: 'section',
    children: [transformText(text)]
  };

  return result;
};

export const transformRichMessage = ({ type, items }: IRichMessage): IGenericSection => {
  const result: IGenericSection = {
    type: 'section',
    children: [].concat(getMethods(type)(items))
  };

  return result;
};


export const transformFileTransfer = (message: IFileTransfer): IGenericSection => {
  // const text = `File Transfer: ${message.type}`;
  const text = message.text  || `File Transfer: ${message.type}`;

  const result: IGenericSection = {
    type: 'section',
    children: [transformText(text)]
  };

  if (message.type === 'Requested') {
    const file:IGenericFile = {
      type: 'file',
      props: {
        multiple: true,
        salesforce: {
          ...message
        }
      }
    };
    result.children.push(file);
  }

  return result;
};
