<?php
declare(strict_types=1);

namespace AuxCF\Entities\Field;

use AuxCF\Entities\Settings\Enum\OperatorEnum;

class ConditionalLogicSetEntity
{
  /**
   * @var ConditionalLogicGroupEntity[]
   */
  protected $groups = [];

  public function __construct() {
  }

  public function hasConditions(): bool
  {
    foreach ($this->groups as $group) {
      if ($group->hasConditions()) {
        return true;
      }
    }

    return false;
  }

  public function addGroup(ConditionalLogicGroupEntity $group): void
  {
    $this->groups[] = $group;
  }

  public function getValueForField(): int|array
  {
    if (empty($this->groups)) {
      return 0;
    }

    $array = [];

    foreach ($this->groups as $group) {
      $array[] = $group->getArray();
    }

    return $array;
  }

  public static function create(array $groups): self
  {
    $set = new self();

    foreach ($groups as $group) {
      if ($group instanceof ConditionalLogicGroupEntity) {
        $set->addGroup($group);
      } else {
        $set->addGroup(ConditionalLogicGroupEntity::create($group));
      }
    }

    return $set;
  }
}
