<?php
declare(strict_types=1);

namespace WP_AUX\Entities\AuxCF\OptionsFields\SearchSidebar;

use AuxCF\Entities\Field\SelectFieldEntity;

class SearchSidebarFilterTaxonomyFieldEntity extends SelectFieldEntity
{
  public static string $name = 'aux_search_sidebar_section_filter_terms_taxonomy';

  protected string $label = 'Taxonomy';

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

    $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;
  }
}
