<?php
/**
 * AUX Link List template
 * @since 1.0.0
 */

use WP_AUX\Entities\AuxFramework\Partials\LinkListEntity;
use WP_AUX\Services\AuxCF\BlocksService;

$linkList =  '';

if ($linkListItems = aux_get_field_array('field_link_list')) {
  $linkList = LinkListEntity::create();
  $linkList->setColumns(aux_get_field('field_link_list_columns'));

  // Loop through top-level link items to add to link list
  foreach ($linkListItems as $linkItem) {
    $linkListItem = $linkList->addLink($linkItem['link_list_link']);

    if (!$linkListItem) {
      continue;
    }

    $linkListItem->setDescription($linkItem['link_description']);

    // Check for and loop through nested link items to add to top-level link item
    if (!empty($linkItem['link_nested_list'])) {
      foreach ($linkItem['link_nested_list'] as $nestedLinkItem) {
        $nestedLinkListItem = $linkListItem->addNestedLink($nestedLinkItem['link_list_nested_link']);
        $nestedLinkListItem->setDescription($nestedLinkItem['link_nested_description']);
      }
    }
  }
}

$section = BlocksService::getSection('link-list-collection');
$section->setContent($linkList);

echo $section;
