<?php

declare(strict_types=1);

namespace Signocore\Multilanguage\Support;

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

use Signocore\Multilanguage\Application\Constants;

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

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

		// Default: all public post types (including attachment since media translation is free)
		/** @var string[] */
		return apply_filters('signocore_ml_translatable_post_types', array_values(get_post_types(['public' => true], 'names')));
	}

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

	/**
	 * Get the human-readable label for a post type.
	 */
	public static function getPostTypeLabel(string $postType): string
	{
		$postTypeObject = get_post_type_object($postType);

		if ($postTypeObject === null) {
			return $postType;
		}

		return $postTypeObject->labels->singular_name ?? $postType;
	}
}
