<?php

namespace WP_AUX\Entities\Components;

use WHOOP\Entities\Elements\A;
use WP_AUX\Entities\ComponentEntity;

class LinkListEntity extends ComponentEntity {
  public const ELEMENT_PRIMARY_CLASS = 'link-list';

  protected array $additionalClasses = [
    'link-list--leading'
  ];

  public static array $augmentations = [
    'cols' => [
      'default' => false,
      'one' => '--one-col',
      'two' => '--two-col',
      'three' => '--three-col',
    ],
  ];

  protected array $links = [];

  public function setLinks(array $links): static
  {
    $this->links = $links;

    return $this;
  }

  public function addLink(array $link): void
  {
    $this->links[] = $link;
  }

  public function getContents(): string
  {
    if (empty($this->links)) {
      return '';
    }

    $linksElements = [];
    foreach ($this->links as $link) {
      $anchor = A::create($link['content'] ?? $link['title'], [
        'href' => $link['url'],
        'target' => !empty($link['target']) ? $link['target'] : '_self',
      ]);

      if (!empty($link['rel'])) {
        $anchor->setAttribute('rel', $link['rel']);
      }

      if (!empty($link['id'])) {
        $anchor->setAttribute('id', $link['id']);
      }

      $linksElements[] = '<li>' . $anchor . @$link['afterLink'] . '</li>';
    }

    return '<ul>' . join($linksElements) . '</ul>';
  }
}
