<?php
declare(strict_types=1);

namespace WP_AUX\Entities\AuxCF\OptionsFields\SearchSidebar;

use AuxCF\Entities\Field\SelectFieldEntity;
use AuxCF\Entities\Settings\Enum\IntSwitchEnum;
use AuxCF\Entities\Settings\Enum\OperatorEnum;

class SearchSidebarFilterPostTypesFieldEntity extends SelectFieldEntity
{
  public static string $name = 'aux_search_sidebar_section_filter_post_types';

  protected string $label = 'Post Types';
  protected IntSwitchEnum $multiple = IntSwitchEnum::On;
  protected IntSwitchEnum $ui = IntSwitchEnum::On;
  protected array $initConditionalLogic = [
    [
      [ SearchSidebarFilterSourceFieldEntity::class, OperatorEnum::EQUALS, 'post_type' ]
    ],
  ];

  protected function init(): void
  {
    $this->choices = function() {
      $postTypes = get_post_types(
        [
          'exclude_from_search' => false,
          'has_archive' => true,
        ],
        'objects'
      );

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

      return $choices;
    };
  }
}
