<?php

declare(strict_types=1);

namespace Signocore\Multilanguage\Support;

if (!defined('ABSPATH')) {
	exit;
}

use Signocore\Multilanguage\Application\Constants;

final class TaxonomyHelper
{
	/**
	 * Get taxonomies enabled for translation in settings.
	 *
	 * Falls back to all public taxonomies if no option has been saved yet.
	 *
	 * @return string[]
	 */
	public static function getTranslatableTaxonomies(): array
	{
		$saved = get_option(Constants::OPTION_PREFIX . 'translatable_taxonomies');

		if ($saved !== false && is_array($saved)) {
			/** @var string[] */
			return apply_filters('signocore_ml_translatable_taxonomies', $saved);
		}

		// Default: all public taxonomies
		/** @var string[] */
		return apply_filters('signocore_ml_translatable_taxonomies', array_values(get_taxonomies(['public' => true], 'names')));
	}

	/**
	 * Check if a taxonomy is translatable.
	 */
	public static function isTranslatable(string $taxonomy): bool
	{
		return in_array($taxonomy, self::getTranslatableTaxonomies(), true);
	}

	/**
	 * Get the human-readable label for a taxonomy.
	 */
	public static function getTaxonomyLabel(string $taxonomy): string
	{
		$taxonomyObject = get_taxonomy($taxonomy);

		if ($taxonomyObject === false) {
			return $taxonomy;
		}

		return $taxonomyObject->labels->singular_name ?? $taxonomy;
	}
}
