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

export const transformMessage = (msg: IRWCMessage): IGenericSection => {
  const section: IGenericSection = {
    type: 'section',
    children: []
  };

  if (msg.message || msg.answer?.message) {
    const text = msg.message || msg.answer.message;
    section.children.push(getMethods('text')(text));
  }
  
  if (msg.answerComponent) {
    const type = msg.answerComponent.component || msg.answerComponent.name;

    const transformed = getMethods(type)(msg.answerComponent);
    if (Array.isArray(transformed)) {
      section.children.push(...transformed);
    } else {
      section.children.push(transformed);
    }
  }

  if (msg.medias?.length) {
    const attachments = getMethods('medias')(msg.medias); 
    section.children.push(attachments);
  }

  return section;
};