<?php
declare(strict_types=1);

namespace WP_AUX\Entities\AuxCF\OptionsFields\PostType;

use AuxCF\Entities\Field\SelectFieldEntity;
use WP_AUX\Services\PostTypeService;

class PostTypeSearchFilterTaxonomyFieldEntity extends SelectFieldEntity
{
  public static string $namePattern = 'aux_search_%s_section_filter_terms_taxonomy';

  protected string $label = 'Taxonomy';

  protected function init(): void
  {
    $postTypes = PostTypeService::getFieldPostTypes($this);

    $choices = [];
    foreach ($postTypes as $postType) {
      if (!$postType instanceof \WP_Post_Type) {
        continue;
      }

      $taxonomies = get_object_taxonomies($postType->name, 'objects');
      foreach ($taxonomies as $taxonomy) {
        $choices[$postType->label][$postType->name . '--' . $taxonomy->name] = $taxonomy->label;
      }
    }

    $this->choices = $choices;
  }
}
