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

import { transformRWCText, transformImage, transformMediaContent } from '../index';

export const transformCard = (card): IGenericCard => {
  const result: IGenericCard = { 
    type: 'card',
    props: {
      rwc: {
        value: card?.value || "``",
        cardId: card?.cardId || "``",
        hasModal: card?.hasModal || false,
        buttonLabel: card?.buttonLabel || "``",
        buttonStyle: card?.buttonStyle || "default",
      }
    },
    children: []
  };

  if (card.hasModal) {
    result.props.rwc.modalType = card.modalType,
    result.props.rwc.childWindowType = card?.childWindowType;
    result.props.rwc.showModalButtonInUserResponse = card?.showModalButtonInUserResponse;

    if (result.props.rwc.childWindowType === 'html') {
      result.props.rwc.html = card?.html;
    } else {
      const additionalContent = card?.additionalContentLink || {};

      if (Object.keys(additionalContent).length) {
        result.children.push(transformMediaContent(additionalContent, 'additionalContentLink'));
      }
    }
  }

  if (card?.video && Object.keys(card.video).length) {
    result.children.push(transformMediaContent(card.video));
  }

  if (card?.image && Object.keys(card.image).length) {
    result.children.push(transformImage(card.image));
  }

  if (card?.title) {
    const header: IGenericHeader = {
      type: 'header',
      children: [transformRWCText(card.title)]
    };

    result.children.push(header);
  }

  if (card?.description) {
    result.children.push(transformRWCText(card.description));
  }

  if (card?.submitButtons && card.submitButtons.length) {
    const btns = [...card.submitButtons];

    btns.forEach(btn => {
      const button: IGenericButton = {
        type: 'button',
        props: {
          label: btn.label,
          value: btn.value,
          rwc: {
            buttonStyle: btn.style
          }
        }
      };

      result.children.push(button);
    });
  }

  return result;
};
