<?php
declare(strict_types=1);

namespace WP_AUX\Entities\Admin\AuxComponents;

use AuxCF\Entities\FieldGroupEntity;
use WP_AUX;

class AuxComponentsAuditEntity
{
  /**
   * @var AuxComponentsAuditComponentEntity[]
   */
  protected array $auditComponents;

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

  protected function prepareComponentsData() {
    $groupsDir = \WP_AUX::filePath(WP_AUX::FIELD_GROUPS_DIR);
    $groupsDirContents = @glob($groupsDir . '/*Entity.php');

    if (!empty($groupsDirContents)) {
      foreach ($groupsDirContents as $groupEntityPath) {
        $auditComponent = new AuxComponentsAuditComponentEntity($groupEntityPath);
        $this->auditComponents[$auditComponent->key] = $auditComponent;
      }
    }
  }

  /**
   * @return AuxComponentsAuditComponentEntity[]
   */
  public function getAuditComponents(): array
  {
    return $this->auditComponents;
  }

  public function getAuditComponent(string $componentKey): ?AuxComponentsAuditComponentEntity
  {
    return @$this->auditComponents[$componentKey];
  }
}
