<?php
/**
 * Frontend output for the AUX Footer Column - Social Links block
 * This is only to be used for footers and should not be brought in to the main page builder
 *
 * AUX documentation: https://aggieux.tamu.edu/?path=/docs/navigation-footers-footer-option-a-about-footer-option-a--docs
 *
 * Outline:
 * 1. Set variables to null
 * 2. Get fields from the post, if they have values, wrap in html
 * 3. Build the component html
 * 4. Sanitize and output
 *
 * @author Hailey Sanders
 * @since 1.0.0
 */

use WP_AUX\Entities\AuxCF\Fields\FooterBlocks\SocialLinks\FooterColumnSocialLinkListFieldEntity;
use WP_AUX\Entities\AuxFramework\Partials\IconEntity;
use WP_AUX\Enum\SocialMediaProfilesEnum;
use WP_AUX\Services\HtmlService;

// Set variables to null
$footerColumnHeading =
$footerColumnSocialLinkList =
$socialListItems = null;

// Get fields from the post, if they have values, wrap in html
$footerColumnHeading = aux_get_field('footer_column_heading');
$footerColumnSocialLinkList = aux_get_field(FooterColumnSocialLinkListFieldEntity::$name);

$footerColumnHeading = HtmlService::formattedValue($footerColumnHeading, '<span class="column__header">%s</span> ');

if (!empty($footerColumnSocialLinkList)) {
  foreach ($footerColumnSocialLinkList as $socialLink) {
    $platform = SocialMediaProfilesEnum::tryFrom($socialLink['footer_column_social_links_platform']);

    if (empty($platform)) {
      continue;
    }

    $handle = @$socialLink['footer_column_social_links_handle'];
    $link = @$socialLink['footer_column_social_links_link'];

    if (SocialMediaProfilesEnum::LinkedIn === $platform) {
      $handle = @$link['url'];
      $text = @$link['title'];
    } else {
      $text = '@' . $handle;
    }

    $icon = IconEntity::create($platform->icon(), false);
    $url = $platform->url($handle);

    if (empty($url)) {
      continue;
    }

    $socialListItems .= sprintf(
      '<li>%s<a href="%s">%s</a></li>',
      (string) $icon,
      $url,
      $text
    );
  }
}

$theSocialLinks = HtmlService::formattedValue($socialListItems, '<div class="social-list"><ul>%s</ul></div>');

// Build the component HTML
echo <<<HTML
<div class="footer__column">
    $footerColumnHeading
    $theSocialLinks
</div>
HTML;
