import { IGenericButton, IGenericCard, IGenericHeader } from "../../../../generic/types";
import { IRWCAnswerComponent } from "../../../types";

import { transformText, transformImage } from '../index';

export const transformCard = (msg: IRWCAnswerComponent): IGenericCard => {
  const result: IGenericCard = {
    type: 'card',
    props: {},
    children: []
  };

  const data = msg.vBind ? msg.vBind : msg;

  if (data?.image?.length) {
    data.image.forEach(img => {
      result.children.push(transformImage(img));
    });
  }

  if (data?.label) {
    // result.children.push(transformText(`**${data.label}**`));
    
    const header: IGenericHeader = {
      type: 'header',
      children: [transformText(data.label)]
    };

    result.children.push(header);
  }

  if (data?.description || data?.text) {
    const description = data.description || data.text;
    result.children.push(transformText(description));
  }

  if (data?.button) {
    const button: IGenericButton = {
      type: 'button',
      props: {
        label: data?.button,
        value: data?.value || data?.sendValue
      }
    };

    result.children.push(button);
  }

  return result;
};
