<?php

/**
 * AUX Card Group template
 *
 * @param \WP_Post[] $featureItems The block settings and attributes.
 */

use WP_AUX\Entities\AuxFramework\Partials\CardGroupEntity;

$cards = aux_get_field('card');
$sectionCtas = aux_get_field('section_ctas');

$cardGroup = CardGroupEntity::create(aux_get_field('card_layout_switch'));
$cardGroup
  ->setStyle(get_field('section_style'))
  ->setBackground(get_field('section_background'))
  ->setCardsCtaType(get_field('section_cards_cta_type'))
  ->setDescription(aux_get_field('section_description'))
  ->setHeading(aux_get_field('section_heading'))
  ->setSuperheader(aux_get_field('section_superheader'))
  ->setSubheader(aux_get_field('section_subheader'));

if ($sectionCtas) {
  foreach ($sectionCtas as $sectionCta) {
    $cardGroup->addCta($sectionCta['section_cta_link']);
  }
}

if ($cards) {
  foreach ($cards as $card) {
    $cardItem = $cardGroup->addCard(
      $card['card_heading'],
      $card['card_content'],
      $card['card_superheader'],
      $card['card_subheader'],
      $card['card_icon'],
      $card['card_image']
    );

    $cardItem->setCta($card['linked_card_cta'] ? $card['linked_card_cta'] : $card['card_cta']);

    if (!empty($card['card_ctas'])) {
      foreach ($card['card_ctas'] as $cardCta) {
        $cardItem->addCta($cardCta['card_link']);
      }
    }
  }
}

echo $cardGroup;
