<?php
declare(strict_types=1);

namespace AuxCF\Entities\Field;

use AuxCF\Entities\Field\Enum\FieldTypeEnum;
use AuxCF\Entities\Field\Enum\RepeaterFieldLayoutEnum;
use AuxCF\Entities\FieldEntity;
use AuxCF\Entities\Settings\Traits\EnumPropertyTraits;
use AuxCF\Entities\Traits\FieldsSetTraits;

/**
 * ACF Group Field
 *
 * Group fields contain child fields, so share some traits with the "FieldGroup"
 *
 * @link https://www.advancedcustomfields.com/resources/group/
 */
class GroupFieldEntity extends FieldEntity
{
  use EnumPropertyTraits, FieldsSetTraits;

  protected FieldTypeEnum $type = FieldTypeEnum::Group;

  protected RepeaterFieldLayoutEnum $layout = RepeaterFieldLayoutEnum::DEFAULT;

  /**
   * On field initialization, initialize the fields within the group
   * @since 1.0.0
   */
  protected final function init(): void
  {
    $this->beforeInitFields();
    $this->initFields();
    $this->afterInitFields();
  }

  /**
   * Set the layout of the group.
   *
   * If a string is provided, the matching enum value will be found. If no match is found
   * an exception will be thrown.
   *
   * @since 1.0.0
   */
  public function setLayout(string|RepeaterFieldLayoutEnum $layout): self
  {
    $this->setEnumProperty(RepeaterFieldLayoutEnum::class, $layout, 'layout');
    return $this;
  }

  /**
   * Get properties specific to the group field
   *
   * @since 1.0.0
   * @uses FieldSetTrait::getSetArray()
   */
  public function getFieldArray(): array
  {
    $array = [
      'layout' => $this->layout->value,
      'sub_fields' => $this->fieldsSet->getSetArray(),
    ];

    return $array;
  }
}
