import { IGenericSection } from "../../../../generic/types";
import { IRWCMessage } from "../../../types";
import { getMethods } from "../methods";

export const transformMessage = (
  {type = 'message@message', components = [], text = '', message = ''}
  : IRWCMessage): IGenericSection => {
  const section: IGenericSection = {
    type: 'section',
    props: {
      rwc: {
        type: type,
      }
    },
    children: []
  };
  
 if (components.length) {
  const children = components.reduce((acc, component) => {
    const name = component.name;

    acc.push(getMethods(name)(component));

    return acc;
  }, []);

  section.children.push(...children);
 } else {
    const msg = text || message;

    msg && section.children.push(getMethods('rwc-text')(msg));
 }

  return section;
};