<?php
declare(strict_types=1);

namespace WHOOP\Entities;

use WHOOP\Entities\Elements\Traits\ElementTraits;

/**
 * A complete set of attributes for elements
 *
 * Use this to create, manage, and render a complete set of attributes for an element,
 * independent of tag, content, and other element object features.
 *
 * @since 1.0.1
 */
class ElementAttributes
{
  use ElementTraits;

  public function __construct() {
    $this->initProperties();
  }

  /**
   * Create a new element atttributes set, starting with classes, attributes, and data values
   * @since 1.0.1
   */
  public static function create(
    array|string|null $classes = null,
    ?array $attributes = null,
    ?array $data = null
  ): static
  {
    $elementAttributes = new static();

    if (!empty($classes)) {
      if (is_string($classes)) {
        $elementAttributes->addClass($classes);
      } elseif (is_array($classes)) {
        foreach ($classes as $class) {
          $elementAttributes->addClass($class);
        }
      }
    }

    if (!empty($attributes)) {
      $elementAttributes->setAttributes($attributes);
    }

    if (!empty($data)) {
      foreach ($data as $dataKey => $dataValue) {
        $elementAttributes->setDataValue($dataKey, $dataValue);
      }
    }

    return $elementAttributes;
  }

  /**
   * Return the full element HTML attributes string
   * @since 1.0.1
   */
  public function __toString(): string
  {
    return $this->getAttributesString();
  }
}
