<?php
declare(strict_types=1);

namespace WP_AUX\Entities\AuxFramework\Partials;

use WHOOP\Entities\Elements\A;
use WHOOP\Entities\Elements\Node;

/**
 * A single header utility CTA item entity
 * @since 1.0.0
 */
class HeaderUtilityCtasItemEntity
{
  protected A $anchor;
  protected bool $mobile = false;

  public function __construct(array $link, bool $mobile = false) {
    $this->mobile = $mobile;
    $this->anchor = A::create('', [
      'href' => $link['url'],
      'target' => !empty($link['target']) ? $link['target'] : '_self',
    ]);

    $this->anchor->appendElement(Node::create($link['content'] ?? $link['title']));

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

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

  public function __toString() {
    $anchor = clone $this->anchor;

    $class = $this->mobile ? 'cta' : 'utility-nav__cta';

    return '<li class="' . $class . '">' . $anchor . '</li>';
  }

  /**
   * Create a header utility CTA item with an array consisting at least of 'url' and 'title'
   * @since 1.0.0
   */
  public static function create(array $link, bool $mobile = false): static
  {
    return new static($link, $mobile);
  }
}
