<?php
declare(strict_types=1);

namespace WHOOP\Entities\Elements;

use WHOOP\Entities\Element;

/**
 * Represents an HTML anchor element (<a>).
 *
 * This class is used to create an anchor element with specified content and
 * an associative array of attributes. The TAG constant defines the HTML tag
 * associated with this class.
 *
 * @since 1.0.0
 */
class A extends Element
{
  public const TAG = 'a';

  /**
   * Creates a new <a> link
   *
   * Attributes are added in key/value pairs, where the key is the attribute name for
   * the link element, and the value is that attributes value
   *
   * <code>
   *   A::create('Link Text', [
   *     'href' => '/path/'
   *     'class' => 'classname',
   *     'target' => '_blank',
   *   ]);
   * </code>
   *
   * @since 1.0.0
   */
  public static function create(mixed $content = null, ?array $attributes = null): static
  {
    if (!empty($attributes)) {
      if (isset($attributes['url'])) {
        $attributes['href'] = $attributes['url'];
        unset($attributes['url']);
      }

      if (isset($attributes['target']) && empty($attributes['target'])) {
        // Do not set target attribute if it is empty
        unset($attributes['target']);
      }
    }

    return parent::create($content, $attributes);
  }
}
