<?php
declare(strict_types=1);

namespace WP_AUX\Entities\AuxCF\Fields\RecentPosts;

use AuxCF\Entities\Field\Enum\ChoicesFieldLayoutEnum;
use AuxCF\Entities\Field\Enum\ChoicesFieldReturnFormatEnum;
use AuxCF\Entities\Field\SelectFieldEntity;
use AuxCF\Entities\Settings\Enum\IntSwitchEnum;

class RecentPostsTypesFieldEntity extends SelectFieldEntity
{
  public static string $name = 'recent_posts_types';
  protected string $label = 'Filter Posts by Type';
  protected string $instructions = 'By default, recent posts will include all post types. Manually select specific post types to limit what post types to include.';
  protected mixed $choices = [];
  protected ChoicesFieldLayoutEnum $layout = ChoicesFieldLayoutEnum::Vertical;
  protected ChoicesFieldReturnFormatEnum $returnFormat = ChoicesFieldReturnFormatEnum::Value;
  protected IntSwitchEnum $multiple = IntSwitchEnum::On;
  protected IntSwitchEnum $ui = IntSwitchEnum::On;
  protected string $placeholder = 'All Post Types';

  protected function init(): void
  {
    add_filter('acf/load_field/name=' . $this::$name, function($field) {
      $postTypes = get_post_types(
        [
          'exclude_from_search' => false,
          'has_archive' => true,
        ],
        'objects'
      );

      $field['choices'] = [];
      foreach ($postTypes as $postType) {
        $field['choices'][$postType->name] = $postType->label;
      }

      return $field;
    });
  }
}
