<?php

function renderTextElement($b, $element, $custom_classes, $elStyles, $colors, $viewport) {
  $data = $element['data'];
  $user_data = $data['user_data'];
  $elMarkup = "";

  $text_enc = urldecode($user_data['text']);
  $text_enc = str_replace("__SLASH__","--",$text_enc);
  $text_html = html_entity_decode($text_enc);
  $text = $text_html;

  $dataFontStyle = "";
  $allowed_themes = defined('FLEX_ALLOWED_THEMES') ? FLEX_ALLOWED_THEMES : [];
  $searchName = strtolower(str_replace(' Child', '', wp_get_theme()->Name));
  if ( defined('IS_PHP_NOT_COPATIBLE') && IS_PHP_NOT_COPATIBLE ) {
    $if_in_allowed_themes = strpos($allowed_themes, $searchName) !== false;
  } else {
    $if_in_allowed_themes = in_array($searchName, $allowed_themes);
  }

  if(isset($user_data['useFontStyle']) && $user_data['useFontStyle']) {
    $dataFontStyle = "data-font='".$user_data['fontStyle']."'";
  } else {
    if(isset($user_data['fontSize'])) {
      $elStyles .= Flo_Flex_Builder::isFlothemes() || !$if_in_allowed_themes ? "--font-size: ".$user_data['fontSize']."em;" : "--font-size: ".$user_data['fontSize']."rem;";
    }
    if(isset($user_data['lineHeight'])) {
      $elStyles .= "--line-height: ".$user_data['lineHeight']."em;";
    }
    if(isset($user_data['letterSpacing'])) {
      $elStyles .= "--letter-spacing: ".$user_data['letterSpacing']."em;";
    }
  }


  $old_editor_class = "";
  if (
    !isset($user_data['fontSize']) &&
    !isset($user_data['lineHeight']) &&
    !isset($user_data['letterSpacing'])
  ) {
    $old_editor_class = "old-editor-content";
  }

  $height_auto_class = "";
  if(isset($user_data['heightAuto']) && $user_data['heightAuto']) {
    $height_auto_class = "height-auto";
  }

  $isFlothemes = Flo_Flex_Builder::isFlothemes();
  if($isFlothemes) {
    $text = apply_filters('the_content', $text);
  }

  $elMarkup .= '
    <div class="'.$b.'__element '.$b.'__element--'.$element["id"].' '.$b.'__element--text '.$custom_classes.'" style="'.$elStyles.'">
      <div '.$dataFontStyle.' class="'.$b.'__text-content '.$old_editor_class.' '.$height_auto_class.'">
        '.$text.'
      </div>
    </div>
  ';

  return $elMarkup;
}
