<?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\Enum\IntSwitchEnum;
use AuxCF\Entities\Settings\Traits\EnumPropertyTraits;
use AuxCF\Entities\Traits\FieldsSetTraits;

class RepeaterFieldEntity extends FieldEntity
{
  use EnumPropertyTraits, FieldsSetTraits;

  protected FieldTypeEnum $type = FieldTypeEnum::Repeater;

  protected RepeaterFieldLayoutEnum $layout = RepeaterFieldLayoutEnum::DEFAULT;
  protected IntSwitchEnum $pagination = IntSwitchEnum::Off;
  protected int $min = 0;
  protected ?int $max = null;
  protected int $rowsPerPage = 0;
  protected string $buttonLabel = 'Add Row';
  protected string $collapsed = '';

  protected final function init(): void
  {
    $this->initFields();
  }

  public function setPagination(int|IntSwitchEnum $pagination): self
  {
    $this->setEnumProperty(IntSwitchEnum::class, $pagination, 'pagination');
    return $this;
  }

  public function setLayout(string|RepeaterFieldLayoutEnum $layout): self
  {
    $this->setEnumProperty(RepeaterFieldLayoutEnum::class, $layout, 'layout');
    return $this;
  }

  public function setButtonLabel(string $buttonLabel): self
  {
    $this->buttonLabel = $buttonLabel;
    return $this;
  }

  public function setCollapsed(string $collapsed): self
  {
    $this->collapsed = $collapsed;
    return $this;
  }

  public function getFieldArray(): array
  {
    $array = [
      'min' => $this->min,
      'max' => is_null($this->max) ? "" : $this->max,
      'rowsPerPage' => $this->rowsPerPage,
      'collapsed' => $this->collapsed,
      'pagination' => $this->pagination->value,
      'layout' => $this->layout->value,
      'button_label' => $this->buttonLabel,
      'sub_fields' => $this->fieldsSet->getSetArray(),
    ];

    return $array;
  }
}
