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

declare(strict_types=1);

use WP_AUX\Entities\AuxCF\Fields\DescriptionList\DescriptionListItemCtaFieldEntity;
use WP_AUX\Entities\AuxCF\Fields\DescriptionList\DescriptionListItemDescriptionFieldEntity;
use WP_AUX\Entities\AuxCF\Fields\DescriptionList\DescriptionListItemIconFieldEntity;
use WP_AUX\Entities\AuxCF\Fields\DescriptionList\DescriptionListItemTermFieldEntity;
use WP_AUX\Entities\AuxFramework\Partials\DescriptionListEntity;
use WP_AUX\Services\AuxCF\BlocksService;

$descriptionList = '';
$itemsCount = 0;

// Create description list from user input
$descriptionListItems = get_field('description_list_item');
if ($descriptionListItems) {
  $descriptionList = DescriptionListEntity::create('custom');

  // Loop through top-level descriptio list items to add to the list
  foreach ($descriptionListItems as $linkItem) {
    $descriptionListItem = $descriptionList->addItem(
      $linkItem[DescriptionListItemTermFieldEntity::$name],
      $linkItem[DescriptionListItemDescriptionFieldEntity::$name],
      $linkItem[DescriptionListItemIconFieldEntity::$name],
      $linkItem[DescriptionListItemCtaFieldEntity::$name],
    );
  }

  $itemsCount = $descriptionList->getCount();
}

$section = BlocksService::getSection('dl-collection')
  ->setContent(<<<HTML
    <div class="dl-collection__wrapper" data-cards="$itemsCount">
      $descriptionList
    </div>
  HTML);

echo $section;
