<?php

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

  $socialLinksStyle = $user_data['socialLinksStyle'];
  $socialLinksType = $user_data['socialLinksType'];
  $socialLinks = $user_data['socialLinks'];
  $shareLinks = $user_data['shareLinks'];
  $color = $user_data['color'];
  $hoverColor = $user_data['hoverColor'];
  $gap = $user_data['gap'];
  $iconSize = $user_data['iconSize'];

  $list = $socialLinksType === "links" ? $socialLinks : $shareLinks;

  $links = [];


  // Sharing code
  $site_title = get_bloginfo('name');
	$post_permalink = urlencode(get_permalink($post->ID));

	$social_mapping = array(
		'facebook' => 'http://www.facebook.com/sharer/sharer.php?u='.$post_permalink,
		'twitter' => 'http://twitter.com/home?status='.urlencode($post->post_title).'+'.$post_permalink,
		'googlePlus' => 'https://plus.google.com/share?url='.$post_permalink,
		'pinterest' => 'https://www.pinterest.com/pin/create/button/'
	);



  foreach ($list  as $itemKey => $item) {
    $attr = '';
    $url = false;

    if ($socialLinksType === 'share') {
      if ($item['type'] === 'pinterest') {
        $pinterest_sharing_enabled = true;
        $attr= 'data-pin-do="buttonBookmark" data-pin-custom="true" href="https://www.pinterest.com/pin/create/button/"';
      } else {
        $url = $social_mapping[$item['type']];
      }
    } else {
      $url = $item['link'];
    }

    if ($url) {
      $url = 'href="'.$url.'"';
    }

    $newLink = '
      <a
        class="flex-element-type-socialLinks__link"
        target="_blank"
        rel="noopener noreferrer"
        '.$url.'
        '.$attr.'
      >
    ';

    if ($socialLinksStyle === 'icons') {
      $newLink .= '<i class="flo-flex-icon-'.$item["type"].'"></i>';
    } else {
      $newLink .= '<span class="flex-element-type-socialLinks__item-text">'.$item["text"].'</span>';
    }

    $newLink .= '
      </a>
    ';
    $links[] = $newLink;
  }

  $elMarkup = '
    <style>
      .'.$b.'__element--'.$element["id"].' .flex-element-type-socialLinks__item-text {
        '.fontMixin($user_data, 'textTypeFont').'
      }
    </style>
    <div class="'.$b.'__element '.$b.'__element--'.$element["id"].' '.$b.'__element--socialLinks '.$custom_classes.'" style="'.$elStyles.'">
      <div
        class="flex-element-type-socialLinks flex-element-type-socialLinks--style-'.$socialLinksStyle.'"
        style="
          --color: '.getStyleKitColor($color).';
          --hover-color: '.getStyleKitColor($hoverColor).';
          --gap: '.($gap / 16).'rem;
          --iconSize: '.($iconSize / 16).'rem;
        "
      >
        '.join($links).'
      </div>
    </div>
  ';

  return $elMarkup;
}
