import { IGenericText } from '../../../generic/types/text';
import { ISlackTextObject } from '../../types/index';
import { parseMarkdownToSlackMrkdwn } from '../../parsers/parse-markdown-to-slack-mrkdwn';
// import { toMarkdown } from '../../../../helpers/html-to-md';
interface IGenericTextObject {
  value: string,
  type?: 'markdown' | 'plain' | 'html',
  slack?: {
    type: 'plain_text' | 'mrkdwn',
    emoji?: boolean,
    verbatim?: boolean
  }
}

const transformTextObject = ({ value, type, slack }: IGenericTextObject): ISlackTextObject => {
  if (slack?.type && !type) {
    type = slack.type === 'mrkdwn' ? 'markdown' : 'plain';
  }
  const slackTextType = slack?.type || (type === 'plain' ? 'plain_text' : 'mrkdwn');
  const text = value || ' '; // (type === 'html') ? toMarkdown(value) : value; // TODO: fix commented line

  const res: ISlackTextObject = {
    type: slackTextType,
    text: slackTextType === 'mrkdwn' ? parseMarkdownToSlackMrkdwn(text) : text
  };

  if (slack?.emoji && slackTextType === 'plain_text') res.emoji = slack.emoji;
  if (slack?.verbatim && slackTextType === 'mrkdwn') res.verbatim = slack.verbatim;

  return res;
};

const transformText = (
  { props }: IGenericText): ISlackTextObject => {
  const { value, type, slack } = props;
  return transformTextObject({ value, type, slack });
};

export {
  transformTextObject,
  transformText
};