<?php

declare(strict_types=1);

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

/**
 * WPML-compatible filter hooks.
 *
 * Provides the most commonly used WPML filter/action hooks so that
 * plugins and themes with WPML support work out of the box.
 * Only loaded when WPML is NOT active to avoid conflicts.
 */

use Signocore\Multilanguage\Features\LanguageDetection;
use Signocore\Multilanguage\Features\LanguageManager;
use Signocore\Multilanguage\Support\TranslationHelper;
use Signocore\Multilanguage\Support\UrlHelper;

// wpml_object_id — Get translated element ID
// Usage: apply_filters('wpml_object_id', $element_id, 'post_type', true, $lang_code)
add_filter('wpml_object_id', function ($elementId, $elementType = 'post', $returnOriginal = true, $langCode = null) {
	if (!is_numeric($elementId)) {
		return $elementId;
	}

	$elementId = (int) $elementId;
	$langCode = $langCode ?? signocore_ml_get_current_language();

	// Map WPML element types to our format
	$type = in_array($elementType, ['category', 'post_tag'], true) ? 'term' : 'post';
	$translatedId = TranslationHelper::getTranslation($elementId, $langCode, $type);

	if ($translatedId !== null) {
		return $translatedId;
	}

	return $returnOriginal ? $elementId : null;
}, 10, 4);

// wpml_current_language — Get current language code
// Usage: apply_filters('wpml_current_language', '')
add_filter('wpml_current_language', function () {
	return signocore_ml_get_current_language();
});

// wpml_default_language — Get default language code
// Usage: apply_filters('wpml_default_language', '')
add_filter('wpml_default_language', function () {
	return signocore_ml_get_default_language();
});

// wpml_active_languages — Get all active languages
// Usage: apply_filters('wpml_active_languages', null, $args)
add_filter('wpml_active_languages', function ($value, $args = []) {
	$languages = LanguageManager::load()->getActiveLanguages();
	$result = [];

	foreach ($languages as $code => $lang) {
		$result[$code] = [
			'code' => $code,
			'native_name' => $lang->native_name,
			'translated_name' => $lang->name,
			'default_locale' => $lang->locale,
			'active' => true,
			'url' => UrlHelper::getUrlForLanguage($code),
		];
	}

	return $result;
}, 10, 2);

// wpml_switch_language — Switch language context
// Usage: do_action('wpml_switch_language', $lang_code)
add_action('wpml_switch_language', function ($langCode) {
	if ($langCode === null || $langCode === 'all') {
		return;
	}
	LanguageDetection::load()->switchLanguage($langCode);
});

// wpml_element_has_translations — Check if element has translations
// Usage: apply_filters('wpml_element_has_translations', null, $post_id, 'post')
add_filter('wpml_element_has_translations', function ($value, $elementId = 0, $elementType = 'post') {
	if (!is_numeric($elementId) || (int) $elementId <= 0) {
		return false;
	}

	$type = in_array($elementType, ['category', 'post_tag'], true) ? 'term' : 'post';
	$translations = signocore_ml_get_translations((int) $elementId, $type);

	return count($translations) > 1;
}, 10, 3);

// wpml_permalink — Get translated permalink
// Usage: apply_filters('wpml_permalink', $url, $lang_code)
add_filter('wpml_permalink', function ($url, $langCode = null) {
	if ($langCode === null) {
		return $url;
	}
	return UrlHelper::getUrlForLanguage($langCode, $url);
}, 10, 2);

// wpml_home_url — Get home URL for a language
// Usage: apply_filters('wpml_home_url', '')
add_filter('wpml_home_url', function () {
	return home_url('/');
});
