<?php
declare(strict_types=1);

namespace WP_AUX\Entities\AuxFramework\Partials;

use WHOOP\Entities\Elements\Div;

class ButtonGroupEntity
{
  protected Div $container;
  /**
   * @var ButtonEntity[]
   */
  protected $buttons = [];
  public static ?string $mode = null;

  public function __construct() {
    $this->container = Div::create(null, ['class' => 'button-group']);

    if (static::$mode) {
      $this->container->addClass('button-group--' . static::$mode);
    }
  }

  public function addButton(array $link): ButtonEntity
  {
    $button = ButtonEntity::create($link, $this);

    $this->buttons[] = $button;

    return $button;
  }

  public static function create(): static
  {
    return new static();
  }

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

    foreach ($this->buttons as $button) {
      $this->container->appendElement($button);
    }

    $this->container->setDataValue('buttons', count($this->buttons));

    return (string) $this->container;
  }
}
