<?php
declare(strict_types=1);

namespace WP_AUX\Services;

class CustomTagsService
{
  public static function createCustomTagLabelsArray(string $labelPlural, string $label): array
  {
    $lcLabelPlural = strtolower($labelPlural);
    $lcLabel = strtolower($label);

    $labels = [
      'name' => $labelPlural,
      'singular_name' => $label,
      'menu_name' => $labelPlural,
      'all_items' => 'All ' . $labelPlural,
      'edit_item' => 'Edit ' . $label,
      'view_item' => 'View ' . $label,
      'update_item' => 'Update ' . $label,
      'add_new_item' => 'Add New ' . $label,
      'new_item_name' => 'New ' . $label . ' Name',
      'search_items' => 'Search ' . $labelPlural,
      'popular_items' => 'Popular ' . $labelPlural,
      'separate_items_with_commas' => 'Separate ' . $lcLabelPlural . ' with commas',
      'add_or_remove_items' => 'Add or remove ' . $lcLabelPlural,
      'choose_from_most_used' => 'Choose from the most used ' . $lcLabelPlural,
      'not_found' => 'No ' . $lcLabelPlural . ' found',
      'no_terms' => 'No ' . $lcLabelPlural,
      'items_list_navigation' => $labelPlural . ' list navigation',
      'items_list' => $labelPlural . ' list',
      'back_to_items' => '← Go to ' . $lcLabelPlural,
      'item_link' => $label . ' Link',
      'item_link_description' => 'A link to a ' . $lcLabel,
    ];

    return $labels;
  }

  public static function createCustomTagArray(
    string $labelPlural,
    string $label,
    bool $showInMenu = true,
    bool $showInNavMenus = true,
    bool $showTagCloud = false
  ): array
  {
    $tagArray = [
      'labels' => self::createCustomTagLabelsArray($labelPlural, $label),
      'public' => true,
      'show_in_menu' => $showInMenu,
      'show_in_nav_menus' => $showInNavMenus,
      'show_in_rest' => true,
      'show_tagcloud' => $showTagCloud,
      'meta_box_cb' => false,
    ];

    return $tagArray;
  }

  public static function registerCustomTags(
    string $slug,
    array|string|null $postTypes = null,
    string $labelPlural,
    string $label,
    bool $showInMenu = true,
    bool $showInNavMenus = true,
    bool $showTagCloud = false
  ): void
  {
    $postTypeArray = self::createCustomTagArray(
      $labelPlural,
      $label,
      $showInMenu,
      $showInNavMenus,
      $showTagCloud
    );

    register_taxonomy($slug, $postTypes, $postTypeArray);
  }
}
